1
//! Boxing an i32-repr value for a `$pair` car or an anyref slot. An index boxes
2
//! through the range-checked `box_index` into an i31. A bool boxes through
3
//! `box_bool` into `ref.null` (nil) / the `$t` singleton (t) — the SAME
4
//! representation whether the cell is typed `Bool` or `AnyRef` (ADR-0033), so
5
//! a bool car never needs rebuilding when its chain widens.
6

            
7
use crate::ast::{PairElement, WasmType};
8
use crate::compiler::context::CompileContext;
9
use crate::compiler::emit::FunctionEmitter;
10

            
11
/// Boxes a value of type `ty` (on the stack) into an anyref slot. References are
12
/// anyref subtypes already.
13
2635711
pub(in crate::compiler) fn emit_box_anyref(
14
2635711
    ctx: &CompileContext,
15
2635711
    emit: &mut FunctionEmitter,
16
2635711
    ty: WasmType,
17
2635711
) {
18
2635711
    match ty {
19
2080232
        WasmType::I32 => emit.call(ctx.ids.box_index),
20
125457
        WasmType::Bool => emit.call(ctx.ids.box_bool),
21
430022
        _ => {}
22
    }
23
2635711
}
24

            
25
/// Boxes a car of element `value` (on the stack) for a chain whose cells are
26
/// `slot`.
27
1755421
pub(in crate::compiler) fn emit_box_cell(
28
1755421
    ctx: &CompileContext,
29
1755421
    emit: &mut FunctionEmitter,
30
1755421
    value: PairElement,
31
1755421
    slot: PairElement,
32
1755421
) {
33
1755421
    match (slot, value) {
34
4587
        (PairElement::AnyRef, _) => emit_box_anyref(ctx, emit, value.as_wasm_type()),
35
1680980
        (_, PairElement::I32) => emit.call(ctx.ids.box_index),
36
10820
        (_, PairElement::Bool) => emit.call(ctx.ids.box_bool),
37
59034
        _ => {}
38
    }
39
1755421
}
40

            
41
#[cfg(test)]
42
#[path = "boxing_tests.rs"]
43
mod tests;