1
//! The host-anyref lambda boundary: the wire values a host-invoked body
2
//! receives as anyref and returns as anyref.
3

            
4
use crate::ast::WasmType;
5
use crate::compiler::context::CompileContext;
6
use crate::compiler::emit::FunctionEmitter;
7
use crate::compiler::expr::emit_box_anyref;
8
use crate::error::{Error, Result};
9

            
10
/// Emits the wasm to convert an `anyref` on the stack to the declared
11
/// user-visible type: a reference `ref.cast`s to its concrete type, an i31-boxed
12
/// index or bool unboxes to its i32. The variable keeps the element's own
13
/// dimension — no int ↔ ratio ↔ commodity bridging (ADR-0014).
14
11448
pub(super) fn emit_anyref_to_typed_downcast(
15
11448
    ctx: &CompileContext,
16
11448
    helper: &mut FunctionEmitter,
17
11448
    ty: WasmType,
18
11448
) -> Result<()> {
19
11448
    match ty {
20
1667
        WasmType::Ratio => helper.ref_cast(ctx.ids.ty_ratio),
21
        WasmType::Commodity => helper.ref_cast(ctx.ids.ty_commodity),
22
        // `PairRef` / `StringRef` / `EntityRef` cells can legitimately be nil
23
        // (ADR-0033 Phase 5): a nullable cast keeps that nil rather than
24
        // trapping the engine — mirrors `emit_pair_car_downcast`. The
25
        // `TYPE-ERROR` null check runs later, only when a consumer
26
        // dereferences the value.
27
        WasmType::PairRef(_) => helper.ref_cast_nullable(ctx.ids.ty_pair),
28
624
        WasmType::StringRef => helper.ref_cast_nullable(ctx.ids.ty_i8_array),
29
        WasmType::SymbolRef => helper.ref_cast_nullable(ctx.ids.ty_symbol),
30
832
        WasmType::EntityRef(kind) => helper.ref_cast_nullable(ctx.ids.entity_type(kind)),
31
        WasmType::StructRef(_) => helper.ref_cast_nullable(ctx.ids.ty_struct),
32
        WasmType::I32 => {
33
6452
            helper.ref_cast_i31();
34
6452
            helper.i31_get_s();
35
        }
36
        // A bool item is `ref.null` (nil) / the `$t` singleton (t), never an
37
        // i31 (ADR-0033) — unboxing is "is it non-null?", mirroring
38
        // `emit_pair_car_downcast`'s `PairElement::Bool` arm.
39
        WasmType::Bool => {
40
1040
            helper.ref_is_null();
41
1040
            helper.i32_eqz();
42
        }
43
832
        WasmType::AnyRef => {}
44
1
        WasmType::Closure(sig) => helper.ref_cast_nullable(ctx.closure_sig(sig)?.closure_type_idx),
45
    }
46
11447
    Ok(())
47
11448
}
48

            
49
/// Wraps the body's typed result as the anyref the host fn receives: an index
50
/// through the range-checked `box_index`, a bool as nil-null / i31 1, a
51
/// reference as itself. A body that diverged left a polymorphic stack, which
52
/// the same boxing satisfies.
53
10193
pub(super) fn promote_result_to_anyref(
54
10193
    ctx: &CompileContext,
55
10193
    helper: &mut FunctionEmitter,
56
10193
    ty: WasmType,
57
10193
) -> Result<()> {
58
10193
    match ty {
59
416
        WasmType::AnyRef => Ok(()),
60
        WasmType::I32 | WasmType::Bool => {
61
7905
            emit_box_anyref(ctx, helper, ty);
62
7905
            Ok(())
63
        }
64
        WasmType::Ratio
65
        | WasmType::Commodity
66
        | WasmType::PairRef(_)
67
        | WasmType::StringRef
68
        | WasmType::SymbolRef
69
        | WasmType::EntityRef(_)
70
        | WasmType::StructRef(_) => {
71
            // Refs widen to anyref freely (subtyping); no instruction needed.
72
1664
            let _ = ctx;
73
1664
            let _ = helper;
74
1664
            Ok(())
75
        }
76
208
        WasmType::Closure(_) => Err(Error::Compile(format!(
77
208
            "host-anyref calling convention cannot widen body return type {ty} to anyref"
78
208
        ))),
79
    }
80
10193
}