1
//! Condition accessors for `(handler-case)` clause variables (Tier 3.3,
2
//! ADR-0026). `(error-code e)` and `(error-message e)` read the two string
3
//! fields of a caught condition, `(error-detail e)` its any-typed detail (nil
4
//! when absent); the condition is bound at `EntityRef(Condition)`. Mirror the
5
//! typed-entity accessor shape: type-check the arg, then `struct.get` the
6
//! field.
7

            
8
use super::super::context::CompileContext;
9
use super::super::emit::FunctionEmitter;
10
use super::NativeSpec;
11
use crate::ast::{EntityKind, Expr, WasmType};
12
use crate::error::{Error, Result};
13
use crate::runtime::SymbolTable;
14

            
15
const CODE_FIELD: u32 = 0;
16
const MESSAGE_FIELD: u32 = 1;
17
const DETAIL_FIELD: u32 = 2;
18

            
19
18441
fn arity_one(name: &str, args: &[Expr]) -> Result<()> {
20
18441
    if args.len() != 1 {
21
        return Err(Error::Arity {
22
            name: name.to_string(),
23
            expected: 1,
24
            actual: args.len(),
25
        });
26
18441
    }
27
18441
    Ok(())
28
18441
}
29

            
30
/// Shared stack emit for a condition field accessor: compile the arg
31
/// (must be `EntityRef(Condition)`), then `struct.get` the field. Returns
32
/// `StringRef`.
33
5685
fn condition_field_stack(
34
5685
    ctx: &mut CompileContext,
35
5685
    emit: &mut FunctionEmitter,
36
5685
    symbols: &mut SymbolTable,
37
5685
    args: &[Expr],
38
5685
    name: &str,
39
5685
    field: u32,
40
5685
) -> Result<()> {
41
5685
    arity_one(name, args)?;
42
5685
    let expected = WasmType::EntityRef(EntityKind::Condition);
43
    // Type-directed nullable lowering (mirrors `STRING=`/typed-entity
44
    // accessors): a literal `nil` argument (e.g. `(setf e nil)` then
45
    // `(error-message e)`, or a bare literal) lowers straight to a nullable
46
    // `EntityRef` rather than folding to `Bool` and failing this as a
47
    // COMPILE-time mismatch — it fails at RUNTIME instead, as a catchable
48
    // `TYPE-ERROR` (ADR-0033 Phase 5). Every other argument shape keeps the
49
    // original compile-time kind check.
50
5685
    let is_nil_literal = matches!(
51
5685
        crate::compiler::expr::eval_value(&mut symbols.clone(), &args[0])?,
52
        Expr::Nil
53
    );
54
5685
    let ty = if is_nil_literal {
55
419
        crate::compiler::expr::compile_for_stack_as(ctx, emit, symbols, &args[0], expected)?;
56
419
        expected
57
    } else {
58
5266
        crate::compiler::expr::compile_for_stack(ctx, emit, symbols, &args[0])?
59
    };
60
5685
    if ty != expected {
61
211
        return Err(Error::Type {
62
211
            expected: format!("{expected} for {name}"),
63
211
            actual: ty.to_string(),
64
211
        });
65
5474
    }
66
5474
    let condition_idx = ctx.ids.ty_nomi_condition;
67
5474
    super::nullable::require_non_null(ctx, emit, expected, ctx.ids.nil_entity)?;
68
5474
    emit.struct_get(condition_idx, field);
69
5474
    Ok(())
70
5685
}
71

            
72
1730
fn error_code_eval(_symbols: &mut SymbolTable, args: &[Expr]) -> Result<Expr> {
73
1730
    arity_one("ERROR-CODE", args)?;
74
1730
    Ok(Expr::WasmRuntime(WasmType::SymbolRef))
75
1730
}
76

            
77
1492
fn error_code_stack(
78
1492
    ctx: &mut CompileContext,
79
1492
    emit: &mut FunctionEmitter,
80
1492
    symbols: &mut SymbolTable,
81
1492
    args: &[Expr],
82
1492
) -> Result<WasmType> {
83
1492
    condition_field_stack(ctx, emit, symbols, args, "ERROR-CODE", CODE_FIELD)?;
84
1284
    emit.ref_cast(ctx.ids.ty_i8_array);
85
1284
    emit.struct_new(ctx.ids.ty_symbol);
86
1284
    Ok(WasmType::SymbolRef)
87
1492
}
88

            
89
2704
fn error_message_eval(_symbols: &mut SymbolTable, args: &[Expr]) -> Result<Expr> {
90
2704
    arity_one("ERROR-MESSAGE", args)?;
91
2704
    Ok(Expr::WasmRuntime(WasmType::StringRef))
92
2704
}
93

            
94
2288
fn error_message_stack(
95
2288
    ctx: &mut CompileContext,
96
2288
    emit: &mut FunctionEmitter,
97
2288
    symbols: &mut SymbolTable,
98
2288
    args: &[Expr],
99
2288
) -> Result<WasmType> {
100
2288
    condition_field_stack(ctx, emit, symbols, args, "ERROR-MESSAGE", MESSAGE_FIELD)?;
101
2288
    Ok(WasmType::StringRef)
102
2288
}
103

            
104
8322
fn error_detail_eval(_symbols: &mut SymbolTable, args: &[Expr]) -> Result<Expr> {
105
8322
    arity_one("ERROR-DETAIL", args)?;
106
8322
    Ok(Expr::WasmRuntime(WasmType::AnyRef))
107
8322
}
108

            
109
1905
fn error_detail_stack(
110
1905
    ctx: &mut CompileContext,
111
1905
    emit: &mut FunctionEmitter,
112
1905
    symbols: &mut SymbolTable,
113
1905
    args: &[Expr],
114
1905
) -> Result<WasmType> {
115
1905
    condition_field_stack(ctx, emit, symbols, args, "ERROR-DETAIL", DETAIL_FIELD)?;
116
1902
    Ok(WasmType::AnyRef)
117
1905
}
118

            
119
pub(in crate::compiler::native) const NATIVES: &[NativeSpec] = &[
120
    NativeSpec {
121
        name: "ERROR-CODE",
122
        eval: error_code_eval,
123
        stack: Some(error_code_stack),
124
        effect: None,
125
    },
126
    NativeSpec {
127
        name: "ERROR-MESSAGE",
128
        eval: error_message_eval,
129
        stack: Some(error_message_stack),
130
        effect: None,
131
    },
132
    NativeSpec {
133
        name: "ERROR-DETAIL",
134
        eval: error_detail_eval,
135
        stack: Some(error_detail_stack),
136
        effect: None,
137
    },
138
];