Lines
93.59 %
Functions
87.5 %
Branches
100 %
//! Condition accessors for `(handler-case)` clause variables (Tier 3.3,
//! ADR-0026). `(error-code e)` and `(error-message e)` read the two string
//! fields of a caught condition, `(error-detail e)` its any-typed detail (nil
//! when absent); the condition is bound at `EntityRef(Condition)`. Mirror the
//! typed-entity accessor shape: type-check the arg, then `struct.get` the
//! field.
use super::super::context::CompileContext;
use super::super::emit::FunctionEmitter;
use super::NativeSpec;
use crate::ast::{EntityKind, Expr, WasmType};
use crate::error::{Error, Result};
use crate::runtime::SymbolTable;
const CODE_FIELD: u32 = 0;
const MESSAGE_FIELD: u32 = 1;
const DETAIL_FIELD: u32 = 2;
fn arity_one(name: &str, args: &[Expr]) -> Result<()> {
if args.len() != 1 {
return Err(Error::Arity {
name: name.to_string(),
expected: 1,
actual: args.len(),
});
}
Ok(())
/// Shared stack emit for a condition field accessor: compile the arg
/// (must be `EntityRef(Condition)`), then `struct.get` the field. Returns
/// `StringRef`.
fn condition_field_stack(
ctx: &mut CompileContext,
emit: &mut FunctionEmitter,
symbols: &mut SymbolTable,
args: &[Expr],
name: &str,
field: u32,
) -> Result<()> {
arity_one(name, args)?;
let expected = WasmType::EntityRef(EntityKind::Condition);
// Type-directed nullable lowering (mirrors `STRING=`/typed-entity
// accessors): a literal `nil` argument (e.g. `(setf e nil)` then
// `(error-message e)`, or a bare literal) lowers straight to a nullable
// `EntityRef` rather than folding to `Bool` and failing this as a
// COMPILE-time mismatch — it fails at RUNTIME instead, as a catchable
// `TYPE-ERROR` (ADR-0033 Phase 5). Every other argument shape keeps the
// original compile-time kind check.
let is_nil_literal = matches!(
crate::compiler::expr::eval_value(&mut symbols.clone(), &args[0])?,
Expr::Nil
);
let ty = if is_nil_literal {
crate::compiler::expr::compile_for_stack_as(ctx, emit, symbols, &args[0], expected)?;
expected
} else {
crate::compiler::expr::compile_for_stack(ctx, emit, symbols, &args[0])?
};
if ty != expected {
return Err(Error::Type {
expected: format!("{expected} for {name}"),
actual: ty.to_string(),
let condition_idx = ctx.ids.ty_nomi_condition;
super::nullable::require_non_null(ctx, emit, expected, ctx.ids.nil_entity)?;
emit.struct_get(condition_idx, field);
fn error_code_eval(_symbols: &mut SymbolTable, args: &[Expr]) -> Result<Expr> {
arity_one("ERROR-CODE", args)?;
Ok(Expr::WasmRuntime(WasmType::SymbolRef))
fn error_code_stack(
) -> Result<WasmType> {
condition_field_stack(ctx, emit, symbols, args, "ERROR-CODE", CODE_FIELD)?;
emit.ref_cast(ctx.ids.ty_i8_array);
emit.struct_new(ctx.ids.ty_symbol);
Ok(WasmType::SymbolRef)
fn error_message_eval(_symbols: &mut SymbolTable, args: &[Expr]) -> Result<Expr> {
arity_one("ERROR-MESSAGE", args)?;
Ok(Expr::WasmRuntime(WasmType::StringRef))
fn error_message_stack(
condition_field_stack(ctx, emit, symbols, args, "ERROR-MESSAGE", MESSAGE_FIELD)?;
Ok(WasmType::StringRef)
fn error_detail_eval(_symbols: &mut SymbolTable, args: &[Expr]) -> Result<Expr> {
arity_one("ERROR-DETAIL", args)?;
Ok(Expr::WasmRuntime(WasmType::AnyRef))
fn error_detail_stack(
condition_field_stack(ctx, emit, symbols, args, "ERROR-DETAIL", DETAIL_FIELD)?;
Ok(WasmType::AnyRef)
pub(in crate::compiler::native) const NATIVES: &[NativeSpec] = &[
NativeSpec {
name: "ERROR-CODE",
eval: error_code_eval,
stack: Some(error_code_stack),
effect: None,
},
name: "ERROR-MESSAGE",
eval: error_message_eval,
stack: Some(error_message_stack),
name: "ERROR-DETAIL",
eval: error_detail_eval,
stack: Some(error_detail_stack),
];