Skip to content

Write the table initializer expression - #2869

Merged
zherczeg merged 3 commits into
WebAssembly:mainfrom
Nishuuzz:table-init-expr
Nov 4, 2026
Merged

zherczeg merged 3 commits into
WebAssembly:mainfrom
Nishuuzz:table-init-expr

Conversation

@Nishuuzz

Copy link
Copy Markdown
Contributor

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))

Table::init_expr holds it, the wat parser fills it in, and so does the binary reader — but WriteTable() never wrote it out. So wasm2wat drops it and wat2wasm can't read the result back:

$ wat2wasm --enable-all t.wat -o t.wasm
$ wasm2wat --enable-all t.wasm -o back.wat
$ cat back.wat
(module
  (type (;0;) (func))
  (func (;0;) (type 0))
  (table (;0;) 10 (ref func)))
$ wat2wasm --enable-all back.wat
back.wat:4:4: error: missing table initializer
  (table (;0;) 10 (ref func))
   ^^^^^

The fix writes it with WriteInitExpr(), the same way WriteGlobal() 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 wasm2wat and back — 3579 modules that wasm-validate accepts — 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 in proposals/function-references. The 4 left over are the memory.init operand 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 funcref table to pin that the defaultable case is untouched. It fails before this change with the missing table initializer error above and passes after.

Full run-tests.py is clean: 1743 passed, 2 failed, both of them wat2wasm_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.

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.
@sbc100
sbc100 requested a review from zherczeg September 30, 2026 18:12
Comment thread src/wat-writer.cc Outdated
/* 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);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WriteCloseNewline drops the next char, so NextChar::Space should be enough.

Comment thread src/wat-writer.cc Outdated
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. */

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread test/roundtrip/table-init-expr.txt Outdated
;; 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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
@Nishuuzz

Nishuuzz commented Oct 1, 2026

Copy link
Copy Markdown
Contributor Author

All three applied, thanks.

You're right about NextChar::Space — WriteClose resets next_char_ before writing the paren, so the conditional wasn't doing anything. Simplified to pass it unconditionally, and the expected output in the test is unchanged, so the pending space really is dropped for the table without an initializer.

The comment is now on WriteInitExpr, where the no-op happens, and says what it does when there is nothing to write.

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.

@zherczeg zherczeg left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@zherczeg zherczeg left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@zherczeg
zherczeg merged commit b50b0fd into WebAssembly:main Nov 4, 2026
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants