forked from WebKit/WebKit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglobal-error.js
211 lines (166 loc) · 5.84 KB
/
global-error.js
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
import * as assert from '../assert.js';
import Builder from '../Builder.js';
{
// Test init from non-import.
const builder = new Builder();
builder.Type().End()
.Function().End()
.Global().GetGlobal("i32", 0, "immutable").End()
.Export()
.Function("getGlobal")
.Global("global", 1)
.End()
.Code()
.Function("getGlobal", { params: [], ret: "i32" })
.GetGlobal(1)
.End()
.End()
const bin = builder.WebAssembly();
bin.trim();
assert.throws(() => new WebAssembly.Module(bin.get()), WebAssembly.CompileError, "WebAssembly.Module doesn't parse at byte 26: get_global's index 0 exceeds the number of globals 0 (evaluating 'new WebAssembly.Module(bin.get())')");
}
{
// Test import mutable.
const builder = new Builder();
builder.Type().End()
.Import()
.Global().I32("imp", "global", "mutable").End()
.End()
.Function().End()
.Global().GetGlobal("i32", 0, "immutable").End()
.Export()
.Function("getGlobal")
.Global("global", 1)
.End()
.Code()
.Function("getGlobal", { params: [], ret: "i32" })
.GetGlobal(1)
.End()
.End()
const bin = builder.WebAssembly();
bin.trim();
assert.throws(() => new WebAssembly.Module(bin.get()), WebAssembly.CompileError, `WebAssembly.Module doesn't parse at byte 43: get_global import kind index 0 is mutable (evaluating 'new WebAssembly.Module(bin.get())')`);
}
{
// Test export mutable.
const builder = new Builder();
builder.Type().End()
.Function().End()
.Global().I32(0, "mutable").End()
.Export()
.Function("setGlobal")
.Global("global", 0)
.End()
.Code()
.Function("setGlobal", { params: [], ret: "i32" })
.GetGlobal(0)
.End()
.End()
const bin = builder.WebAssembly();
bin.trim();
new WebAssembly.Module(bin.get());
}
{
// Test set immutable.
const builder = new Builder();
builder.Type().End()
.Function().End()
.Global().I32(0, "immutable").End()
.Export()
.Function("setGlobal")
.Global("global", 0)
.End()
.Code()
.Function("setGlobal", { params: [], ret: "i32" })
.I32Const(0)
.SetGlobal(0)
.End()
.End()
const bin = builder.WebAssembly();
bin.trim();
assert.throws(() => new WebAssembly.Module(bin.get()), WebAssembly.CompileError, "WebAssembly.Module doesn't validate: set_global 0 is immutable, in function at index 0 (evaluating 'new WebAssembly.Module(bin.get())')");
}
{
// Test set non-existant global.
const builder = new Builder();
builder.Type().End()
.Function().End()
.Global().I32(0, "immutable").End()
.Export()
.Function("setGlobal")
.Global("global", 0)
.End()
.Code()
.Function("setGlobal", { params: [], ret: "i32" })
.I32Const(0)
.SetGlobal(1)
.End()
.End()
const bin = builder.WebAssembly();
bin.trim();
assert.throws(() => new WebAssembly.Module(bin.get()), WebAssembly.CompileError, "WebAssembly.Module doesn't validate: 1 of unknown global, limit is 1, in function at index 0 (evaluating 'new WebAssembly.Module(bin.get())')");
}
{
// Test set to incorrect type
const builder = new Builder();
builder.Type().End()
.Function().End()
.Global().F32(0, "mutable").End()
.Export()
.Function("setGlobal")
.End()
.Code()
.Function("setGlobal", { params: [], ret: "i32" })
.I32Const(0)
.SetGlobal(0)
.End()
.End()
const bin = builder.WebAssembly();
bin.trim();
assert.throws(() => new WebAssembly.Module(bin.get()), WebAssembly.CompileError, "WebAssembly.Module doesn't validate: set_global 0 with type F32 with a variable of type I32, in function at index 0 (evaluating 'new WebAssembly.Module(bin.get())')");
}
for ( let imp of [undefined, null, {}, () => {}, "number", new Number(4)]) {
// Test import non-number.
const builder = new Builder();
builder.Type().End()
.Import()
.Global().I32("imp", "global", "immutable").End()
.End()
.Function().End()
.Global().GetGlobal("i32", 0, "immutable").End()
.Export()
.Function("getGlobal")
.Global("global", 1)
.End()
.Code()
.Function("getGlobal", { params: [], ret: "i32" })
.GetGlobal(1)
.End()
.End()
const bin = builder.WebAssembly();
bin.trim();
const module = new WebAssembly.Module(bin.get());
assert.throws(() => new WebAssembly.Instance(module, { imp: { global: imp } }), WebAssembly.LinkError, "imported global imp:global must be a number (evaluating 'new WebAssembly.Instance(module, { imp: { global: imp } })')");
}
{
const builder = new Builder()
.Type().End()
.Global().I64(0, "immutable").End()
.Export()
.Global("bigInt", 0)
.End();
const module = new WebAssembly.Module(builder.WebAssembly().get());
let instance = new WebAssembly.Instance(module);
assert.eq(instance.exports.bigInt.value, 0n);
}
{
const builder = new Builder()
.Type().End()
.Import()
.Global().I64("imp", "global", "immutable").End()
.End()
.Function().End()
.Global().GetGlobal("i64", 0, "immutable").End();
const module = new WebAssembly.Module(builder.WebAssembly().get());
assert.throws(() => new WebAssembly.Instance(module, { imp: { global: undefined } }), WebAssembly.LinkError, "imported global imp:global must be a BigInt (evaluating 'new WebAssembly.Instance(module, { imp: { global: undefined } })')");
}