-
Notifications
You must be signed in to change notification settings - Fork 3
/
linker.go
302 lines (256 loc) · 7.77 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
package wasman
import (
"errors"
"fmt"
"reflect"
"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")
)
// 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
}
// AdvancedFunc is a advanced host func comparing to normal go host func
// Dev will be able to handle the pre/post-call process of the func and manipulate
// the Instance's fields like memory
//
// e.g. when we wanna add toll after calling the host func f
// func ExampleFuncGenerator_addToll() {
// var linker = wasman.NewLinker()
// var f = func() {fmt.Println("wasm")}
//
// var af = wasman.AdvancedFunc(func(ins *wasman.Instance) interface{} {
// return func() {
// f()
// ins.AddGas(11)
// }
// })
// linker.DefineAdvancedFunc("env", "add_gas", af)
// }
//
// e.g. when we wanna manipulate memory
// func ExampleFuncGenerator_addToll() {
// var linker = wasman.NewLinker()
//
// var af = wasman.AdvancedFunc(func(ins *wasman.Instance) interface{} {
// return func(ptr uint32, length uint32) {
// msg := ins.Memory[int(ptr), int(ptr+uint32)]
// fmt.Println(b)
// }
// })
//
// linker.DefineAdvancedFunc("env", "print_msg", af)
// }
type AdvancedFunc func(ins *Instance) interface{}
// DefineAdvancedFunc will define a AdvancedFunc on linker
func (l *Linker) DefineAdvancedFunc(modName, funcName string, funcGenerator AdvancedFunc) error {
sig, err := getSignature(reflect.ValueOf(funcGenerator(&Instance{})).Type())
if err != nil {
return ErrInvalidSign
}
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: funcGenerator,
Signature: sig,
})
return nil
}
// 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 interface{}) error {
fn := func(ins *Instance) interface{} {
return f
}
sig, err := getSignature(reflect.ValueOf(f).Type())
if err != nil {
return ErrInvalidSign
}
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: fn,
Signature: sig,
})
return nil
}
// DefineGlobal will defined an external global for the main module
func (l *Linker) DefineGlobal(modName, globalName string, global interface{}) error {
ty, err := getTypeOf(reflect.TypeOf(global).Kind())
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[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 getSignature(p reflect.Type) (*types.FuncType, error) {
var err error
in := make([]types.ValueType, p.NumIn())
for i := range in {
in[i], err = getTypeOf(p.In(i).Kind())
if err != nil {
return nil, err
}
}
out := make([]types.ValueType, p.NumOut())
for i := range out {
out[i], err = getTypeOf(p.Out(i).Kind())
if err != nil {
return nil, err
}
}
return &types.FuncType{InputTypes: in, ReturnTypes: out}, nil
}
const is64Bit = uint64(^uintptr(0)) == ^uint64(0)
// getTypeOf converts the go type into wasm val type
func getTypeOf(kind reflect.Kind) (types.ValueType, error) {
if is64Bit {
switch kind {
case reflect.Float64:
return types.ValueTypeF64, nil
case reflect.Float32:
return types.ValueTypeF32, nil
case reflect.Int32, reflect.Uint32:
return types.ValueTypeI32, nil
case reflect.Int64, reflect.Uint64, reflect.Uintptr, reflect.UnsafePointer, reflect.Ptr:
return types.ValueTypeI64, nil
default:
return 0x00, fmt.Errorf("invalid type: %s", kind.String())
}
} else {
switch kind {
case reflect.Float64:
return types.ValueTypeF64, nil
case reflect.Float32:
return types.ValueTypeF32, nil
case reflect.Int32, reflect.Uint32, reflect.Uintptr, reflect.UnsafePointer, reflect.Ptr:
return types.ValueTypeI32, nil
case reflect.Int64, reflect.Uint64:
return types.ValueTypeI64, nil
default:
return 0x00, fmt.Errorf("invalid type: %s", kind.String())
}
}
}