Skip to main content

NumericHook

Type Alias NumericHook 

Source
pub type NumericHook = Arc<dyn Fn(NumOp, &Value, &Value) -> Result<Value, String> + Send + Sync>;
Expand description

Host callback for arithmetic fusevm cannot complete natively.

fusevm’s numeric ops are awk/shell-flavoured by default: a non-numeric operand is coerced (to_float, so "a" becomes 0.0) and integer overflow wraps. That is correct for zshrs, awkrs and stryke, and wrong for a frontend whose language signals on non-numbers or promotes on overflow.

Installing a hook with VM::set_numeric_hook switches the VM to strict numeric mode, where an op that cannot be computed exactly in i64/f64 hands off to the host instead of guessing:

  • an operand that is not Int or Float (a string, a Value::Obj handle, a bool, Undef) — the host decides whether that is an error (elisp: (wrong-type-argument number-or-marker-p "a")) or a value it knows how to add (elisp: a marker, or a bignum living in its own object heap);
  • integer Add/Sub/Mul/Neg that overflows i64 — the host returns the exact result (elisp: a bignum), rather than the wrapped one.

Err raises a VM error carrying the message. Mixed int/float and float/float arithmetic never delegates: it is exact in f64 and stays on the fast path.

Aliased Type§

pub struct NumericHook { /* private fields */ }