Skip to main content

Module runtime

Module runtime 

Source
Expand description

Runtime helper functions emitted into the WASM module.

These provide the value-manipulation primitives that expression and statement codegen builds upon. Every function is registered during module assembly (in compiler.rs) and referenced by its function index.

Structs§

DataSegmentTracker
Tracks data-segment offsets for well-known constant strings.

Constants§

RT_ALLOC
Bump-allocate size bytes; returns pointer.
RT_CHECK_NAN
NaN check + trap: check_nan(val_ptr: i32) -> i32 — traps if NaN, returns val_ptr
RT_FUNC_COUNT
Total number of runtime helper functions.
RT_MEMCMP
Byte-by-byte memory comparison: memcmp(ptr_a: i32, ptr_b: i32, len: i32) -> i32 Returns 1 if all bytes match, 0 otherwise.
RT_VAL_ACTION_REF
Create an ACTION_REF value; val_action_ref(action_id: i32) -> i32
RT_VAL_ADD
Arithmetic: add two numbers; val_add(a: i32, b: i32) -> i32
RT_VAL_BOOL
Create a BOOL value; val_bool(b: i32) -> i32
RT_VAL_DIV
val_div(a: i32, b: i32) -> i32 — traps on /0 and NaN
RT_VAL_EQ
Compare two values for structural equality; val_eq(a: i32, b: i32) -> i32
RT_VAL_GE
RT_VAL_GET_NUMBER
Read the f64 payload from a NUMBER value; val_get_number(ptr: i32) -> f64 We return the raw bits as (i32, i32) or just store to a scratch global. Actually WASM does support f64 on the value stack, so we can return f64. But our function type table only has i32 returns. We’ll add a special type.
RT_VAL_GET_W1
Read the i32 payload word-1; val_get_w1(ptr: i32) -> i32
RT_VAL_GET_W2
Read the i32 payload word-2; val_get_w2(ptr: i32) -> i32
RT_VAL_GT
RT_VAL_LE
RT_VAL_LIST
Create a LIST value; val_list(arr_ptr: i32, count: i32) -> i32
RT_VAL_LIST_GET
List index access: val_list_get(list_ptr: i32, index: i32) -> i32
RT_VAL_LT
Comparisons returning BOOL value ptr: val_lt(a, b) -> i32, val_le, val_gt, val_ge
RT_VAL_MOD
val_mod(a: i32, b: i32) -> i32
RT_VAL_MUL
val_mul(a: i32, b: i32) -> i32
RT_VAL_NEG
val_neg(a: i32) -> i32 — unary negate
RT_VAL_NIL
Create a NIL value on the heap; returns pointer.
RT_VAL_NOT
val_not(a: i32) -> i32 — logical not (bool)
RT_VAL_NUMBER
Create a NUMBER value from an f64; returns pointer.
RT_VAL_RECORD
Create a RECORD value; val_record(entries_ptr: i32, count: i32) -> i32
RT_VAL_RECORD_GET
Record field access: val_record_get(rec_ptr: i32, key_ptr: i32, key_len: i32) -> i32
RT_VAL_STRING
Create a STRING value; val_string(data_ptr: i32, len: i32) -> i32
RT_VAL_STRING_CONCAT
Concatenate two STRING values; val_string_concat(a: i32, b: i32) -> i32
RT_VAL_SUB
val_sub(a: i32, b: i32) -> i32
RT_VAL_TAG
Read the tag of a value; val_tag(ptr: i32) -> i32
RT_VAL_TO_STRING
Convert a value to its string representation for interpolation. val_to_string(ptr: i32) -> i32 (returns a STRING value ptr)
RT_VAL_VARIANT
Create a VARIANT value; val_variant(id: i32, data_ptr: i32) -> i32

Functions§

emit_alloc
Emit the alloc(size: i32) -> i32 function.
emit_check_nan
Emit check_nan(val_ptr: i32) -> i32 — traps if NUMBER value is NaN.
emit_memcmp
Emit memcmp(ptr_a: i32, ptr_b: i32, len: i32) -> i32 — byte-by-byte comparison.
emit_val_action_ref
Emit val_action_ref(action_id: i32) -> i32.
emit_val_add
emit_val_bool
Emit val_bool(b: i32) -> i32.
emit_val_div
Emit val_div — with division-by-zero and NaN trap guards.
emit_val_eq
Emit val_eq(a: i32, b: i32) -> i32 — structural equality, returns bool value ptr.
emit_val_ge
emit_val_get_number
Emit val_get_number(ptr: i32) -> i32.
emit_val_get_w1
Emit val_get_w1(ptr: i32) -> i32.
emit_val_get_w2
Emit val_get_w2(ptr: i32) -> i32.
emit_val_gt
emit_val_le
emit_val_list
Emit val_list(arr_ptr: i32, count: i32) -> i32.
emit_val_list_get
Emit val_list_get(list_ptr, index) -> i32.
emit_val_lt
emit_val_mod
Emit val_mod(a, b) -> i32 — f64 remainder.
emit_val_mul
emit_val_neg
Emit val_neg(a: i32) -> i32 — unary negate.
emit_val_nil
Emit val_nil() -> i32.
emit_val_not
Emit val_not(a: i32) -> i32 — logical not (bool).
emit_val_number
Emit val_number(n_bits_lo: i32, n_bits_hi: i32) -> i32.
emit_val_record
Emit val_record(entries_ptr: i32, count: i32) -> i32.
emit_val_record_get
Emit val_record_get(rec_ptr, key_ptr, key_len) -> i32.
emit_val_string
Emit val_string(data_ptr: i32, len: i32) -> i32.
emit_val_string_concat
Emit val_string_concat(a: i32, b: i32) -> i32.
emit_val_sub
emit_val_tag
Emit val_tag(ptr: i32) -> i32 — reads the tag word.
emit_val_to_string
Emit val_to_string(ptr: i32) -> i32 — converts any value to a STRING value.
emit_val_variant
Emit val_variant(id: i32, data_ptr: i32) -> i32.
rt_func_idx
Compute the absolute WASM function index of a runtime helper.