Skip to main content

Module cast

Module cast 

Source
Expand description

Box / unbox helpers — the unified mechanism for moving values between language types and the universal (ref $Object) slot type used by arrays, unknown, and erased generics.

Functions§

emit_box
Emit Wasm instructions that wrap the value currently on top of the stack (of language type ty) into a (ref $Object).
emit_cast_to
Emit Wasm instructions that pop a (ref null $Object) from the stack and produce a ty-typed value.
emit_coerce_to_return_slot
Coerce the returned value on top of the stack (static type source_ty) into the enclosing body’s ReturnTarget.
emit_coerce_to_slot
Coerce a source_ty-typed value into the Wasm slot type for target_ty. Boxes primitives (f64/i32 → ref); WasmGC subtyping handles ref→ref cases.
emit_coerce_to_wasm_slot
Coerce a source_ty-typed value into an already-known Wasm slot type.
emit_condition_to_i32
Consume the condition value (static type cond_ty) on the stack and leave an i32 where nonzero = truthy, per JS semantics: null, "", 0, -0, NaN, false, and 0n are falsy; everything else is truthy.
emit_narrowing_cast
Emit the Wasm cast from cast_info.from_ty to cast_info.to_ty. The cast is unchecked — codegen trusts the typechecker’s narrowing decision; the Wasm-level cast traps if the predicate’s runtime check was unsound.
emit_nominal_instance_test
Emit the nominal class-membership test for the value on top of the stack (any (ref null $Object)-compatible ref), leaving an i32 (0/1): walk the value’s vtable parent chain, comparing each link by ref.eq against the target class’s vtable-singleton global. Value identity, not ref.test — same-shape sibling classes canonicalize to one WasmGC type, so shape can’t carry instanceof. Non-class values (strings, boxed primitives, structural objects, closures) exit false at the $ClassVTable test — their vtables are plain $VTable subtypes.
emit_unerase
Recover ty’s value type from a slot that stores it erased — a box cell’s payload, keyed on slot_value_type.
target_allows_null
Whether emit_cast_to lets a null reach the target rather than lifting to non-null first. True for the targets whose lowering is a nullable (ref null $Object): bare Null, nullable unions, and the no-op erased/dynamic types — unknown, never, an erased generic (which can hold null after instantiation, e.g. T = string | null), and a recursion back-edge whose body may list null. emit_cast_to’s Union arm uses RefCastNullable for the same reason. A union’s nullability is its lowering, not whether a member is spelled null: T | number lowers nullable because T does.