forked from c0mm4nd/wasman
-
Notifications
You must be signed in to change notification settings - Fork 1
/
linker.go
312 lines (265 loc) · 7.87 KB
/
linker.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package wasman
import (
"errors"
"fmt"
"math"
"github.com/c0mm4nd/wasman/config"
"github.com/c0mm4nd/wasman/wasm"
"github.com/c0mm4nd/wasman/segments"
"github.com/c0mm4nd/wasman/types"
)
// errors on linking modules
var (
ErrInvalidSign = errors.New("invalid signature")
)
// Primitive is a type constraint for arguments and results of host-defined functions
// that can be used with the linker.
type Primitive interface {
int | int8 | int16 | int32 | int64 |
uint | uint8 | uint16 | uint32 | uint64 | uintptr |
float32 | float64
}
// Linker is a helper to instantiate new modules
type Linker struct {
config.LinkerConfig
Modules map[string]*Module // the built-in modules which acts as externs when instantiating coming main module
}
// NewLinker creates a new Linker
func NewLinker(config config.LinkerConfig) *Linker {
return &Linker{
LinkerConfig: config,
Modules: map[string]*Module{},
}
}
// NewLinkerWithModuleMap creates a new Linker with the built-in modules
func NewLinkerWithModuleMap(config config.LinkerConfig, in map[string]*Module) *Linker {
return &Linker{
LinkerConfig: config,
Modules: in,
}
}
// Define put the module on its namespace
func (l *Linker) Define(modName string, mod *Module) {
l.Modules[modName] = mod
}
func DefineFunc01[Z Primitive](l *Linker, modName, funcName string, f func() Z) error {
return l.defineFunc(modName, funcName, wrapFunc01(f), []any{}, []any{*new(Z)})
}
func DefineFunc10[A Primitive](l *Linker, modName, funcName string, f func(A)) error {
return l.defineFunc(modName, funcName, wrapFunc10(f), []any{*new(A)}, []any{})
}
func DefineFunc11[A, Z Primitive](l *Linker, modName, funcName string, f func(A) Z) error {
return l.defineFunc(modName, funcName, wrapFunc11(f), []any{*new(A)}, []any{*new(Z)})
}
func DefineFunc20[A, B Primitive](l *Linker, modName, funcName string, f func(A, B)) error {
return l.defineFunc(modName, funcName, wrapFunc20(f), []any{*new(A), *new(B)}, []any{})
}
func DefineFunc21[A, B, Z Primitive](l *Linker, modName, funcName string, f func(A, B) Z) error {
return l.defineFunc(modName, funcName, wrapFunc21(f), []any{*new(A), *new(B)}, []any{*new(Z)})
}
// DefineFunc puts a simple go style func into Linker's modules.
// This f should be a simply func which doesnt handle ins's fields.
func (l *Linker) defineFunc(modName, funcName string, f wasm.RawHostFunc, ins []any, outs []any) error {
var err error
sig := &types.FuncType{}
sig.InputTypes, err = getTypesOf(ins)
if err != nil {
return err
}
sig.ReturnTypes, err = getTypesOf(outs)
if err != nil {
return err
}
mod, exists := l.Modules[modName]
if !exists {
mod = &Module{IndexSpace: new(wasm.IndexSpace), ExportSection: map[string]*segments.ExportSegment{}}
l.Modules[modName] = mod
}
if l.DisableShadowing && mod.ExportSection[funcName] != nil {
return config.ErrShadowing
}
mod.ExportSection[funcName] = &segments.ExportSegment{
Name: funcName,
Desc: &segments.ExportDesc{
Kind: segments.KindFunction,
Index: uint32(len(mod.IndexSpace.Functions)),
},
}
mod.IndexSpace.Functions = append(mod.IndexSpace.Functions, &wasm.HostFunc{
Generator: func(_ *Instance) wasm.RawHostFunc {
return f
},
Signature: sig,
})
return nil
}
func DefineGlobal[T any](l *Linker, modName, globalName string, global T) error {
ty, err := getTypeOf(*new(T))
if err != nil {
return err
}
return l.defineGlobal(modName, globalName, ty, global)
}
// DefineGlobal will defined an external global for the main module
func (l *Linker) defineGlobal(modName, globalName string, ty types.ValueType, global any) error {
mod, exists := l.Modules[modName]
if !exists {
mod = &Module{IndexSpace: new(wasm.IndexSpace), ExportSection: map[string]*segments.ExportSegment{}}
l.Modules[modName] = mod
}
if l.DisableShadowing && mod.ExportSection[globalName] != nil {
return config.ErrShadowing
}
mod.ExportSection[globalName] = &segments.ExportSegment{
Name: globalName,
Desc: &segments.ExportDesc{
Kind: segments.KindGlobal,
Index: uint32(len(mod.IndexSpace.Globals)),
},
}
mod.IndexSpace.Globals = append(mod.IndexSpace.Globals, &wasm.Global{
GlobalType: &types.GlobalType{
ValType: ty,
Mutable: true,
},
Val: global,
})
return nil
}
// DefineTable will defined an external table for the main module
func (l *Linker) DefineTable(modName, tableName string, table []*uint32) error {
mod, exists := l.Modules[modName]
if !exists {
mod = &Module{IndexSpace: new(wasm.IndexSpace), ExportSection: map[string]*segments.ExportSegment{}}
l.Modules[modName] = mod
}
if l.DisableShadowing && mod.ExportSection[tableName] != nil {
return config.ErrShadowing
}
mod.ExportSection[tableName] = &segments.ExportSegment{
Name: tableName,
Desc: &segments.ExportDesc{
Kind: segments.KindTable,
Index: uint32(len(mod.IndexSpace.Tables)),
},
}
mod.IndexSpace.Tables = append(mod.IndexSpace.Tables, &wasm.Table{
TableType: *mod.TableSection[0],
Value: table,
})
return nil
}
// DefineMemory will defined an external memory for the main module
func (l *Linker) DefineMemory(modName, memName string, mem []byte) error {
mod, exists := l.Modules[modName]
if !exists {
mod = &Module{IndexSpace: new(wasm.IndexSpace), ExportSection: map[string]*segments.ExportSegment{}}
l.Modules[modName] = mod
}
if l.DisableShadowing && mod.ExportSection[memName] != nil {
return config.ErrShadowing
}
mod.ExportSection[memName] = &segments.ExportSegment{
Name: memName,
Desc: &segments.ExportDesc{
Kind: segments.KindMem,
Index: uint32(len(mod.IndexSpace.Memories)),
},
}
mod.IndexSpace.Memories = append(mod.IndexSpace.Memories, &wasm.Memory{
MemoryType: *mod.MemorySection[0],
Value: mem,
})
return nil
}
// Instantiate will instantiate a Module into an runnable Instance
func (l *Linker) Instantiate(mainModule *Module) (*Instance, error) {
return NewInstance(mainModule, l.Modules)
}
func getTypesOf(defaults []any) ([]types.ValueType, error) {
var err error
types := make([]types.ValueType, len(defaults))
for i, def := range defaults {
types[i], err = getTypeOf(def)
if err != nil {
return nil, err
}
}
return types, nil
}
// getTypeOf converts the go type into wasm val type
func getTypeOf(def any) (types.ValueType, error) {
switch def.(type) {
case float64:
return types.ValueTypeF64, nil
case float32:
return types.ValueTypeF32, nil
case int32, uint32, int, int16, int8, bool:
return types.ValueTypeI32, nil
case int64, uint64, uintptr, uint:
return types.ValueTypeI64, nil
default:
return 0x00, fmt.Errorf("invalid type: %T", def)
}
}
func wrapFunc01[Z Primitive](f func() Z) wasm.RawHostFunc {
wrapper := func(a []uint64) []uint64 {
r1 := f()
return []uint64{toU(r1)}
}
return wrapper
}
func wrapFunc10[A Primitive](f func(A)) wasm.RawHostFunc {
wrapper := func(a []uint64) []uint64 {
a1 := fromU[A](a[0])
f(a1)
return []uint64{}
}
return wrapper
}
func wrapFunc11[A, Z Primitive](f func(A) Z) wasm.RawHostFunc {
wrapper := func(a []uint64) []uint64 {
a1 := fromU[A](a[0])
r1 := f(a1)
return []uint64{toU(r1)}
}
return wrapper
}
func wrapFunc20[A, B Primitive](f func(A, B)) wasm.RawHostFunc {
wrapper := func(a []uint64) []uint64 {
a1 := fromU[A](a[0])
a2 := fromU[B](a[1])
f(a1, a2)
return []uint64{}
}
return wrapper
}
func wrapFunc21[A, B, Z Primitive](f func(A, B) Z) wasm.RawHostFunc {
wrapper := func(a []uint64) []uint64 {
a1 := fromU[A](a[0])
a2 := fromU[B](a[1])
r1 := f(a1, a2)
return []uint64{toU(r1)}
}
return wrapper
}
func fromU[T Primitive](val uint64) T {
switch any(*new(T)).(type) {
case float32:
return T(float32(math.Float64frombits(val)))
case float64:
return T(math.Float64frombits(val))
default:
return T(val)
}
}
func toU[T Primitive](val T) uint64 {
switch v := any(val).(type) {
case float32:
return math.Float64bits(float64(v))
case float64:
return math.Float64bits(v)
default:
return uint64(val)
}
}