Debian Codegraph @

js/src/jit-test/tests/wasm/spec/exception-handling/harness/harness.js

raw
1 "use strict";
2
3 /* Copyright 2021 Mozilla Foundation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 if (!wasmIsSupported()) {
19 quit();
20 }
21
22 function bytes(type, bytes) {
23 var typedBuffer = new Uint8Array(bytes);
24 return wasmGlobalFromArrayBuffer(type, typedBuffer.buffer);
25 }
26 function value(type, value) {
27 return new WebAssembly.Global({
28 value: type,
29 mutable: false,
30 }, value);
31 }
32
33 function i8x16(elements) {
34 let typedBuffer = new Uint8Array(elements);
35 return wasmGlobalFromArrayBuffer("v128", typedBuffer.buffer);
36 }
37 function i16x8(elements) {
38 let typedBuffer = new Uint16Array(elements);
39 return wasmGlobalFromArrayBuffer("v128", typedBuffer.buffer);
40 }
41 function i32x4(elements) {
42 let typedBuffer = new Uint32Array(elements);
43 return wasmGlobalFromArrayBuffer("v128", typedBuffer.buffer);
44 }
45 function i64x2(elements) {
46 let typedBuffer = new BigUint64Array(elements);
47 return wasmGlobalFromArrayBuffer("v128", typedBuffer.buffer);
48 }
49 function f32x4(elements) {
50 let typedBuffer = new Float32Array(elements);
51 return wasmGlobalFromArrayBuffer("v128", typedBuffer.buffer);
52 }
53 function f64x2(elements) {
54 let typedBuffer = new Float64Array(elements);
55 return wasmGlobalFromArrayBuffer("v128", typedBuffer.buffer);
56 }
57
58 function either(...arr) {
59 return new EitherVariants(arr);
60 }
61
62 class F32x4Pattern {
63 constructor(x, y, z, w) {
64 this.x = x;
65 this.y = y;
66 this.z = z;
67 this.w = w;
68 }
69 }
70
71 class F64x2Pattern {
72 constructor(x, y) {
73 this.x = x;
74 this.y = y;
75 }
76 }
77
78 class RefWithType {
79 constructor(type) {
80 this.type = type;
81 }
82
83 formatExpected() {
84 return `RefWithType(${this.type})`;
85 }
86
87 test(refGlobal) {
88 try {
89 new WebAssembly.Global({value: this.type}, refGlobal.value);
90 return true;
91 } catch (err) {
92 assertEq(err instanceof TypeError, true, `wrong type of error when creating global: ${err}`);
93 assertEq(!!err.message.match(/can only pass/), true, `wrong type of error when creating global: ${err}`);
94 return false;
95 }
96 }
97 }
98
99 // ref.extern values created by spec tests will be JS objects of the form
100 // { [externsym]: <number> }. Other externref values are possible to observe
101 // if extern.convert_any is used.
102 let externsym = Symbol("externref");
103 function externref(s) {
104 return { [externsym]: s };
105 }
106 function is_externref(x) {
107 return (x !== null && externsym in x) ? 1 : 0;
108 }
109 function is_funcref(x) {
110 return typeof x === "function" ? 1 : 0;
111 }
112 function eq_externref(x, y) {
113 return x === y ? 1 : 0;
114 }
115 function eq_funcref(x, y) {
116 return x === y ? 1 : 0;
117 }
118
119 class ExternRefResult {
120 constructor(n) {
121 this.n = n;
122 }
123
124 formatExpected() {
125 return `ref.extern ${this.n}`;
126 }
127
128 test(global) {
129 // the global's value can either be an externref or just a plain old JS number
130 let result = global.value;
131 if (typeof global.value === "object" && externsym in global.value) {
132 result = global.value[externsym];
133 }
134 return result === this.n;
135 }
136 }
137
138 // ref.host values created by spectests will be whatever the JS API does to
139 // convert the given value to anyref. It should implicitly be like any.convert_extern.
140 function hostref(v) {
141 const { internalizeNum } = new WebAssembly.Instance(
142 new WebAssembly.Module(wasmTextToBinary(`(module
143 (func (import "test" "coerce") (param i32) (result anyref))
144 (func (export "internalizeNum") (param i32) (result anyref)
145 (call 0 (local.get 0))
146 )
147 )`)),
148 { "test": { "coerce": x => x } },
149 ).exports;
150 return internalizeNum(v);
151 }
152
153 class HostRefResult {
154 constructor(n) {
155 this.n = n;
156 }
157
158 formatExpected() {
159 return `ref.host ${this.n}`;
160 }
161
162 test(externrefGlobal) {
163 assertEq(externsym in externrefGlobal.value, true, `HostRefResult only works with externref inputs`);
164 return externrefGlobal.value[externsym] === this.n;
165 }
166 }
167
168 let spectest = {
169 externref: externref,
170 is_externref: is_externref,
171 is_funcref: is_funcref,
172 eq_externref: eq_externref,
173 eq_funcref: eq_funcref,
174 print: console.log.bind(console),
175 print_i32: console.log.bind(console),
176 print_i32_f32: console.log.bind(console),
177 print_f64_f64: console.log.bind(console),
178 print_f32: console.log.bind(console),
179 print_f64: console.log.bind(console),
180 global_i32: 666,
181 global_i64: 666n,
182 global_f32: 666,
183 global_f64: 666,
184 table: new WebAssembly.Table({
185 initial: 10,
186 maximum: 20,
187 element: "anyfunc",
188 }),
189 memory: new WebAssembly.Memory({ initial: 1, maximum: 2 }),
190 };
191
192 let linkage = {
193 spectest,
194 };
195
196 function module(source) {
197 let bytecode = wasmTextToBinary(source);
198 let module = new WebAssembly.Module(bytecode);
199 return module;
200 }
201
202 function instantiate(source) {
203 let bytecode = wasmTextToBinary(source);
204 let module = new WebAssembly.Module(bytecode);
205 let instance = new WebAssembly.Instance(module, linkage);
206 return instance.exports;
207 }
208
209 function instantiateFromModule(module) {
210 let instance = new WebAssembly.Instance(module, linkage);
211 return instance.exports;
212 }
213
214 function register(instance, name) {
215 linkage[name] = instance;
216 }
217
218 function invoke(instance, field, params) {
219 let func = instance[field];
220 assertEq(func instanceof Function, true, "expected a function");
221 return wasmLosslessInvoke(func, ...params);
222 }
223
224 function get(instance, field) {
225 let global = instance[field];
226 assertEq(
227 global instanceof WebAssembly.Global,
228 true,
229 "expected a WebAssembly.Global",
230 );
231 return global;
232 }
233
234 function assert_trap(thunk, message) {
235 try {
236 thunk();
237 throw new Error(`got no error`);
238 } catch (err) {
239 if (err instanceof WebAssembly.RuntimeError) {
240 return;
241 }
242 err.message = `expected trap (${message}): ${err.message}`;
243 throw err;
244 }
245 }
246
247 let StackOverflow;
248 try {
249 (function f() {
250 1 + f();
251 })();
252 } catch (e) {
253 StackOverflow = e.constructor;
254 }
255 function assert_exhaustion(thunk, message) {
256 try {
257 thunk();
258 throw new Error(`got no error`);
259 } catch (err) {
260 if (err instanceof StackOverflow) {
261 return;
262 }
263 err.message = `expected exhaustion (${message}): ${err.message}`;
264 throw err;
265 }
266 }
267
268 function assert_invalid(thunk, message) {
269 try {
270 thunk();
271 throw new Error(`got no error`);
272 } catch (err) {
273 if (err instanceof WebAssembly.LinkError || err instanceof WebAssembly.CompileError) {
274 return;
275 }
276 err.message = `expected invalid module (${message}): ${err.message}`;
277 throw err;
278 }
279 }
280
281 function assert_unlinkable(thunk, message) {
282 try {
283 thunk();
284 throw new Error(`got no error`);
285 } catch (err) {
286 if (err instanceof WebAssembly.LinkError || err instanceof WebAssembly.CompileError) {
287 return;
288 }
289 err.message = `expected an unlinkable module (${message}): ${err.message}`;
290 throw err;
291 }
292 }
293
294 function assert_malformed(thunk, message) {
295 try {
296 thunk();
297 throw new Error(`got no error`);
298 } catch (err) {
299 if (
300 err instanceof TypeError ||
301 err instanceof SyntaxError ||
302 err instanceof WebAssembly.CompileError ||
303 err instanceof WebAssembly.LinkError
304 ) {
305 return;
306 }
307 err.message = `expected a malformed module (${message}): ${err.message}`;
308 throw err;
309 }
310 }
311
312 function assert_exception(thunk) {
313 let thrown = false;
314 try {
315 thunk();
316 } catch (err) {
317 thrown = true;
318 }
319 assertEq(thrown, true, "expected an exception to be thrown");
320 }
321
322 function assert_return(thunk, expected) {
323 let results = thunk();
324
325 if (results === undefined) {
326 results = [];
327 } else if (!Array.isArray(results)) {
328 results = [results];
329 }
330 if (!Array.isArray(expected)) {
331 expected = [expected];
332 }
333
334 if (!compareResults(results, expected)) {
335 let got = results.map((x) => formatResult(x)).join(", ");
336 let wanted = expected.map((x) => formatExpected(x)).join(", ");
337 assertEq(
338 `[${got}]`,
339 `[${wanted}]`,
340 );
341 assertEq(true, false, `${got} !== ${wanted}`);
342 }
343 }
344
345 function formatResult(result) {
346 if (typeof (result) === "object") {
347 return wasmGlobalToString(result);
348 } else {
349 return `${result}`;
350 }
351 }
352
353 function formatExpected(expected) {
354 if (
355 expected === `f32_canonical_nan` ||
356 expected === `f32_arithmetic_nan` ||
357 expected === `f64_canonical_nan` ||
358 expected === `f64_arithmetic_nan`
359 ) {
360 return expected;
361 } else if (expected instanceof F32x4Pattern) {
362 return `f32x4(${formatExpected(expected.x)}, ${
363 formatExpected(expected.y)
364 }, ${formatExpected(expected.z)}, ${formatExpected(expected.w)})`;
365 } else if (expected instanceof F64x2Pattern) {
366 return `f64x2(${formatExpected(expected.x)}, ${
367 formatExpected(expected.y)
368 })`;
369 } else if (expected instanceof EitherVariants) {
370 return expected.formatExpected();
371 } else if (expected instanceof RefWithType) {
372 return expected.formatExpected();
373 } else if (expected instanceof ExternRefResult) {
374 return expected.formatExpected();
375 } else if (expected instanceof HostRefResult) {
376 return expected.formatExpected();
377 } else if (typeof (expected) === "object") {
378 return wasmGlobalToString(expected);
379 } else {
380 throw new Error("unknown expected result");
381 }
382 }
383
384 class EitherVariants {
385 constructor(arr) {
386 this.arr = arr;
387 }
388 matches(v) {
389 return this.arr.some((e) => compareResult(v, e));
390 }
391 formatExpected() {
392 return `either(${this.arr.map(formatExpected).join(", ")})`;
393 }
394 }
395
396 function compareResults(results, expected) {
397 if (results.length !== expected.length) {
398 return false;
399 }
400 for (let i in results) {
401 if (expected[i] instanceof EitherVariants) {
402 return expected[i].matches(results[i]);
403 }
404 if (!compareResult(results[i], expected[i])) {
405 return false;
406 }
407 }
408 return true;
409 }
410
411 function compareResult(result, expected) {
412 if (
413 expected === `canonical_nan` ||
414 expected === `arithmetic_nan`
415 ) {
416 return wasmGlobalIsNaN(result, expected);
417 } else if (expected === null) {
418 return result.value === null;
419 } else if (expected instanceof F32x4Pattern) {
420 return compareResult(
421 wasmGlobalExtractLane(result, "f32x4", 0),
422 expected.x,
423 ) &&
424 compareResult(wasmGlobalExtractLane(result, "f32x4", 1), expected.y) &&
425 compareResult(wasmGlobalExtractLane(result, "f32x4", 2), expected.z) &&
426 compareResult(wasmGlobalExtractLane(result, "f32x4", 3), expected.w);
427 } else if (expected instanceof F64x2Pattern) {
428 return compareResult(
429 wasmGlobalExtractLane(result, "f64x2", 0),
430 expected.x,
431 ) &&
432 compareResult(wasmGlobalExtractLane(result, "f64x2", 1), expected.y);
433 } else if (expected instanceof RefWithType) {
434 return expected.test(result);
435 } else if (expected instanceof ExternRefResult) {
436 return expected.test(result);
437 } else if (expected instanceof HostRefResult) {
438 return expected.test(result);
439 } else if (typeof (expected) === "object") {
440 return wasmGlobalsEqual(result, expected);
441 } else {
442 throw new Error("unknown expected result");
443 }
444 }