-
Notifications
You must be signed in to change notification settings - Fork 79
/
multi_memory_test.go
76 lines (63 loc) · 2.14 KB
/
multi_memory_test.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
package wasmtime
import (
"testing"
"github.com/stretchr/testify/require"
)
func multiMemoryStore() *Store {
config := NewConfig()
config.SetWasmMultiMemory(true)
return NewStore(NewEngineWithConfig(config))
}
func TestMultiMemoryExported(t *testing.T) {
wasm, err := Wat2Wasm(`
(module
(memory (export "memory0") 2 3)
(memory (export "memory1") 2 4)
(data (memory 0) (i32.const 0x1000) "\01\02\03\04")
(data (memory 1) (i32.const 0x1000) "\04\03\02\01")
)`)
require.NoError(t, err)
store := multiMemoryStore()
module, err := NewModule(store.Engine, wasm)
require.NoError(t, err)
exports := module.Exports()
require.Len(t, exports, 2)
require.NotNil(t, exports[0].Type().MemoryType())
require.Equal(t, uint64(2), exports[0].Type().MemoryType().Minimum())
present, max := exports[0].Type().MemoryType().Maximum()
require.True(t, present, "wrong memory limits")
require.Equal(t, uint64(3), max, "wrong memory limits")
require.NotNil(t, exports[1].Type().MemoryType())
require.Equal(t, uint64(2), exports[0].Type().MemoryType().Minimum())
present, max = exports[1].Type().MemoryType().Maximum()
require.True(t, present, "wrong memory limits")
require.Equal(t, uint64(4), max, "wrong memory limits")
_, err = NewInstance(store, module, nil)
require.NoError(t, err)
}
func TestMultiMemoryImported(t *testing.T) {
wasm, err := Wat2Wasm(`
(module
(import "" "m0" (memory 1))
(import "" "m1" (memory $m 1))
(func (export "load1") (result i32)
i32.const 2
i32.load8_s $m
)
)`)
require.NoError(t, err)
store := multiMemoryStore()
mem0, err := NewMemory(store, NewMemoryType(1, true, 3))
require.NoError(t, err)
mem1, err := NewMemory(store, NewMemoryType(2, true, 4))
require.NoError(t, err)
module, err := NewModule(store.Engine, wasm)
require.NoError(t, err)
instance, err := NewInstance(store, module, []AsExtern{mem0, mem1})
require.NoError(t, err)
copy(mem1.UnsafeData(store)[2:3], []byte{100})
res, err := instance.GetFunc(store, "load1").Call(store)
require.NoError(t, err)
require.IsType(t, res, int32(0))
require.Equal(t, int32(100), res.(int32))
}