This repository has been archived by the owner on Nov 19, 2022. It is now read-only.
forked from bytecodealliance/wasmtime-go
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmulti_memory_test.go
101 lines (92 loc) · 2.21 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
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
package wasmtime
import "testing"
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")
)`)
if err != nil {
panic(err)
}
store := multiMemoryStore()
module, err := NewModule(store.Engine, wasm)
if err != nil {
panic(err)
}
ty := module.Type()
exports := ty.Exports()
if len(exports) != 2 {
panic("wrong number of exports")
}
if exports[0].Type().MemoryType() == nil {
panic("wrong export type")
}
if exports[0].Type().MemoryType().Minimum() != 2 {
panic("wrong memory limits")
}
present, max := exports[0].Type().MemoryType().Maximum()
if !present || max != 3 {
panic("wrong memory limits")
}
if exports[1].Type().MemoryType() == nil {
panic("wrong export type")
}
if exports[1].Type().MemoryType().Minimum() != 2 {
panic("wrong memory limits")
}
present, max = exports[1].Type().MemoryType().Maximum()
if !present || max != 4 {
panic("wrong memory limits")
}
_, err = NewInstance(store, module, nil)
if err != nil {
panic(err)
}
}
func TestMultiMemoryImported(t *testing.T) {
wasm, err := Wat2Wasm(`
(module
(import "" "m0" (memory 1))
(import "" "m1" (memory 1))
(func (export "load1") (result i32)
i32.const 2
i32.load8_s (memory 1)
)
)`)
if err != nil {
panic(err)
}
store := multiMemoryStore()
mem0, err := NewMemory(store, NewMemoryType(1, true, 3))
if err != nil {
panic(err)
}
mem1, err := NewMemory(store, NewMemoryType(2, true, 4))
if err != nil {
panic(err)
}
module, err := NewModule(store.Engine, wasm)
if err != nil {
panic(err)
}
instance, err := NewInstance(store, module, []AsExtern{mem0, mem1})
if err != nil {
panic(err)
}
copy(mem1.UnsafeData(store)[2:3], []byte{100})
res, err := instance.GetFunc(store, "load1").Call(store)
if err != nil {
panic(err)
}
if v, ok := res.(int32); !ok || v != 100 {
panic("unexpected result")
}
}