Skip to content

Commit

Permalink
test: update wasm/jsapi WPT
Browse files Browse the repository at this point in the history
PR-URL: #47210
Reviewed-By: Michael Dawson <midawson@redhat.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Filip Skokan <panva.ip@gmail.com>
  • Loading branch information
targos authored and RafaelGSS committed Apr 8, 2023
1 parent 30c667e commit e2eb054
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 40 deletions.
2 changes: 1 addition & 1 deletion test/fixtures/wpt/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Last update:
- streams: https://github.com/web-platform-tests/wpt/tree/51750bc8d7/streams
- url: https://github.com/web-platform-tests/wpt/tree/7c5c3cc125/url
- user-timing: https://github.com/web-platform-tests/wpt/tree/df24fb604e/user-timing
- wasm/jsapi: https://github.com/web-platform-tests/wpt/tree/d8dbe6990b/wasm/jsapi
- wasm/jsapi: https://github.com/web-platform-tests/wpt/tree/cde25e7e3c/wasm/jsapi
- wasm/webapi: https://github.com/web-platform-tests/wpt/tree/fd1b23eeaa/wasm/webapi
- WebCryptoAPI: https://github.com/web-platform-tests/wpt/tree/188993d46b/WebCryptoAPI
- webidl/ecmascript-binding/es-exceptions: https://github.com/web-platform-tests/wpt/tree/a370aad338/webidl/ecmascript-binding/es-exceptions
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/wpt/versions.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
"path": "user-timing"
},
"wasm/jsapi": {
"commit": "d8dbe6990bed03ec03beee25069a9347d4c3d6d5",
"commit": "cde25e7e3c3b9d2280eb088a3fb9da988793d255",
"path": "wasm/jsapi"
},
"wasm/webapi": {
Expand Down
16 changes: 8 additions & 8 deletions test/fixtures/wpt/wasm/jsapi/exception/basic.tentative.any.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ promise_test(async () => {
const kWasmAnyRef = 0x6f;
const kSig_v_r = makeSig([kWasmAnyRef], []);
const builder = new WasmModuleBuilder();
const except = builder.addException(kSig_v_r);
const tagIndex = builder.addTag(kSig_v_r);
builder.addFunction("throw_param", kSig_v_r)
.addBody([
kExprLocalGet, 0,
kExprThrow, except,
kExprThrow, tagIndex,
])
.exportFunc();
const buffer = builder.toBuffer();
Expand All @@ -45,11 +45,11 @@ promise_test(async () => {

promise_test(async () => {
const builder = new WasmModuleBuilder();
const except = builder.addException(kSig_v_a);
const tagIndex = builder.addTag(kSig_v_a);
builder.addFunction("throw_null", kSig_v_v)
.addBody([
kExprRefNull, kWasmAnyFunc,
kExprThrow, except,
kExprThrow, tagIndex,
])
.exportFunc();
const buffer = builder.toBuffer();
Expand All @@ -59,11 +59,11 @@ promise_test(async () => {

promise_test(async () => {
const builder = new WasmModuleBuilder();
const except = builder.addException(kSig_v_i);
const tagIndex = builder.addTag(kSig_v_i);
builder.addFunction("throw_int", kSig_v_v)
.addBody([
...wasmI32Const(7),
kExprThrow, except,
kExprThrow, tagIndex,
])
.exportFunc();
const buffer = builder.toBuffer();
Expand All @@ -74,12 +74,12 @@ promise_test(async () => {
promise_test(async () => {
const builder = new WasmModuleBuilder();
const fnIndex = builder.addImport("module", "fn", kSig_v_v);
const except = builder.addException(kSig_v_r);
const tagIndex= builder.addTag(kSig_v_r);
builder.addFunction("catch_exception", kSig_r_v)
.addBody([
kExprTry, kWasmStmt,
kExprCallFunction, fnIndex,
kExprCatch, except,
kExprCatch, tagIndex,
kExprReturn,
kExprEnd,
kExprRefNull, kWasmAnyRef,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ test(() => {
}, "No arguments");

test(() => {
const argument = new WebAssembly.Tag({ parameters: [] });
assert_throws_js(TypeError, () => WebAssembly.Exception(argument));
const tag = new WebAssembly.Tag({ parameters: [] });
assert_throws_js(TypeError, () => WebAssembly.Exception(tag));
}, "Calling");

test(() => {
Expand Down Expand Up @@ -53,10 +53,10 @@ test(() => {
["i64", undefined],
];
for (const typeAndArg of typesAndArgs) {
const exn = new WebAssembly.Tag({ parameters: [typeAndArg[0]] });
const tag = new WebAssembly.Tag({ parameters: [typeAndArg[0]] });
assert_throws_js(
TypeError,
() => new WebAssembly.Exception(exn, typeAndArg[1])
() => new WebAssembly.Exception(tag, typeAndArg[1])
);
}
}, "Invalid exception argument");
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ test(() => {
test(() => {
const tag = new WebAssembly.Tag({ parameters: [] });
const exn = new WebAssembly.Exception(tag, []);
assert_throws_js(TypeError, () => exn.getArg(tag, 1));
assert_throws_js(RangeError, () => exn.getArg(tag, 1));
}, "Index out of bounds");

test(() => {
Expand All @@ -43,7 +43,7 @@ test(() => {
const tag = new WebAssembly.Tag({ parameters: [] });
const exn = new WebAssembly.Exception(tag, []);
for (const value of outOfRangeValues) {
assert_throws_js(TypeError, () => exn.getArg(tag, value));
assert_throws_js(RangeError, () => exn.getArg(tag, value));
}
}, "Getting out-of-range argument");

Expand Down
61 changes: 61 additions & 0 deletions test/fixtures/wpt/wasm/jsapi/exception/identity.tentative.any.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// META: global=window,dedicatedworker,jsshell
// META: script=/wasm/jsapi/assertions.js
// META: script=/wasm/jsapi/wasm-module-builder.js

test(() => {
const tag = new WebAssembly.Tag({ parameters: ["i32"] });
const exn = new WebAssembly.Exception(tag, [42]);
const exn_same_payload = new WebAssembly.Exception(tag, [42]);
const exn_diff_payload = new WebAssembly.Exception(tag, [53]);

const builder = new WasmModuleBuilder();
const jsfuncIndex = builder.addImport("module", "jsfunc", kSig_v_v);
const tagIndex = builder.addImportedTag("module", "tag", kSig_v_i);
const imports = {
module: {
jsfunc: function() { throw exn; },
tag: tag
}
};

builder
.addFunction("catch_rethrow", kSig_v_v)
.addBody([
kExprTry, kWasmStmt,
kExprCallFunction, jsfuncIndex,
kExprCatch, tagIndex,
kExprDrop,
kExprRethrow, 0x00,
kExprEnd
])
.exportFunc();

builder
.addFunction("catch_all_rethrow", kSig_v_v)
.addBody([
kExprTry, kWasmStmt,
kExprCallFunction, jsfuncIndex,
kExprCatchAll,
kExprRethrow, 0x00,
kExprEnd
])
.exportFunc();

const buffer = builder.toBuffer();
WebAssembly.instantiate(buffer, imports).then(result => {
try {
result.instance.exports.catch_rethrow();
} catch (e) {
assert_equals(e, exn);
assert_not_equals(e, exn_same_payload);
assert_not_equals(e, exn_diff_payload);
}
try {
result.instance.exports.catch_all_rethrow();
} catch (e) {
assert_equals(e, exn);
assert_not_equals(e, exn_same_payload);
assert_not_equals(e, exn_diff_payload);
}
});
}, "Identity check");
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
test(() => {
const argument = { parameters: [] };
const tag = new WebAssembly.Tag(argument);
const exception = new WebAssembly.Exception(tag, []);
assert_class_string(exception, "WebAssembly.Exception");
const exn = new WebAssembly.Exception(tag, []);
assert_class_string(exn, "WebAssembly.Exception");
}, "Object.prototype.toString on an Exception");

test(() => {
Expand Down
44 changes: 22 additions & 22 deletions test/fixtures/wpt/wasm/jsapi/wasm-module-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ let kElementSectionCode = 9; // Elements section
let kCodeSectionCode = 10; // Function code
let kDataSectionCode = 11; // Data segments
let kDataCountSectionCode = 12; // Data segment count (between Element & Code)
let kExceptionSectionCode = 13; // Exception section (between Global & Export)
let kTagSectionCode = 13; // Tag section (between Memory & Global)

// Name section types
let kModuleNameCode = 0;
Expand Down Expand Up @@ -104,13 +104,13 @@ let kExternalFunction = 0;
let kExternalTable = 1;
let kExternalMemory = 2;
let kExternalGlobal = 3;
let kExternalException = 4;
let kExternalTag = 4;

let kTableZero = 0;
let kMemoryZero = 0;
let kSegmentZero = 0;

let kExceptionAttribute = 0;
let kTagAttribute = 0;

// Useful signatures
let kSig_i_i = makeSig([kWasmI32], [kWasmI32]);
Expand Down Expand Up @@ -681,15 +681,15 @@ class WasmModuleBuilder {
this.exports = [];
this.globals = [];
this.tables = [];
this.exceptions = [];
this.tags = [];
this.functions = [];
this.element_segments = [];
this.data_segments = [];
this.explicit = [];
this.num_imported_funcs = 0;
this.num_imported_globals = 0;
this.num_imported_tables = 0;
this.num_imported_exceptions = 0;
this.num_imported_tags = 0;
return this;
}

Expand Down Expand Up @@ -752,10 +752,10 @@ class WasmModuleBuilder {
return table;
}

addException(type) {
addTag(type) {
let type_index = (typeof type) == "number" ? type : this.addType(type);
let except_index = this.exceptions.length + this.num_imported_exceptions;
this.exceptions.push(type_index);
let except_index = this.tags.length + this.num_imported_tags;
this.tags.push(type_index);
return except_index;
}

Expand Down Expand Up @@ -804,14 +804,14 @@ class WasmModuleBuilder {
return this.num_imported_tables++;
}

addImportedException(module, name, type) {
if (this.exceptions.length != 0) {
throw new Error('Imported exceptions must be declared before local ones');
addImportedTag(module, name, type) {
if (this.tags.length != 0) {
throw new Error('Imported tags must be declared before local ones');
}
let type_index = (typeof type) == "number" ? type : this.addType(type);
let o = {module: module, name: name, kind: kExternalException, type: type_index};
let o = {module: module, name: name, kind: kExternalTag, type: type_index};
this.imports.push(o);
return this.num_imported_exceptions++;
return this.num_imported_tags++;
}

addExport(name, index) {
Expand Down Expand Up @@ -938,8 +938,8 @@ class WasmModuleBuilder {
section.emit_u8(has_max ? 1 : 0); // flags
section.emit_u32v(imp.initial); // initial
if (has_max) section.emit_u32v(imp.maximum); // maximum
} else if (imp.kind == kExternalException) {
section.emit_u32v(kExceptionAttribute);
} else if (imp.kind == kExternalTag) {
section.emit_u32v(kTagAttribute);
section.emit_u32v(imp.type);
} else {
throw new Error("unknown/unsupported import kind " + imp.kind);
Expand Down Expand Up @@ -1036,13 +1036,13 @@ class WasmModuleBuilder {
});
}

// Add exceptions.
if (wasm.exceptions.length > 0) {
if (debug) print("emitting exceptions @ " + binary.length);
binary.emit_section(kExceptionSectionCode, section => {
section.emit_u32v(wasm.exceptions.length);
for (let type of wasm.exceptions) {
section.emit_u32v(kExceptionAttribute);
// Add tags.
if (wasm.tags.length > 0) {
if (debug) print("emitting tags @ " + binary.length);
binary.emit_section(kTagSectionCode, section => {
section.emit_u32v(wasm.tags.length);
for (let type of wasm.tags) {
section.emit_u32v(kTagAttribute);
section.emit_u32v(type);
}
});
Expand Down

0 comments on commit e2eb054

Please sign in to comment.