Skip to main content

emit_checked_index

Function emit_checked_index 

Source
pub fn emit_checked_index(
    emitter: &mut FunctionEmitter<'_>,
    ctx: &CodegenCtx<'_>,
    raw_arr_local: u32,
    idx_f64_local: u32,
) -> Result<u32, CompilerFailure>
Expand description

Throws unless idx >= 0 && idx < len && idx === Math.floor(idx), then returns a fresh i32 local holding the index truncated.

Checking the f64 rather than the truncated i32 is what makes the negative and fractional halves of the domain reachable at all: i32.trunc_sat_f64_u saturates NaN, -1, -Infinity, and 0.5 alike to 0, an in-bounds slot, so a post-truncation >= len test sees only indices past the end. Stack is left untouched on the in-bounds path. NaN fails every comparison, so the first conjunct alone rejects it; -0 passes all three and truncates to 0, as in JS.