Skip to content

Commit

Permalink
[js-api] Update wasm-module-builder.js for exnref (#314)
Browse files Browse the repository at this point in the history
This pulls in some exnref-related changes from
https://github.com/v8/v8/blob/main/test/mjsunit/wasm/wasm-module-builder.js
but does not sync with it completely. That v8 version contains a lot of
features from new proposals that are not really relevant to EH and
exnref, so this pulls only the relevant EH parts in.

This also renames `kWasmStmt` to `kWasmVoid` as in the V8 version,
because it looks more intuitive. Also renames `kTagAttribute` to
`kExceptionAttribute`.
  • Loading branch information
aheejin authored Jun 25, 2024
1 parent f0282c8 commit 2f5a7c5
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
25 changes: 18 additions & 7 deletions test/js-api/wasm-module-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ let kDeclFunctionImport = 0x02;
let kDeclFunctionLocals = 0x04;
let kDeclFunctionExport = 0x08;

// Local types
let kWasmStmt = 0x40;
// Value types and related
let kWasmVoid = 0x40;
let kWasmI32 = 0x7f;
let kWasmI64 = 0x7e;
let kWasmF32 = 0x7d;
Expand All @@ -107,6 +107,8 @@ let kWasmFuncRef = -0x10;
let kWasmAnyFunc = kWasmFuncRef; // Alias named as in the JS API spec
let kWasmExternRef = -0x11;
let kWasmAnyRef = -0x12;
let kWasmExnRef = -0x17;
let kWasmNullExnRef = -0x0c;

// Use the positive-byte versions inside function bodies.
let kLeb128Mask = 0x7f;
Expand All @@ -116,6 +118,8 @@ let kExternRefCode = kWasmExternRef & kLeb128Mask;
let kAnyRefCode = kWasmAnyRef & kLeb128Mask;
let kNullExternRefCode = kWasmNullExternRef & kLeb128Mask;
let kNullFuncRefCode = kWasmNullFuncRef & kLeb128Mask;
let kExnRefCode = kWasmExnRef & kLeb128Mask;
let kNullExnRefCode = kWasmNullExnRef & kLeb128Mask;
let kNullRefCode = kWasmNullRef & kLeb128Mask;

let kWasmRefNull = 0x63;
Expand All @@ -137,7 +141,7 @@ let kTableZero = 0;
let kMemoryZero = 0;
let kSegmentZero = 0;

let kTagAttribute = 0;
let kExceptionAttribute = 0;

// Useful signatures
let kSig_i_i = makeSig([kWasmI32], [kWasmI32]);
Expand Down Expand Up @@ -217,10 +221,9 @@ let kExprIf = 0x04;
let kExprElse = 0x05;
let kExprTry = 0x06;
let kExprCatch = 0x07;
let kExprCatchAll = 0x19;
let kExprThrow = 0x08;
let kExprRethrow = 0x09;
let kExprBrOnExn = 0x0a;
let kExprThrowRef = 0x0a;
let kExprEnd = 0x0b;
let kExprBr = 0x0c;
let kExprBrIf = 0x0d;
Expand All @@ -230,8 +233,10 @@ let kExprCallFunction = 0x10;
let kExprCallIndirect = 0x11;
let kExprReturnCall = 0x12;
let kExprReturnCallIndirect = 0x13;
let kExprCatchAll = 0x19;
let kExprDrop = 0x1a;
let kExprSelect = 0x1b;
let kExprTryTable = 0x1f;
let kExprLocalGet = 0x20;
let kExprLocalSet = 0x21;
let kExprLocalTee = 0x22;
Expand Down Expand Up @@ -494,6 +499,12 @@ let kExprI32x4Eq = 0x2c;
let kExprS1x4AllTrue = 0x75;
let kExprF32x4Min = 0x9e;

// Exception handling with exnref.
let kCatchNoRef = 0x0;
let kCatchRef = 0x1;
let kCatchAllNoRef = 0x2;
let kCatchAllRef = 0x3;

class Binary {
constructor() {
this.length = 0;
Expand Down Expand Up @@ -976,7 +987,7 @@ class WasmModuleBuilder {
section.emit_u32v(imp.initial); // initial
if (has_max) section.emit_u32v(imp.maximum); // maximum
} else if (imp.kind == kExternalTag) {
section.emit_u32v(kTagAttribute);
section.emit_u32v(kExceptionAttribute);
section.emit_u32v(imp.type);
} else {
throw new Error("unknown/unsupported import kind " + imp.kind);
Expand Down Expand Up @@ -1079,7 +1090,7 @@ class WasmModuleBuilder {
binary.emit_section(kTagSectionCode, section => {
section.emit_u32v(wasm.tags.length);
for (let type of wasm.tags) {
section.emit_u32v(kTagAttribute);
section.emit_u32v(kExceptionAttribute);
section.emit_u32v(type);
}
});
Expand Down
4 changes: 2 additions & 2 deletions test/legacy/exceptions/js-api/basic.tentative.any.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ promise_test(async () => {
const tagIndex= builder.addTag(kSig_v_r);
builder.addFunction("catch_exception", kSig_r_v)
.addBody([
kExprTry, kWasmStmt,
kExprTry, kWasmVoid,
kExprCallFunction, fnIndex,
kExprCatch, tagIndex,
kExprReturn,
Expand All @@ -100,7 +100,7 @@ promise_test(async () => {
const fnIndex = builder.addImport("module", "fn", kSig_v_v);
builder.addFunction("catch_and_rethrow", kSig_r_v)
.addBody([
kExprTry, kWasmStmt,
kExprTry, kWasmVoid,
kExprCallFunction, fnIndex,
kExprCatchAll,
kExprRethrow, 0x00,
Expand Down
10 changes: 5 additions & 5 deletions test/legacy/exceptions/js-api/identity.tentative.any.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ test(() => {
builder
.addFunction("catch_js_tag_rethrow", kSig_v_v)
.addBody([
kExprTry, kWasmStmt,
kExprTry, kWasmVoid,
kExprCallFunction, throwJSTagExnIndex,
kExprCatch, jsTagIndex,
kExprDrop,
Expand All @@ -49,7 +49,7 @@ test(() => {
builder
.addFunction("catch_wasm_tag_rethrow", kSig_v_v)
.addBody([
kExprTry, kWasmStmt,
kExprTry, kWasmVoid,
kExprCallFunction, throwWasmTagExnIndex,
kExprCatch, wasmTagIndex,
kExprDrop,
Expand All @@ -63,7 +63,7 @@ test(() => {
builder
.addFunction("catch_all_js_tag_rethrow", kSig_v_v)
.addBody([
kExprTry, kWasmStmt,
kExprTry, kWasmVoid,
kExprCallFunction, throwJSTagExnIndex,
kExprCatchAll,
kExprRethrow, 0x00,
Expand All @@ -76,7 +76,7 @@ test(() => {
builder
.addFunction("catch_all_wasm_tag_rethrow", kSig_v_v)
.addBody([
kExprTry, kWasmStmt,
kExprTry, kWasmVoid,
kExprCallFunction, throwWasmTagExnIndex,
kExprCatchAll,
kExprRethrow, 0x00,
Expand All @@ -103,7 +103,7 @@ test(() => {
builder
.addFunction("catch_js_tag_throw_payload", kSig_v_v)
.addBody([
kExprTry, kWasmStmt,
kExprTry, kWasmVoid,
kExprCallFunction, throwJSTagExnIndex,
kExprCatch, jsTagIndex,
kExprThrow, jsTagIndex,
Expand Down

0 comments on commit 2f5a7c5

Please sign in to comment.