Skip to content
This repository was archived by the owner on Apr 25, 2025. It is now read-only.

Commit c0f30ca

Browse files
authored
[test] Update JS harnesses to match interpreter harness (#86)
1 parent 9f6a39f commit c0f30ca

File tree

2 files changed

+80
-9
lines changed

2 files changed

+80
-9
lines changed

test/harness/async_index.js

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,22 @@ const EXPECT_INVALID = false;
5252

5353
/* DATA **********************************************************************/
5454

55+
let hostrefs = {};
56+
let hostsym = Symbol("hostref");
57+
function hostref(s) {
58+
if (! (s in hostrefs)) hostrefs[s] = {[hostsym]: s};
59+
return hostrefs[s];
60+
}
61+
function is_hostref(x) {
62+
return (x !== null && hostsym in x) ? 1 : 0;
63+
}
64+
function is_funcref(x) {
65+
return typeof x === "function" ? 1 : 0;
66+
}
67+
function eq_ref(x, y) {
68+
return x === y ? 1 : 0;
69+
}
70+
5571
// Default imports.
5672
var registry = {};
5773

@@ -67,6 +83,10 @@ function reinitializeRegistry() {
6783

6884
chain = chain.then(_ => {
6985
let spectest = {
86+
hostref: hostref,
87+
is_hostref: is_hostref,
88+
is_funcref: is_funcref,
89+
eq_ref: eq_ref,
7090
print: console.log.bind(console),
7191
print_i32: console.log.bind(console),
7292
print_i32_f32: console.log.bind(console),
@@ -180,9 +200,9 @@ function instance(bytes, imports, valid = true) {
180200
return chain;
181201
}
182202

183-
function exports(name, instance) {
203+
function exports(instance) {
184204
return instance.then(inst => {
185-
return { [name]: inst.exports };
205+
return { module: inst.exports, spectest: registry.spectest };
186206
});
187207
}
188208

@@ -243,7 +263,23 @@ function assert_return(action, expected) {
243263
.then(
244264
values => {
245265
uniqueTest(_ => {
246-
assert_equals(values[0], expected, loc);
266+
let actual = values[0];
267+
switch (expected) {
268+
case "nan:canonical":
269+
case "nan:arithmetic":
270+
// Note that JS can't reliably distinguish different NaN values,
271+
// so there's no good way to test that it's a canonical NaN.
272+
assert_true(Number.isNaN(actual), `expected NaN, observed ${actual}.`);
273+
return;
274+
case "ref.func":
275+
assert_true(typeof actual === "function", `expected Wasm function, got ${actual}`);
276+
return;
277+
case "ref.any":
278+
assert_true(actual !== null, `expected Wasm reference, got ${actual}`);
279+
return;
280+
default:
281+
assert_equals(actual, expected);
282+
}
247283
}, test);
248284
},
249285
error => {

test/harness/sync_index.js

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,22 @@ const EXPECT_INVALID = false;
6666

6767
/* DATA **********************************************************************/
6868

69+
let hostrefs = {};
70+
let hostsym = Symbol("hostref");
71+
function hostref(s) {
72+
if (! (s in hostrefs)) hostrefs[s] = {[hostsym]: s};
73+
return hostrefs[s];
74+
}
75+
function is_hostref(x) {
76+
return (x !== null && hostsym in x) ? 1 : 0;
77+
}
78+
function is_funcref(x) {
79+
return typeof x === "function" ? 1 : 0;
80+
}
81+
function eq_ref(x, y) {
82+
return x === y ? 1 : 0;
83+
}
84+
6985
let $$;
7086

7187
// Default imports.
@@ -77,6 +93,10 @@ function reinitializeRegistry() {
7793
return;
7894

7995
let spectest = {
96+
hostref: hostref,
97+
is_hostref: is_hostref,
98+
is_funcref: is_funcref,
99+
eq_ref: eq_ref,
80100
print: console.log.bind(console),
81101
print_i32: console.log.bind(console),
82102
print_i32_f32: console.log.bind(console),
@@ -228,13 +248,13 @@ function get(instance, name) {
228248
return ValueResult((v instanceof WebAssembly.Global) ? v.value : v);
229249
}
230250

231-
function exports(name, instance) {
251+
function exports(instance) {
232252
_assert(instance instanceof Result);
233253

234254
if (instance.isError())
235255
return instance;
236256

237-
return ValueResult({ [name]: instance.value.exports });
257+
return ValueResult({ module: instance.value.exports, spectest: registry.spectest });
238258
}
239259

240260
function run(action) {
@@ -320,11 +340,26 @@ function assert_return(action, expected) {
320340

321341
uniqueTest(() => {
322342
assert_true(!result.isError(), `expected success result, got: ${result.value}.`);
323-
if (!result.isError()) {
324-
assert_equals(result.value, expected);
325-
};
343+
let actual = result.value;
344+
switch (expected) {
345+
case "nan:canonical":
346+
case "nan:arithmetic":
347+
// Note that JS can't reliably distinguish different NaN values,
348+
// so there's no good way to test that it's a canonical NaN.
349+
assert_true(Number.isNaN(actual), `expected NaN, observed ${actual}.`);
350+
return;
351+
case "ref.func":
352+
assert_true(typeof actual === "function", `expected Wasm function, got ${actual}`);
353+
return;
354+
case "ref.any":
355+
assert_true(actual !== null, `expected Wasm reference, got ${actual}`);
356+
return;
357+
default:
358+
assert_equals(actual, expected);
359+
}
360+
326361
}, "A wast module that must return a particular value.");
327-
};
362+
}
328363

329364
function assert_return_nan(action) {
330365
let result = action();

0 commit comments

Comments
 (0)