Lines
99.17 %
Functions
95 %
Branches
100 %
//! `(error 'code "message")` special form. Lowers to a native wasm
//! `struct.new $nomi_condition` + `throw $nomi_error` (Tier 3,
//! ADR-0026; an optional third argument is the structured detail), so an
//! enclosing `(handler-case)` / `(unwind-protect)` `try_table` can catch it
//! in-module. An uncaught raise is caught by the compiler-emitted boundary
//! wrapper around each host-invoked body, which bridges it to `__nomi_raise`
//! for the classifier. The form never returns normally — execution does not
//! continue past the throw.
use crate::ast::{Expr, WasmType};
use crate::compiler::context::CompileContext;
use crate::compiler::emit::FunctionEmitter;
use crate::compiler::expr::{compile_for_stack, compile_for_stack_as, probe_value};
use crate::compiler::expr::{fold_value, format_form};
use crate::compiler::join::NEVER_RETURNS;
use crate::compiler::native::push_anyref_car;
use crate::compiler::special::binding::shared::fully_macro_expanded;
use crate::error::{Error, Result};
use crate::runtime::SymbolTable;
const NAME: &str = "error";
pub(super) fn eval_error(symbols: &mut SymbolTable, args: &[Expr]) -> Result<Expr> {
expect_arity(args)?;
expect_code_symbol(symbols, &args[0])?;
if let Some(Expr::String(message)) = args.get(1)
&& symbols.is_macro_expansion()
&& !symbols.is_under_any_runtime_guard()
{
return Err(Error::Compile(message.clone()));
}
symbols.measurements().note_exit();
Ok(Expr::WasmRuntime(NEVER_RETURNS))
pub(in crate::compiler) fn compile_error(
ctx: &mut CompileContext,
emit: &mut FunctionEmitter,
symbols: &mut SymbolTable,
args: &[Expr],
) -> Result<()> {
emit_raise_call(ctx, emit, symbols, args)
pub(in crate::compiler) fn compile_error_for_stack(
) -> Result<WasmType> {
emit_raise_call(ctx, emit, symbols, args)?;
// The host fn never returns normally, but the wasm validator still
// demands a stack-shaped value of the caller's expected type.
// `unreachable` engages wasm's stack-polymorphism so any branch
// type the IF / COND BlockType picked matches without forcing the
// caller to plumb an expected type down here.
emit.unreachable();
Ok(NEVER_RETURNS)
fn emit_raise_call(
let code = expect_code_symbol(symbols, &args[0])?;
push_string_literal(ctx, emit, &code)?;
push_message(ctx, emit, symbols, &args[1])?;
// The detail is evaluated before the message check (CL argument order);
// it waits in a local while the check runs on the message.
push_detail(ctx, emit, symbols, args.get(2))?;
let detail = ctx.alloc_local(WasmType::AnyRef)?;
emit.local_set(detail);
// A Bool-typed message is a compile-time type error; this guards a
// nil-able message (e.g. an `if` whose branch is nil) at runtime —
// `TYPE-ERROR` instead of a condition with no readable message.
// A real "" message stays valid.
crate::compiler::native::nullable::require_non_null(
ctx,
emit,
WasmType::StringRef,
ctx.ids.nil_string,
)?;
// code + message + detail are on the stack in field order; pack them
// into a `$nomi_condition` and throw `$nomi_error`. Catchable by an
// in-module `try_table`; the boundary wrapper bridges an uncaught
// throw to `__nomi_raise`.
emit.local_get(detail);
emit.struct_new(ctx.condition_type_idx());
emit.throw(ctx.nomi_error_tag()?);
Ok(())
/// Pushes the optional detail as an `anyref`; an absent one is nil.
fn push_detail(
detail: Option<&Expr>,
match detail {
Some(form) => push_anyref_car(ctx, emit, symbols, form),
None => {
emit.ref_null_any();
/// Pushes the message as a `StringRef`; a literal `nil` lowers to a nullable
/// one (ADR-0033 Phase 5), and a message of any other type is a compile error.
fn push_message(
message: &Expr,
if matches!(probe_value(&mut symbols.clone(), message)?, Expr::Nil) {
return compile_for_stack_as(ctx, emit, symbols, message, WasmType::StringRef);
match compile_for_stack(ctx, emit, symbols, message)? {
WasmType::StringRef => Ok(()),
ty => emit.reject_if_live(message_type_error(ty)),
fn message_type_error(ty: WasmType) -> Error {
Error::Type {
expected: format!("a string message for '{NAME}'"),
actual: ty.to_string(),
pub(in crate::compiler) fn push_string_literal(
value: &str,
let data_idx = ctx.add_data(value.as_bytes())?;
emit.i32_const(0);
emit.i32_const(value.len() as i32);
emit.array_new_data(ctx.ids.ty_i8_array, data_idx);
fn expect_arity(args: &[Expr]) -> Result<()> {
let expected = args.len().clamp(2, 3);
if args.len() != expected {
return Err(Error::Arity {
name: NAME.to_string(),
expected,
actual: args.len(),
});
fn expect_code_symbol(symbols: &mut SymbolTable, arg: &Expr) -> Result<String> {
let arg = &fully_macro_expanded(symbols, arg)?;
let datum = match arg {
Expr::Quote(inner) | Expr::Datum(inner) => inner.as_ref().clone(),
Expr::List(_) => match fold_value(symbols, arg)? {
Some(Expr::Datum(inner)) => *inner,
_ => arg.clone(),
},
other => other.clone(),
};
match datum {
Expr::Symbol(name) if !matches!(arg, Expr::Symbol(_)) => Ok(name),
other => Err(Error::Type {
expected: format!("quoted symbol for argument 0 of '{NAME}'"),
actual: format_form(&other),
}),