From 2f5a7c524fdc4dec280c6a53bc3cc800b6ad1c11 Mon Sep 17 00:00:00 2001 From: Heejin Ahn Date: Mon, 24 Jun 2024 20:57:01 -0700 Subject: [PATCH] [js-api] Update wasm-module-builder.js for exnref (#314) 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`. --- test/js-api/wasm-module-builder.js | 25 +++++++++++++------ .../exceptions/js-api/basic.tentative.any.js | 4 +-- .../js-api/identity.tentative.any.js | 10 ++++---- 3 files changed, 25 insertions(+), 14 deletions(-) diff --git a/test/js-api/wasm-module-builder.js b/test/js-api/wasm-module-builder.js index 58b616f0..b23cea5f 100644 --- a/test/js-api/wasm-module-builder.js +++ b/test/js-api/wasm-module-builder.js @@ -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; @@ -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; @@ -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; @@ -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]); @@ -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; @@ -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; @@ -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; @@ -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); @@ -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); } }); diff --git a/test/legacy/exceptions/js-api/basic.tentative.any.js b/test/legacy/exceptions/js-api/basic.tentative.any.js index 9b82d2ea..c57c79cc 100644 --- a/test/legacy/exceptions/js-api/basic.tentative.any.js +++ b/test/legacy/exceptions/js-api/basic.tentative.any.js @@ -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, @@ -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, diff --git a/test/legacy/exceptions/js-api/identity.tentative.any.js b/test/legacy/exceptions/js-api/identity.tentative.any.js index 0cdb41d2..36651c7d 100644 --- a/test/legacy/exceptions/js-api/identity.tentative.any.js +++ b/test/legacy/exceptions/js-api/identity.tentative.any.js @@ -35,7 +35,7 @@ test(() => { builder .addFunction("catch_js_tag_rethrow", kSig_v_v) .addBody([ - kExprTry, kWasmStmt, + kExprTry, kWasmVoid, kExprCallFunction, throwJSTagExnIndex, kExprCatch, jsTagIndex, kExprDrop, @@ -49,7 +49,7 @@ test(() => { builder .addFunction("catch_wasm_tag_rethrow", kSig_v_v) .addBody([ - kExprTry, kWasmStmt, + kExprTry, kWasmVoid, kExprCallFunction, throwWasmTagExnIndex, kExprCatch, wasmTagIndex, kExprDrop, @@ -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, @@ -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, @@ -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,