Skip to content

Commit 2cbe448

Browse files
authored
Replace i31.new with ref.i31 everywhere (#5931)
Replace i31.new with ref.i31 in the printer, tests, and source code. Continue parsing i31.new for the time being to allow a graceful transition. Also update the JS API to reflect the new instruction name.
1 parent 9d79632 commit 2cbe448

40 files changed

+183
-142
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ full changeset diff at the end of each section.
1515
Current Trunk
1616
-------------
1717

18-
- "I31New" changed to "RefI31" everywhere it appears in the C API.
18+
- "I31New" changed to "RefI31" everywhere it appears in the C API and similarly
19+
"i31.new" has been replaced with "ref.i31" in the JS API and in printed
20+
output.
1921

2022
v115
2123
----

scripts/gen-s-parser.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,8 @@
564564
("call_ref", "makeCallRef(s, /*isReturn=*/false)"),
565565
("return_call_ref", "makeCallRef(s, /*isReturn=*/true)"),
566566
# GC
567-
("i31.new", "makeRefI31(s)"),
567+
("i31.new", "makeRefI31(s)"), # deprecated
568+
("ref.i31", "makeRefI31(s)"),
568569
("i31.get_s", "makeI31Get(s, true)"),
569570
("i31.get_u", "makeI31Get(s, false)"),
570571
("ref.test", "makeRefTest(s)"),

src/binaryen-c.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2308,10 +2308,10 @@ BINARYEN_API void BinaryenTupleExtractSetIndex(BinaryenExpressionRef expr,
23082308

23092309
// RefI31
23102310

2311-
// Gets the value expression of an `i31.new` expression.
2311+
// Gets the value expression of a `ref.i31` expression.
23122312
BINARYEN_API BinaryenExpressionRef
23132313
BinaryenRefI31GetValue(BinaryenExpressionRef expr);
2314-
// Sets the value expression of an `i31.new` expression.
2314+
// Sets the value expression of a `ref.i31` expression.
23152315
BINARYEN_API void BinaryenRefI31SetValue(BinaryenExpressionRef expr,
23162316
BinaryenExpressionRef valueExpr);
23172317

src/gen-s-parser.inc

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2984,9 +2984,17 @@ switch (buf[0]) {
29842984
case 'f':
29852985
if (op == "ref.func"sv) { return makeRefFunc(s); }
29862986
goto parse_error;
2987-
case 'i':
2988-
if (op == "ref.is_null"sv) { return makeRefIsNull(s); }
2989-
goto parse_error;
2987+
case 'i': {
2988+
switch (buf[5]) {
2989+
case '3':
2990+
if (op == "ref.i31"sv) { return makeRefI31(s); }
2991+
goto parse_error;
2992+
case 's':
2993+
if (op == "ref.is_null"sv) { return makeRefIsNull(s); }
2994+
goto parse_error;
2995+
default: goto parse_error;
2996+
}
2997+
}
29902998
case 'n':
29912999
if (op == "ref.null"sv) { return makeRefNull(s); }
29923000
goto parse_error;
@@ -8585,13 +8593,25 @@ switch (buf[0]) {
85858593
return *ret;
85868594
}
85878595
goto parse_error;
8588-
case 'i':
8589-
if (op == "ref.is_null"sv) {
8590-
auto ret = makeRefIsNull(ctx, pos);
8591-
CHECK_ERR(ret);
8592-
return *ret;
8596+
case 'i': {
8597+
switch (buf[5]) {
8598+
case '3':
8599+
if (op == "ref.i31"sv) {
8600+
auto ret = makeRefI31(ctx, pos);
8601+
CHECK_ERR(ret);
8602+
return *ret;
8603+
}
8604+
goto parse_error;
8605+
case 's':
8606+
if (op == "ref.is_null"sv) {
8607+
auto ret = makeRefIsNull(ctx, pos);
8608+
CHECK_ERR(ret);
8609+
return *ret;
8610+
}
8611+
goto parse_error;
8612+
default: goto parse_error;
85938613
}
8594-
goto parse_error;
8614+
}
85958615
case 'n':
85968616
if (op == "ref.null"sv) {
85978617
auto ret = makeRefNull(ctx, pos);

src/js/binaryen.js-post.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2370,6 +2370,9 @@ function wrapModule(module, self = {}) {
23702370
'func'(func, type) {
23712371
return preserveStack(() => Module['_BinaryenRefFunc'](module, strToStack(func), type));
23722372
},
2373+
'i31'(value) {
2374+
return Module['_BinaryenRefI31'](module, value);
2375+
},
23732376
'eq'(left, right) {
23742377
return Module['_BinaryenRefEq'](module, left, right);
23752378
}
@@ -2418,9 +2421,6 @@ function wrapModule(module, self = {}) {
24182421
};
24192422

24202423
self['i31'] = {
2421-
'new'(value) {
2422-
return Module['_BinaryenRefI31'](module, value);
2423-
},
24242424
'get_s'(i31) {
24252425
return Module['_BinaryenI31Get'](module, i31, 1);
24262426
},

src/passes/Print.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1959,7 +1959,7 @@ struct PrintExpressionContents
19591959
printMedium(o, "tuple.extract ");
19601960
o << curr->index;
19611961
}
1962-
void visitRefI31(RefI31* curr) { printMedium(o, "i31.new"); }
1962+
void visitRefI31(RefI31* curr) { printMedium(o, "ref.i31"); }
19631963
void visitI31Get(I31Get* curr) {
19641964
printMedium(o, curr->signed_ ? "i31.get_s" : "i31.get_u");
19651965
}

src/passes/RemoveUnusedBrs.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -732,7 +732,7 @@ struct RemoveUnusedBrs : public WalkerPass<PostWalker<RemoveUnusedBrs>> {
732732
//
733733
// (br_on_cast $l anyref i31ref
734734
// (block (result anyref)
735-
// (i31.new ...)))
735+
// (ref.i31 ...)))
736736
//
737737
// We could just always do the cast and leave removing the casts to
738738
// OptimizeInstructions, but it's simple enough to avoid unnecessary

src/tools/fuzzing/fuzzing.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2321,7 +2321,7 @@ Expression* TranslateToFuzzReader::makeBasicRef(Type type) {
23212321
return builder.makeRefNull(HeapType::none);
23222322
}
23232323
auto nullability = getSubType(type.getNullability());
2324-
// i31.new is not allowed in initializer expressions.
2324+
// ref.i31 is not allowed in initializer expressions.
23252325
HeapType subtype;
23262326
switch (upTo(3)) {
23272327
case 0:

src/wasm/wasm-validator.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2504,11 +2504,11 @@ void FunctionValidator::visitCallRef(CallRef* curr) {
25042504

25052505
void FunctionValidator::visitRefI31(RefI31* curr) {
25062506
shouldBeTrue(
2507-
getModule()->features.hasGC(), curr, "i31.new requires gc [--enable-gc]");
2507+
getModule()->features.hasGC(), curr, "ref.i31 requires gc [--enable-gc]");
25082508
shouldBeSubType(curr->value->type,
25092509
Type::i32,
25102510
curr->value,
2511-
"i31.new's argument should be i32");
2511+
"ref.i31's argument should be i32");
25122512
}
25132513

25142514
void FunctionValidator::visitI31Get(I31Get* curr) {

test/binaryen.js/expressions.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1776,7 +1776,7 @@ console.log("# RefI31");
17761776
const module = new binaryen.Module();
17771777

17781778
var value = module.local.get(1, binaryen.i32);
1779-
const theRefI31 = binaryen.RefI31(module.i31.new(value));
1779+
const theRefI31 = binaryen.RefI31(module.ref.i31(value));
17801780
assert(theRefI31 instanceof binaryen.RefI31);
17811781
assert(theRefI31 instanceof binaryen.Expression);
17821782
assert(theRefI31.value === value);
@@ -1792,7 +1792,7 @@ console.log("# RefI31");
17921792
assert(
17931793
theRefI31.toText()
17941794
==
1795-
"(i31.new\n (local.get $2)\n)\n"
1795+
"(ref.i31\n (local.get $2)\n)\n"
17961796
);
17971797

17981798
module.dispose();

0 commit comments

Comments
 (0)