Skip to content

Commit 9d20a4e

Browse files
authored
Make i31ref and dataref nullable (#4843)
Match the latest version of the GC spec. This change does not depend on V8 changing its interpretation of the shorthands because we are still temporarily not emitting the binary shorthands, but all Binaryen users will have to update their interpretations along with this change if they use the text or binary shorthands.
1 parent 3777624 commit 9d20a4e

40 files changed

+200
-228
lines changed

CHANGELOG.md

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,7 @@ Current Trunk
2121
- Change constant values of some reference types in the C and JS APIs. This is
2222
only observable if you hardcode specific values instead of calling the
2323
relevant methods (like `BinaryenTypeDataref()`). (#4755)
24-
- `BinaryenModulePrintStackIR`, `BinaryenModuleWriteStackIR` and
25-
`BinaryenModuleAllocateAndWriteStackIR` now have an extra boolean
26-
argument `optimize`. (#4832)
27-
- Remove support for the `let` instruction that has been removed from the typed
28-
function references spec.
29-
- HeapType::ext has been restored but is no longer a subtype of HeapType::any to
30-
match the latest updates in the GC spec. (#4898)
24+
- `i31ref` and `dataref` are now nullable to match the latest GC spec. (#4843)
3125

3226
v109
3327
----

src/passes/Print.cpp

Lines changed: 32 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -85,56 +85,38 @@ static bool maybePrintRefShorthand(std::ostream& o, Type type) {
8585
return false;
8686
}
8787
auto heapType = type.getHeapType();
88-
if (heapType.isBasic()) {
89-
if (type.isNullable()) {
90-
switch (heapType.getBasic()) {
91-
case HeapType::ext:
92-
o << "externref";
93-
return true;
94-
case HeapType::func:
95-
o << "funcref";
96-
return true;
97-
case HeapType::any:
98-
o << "anyref";
99-
return true;
100-
case HeapType::eq:
101-
o << "eqref";
102-
return true;
103-
case HeapType::i31:
104-
case HeapType::data:
105-
break;
106-
case HeapType::string:
107-
o << "stringref";
108-
return true;
109-
case HeapType::stringview_wtf8:
110-
o << "stringview_wtf8";
111-
return true;
112-
case HeapType::stringview_wtf16:
113-
o << "stringview_wtf16";
114-
return true;
115-
case HeapType::stringview_iter:
116-
o << "stringview_iter";
117-
return true;
118-
}
119-
} else {
120-
switch (heapType.getBasic()) {
121-
case HeapType::ext:
122-
case HeapType::func:
123-
case HeapType::any:
124-
case HeapType::eq:
125-
break;
126-
case HeapType::i31:
127-
o << "i31ref";
128-
return true;
129-
case HeapType::data:
130-
o << "dataref";
131-
return true;
132-
case HeapType::string:
133-
case HeapType::stringview_wtf8:
134-
case HeapType::stringview_wtf16:
135-
case HeapType::stringview_iter:
136-
break;
137-
}
88+
if (heapType.isBasic() && type.isNullable()) {
89+
switch (heapType.getBasic()) {
90+
case HeapType::ext:
91+
o << "externref";
92+
return true;
93+
case HeapType::func:
94+
o << "funcref";
95+
return true;
96+
case HeapType::any:
97+
o << "anyref";
98+
return true;
99+
case HeapType::eq:
100+
o << "eqref";
101+
return true;
102+
case HeapType::i31:
103+
o << "i31ref";
104+
return true;
105+
case HeapType::data:
106+
o << "dataref";
107+
return true;
108+
case HeapType::string:
109+
o << "stringref";
110+
return true;
111+
case HeapType::stringview_wtf8:
112+
o << "stringview_wtf8";
113+
return true;
114+
case HeapType::stringview_wtf16:
115+
o << "stringview_wtf16";
116+
return true;
117+
case HeapType::stringview_iter:
118+
o << "stringview_iter";
119+
return true;
138120
}
139121
}
140122
return false;

src/wasm-type.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,8 @@ class Type {
143143
// │ funcref ║ x │ │ x │ x │ f n │ ┐ Ref
144144
// │ anyref ║ x │ │ x │ x │ f? n │ │ f_unc
145145
// │ eqref ║ x │ │ x │ x │ n │ │ n_ullable
146-
// │ i31ref ║ x │ │ x │ x │ │ │
147-
// │ dataref ║ x │ │ x │ x │ │ │
146+
// │ i31ref ║ x │ │ x │ x │ n │ │
147+
// │ dataref ║ x │ │ x │ x │ n │ │
148148
// ├─ Compound ──╫───┼───┼───┼───┤───────┤ │
149149
// │ Ref ║ │ x │ x │ x │ f? n? │◄┘
150150
// │ Tuple ║ │ x │ │ x │ │

src/wasm/wasm-binary.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1849,10 +1849,10 @@ bool WasmBinaryBuilder::getBasicType(int32_t code, Type& out) {
18491849
out = Type(HeapType::eq, Nullable);
18501850
return true;
18511851
case BinaryConsts::EncodedType::i31ref:
1852-
out = Type(HeapType::i31, NonNullable);
1852+
out = Type(HeapType::i31, Nullable);
18531853
return true;
18541854
case BinaryConsts::EncodedType::dataref:
1855-
out = Type(HeapType::data, NonNullable);
1855+
out = Type(HeapType::data, Nullable);
18561856
return true;
18571857
case BinaryConsts::EncodedType::stringref:
18581858
out = Type(HeapType::string, Nullable);

src/wasm/wasm-s-parser.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1179,10 +1179,10 @@ Type SExpressionWasmBuilder::stringToType(const char* str,
11791179
return Type(HeapType::eq, Nullable);
11801180
}
11811181
if (strncmp(str, "i31ref", 6) == 0 && (prefix || str[6] == 0)) {
1182-
return Type(HeapType::i31, NonNullable);
1182+
return Type(HeapType::i31, Nullable);
11831183
}
11841184
if (strncmp(str, "dataref", 7) == 0 && (prefix || str[7] == 0)) {
1185-
return Type(HeapType::data, NonNullable);
1185+
return Type(HeapType::data, Nullable);
11861186
}
11871187
if (strncmp(str, "stringref", 9) == 0 && (prefix || str[9] == 0)) {
11881188
return Type(HeapType::string, Nullable);

src/wasm/wat-parser.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1343,10 +1343,10 @@ MaybeResult<typename Ctx::TypeT> reftype(Ctx& ctx, ParseInput& in) {
13431343
return ctx.makeRefType(ctx.makeEq(), Nullable);
13441344
}
13451345
if (in.takeKeyword("i31ref"sv)) {
1346-
return ctx.makeRefType(ctx.makeI31(), NonNullable);
1346+
return ctx.makeRefType(ctx.makeI31(), Nullable);
13471347
}
13481348
if (in.takeKeyword("dataref"sv)) {
1349-
return ctx.makeRefType(ctx.makeData(), NonNullable);
1349+
return ctx.makeRefType(ctx.makeData(), Nullable);
13501350
}
13511351
if (in.takeKeyword("arrayref"sv)) {
13521352
return in.err("arrayref not yet supported");

test/binaryen.js/kitchen-sink.js.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2161,10 +2161,10 @@ getExpressionInfo(tuple[3])={"id":14,"type":5,"value":3.7}
21612161
(pop eqref)
21622162
)
21632163
(drop
2164-
(pop i31ref)
2164+
(pop (ref i31))
21652165
)
21662166
(drop
2167-
(pop dataref)
2167+
(pop (ref data))
21682168
)
21692169
(drop
21702170
(pop stringref)
@@ -4265,10 +4265,10 @@ getExpressionInfo(tuple[3])={"id":14,"type":5,"value":3.7}
42654265
(pop eqref)
42664266
)
42674267
(drop
4268-
(pop i31ref)
4268+
(pop (ref i31))
42694269
)
42704270
(drop
4271-
(pop dataref)
4271+
(pop (ref data))
42724272
)
42734273
(drop
42744274
(pop stringref)

test/gc.wast.from-wast

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
(module
22
(type $i31ref_dataref_=>_none (func (param i31ref dataref)))
3-
(type $ref?|i31|_i31ref_ref?|data|_dataref_=>_none (func (param (ref null i31) i31ref (ref null data) dataref)))
3+
(type $i31ref_ref|i31|_dataref_ref|data|_=>_none (func (param i31ref (ref i31) dataref (ref data))))
44
(global $global_anyref (mut anyref) (ref.null any))
55
(global $global_eqref (mut eqref) (ref.null eq))
66
(global $global_i31ref (mut i31ref) (i31.new
@@ -148,7 +148,7 @@
148148
)
149149
)
150150
)
151-
(func $test-variants (param $local_i31refnull (ref null i31)) (param $local_i31refnonnull i31ref) (param $local_datarefnull (ref null data)) (param $local_datarefnonnull dataref)
151+
(func $test-variants (param $local_i31refnull i31ref) (param $local_i31refnonnull (ref i31)) (param $local_datarefnull dataref) (param $local_datarefnonnull (ref data))
152152
(nop)
153153
)
154154
)

test/gc.wast.fromBinary

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
(module
22
(type $i31ref_dataref_=>_none (func (param i31ref dataref)))
3-
(type $ref?|i31|_i31ref_ref?|data|_dataref_=>_none (func (param (ref null i31) i31ref (ref null data) dataref)))
3+
(type $i31ref_ref|i31|_dataref_ref|data|_=>_none (func (param i31ref (ref i31) dataref (ref data))))
44
(global $global_anyref (mut anyref) (ref.null any))
55
(global $global_eqref (mut eqref) (ref.null eq))
66
(global $global_i31ref (mut i31ref) (i31.new
@@ -148,7 +148,7 @@
148148
)
149149
)
150150
)
151-
(func $test-variants (param $local_i31refnull (ref null i31)) (param $local_i31refnonnull i31ref) (param $local_datarefnull (ref null data)) (param $local_datarefnonnull dataref)
151+
(func $test-variants (param $local_i31refnull i31ref) (param $local_i31refnonnull (ref i31)) (param $local_datarefnull dataref) (param $local_datarefnonnull (ref data))
152152
(nop)
153153
)
154154
)

test/gc.wast.fromBinary.noDebugInfo

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
(module
22
(type $i31ref_dataref_=>_none (func (param i31ref dataref)))
3-
(type $ref?|i31|_i31ref_ref?|data|_dataref_=>_none (func (param (ref null i31) i31ref (ref null data) dataref)))
3+
(type $i31ref_ref|i31|_dataref_ref|data|_=>_none (func (param i31ref (ref i31) dataref (ref data))))
44
(global $global$0 (mut anyref) (ref.null any))
55
(global $global$1 (mut eqref) (ref.null eq))
66
(global $global$2 (mut i31ref) (i31.new
@@ -148,7 +148,7 @@
148148
)
149149
)
150150
)
151-
(func $1 (param $0 (ref null i31)) (param $1 i31ref) (param $2 (ref null data)) (param $3 dataref)
151+
(func $1 (param $0 i31ref) (param $1 (ref i31)) (param $2 dataref) (param $3 (ref data))
152152
(nop)
153153
)
154154
)

0 commit comments

Comments
 (0)