Skip to content
This repository was archived by the owner on Nov 3, 2021. It is now read-only.

Commit cad266e

Browse files
committed
Check multiple returns in generated JS code
This change modifies `assert_return`. After the Wasm function is invoked, the result will either be `undefined`, a JS `Number`, or a JS `Array`. It's simpler to treat them all as an array, with `undefined` representing an empty array, and `Number` representing an array with one element. We don't check for `Number` here, since as soon as we add support for the reference-types proposal, a scalar result may be an `Object` too. Fixes issue #44.
1 parent ceb8014 commit cad266e

File tree

1 file changed

+25
-15
lines changed

1 file changed

+25
-15
lines changed

Diff for: interpreter/script/js.ml

+25-15
Original file line numberDiff line numberDiff line change
@@ -120,22 +120,32 @@ function assert_exhaustion(action) {
120120
throw new Error("Wasm resource exhaustion expected");
121121
}
122122

123-
function assert_return(action, expected) {
123+
function assert_return(action, ...expected) {
124124
let actual = action();
125-
switch (expected) {
126-
case "nan:canonical":
127-
case "nan:arithmetic":
128-
case "nan:any":
129-
// Note that JS can't reliably distinguish different NaN values,
130-
// so there's no good way to test that it's a canonical NaN.
131-
if (!Number.isNaN(actual)) {
132-
throw new Error("Wasm return value NaN expected, got " + actual);
133-
};
134-
return;
135-
default:
136-
if (!Object.is(actual, expected)) {
137-
throw new Error("Wasm return value " + expected + " expected, got " + actual);
138-
};
125+
if (actual === undefined) {
126+
actual = [];
127+
} else if (!Array.isArray(actual)) {
128+
actual = [actual];
129+
}
130+
if (actual.length !== expected.length) {
131+
throw new Error(expected.length + " value(s) expected, got " + actual.length);
132+
}
133+
for (let i = 0; i < actual.length; ++i) {
134+
switch (expected[i]) {
135+
case "nan:canonical":
136+
case "nan:arithmetic":
137+
case "nan:any":
138+
// Note that JS can't reliably distinguish different NaN values,
139+
// so there's no good way to test that it's a canonical NaN.
140+
if (!Number.isNaN(actual[i])) {
141+
throw new Error("Wasm return value NaN expected, got " + actual[i]);
142+
};
143+
return;
144+
default:
145+
if (!Object.is(actual[i], expected[i])) {
146+
throw new Error("Wasm return value " + expected[i] + " expected, got " + actual[i]);
147+
};
148+
}
139149
}
140150
}
141151
|}

0 commit comments

Comments
 (0)