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

Commit 65025f9

Browse files
committed
Add JS-API tests for exception handling proposal
1 parent a5e87e4 commit 65025f9

File tree

7 files changed

+241
-0
lines changed

7 files changed

+241
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// META: global=window,dedicatedworker,jsshell
2+
// META: script=/wasm/jsapi/assertions.js
3+
4+
test(() => {
5+
assert_function_name(WebAssembly.Exception, "Exception", "WebAssembly.Exception");
6+
}, "name");
7+
8+
test(() => {
9+
assert_function_length(WebAssembly.Exception, 1, "WebAssembly.Exception");
10+
}, "length");
11+
12+
test(() => {
13+
assert_throws_js(TypeError, () => new WebAssembly.Exception());
14+
}, "No arguments");
15+
16+
test(() => {
17+
const argument = { "parameters": [] };
18+
assert_throws_js(TypeError, () => WebAssembly.Exception(argument));
19+
}, "Calling");
20+
21+
test(() => {
22+
const invalidArguments = [
23+
undefined,
24+
null,
25+
false,
26+
true,
27+
"",
28+
"test",
29+
Symbol(),
30+
1,
31+
NaN,
32+
{},
33+
];
34+
for (const invalidArgument of invalidArguments) {
35+
assert_throws_js(TypeError,
36+
() => new WebAssembly.Exception(invalidArgument),
37+
`new Exception(${format_value(invalidArgument)})`);
38+
}
39+
}, "Invalid descriptor argument");
40+
41+
test(() => {
42+
const invalidTypes = ["i16", "i128", "f16", "f128", "u32", "u64", "i32\0"];
43+
for (const value of invalidTypes) {
44+
const argument = { parameters: [value] };
45+
assert_throws_js(TypeError, () => new WebAssembly.Exception(argument));
46+
}
47+
}, "Invalid type parameter");

test/js-api/exception/toString.any.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// META: global=window,dedicatedworker,jsshell
2+
3+
test(() => {
4+
const argument = { "parameters": [] };
5+
const exception = new WebAssembly.Exception(argument);
6+
assert_class_string(exception, "WebAssembly.Exception");
7+
}, "Object.prototype.toString on an Exception");
8+
9+
test(() => {
10+
assert_own_property(WebAssembly.Exception.prototype, Symbol.toStringTag);
11+
12+
const propDesc = Object.getOwnPropertyDescriptor(WebAssembly.Exception.prototype, Symbol.toStringTag);
13+
assert_equals(propDesc.value, "WebAssembly.Exception", "value");
14+
assert_equals(propDesc.configurable, true, "configurable");
15+
assert_equals(propDesc.enumerable, false, "enumerable");
16+
assert_equals(propDesc.writable, false, "writable");
17+
}, "@@toStringTag exists on the prototype with the appropriate descriptor");
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// META: global=window,dedicatedworker,jsshell
2+
// META: script=/wasm/jsapi/assertions.js
3+
4+
function assert_type(argument) {
5+
const exception = new WebAssembly.Exception(argument);
6+
const exceptiontype = exception.type();
7+
8+
assert_array_equals(exceptiontype.parameters, argument.parameters);
9+
}
10+
11+
test(() => {
12+
assert_type({ "parameters": [] });
13+
}, "[]");
14+
15+
test(() => {
16+
assert_type({ "parameters": ["i32", "i64"] });
17+
}, "[i32 i64]");
18+
19+
test(() => {
20+
assert_type({ "parameters": ["i32", "i64", "f32", "f64"] });
21+
}, "[i32 i64 f32 f64]");
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// META: global=window,dedicatedworker,jsshell
2+
// META: script=/wasm/jsapi/assertions.js
3+
4+
test(() => {
5+
assert_function_name(WebAssembly.RuntimeException, "RuntimeException", "WebAssembly.RuntimeException");
6+
}, "name");
7+
8+
test(() => {
9+
assert_function_length(WebAssembly.RuntimeException, 1, "WebAssembly.RuntimeException");
10+
}, "length");
11+
12+
test(() => {
13+
assert_throws_js(TypeError, () => new WebAssembly.RuntimeException());
14+
}, "No arguments");
15+
16+
test(() => {
17+
const argument = new WebAssembly.Exception({ parameters: [] });
18+
assert_throws_js(TypeError, () => WebAssembly.RuntimeException(argument));
19+
}, "Calling");
20+
21+
test(() => {
22+
const invalidArguments = [
23+
undefined,
24+
null,
25+
false,
26+
true,
27+
"",
28+
"test",
29+
Symbol(),
30+
1,
31+
NaN,
32+
{},
33+
];
34+
for (const invalidArgument of invalidArguments) {
35+
assert_throws_js(TypeError,
36+
() => new WebAssembly.RuntimeException(invalidArgument),
37+
`new RuntimeException(${format_value(invalidArgument)})`);
38+
}
39+
}, "Invalid descriptor argument");
40+
41+
test(() => {
42+
const typesAndArgs = [["i32", 123n], ["i32", Symbol()], ["f32", 123n], ["f64", 123n], ["i64", undefined]];
43+
for (const typeAndArg of typesAndArgs) {
44+
const exn = new WebAssembly.Exception({ parameters: [typeAndArg[0]] });
45+
assert_throws_js(TypeError, () => new WebAssembly.RuntimeException(exn, typeAndArg[1]));
46+
}
47+
}, "Invalid exception argument");
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// META: global=window,dedicatedworker,jsshell
2+
// META: script=/wasm/jsapi/memory/assertions.js
3+
4+
test(() => {
5+
const exn = new WebAssembly.Exception({ "parameters": [] })
6+
const runtimeExn = new WebAssembly.RuntimeException(exn);
7+
assert_throws_js(TypeError, () => runtimeExn.getArg());
8+
assert_throws_js(TypeError, () => runtimeExn.getArg(exn));
9+
}, "Missing arguments");
10+
11+
test(() => {
12+
const invalidValues = [
13+
undefined,
14+
null,
15+
true,
16+
"",
17+
Symbol(),
18+
1,
19+
{}
20+
];
21+
const exn = new WebAssembly.Exception({ "parameters": [] })
22+
const runtimeExn = new WebAssembly.RuntimeException(exn);
23+
for (argument of invalidValues) {
24+
assert_throws_js(TypeError, () => runtimeExn.getArg(argument, 0));
25+
}
26+
}, "Invalid exception argument");
27+
28+
test(() => {
29+
const exn = new WebAssembly.Exception({ "parameters": [] })
30+
const runtimeExn = new WebAssembly.RuntimeException(exn);
31+
assert_throws_js(RangeError, () => runtimeExn.getArg(exn, 1));
32+
}, "Index out of bounds");
33+
34+
test(() => {
35+
const outOfRangeValues = [
36+
undefined,
37+
NaN,
38+
Infinity,
39+
-Infinity,
40+
-1,
41+
0x100000000,
42+
0x1000000000,
43+
"0x100000000",
44+
{ valueOf() { return 0x100000000; } },
45+
];
46+
47+
const exn = new WebAssembly.Exception({ "parameters": [] })
48+
const runtimeExn = new WebAssembly.RuntimeException(exn);
49+
for (const value of outOfRangeValues) {
50+
assert_throws_js(TypeError, () => runtimeExn.getArg(exn, value));
51+
}
52+
}, "Getting out-of-range argument");
53+
54+
test(() => {
55+
const exn = new WebAssembly.Exception({ "parameters": ["i32"] })
56+
const runtimeExn = new WebAssembly.RuntimeException(exn, 42);
57+
assert_equals(runtimeExn.getArg(exn, 0), 42);
58+
}, "getArg");
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// META: global=window,dedicatedworker,jsshell
2+
// META: script=/wasm/jsapi/memory/assertions.js
3+
4+
test(() => {
5+
const exn = new WebAssembly.Exception({ "parameters": [] })
6+
const runtimeExn = new WebAssembly.RuntimeException(exn);
7+
assert_throws_js(TypeError, () => runtimeExn.is());
8+
}, "Missing arguments");
9+
10+
test(() => {
11+
const invalidValues = [
12+
undefined,
13+
null,
14+
true,
15+
"",
16+
Symbol(),
17+
1,
18+
{}
19+
];
20+
const exn = new WebAssembly.Exception({ "parameters": [] })
21+
const runtimeExn = new WebAssembly.RuntimeException(exn);
22+
for (argument of invalidValues) {
23+
assert_throws_js(TypeError, () => runtimeExn.is(argument));
24+
}
25+
}, "Invalid exception argument");
26+
27+
test(() => {
28+
const exn1 = new WebAssembly.Exception({ "parameters": ["i32"] })
29+
const exn2 = new WebAssembly.Exception({ "parameters": ["i32"] })
30+
const runtimeExn = new WebAssembly.RuntimeException(exn1, 42);
31+
assert_true(runtimeExn.is(exn1));
32+
assert_false(runtimeExn.is(exn2));
33+
}, "is");
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// META: global=window,dedicatedworker,jsshell
2+
3+
test(() => {
4+
const argument = { "parameters": [] };
5+
const exception = new WebAssembly.Exception(argument);
6+
const runtimeException = new WebAssembly.RuntimeException(exception);
7+
assert_class_string(runtimeException, "WebAssembly.RuntimeException");
8+
}, "Object.prototype.toString on a RuntimeException");
9+
10+
test(() => {
11+
assert_own_property(WebAssembly.RuntimeException.prototype, Symbol.toStringTag);
12+
13+
const propDesc = Object.getOwnPropertyDescriptor(WebAssembly.RuntimeException.prototype, Symbol.toStringTag);
14+
assert_equals(propDesc.value, "WebAssembly.RuntimeException", "value");
15+
assert_equals(propDesc.configurable, true, "configurable");
16+
assert_equals(propDesc.enumerable, false, "enumerable");
17+
assert_equals(propDesc.writable, false, "writable");
18+
}, "@@toStringTag exists on the prototype with the appropriate descriptor");

0 commit comments

Comments
 (0)