Skip to content

Commit 2f5a7c5

Browse files
authored
[js-api] Update wasm-module-builder.js for exnref (WebAssembly#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`.
1 parent f0282c8 commit 2f5a7c5

File tree

3 files changed

+25
-14
lines changed

3 files changed

+25
-14
lines changed

test/js-api/wasm-module-builder.js

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ let kDeclFunctionImport = 0x02;
9090
let kDeclFunctionLocals = 0x04;
9191
let kDeclFunctionExport = 0x08;
9292

93-
// Local types
94-
let kWasmStmt = 0x40;
93+
// Value types and related
94+
let kWasmVoid = 0x40;
9595
let kWasmI32 = 0x7f;
9696
let kWasmI64 = 0x7e;
9797
let kWasmF32 = 0x7d;
@@ -107,6 +107,8 @@ let kWasmFuncRef = -0x10;
107107
let kWasmAnyFunc = kWasmFuncRef; // Alias named as in the JS API spec
108108
let kWasmExternRef = -0x11;
109109
let kWasmAnyRef = -0x12;
110+
let kWasmExnRef = -0x17;
111+
let kWasmNullExnRef = -0x0c;
110112

111113
// Use the positive-byte versions inside function bodies.
112114
let kLeb128Mask = 0x7f;
@@ -116,6 +118,8 @@ let kExternRefCode = kWasmExternRef & kLeb128Mask;
116118
let kAnyRefCode = kWasmAnyRef & kLeb128Mask;
117119
let kNullExternRefCode = kWasmNullExternRef & kLeb128Mask;
118120
let kNullFuncRefCode = kWasmNullFuncRef & kLeb128Mask;
121+
let kExnRefCode = kWasmExnRef & kLeb128Mask;
122+
let kNullExnRefCode = kWasmNullExnRef & kLeb128Mask;
119123
let kNullRefCode = kWasmNullRef & kLeb128Mask;
120124

121125
let kWasmRefNull = 0x63;
@@ -137,7 +141,7 @@ let kTableZero = 0;
137141
let kMemoryZero = 0;
138142
let kSegmentZero = 0;
139143

140-
let kTagAttribute = 0;
144+
let kExceptionAttribute = 0;
141145

142146
// Useful signatures
143147
let kSig_i_i = makeSig([kWasmI32], [kWasmI32]);
@@ -217,10 +221,9 @@ let kExprIf = 0x04;
217221
let kExprElse = 0x05;
218222
let kExprTry = 0x06;
219223
let kExprCatch = 0x07;
220-
let kExprCatchAll = 0x19;
221224
let kExprThrow = 0x08;
222225
let kExprRethrow = 0x09;
223-
let kExprBrOnExn = 0x0a;
226+
let kExprThrowRef = 0x0a;
224227
let kExprEnd = 0x0b;
225228
let kExprBr = 0x0c;
226229
let kExprBrIf = 0x0d;
@@ -230,8 +233,10 @@ let kExprCallFunction = 0x10;
230233
let kExprCallIndirect = 0x11;
231234
let kExprReturnCall = 0x12;
232235
let kExprReturnCallIndirect = 0x13;
236+
let kExprCatchAll = 0x19;
233237
let kExprDrop = 0x1a;
234238
let kExprSelect = 0x1b;
239+
let kExprTryTable = 0x1f;
235240
let kExprLocalGet = 0x20;
236241
let kExprLocalSet = 0x21;
237242
let kExprLocalTee = 0x22;
@@ -494,6 +499,12 @@ let kExprI32x4Eq = 0x2c;
494499
let kExprS1x4AllTrue = 0x75;
495500
let kExprF32x4Min = 0x9e;
496501

502+
// Exception handling with exnref.
503+
let kCatchNoRef = 0x0;
504+
let kCatchRef = 0x1;
505+
let kCatchAllNoRef = 0x2;
506+
let kCatchAllRef = 0x3;
507+
497508
class Binary {
498509
constructor() {
499510
this.length = 0;
@@ -976,7 +987,7 @@ class WasmModuleBuilder {
976987
section.emit_u32v(imp.initial); // initial
977988
if (has_max) section.emit_u32v(imp.maximum); // maximum
978989
} else if (imp.kind == kExternalTag) {
979-
section.emit_u32v(kTagAttribute);
990+
section.emit_u32v(kExceptionAttribute);
980991
section.emit_u32v(imp.type);
981992
} else {
982993
throw new Error("unknown/unsupported import kind " + imp.kind);
@@ -1079,7 +1090,7 @@ class WasmModuleBuilder {
10791090
binary.emit_section(kTagSectionCode, section => {
10801091
section.emit_u32v(wasm.tags.length);
10811092
for (let type of wasm.tags) {
1082-
section.emit_u32v(kTagAttribute);
1093+
section.emit_u32v(kExceptionAttribute);
10831094
section.emit_u32v(type);
10841095
}
10851096
});

test/legacy/exceptions/js-api/basic.tentative.any.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ promise_test(async () => {
7676
const tagIndex= builder.addTag(kSig_v_r);
7777
builder.addFunction("catch_exception", kSig_r_v)
7878
.addBody([
79-
kExprTry, kWasmStmt,
79+
kExprTry, kWasmVoid,
8080
kExprCallFunction, fnIndex,
8181
kExprCatch, tagIndex,
8282
kExprReturn,
@@ -100,7 +100,7 @@ promise_test(async () => {
100100
const fnIndex = builder.addImport("module", "fn", kSig_v_v);
101101
builder.addFunction("catch_and_rethrow", kSig_r_v)
102102
.addBody([
103-
kExprTry, kWasmStmt,
103+
kExprTry, kWasmVoid,
104104
kExprCallFunction, fnIndex,
105105
kExprCatchAll,
106106
kExprRethrow, 0x00,

test/legacy/exceptions/js-api/identity.tentative.any.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ test(() => {
3535
builder
3636
.addFunction("catch_js_tag_rethrow", kSig_v_v)
3737
.addBody([
38-
kExprTry, kWasmStmt,
38+
kExprTry, kWasmVoid,
3939
kExprCallFunction, throwJSTagExnIndex,
4040
kExprCatch, jsTagIndex,
4141
kExprDrop,
@@ -49,7 +49,7 @@ test(() => {
4949
builder
5050
.addFunction("catch_wasm_tag_rethrow", kSig_v_v)
5151
.addBody([
52-
kExprTry, kWasmStmt,
52+
kExprTry, kWasmVoid,
5353
kExprCallFunction, throwWasmTagExnIndex,
5454
kExprCatch, wasmTagIndex,
5555
kExprDrop,
@@ -63,7 +63,7 @@ test(() => {
6363
builder
6464
.addFunction("catch_all_js_tag_rethrow", kSig_v_v)
6565
.addBody([
66-
kExprTry, kWasmStmt,
66+
kExprTry, kWasmVoid,
6767
kExprCallFunction, throwJSTagExnIndex,
6868
kExprCatchAll,
6969
kExprRethrow, 0x00,
@@ -76,7 +76,7 @@ test(() => {
7676
builder
7777
.addFunction("catch_all_wasm_tag_rethrow", kSig_v_v)
7878
.addBody([
79-
kExprTry, kWasmStmt,
79+
kExprTry, kWasmVoid,
8080
kExprCallFunction, throwWasmTagExnIndex,
8181
kExprCatchAll,
8282
kExprRethrow, 0x00,
@@ -103,7 +103,7 @@ test(() => {
103103
builder
104104
.addFunction("catch_js_tag_throw_payload", kSig_v_v)
105105
.addBody([
106-
kExprTry, kWasmStmt,
106+
kExprTry, kWasmVoid,
107107
kExprCallFunction, throwJSTagExnIndex,
108108
kExprCatch, jsTagIndex,
109109
kExprThrow, jsTagIndex,

0 commit comments

Comments
 (0)