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
|
wasmFullPass(`(module
(func $test (param i32) (param i32) (result i32) (i32.add (local.get 0) (local.get 1)))
(func $run (result i32) (call $test (i32.const 1) (i32.const ${Math.pow(2, 31) - 1})))
(export "run" (func $run))
)`, -Math.pow(2, 31));
wasmFullPass(`(module
(func (result i32)
i32.const 1
i32.const 42
i32.add
return
unreachable
i32.const 0
call 3
i32.const 42
i32.add
)
(func) (func) (func)
(export "run" (func 0)))`, 43);
wasmFullPass(`
(module
(import "env" "a" (global $a i32))
(import "env" "b" (func $b (param i32) (result i32)))
(func (export "run") (param $0 i32) (result i32) local.get 0 call $b)
)`, 43, { env: { a: 1337, b: x => x+1 } }, 42);
// Global section.
wasmFullPass(`(module
(import "globals" "x" (global $imported i32))
(global $mut_local (mut i32) (i32.const 0))
(global $imm_local i32 (i32.const 37))
(global $imm_local_2 i32 (global.get 0))
(func $get (result i32)
i32.const 13
global.set $mut_local
global.get $imported
global.get $mut_local
i32.add
global.get $imm_local
i32.add
global.get $imm_local_2
i32.add
)
(export "run" (func $get))
)`, 13 + 42 + 37 + 42, { globals: {x: 42} });
// Memory.
wasmFullPass(`(module
(memory (export "memory") 1 2)
(data (i32.const 0) "\\00\\01\\02" "\\03\\04\\05")
(data (i32.const 6) "\\06")
(func (export "run") (result i32)
i32.const 1
i32.load offset=2
)
)`, 0x06050403);
let memory = new WebAssembly.Memory({ initial: 1, maximum: 2 });
wasmFullPass(`(module
(memory (import "" "memory") 1 2)
(data (i32.const 0) "\\00\\01\\02\\03\\04\\05")
(func (export "run") (result i32)
i32.const 1
i32.load offset=2
)
(export "mem" (memory 0))
)`, 0x050403, {"": {memory}});
// Tables.
wasmFullPass(`(module
(table (export "table") 3 funcref)
(type $t (func (result i32)))
(func $foo (result i32) (i32.const 1))
(func $bar (result i32) (i32.const 2))
(func $baz (result i32) (i32.const 3))
(elem (i32.const 0) $baz $bar)
(elem (i32.const 2) $foo)
(func (export "run") (param i32) (result i32)
local.get 0
call_indirect (type $t)
)
)`, 3, {}, 0);
let table = new WebAssembly.Table({ element: 'anyfunc', initial: 3, maximum: 3 });
wasmFullPass(`(module
(table (import "" "table") 3 4 funcref)
(type $t (func (result i32)))
(func $foo (result i32) (i32.const 1))
(func $bar (result i32) (i32.const 2))
(func $baz (result i32) (i32.const 3))
(elem (i32.const 0) $baz $bar)
(elem (i32.const 2) $foo)
(func (export "run") (param i32) (result i32)
local.get 0
call_indirect (type $t)
)
)`, 3, {"":{table}}, 0);
// Start function.
wasmFullPass(`(module
(global $g (mut i32) (i32.const 0))
(func $start
global.get $g
i32.const 1
i32.add
global.set $g
)
(start $start)
(func (export "run") (result i32)
global.get $g
)
)`, 1);
// Branch table.
for (let [p, result] of [
[0, 7],
[1, 6],
[2, 4],
[42, 4]
]) {
wasmFullPass(`(module
(func (export "run") (param $p i32) (result i32) (local $n i32)
i32.const 0
local.set $n
block $c block $b block $a
local.get $p
br_table $a $b $c
end $a
local.get $n
i32.const 1
i32.add
local.set $n
end $b
local.get $n
i32.const 2
i32.add
local.set $n
end $c
local.get $n
i32.const 4
i32.add
)
)`, result, {}, p);
}
|