-
Notifications
You must be signed in to change notification settings - Fork 2
/
types.go
48 lines (38 loc) · 1.42 KB
/
types.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
package wasmtest
import (
"io"
"github.com/stealthrocket/wazergo/types"
"github.com/stealthrocket/wazergo/wasm"
"github.com/tetratelabs/wazero/api"
)
var malloc uint32
func brk(memory api.Memory, offset, length uint32) ([]byte, uint32) {
b := wasm.Read(memory, offset, length)
malloc = offset + length
return b, offset
}
func sbrk(memory api.Memory, size uint32) ([]byte, uint32) {
return brk(memory, malloc, size)
}
// Bytes is an extension of the types.Bytes type which adds the ability to treat
// those values as results, so they can be passed as argument to Call* functions
// in tests. The content of the byte slice is copied to the WebAssembly module
// memory, starting at address zero in each invocation of Call* functions. This
// would be really unsafe to do in a production applicaiton, which is why the
// feature is only made available to unit tests.
type Bytes types.Bytes
func (arg Bytes) FormatValue(w io.Writer, memory api.Memory, stack []uint64) {
types.Bytes(arg).FormatValue(w, memory, stack)
}
func (arg Bytes) LoadValue(memory api.Memory, stack []uint64) Bytes {
return Bytes(types.Bytes(arg).LoadValue(memory, stack))
}
func (arg Bytes) StoreValue(memory api.Memory, stack []uint64) {
b, offset := sbrk(memory, uint32(len(arg)))
copy(b, arg)
stack[0] = api.EncodeU32(offset)
stack[1] = api.EncodeU32(uint32(len(arg)))
}
func (arg Bytes) ValueTypes() []api.ValueType {
return types.Bytes(arg).ValueTypes()
}