Skip to content

Instantly share code, notes, and snippets.

@Xe

Xe/importer.go Secret

Created June 18, 2018 04:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Xe/a29c86755a04a8096082ec8a32e0c13f to your computer and use it in GitHub Desktop.
Save Xe/a29c86755a04a8096082ec8a32e0c13f to your computer and use it in GitHub Desktop.
func (p *Process) importer(name string) (*wasm.Module, error) {
switch name {
case "env":
m := wasm.NewModule()
m.Types = &wasm.SectionTypes{
Entries: []wasm.FunctionSig{
{
Form: 0,
ParamTypes: []wasm.ValueType{wasm.ValueTypeI32, wasm.ValueTypeI32},
ReturnTypes: []wasm.ValueType{wasm.ValueTypeI32},
},
{
Form: 0,
ParamTypes: []wasm.ValueType{wasm.ValueTypeI32, wasm.ValueTypeI32, wasm.ValueTypeI32},
ReturnTypes: []wasm.ValueType{wasm.ValueTypeI32},
},
{
Form: 0,
ParamTypes: []wasm.ValueType{wasm.ValueTypeI32},
ReturnTypes: []wasm.ValueType{wasm.ValueTypeI32},
},
},
}
m.FunctionIndexSpace = []wasm.Function{
{
Sig: &m.Types.Entries[0],
Host: reflect.ValueOf(p.log),
Body: &wasm.FunctionBody{},
},
{
Sig: &m.Types.Entries[0],
Host: reflect.ValueOf(p.open),
Body: &wasm.FunctionBody{},
},
{
Sig: &m.Types.Entries[1],
Host: reflect.ValueOf(p.write),
Body: &wasm.FunctionBody{},
},
{
Sig: &m.Types.Entries[1],
Host: reflect.ValueOf(p.read),
Body: &wasm.FunctionBody{},
},
{
Sig: &m.Types.Entries[2],
Host: reflect.ValueOf(p.close),
Body: &wasm.FunctionBody{},
},
{
Sig: &m.Types.Entries[2],
Host: reflect.ValueOf(isatty),
Body: &wasm.FunctionBody{},
},
{
Sig: &m.Types.Entries[2],
Host: reflect.ValueOf(p.unlink),
Body: &wasm.FunctionBody{},
},
}
m.Export = &wasm.SectionExports{
Entries: map[string]wasm.ExportEntry{
"log": {
FieldStr: "log",
Kind: wasm.ExternalFunction,
Index: 0,
},
"open": {
FieldStr: "open",
Kind: wasm.ExternalFunction,
Index: 1,
},
"write": {
FieldStr: "write",
Kind: wasm.ExternalFunction,
Index: 2,
},
"read": {
FieldStr: "read",
Kind: wasm.ExternalFunction,
Index: 3,
},
"close": {
FieldStr: "close",
Kind: wasm.ExternalFunction,
Index: 4,
},
"isatty": {
FieldStr: "isatty",
Kind: wasm.ExternalFunction,
Index: 5,
},
"unlink": {
FieldStr: "unlink",
Kind: wasm.ExternalFunction,
Index: 6,
},
},
}
return m, nil
default:
f, err := os.Open(name + ".wasm")
if err != nil {
return nil, err
}
defer f.Close()
m, err := wasm.ReadModule(f, nil)
if err != nil {
return nil, err
}
err = validate.VerifyModule(m)
if err != nil {
return nil, err
}
return m, nil
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment