Repository navigation
Write the table initializer expression - #2869
Conversation
A table whose element type has no default value carries an initializer,
which the text format requires after the element type:
(table $t 10 (ref func) (ref.func $f))
wat-parser stores it in Table::init_expr and the binary reader fills it
in too, but WriteTable() never wrote it out, so wasm2wat dropped it and
wat2wasm could not read the result back:
$ wasm2wat --enable-all t.wasm -o t.wat
$ wat2wasm --enable-all t.wat
t.wat:4:4: error: missing table initializer
(table (;0;) 10 (ref func))
^^^^^
Write it with WriteInitExpr(), the same way WriteGlobal() writes a
global's initializer, and only emit the separating space when there is
something to write, so tables with a defaultable element type are
unchanged.
Support for these initializers was added in WebAssembly#2593, which covered the
parser and the readers but not the text writer.
Running the spec testsuite through wasm2wat and back, this takes the
modules that fail to reparse from 52 to 4, the remaining 4 being the
memory.init operand order fixed in WebAssembly#2838.
| /* A table whose element type has no default value carries an initializer, | ||
| * which the text format requires after the type. */ | ||
| WriteType(table.elem_type, | ||
| table.init_expr.empty() ? NextChar::None : NextChar::Space); |
There was a problem hiding this comment.
WriteCloseNewline drops the next char, so NextChar::Space should be enough.
| WriteLimits(table.elem_limits); | ||
| WriteType(table.elem_type, NextChar::None); | ||
| /* A table whose element type has no default value carries an initializer, | ||
| * which the text format requires after the type. */ |
There was a problem hiding this comment.
This comment is confusing, since it does not describe what happens in the other case. It would be better to move it before WriteInitExpr and explain that when no need an initializer expression, the call has no effect.
| ;; non-defaultable element type, so these carry an initializer | ||
| (table $t1 10 (ref func) (ref.func $f)) | ||
| (table $t2 2 4 (ref func) (ref.func $f)) | ||
| ;; defaultable, so no initializer to write |
There was a problem hiding this comment.
This is not precise, since it can still have an optional initializer.
- Pass NextChar::Space unconditionally. WriteClose() resets next_char_ before writing the paren, so the pending space is dropped when there is no initializer and the conditional was not needed. - Move the comment to WriteInitExpr, where the no-op case is, and say what happens when there is no initializer. - The funcref table in the test is defaultable, so its initializer is optional rather than absent. Reword the comment to match.
|
All three applied, thanks. You're right about The comment is now on And agreed on the test comment — a defaultable element type can still be given an initializer, so "no initializer to write" was wrong. Reworded to say it is optional and omitted here. |
A table whose element type has no default value carries an initializer, which the text format requires after the element type:
Table::init_exprholds it, the wat parser fills it in, and so does the binary reader — butWriteTable()never wrote it out. Sowasm2watdrops it andwat2wasmcan't read the result back:The fix writes it with
WriteInitExpr(), the same wayWriteGlobal()writes a global's initializer. The separating space is only emitted when there is an initializer, so a table with a defaultable element type is written exactly as before.Support for these initializers landed in #2593, which covered the parser and the readers; the text writer looks to have been missed.
Checking it
The minimal case above now round trips, and the resulting binary is byte-identical to the original.
Running every module in the spec testsuite through
wasm2watand back — 3579 modules thatwasm-validateaccepts — the count that fails to reparse goes from 52 to 4, and byte-identical round trips go from 3352 to 3399. All 48 that this fixes are inproposals/function-references. The 4 left over are thememory.initoperand order in #2838, unrelated to this.The new roundtrip test covers a table with a minimum only, one with a minimum and maximum, and a plain
funcreftable to pin that the defaultable case is untouched. It fails before this change with themissing table initializererror above and passes after.Full
run-tests.pyis clean: 1743 passed, 2 failed, both of themwat2wasm_stdout.txt, which fails the same way with the writer reverted to main — it's a pre-existing Windows stdout issue on my machine, not something from this change. The only difference between the two runs is the new test.