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

Commit f92692f

Browse files
authored
[js-api] Add spec tests for proposed API additions (#155)
1 parent 059df76 commit f92692f

8 files changed

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

test/js-api/tag/type.tentative.any.js

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 tag = new WebAssembly.Tag(argument);
6+
const tagtype = tag.type();
7+
8+
assert_array_equals(tagtype.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]");

0 commit comments

Comments
 (0)