Skip to main content

Module init_guard

Module init_guard 

Source
Expand description

Module variables read or written before their declaration has run.

A function can run during module initialization, before a later let, const or static field has been initialized. JavaScript throws a ReferenceError there. A Wasm global already holds a zero or null default, so a guarded global gets an i32 flag that the start function sets once its binding is initialized; every other access checks the flag first.

Each guarded global is initialized once its declaration stores its value. A static field is guarded field by field, so a function that runs during its class’s static initialization reads an earlier static and throws on a later one. JavaScript would find undefined there, which a typed Wasm slot can’t hold, and its default would be a wrong value or a null reference. The message names the class, as Node’s does for a class not yet initialized.

A class is guarded the same way: constructing it, calling one of its static methods or testing instanceof against it before its declaration has run throws. Its flag is set where the declaration stands, before its static fields initialize, so they can use the class.

Only a binding declared after some top-level code that could call a function is guarded. Before that point nothing can reach a function body, so the binding is always initialized by the time any function uses it.

Structs§

ClassGuard
A class that can be used before its declaration has run.
InitFlag
A guarded binding’s flag, as codegen allocated it.
InitGuard
One guarded global and the top-level statement that declares it.

Functions§

allocate_flags
Allocates one flag per guarded global and class, starting unset. Returns how many globals were added.
before_initialization_message
The ReferenceError message for a binding, worded as Node words it.
guarded_classes
The top-level classes that need an initialization flag.
guarded_globals
The module globals that need an initialization flag, in declaration order.