Lines
99.5 %
Functions
100 %
Branches
//! Guest-side i32-repr value helpers: range-checked i31 boxing of an index,
//! nil-as-null boxing of a bool, and index arithmetic / car-of-nil that throw
//! catchable `$nomi_error` conditions (ADR-0026) instead of silently wrapping
//! or trapping the engine.
use super::CompileContext;
use super::exception::ConditionTrap;
use crate::error::Result;
use wasm_encoder::{BlockType, Function, HeapType, Instruction, ValType};
impl CompileContext {
pub(super) fn declare_index_helpers(&mut self) -> Result<()> {
let anyref = self.anyref();
let index = ValType::I32;
self.register_function("box_index", &[index], &[anyref])?;
self.register_function("box_bool", &[index], &[anyref])?;
self.register_function("index_div", &[index, index], &[index])?;
self.register_function("index_rem", &[index, index], &[index])?;
self.register_function("index_add", &[index, index], &[index])?;
self.register_function("index_sub", &[index, index], &[index])?;
self.register_function("index_mul", &[index, index], &[index])?;
self.register_function("index_narrow", &[ValType::I64], &[index])?;
self.register_function("car_of_nil", &[], &[])?;
self.register_function("not_a_list", &[], &[])?;
self.register_function("nil_string", &[], &[])?;
self.register_function("nil_entity", &[], &[])?;
self.register_function("output_overflow", &[], &[])?;
self.register_function("not_a_symbol", &[], &[])?;
for name in [
"not_an_index",
"not_a_scalar",
"not_money",
"not_a_string",
"not_printable",
"not_a_function",
"wrong_function_arguments",
"symbol_not_callable",
] {
self.register_function(name, &[], &[])?;
}
Ok(())
/// Emits the helper bodies in [`Self::declare_index_helpers`] order.
pub(super) fn build_index_helpers(&mut self) -> Result<()> {
let beyond_i31 = self.register_condition_trap(
"INDEX-OVERFLOW",
"index value outside the 31-bit range -1073741824..1073741823",
)?;
let quotient_overflow = self.register_condition_trap(
"index quotient -2147483648 / -1 overflows 32 bits",
let beyond_i32 = self.register_condition_trap(
"index arithmetic result outside the 32-bit range",
let div_by_zero = self.register_condition_trap("DIVISION-BY-ZERO", "division by zero")?;
let car_of_nil =
self.register_condition_trap("TYPE-ERROR", "car of an empty list has no value")?;
let not_a_list = self.register_condition_trap("TYPE-ERROR", "not a list")?;
let nil_string = self.register_condition_trap("TYPE-ERROR", "nil is not a string")?;
let nil_entity = self.register_condition_trap("TYPE-ERROR", "nil is not an entity")?;
let not_a_symbol = self.register_condition_trap("TYPE-ERROR", "not a symbol")?;
let narrowing_traps = [
"not an index",
"not a scalar",
"not money",
"not a string",
"not printable",
"not a function",
"the function does not take these arguments",
"a built-in function designator is not callable as an any value yet",
]
.map(|message| self.register_condition_trap("TYPE-ERROR", message))
.into_iter()
.collect::<Result<Vec<_>>>()?;
let output_overflow = self.register_condition_trap(
"OUTPUT-OVERFLOW",
"the script's output does not fit in its output buffer",
self.pending_helpers.push(box_index_body(&beyond_i31));
self.pending_helpers
.push(box_bool_body(self.anyref(), self.ids.global_t));
.push(index_div_body(&div_by_zero, "ient_overflow));
self.pending_helpers.push(index_rem_body(&div_by_zero));
for op in [
Instruction::I64Add,
Instruction::I64Sub,
Instruction::I64Mul,
self.pending_helpers.push(index_wide_body(&op, &beyond_i32));
self.pending_helpers.push(index_narrow_body(&beyond_i32));
self.pending_helpers.push(throw_body(&car_of_nil));
self.pending_helpers.push(throw_body(¬_a_list));
self.pending_helpers.push(throw_body(&nil_string));
self.pending_helpers.push(throw_body(&nil_entity));
self.pending_helpers.push(throw_body(&output_overflow));
self.pending_helpers.push(throw_body(¬_a_symbol));
for trap in &narrowing_traps {
self.pending_helpers.push(throw_body(trap));
/// `box_bool(i32) -> anyref` (ADR-0033): in an anyref slot `nil` is `ref.null`
/// and `t` is the `$t` singleton global — the same representation whether the
/// slot is typed `Bool` or `AnyRef`, so a bool car never needs rebuilding when
/// its chain widens.
fn box_bool_body(anyref: ValType, t_global: u32) -> Function {
let mut f = Function::new([]);
f.instruction(&Instruction::LocalGet(0));
f.instruction(&Instruction::If(BlockType::Result(anyref)));
f.instruction(&Instruction::GlobalGet(t_global));
f.instruction(&Instruction::Else);
f.instruction(&Instruction::RefNull(HeapType::ANY));
f.instruction(&Instruction::End);
f
/// A no-result helper that only throws `trap` (e.g. `car_of_nil`); callers
/// follow the call with `unreachable`.
fn throw_body(trap: &ConditionTrap) -> Function {
trap.emit_throw(&mut f);
/// `box_index(i32) -> anyref`: `ref.i31` keeps only 31 bits, so a value outside
/// [-2^30, 2^30-1] throws rather than wrapping.
fn box_index_body(beyond_i31: &ConditionTrap) -> Function {
// v + 2^30 lands in [0, 2^31) exactly when v is in the i31 range.
f.instruction(&Instruction::I32Const(1 << 30));
f.instruction(&Instruction::I32Add);
f.instruction(&Instruction::I32Const(i32::MIN));
f.instruction(&Instruction::I32GeU);
throw_if(&mut f, beyond_i31);
f.instruction(&Instruction::RefI31);
/// `index_div(a, b) -> i32`: truncating division that throws on a zero divisor
/// and on `i32::MIN / -1`, the one quotient that overflows.
fn index_div_body(div_by_zero: &ConditionTrap, overflow: &ConditionTrap) -> Function {
throw_on_zero_divisor(&mut f, div_by_zero);
f.instruction(&Instruction::I32Eq);
f.instruction(&Instruction::LocalGet(1));
f.instruction(&Instruction::I32Const(-1));
f.instruction(&Instruction::I32And);
throw_if(&mut f, overflow);
finish_binop(f, &Instruction::I32DivS)
/// `index_rem(a, b) -> i32`: floored modulo, the result taking the divisor's
/// sign as CL's `MOD` does. Throws on a zero divisor (`i32.rem_s` defines
/// `i32::MIN rem -1` as 0, so no overflow check is needed).
fn index_rem_body(div_by_zero: &ConditionTrap) -> Function {
let mut f = Function::new([(1, ValType::I32)]);
f.instruction(&Instruction::I32RemS);
f.instruction(&Instruction::LocalSet(2));
// r != 0 and r, b of opposite signs: step r into the divisor's sign.
f.instruction(&Instruction::LocalGet(2));
f.instruction(&Instruction::I32Xor);
f.instruction(&Instruction::I32Const(0));
f.instruction(&Instruction::I32LtS);
f.instruction(&Instruction::I32Ne);
f.instruction(&Instruction::If(BlockType::Result(ValType::I32)));
/// `index_add` / `index_sub` / `index_mul(a, b) -> i32`: the operation in 64
/// bits, where no i32 operands overflow, throwing when the result leaves the
/// i32 range instead of wrapping.
fn index_wide_body(op: &Instruction<'_>, beyond_i32: &ConditionTrap) -> Function {
let mut f = Function::new([(1, ValType::I64)]);
f.instruction(&Instruction::I64ExtendI32S);
f.instruction(op);
narrow_local(&mut f, 2, beyond_i32);
/// `index_narrow(n: i64) -> i32`: a scalar truncated to an index, throwing
/// when it leaves the i32 range instead of wrapping.
fn index_narrow_body(beyond_i32: &ConditionTrap) -> Function {
narrow_local(&mut f, 0, beyond_i32);
/// Pushes i64 local `n` as an i32, throwing when it is outside the i32 range.
fn narrow_local(f: &mut Function, n: u32, beyond_i32: &ConditionTrap) {
// n + 2^31 lands in [0, 2^32) exactly when n is in the i32 range.
f.instruction(&Instruction::LocalGet(n));
f.instruction(&Instruction::I64Const(1 << 31));
f.instruction(&Instruction::I64Add);
f.instruction(&Instruction::I64Const(1 << 32));
f.instruction(&Instruction::I64GeU);
throw_if(f, beyond_i32);
f.instruction(&Instruction::I32WrapI64);
fn throw_on_zero_divisor(f: &mut Function, div_by_zero: &ConditionTrap) {
f.instruction(&Instruction::I32Eqz);
throw_if(f, div_by_zero);
/// Consumes the i32 condition on the stack, throwing `trap` when it is set.
fn throw_if(f: &mut Function, trap: &ConditionTrap) {
f.instruction(&Instruction::If(BlockType::Empty));
trap.emit_throw(f);
fn finish_binop(mut f: Function, op: &Instruction<'_>) -> Function {