-
Notifications
You must be signed in to change notification settings - Fork 0
/
resolver.go
71 lines (62 loc) · 1.53 KB
/
resolver.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package main
import (
"reflect"
"unsafe"
"github.com/go-interpreter/wagon/exec"
"github.com/go-interpreter/wagon/wasm"
"github.com/icexin/gowasm"
)
type process struct {
vm *exec.VM
}
func gomodule(r *gowasm.Resolver) *wasm.Module {
methods := []string{
"debug",
"runtime.wasmExit",
"runtime.wasmWrite",
"runtime.nanotime",
"runtime.walltime",
"runtime.scheduleCallback",
"runtime.clearScheduledCallback",
"runtime.getRandomData",
"syscall/js.valueGet",
"syscall/js.valueSet",
"syscall/js.valueIndex",
"syscall/js.valueSetIndex",
"syscall/js.valueLength",
"syscall/js.valueNew",
"syscall/js.valuePrepareString",
"syscall/js.valueCall",
"syscall/js.valueInvoke",
"syscall/js.valueVal",
"syscall/js.stringVal",
"syscall/js.valueLoadString",
"syscall/js.valueInstanceOf",
}
m := wasm.NewModule()
m.Export.Entries = map[string]wasm.ExportEntry{}
for i, method := range methods {
method := method
sig := wasm.FunctionSig{
Form: 0,
ParamTypes: []wasm.ValueType{wasm.ValueTypeI32},
ReturnTypes: nil,
}
m.Types.Entries = append(m.Types.Entries, sig)
fun := wasm.Function{
Sig: &sig,
Host: reflect.ValueOf(func(proc *exec.Process, sp int32) {
p := (*process)(unsafe.Pointer(proc))
r.CallMethod("go", method, p.vm, int64(sp))
}),
Body: &wasm.FunctionBody{},
}
m.FunctionIndexSpace = append(m.FunctionIndexSpace, fun)
m.Export.Entries[method] = wasm.ExportEntry{
FieldStr: method,
Kind: wasm.ExternalFunction,
Index: uint32(i),
}
}
return m
}