-
Notifications
You must be signed in to change notification settings - Fork 1
/
wasm.go
122 lines (97 loc) · 2.82 KB
/
wasm.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package wasm
import (
"context"
"fmt"
"log"
"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/api"
)
type Wasm interface {
Call(name string, args ...uint64) (interface{}, error)
Close()
}
type WasmInstance struct {
Context context.Context
Instance api.Module
}
func (w WasmInstance) Call(name string, args ...uint64) (interface{}, error) {
function := w.Instance.ExportedFunction(name)
result, err := function.Call(context.Background(), args...)
if err != nil {
return nil, err
}
if len(result) > 1 {
return result, nil
} else if len(result) == 1 {
return result[0], nil
} else {
return nil, nil
}
}
func (w WasmInstance) Close() {
if err := w.Instance.Close(w.Context); err != nil {
panic(err)
}
}
func getStringFromMemory(m api.Memory, i, len uint32) (string, error) {
b, ok := m.Read(context.Background(), i, len)
if !ok {
return "", fmt.Errorf("Failed to read memory at %d with length %d. Memory Size: %d", i, len, m.Size(context.Background()))
}
return string(b), nil
}
func wbingenThrow(ctx context.Context, mod api.Module, i, l uint32) {
s, err := getStringFromMemory(mod.Memory(), i, l)
if err != nil {
panic(err)
} else {
panic(fmt.Errorf("__wbindgen_throw %s", s))
}
}
type StringHeap struct {
Stack []string
}
func (h *StringHeap) wbingenObjectDropRef(ctx context.Context, mod api.Module, i uint32) {
if i >= uint32(len(h.Stack)) {
panic(fmt.Errorf("Index %d is out of range for %d", i, len(h.Stack)))
}
h.Stack = append(h.Stack[:i], h.Stack[i+i:]...)
}
func (h *StringHeap) wbingenStringNew(ctx context.Context, mod api.Module, i, l uint32) uint32 {
s, err := getStringFromMemory(mod.Memory(), i, l)
if err != nil {
panic(err)
}
index := len(h.Stack)
h.Stack = append(h.Stack, s)
return uint32(index)
}
func (h *StringHeap) wbingenLog9a99fb1af846153b(ctx context.Context, i uint32) {
if i >= uint32(len(h.Stack)) {
panic(fmt.Errorf("Index %d is out of range for %d", i, len(h.Stack)))
}
log.Printf("WBG: %v\n", h.Stack[i])
}
func NewWasmInstance(ctx context.Context) (Wasm, error) {
wasmInstance := WasmInstance{Context: ctx}
wasmBytes, err := WasmBin()
if err != nil {
return wasmInstance, err
}
r := wazero.NewRuntime(ctx)
h := &StringHeap{}
if _, err := r.NewHostModuleBuilder("wbg").
NewFunctionBuilder().WithFunc(wbingenThrow).Export("__wbindgen_throw").
NewFunctionBuilder().WithFunc(h.wbingenObjectDropRef).Export("__wbindgen_object_drop_ref").
NewFunctionBuilder().WithFunc(h.wbingenStringNew).Export("__wbindgen_string_new").
NewFunctionBuilder().WithFunc(h.wbingenLog9a99fb1af846153b).Export("__wbg_log_9a99fb1af846153b").
Instantiate(ctx, r); err != nil {
return wasmInstance, err
}
mod, err := r.InstantiateModuleFromBinary(ctx, wasmBytes)
if err != nil {
return wasmInstance, err
}
wasmInstance.Instance = mod
return wasmInstance, nil
}