1

The Wasm spec defines 3 kinds of element segments: passive, active, and declarative.

Passive element segments can be used to set the values of a Table at runtime, and Active element segments automatically set the values of a Table during module instantiation.

However, I don't understand what purpose declarative element segments serve. The spec says:

A declarative element segment is not available at runtime but merely serves to forward-declare references that are formed in code with instructions like ref.func.

Ok, but what purpose does that serve? Why would I want or need to forward-declare references in an element segment?

The only other places I've found that this might influence are in module instantiation. However, there the only thing that seems to be done with declarative element segments is that they are immediately dropped in step 15 of module instantiation. They do appear in module instantiation Step 9 as well, however while briefly skimming that definition of (ref*)* seems to only be used to define elementaddr*, so as far as I can tell, this just means that declarative elements are included in element indexing/addressing but aren't used anywhere else.

Am I missing something? Are declarative elements used in validation somehow? Or are they intended for possible future expansion and don't do anything in the current version of the WebAssembly specification?

This question is based on WebAssembly Specification Release 2.0 (Draft 2024-04-28)

1 Answer 1

1

The function indices occurring in declarative segments become part of the set C.refs during module validation. Hence, the use of these indices is allowed with the ref.func instruction. That is their only role, they are otherwise ignored.

The reason Wasm requires this (admittedly ugly) forward declaration is to support streaming compilation: some engines might have to compile functions that are invoked indirectly differently, potentially more costly. In order to still enable compilation of individual functions in the module binary as soon as they arrive from the wire, these forward declarations have been introduced. Otherwise such engines would have to wait until they have seen all function bodies in order to know which function references are formed and could lead to indirect calls.

Not the answer you're looking for? Browse other questions tagged or ask your own question.