1
//! Global / context accessors that don't take an entity index, plus
2
//! the two index-driven header readers `ENTITY-TYPE` and
3
//! `ENTITY-PARENT-IDX`, plus the legacy `GET-INPUT-ENTITIES` builder.
4
//!
5
//! Every eval handler here returns a `WasmRuntime(I32)` placeholder —
6
//! all real work happens at codegen time via the wasm import calls in
7
//! the compile_* counterparts.
8

            
9
use crate::ast::{Expr, PairElement, WasmType};
10
use crate::compiler::context::CompileContext;
11
use crate::compiler::emit::FunctionEmitter;
12
use crate::compiler::expr::{LOCAL_TEMP_I32, eval_value};
13
use crate::error::Result;
14
use crate::runtime::SymbolTable;
15

            
16
use super::{arity_check, compile_idx_to_stack, emit_entity_header_offset};
17

            
18
570289
pub(super) fn entity_count(_symbols: &mut SymbolTable, args: &[Expr]) -> Result<Expr> {
19
570289
    arity_check("ENTITY-COUNT", args, 0)?;
20
570288
    Ok(Expr::WasmRuntime(WasmType::I32))
21
570289
}
22

            
23
1
pub(super) fn context_type(_symbols: &mut SymbolTable, args: &[Expr]) -> Result<Expr> {
24
1
    arity_check("CONTEXT-TYPE", args, 0)?;
25
1
    Ok(Expr::WasmRuntime(WasmType::I32))
26
1
}
27

            
28
18115
pub(super) fn primary_entity_type(_symbols: &mut SymbolTable, args: &[Expr]) -> Result<Expr> {
29
18115
    arity_check("PRIMARY-ENTITY-TYPE", args, 0)?;
30
18115
    Ok(Expr::WasmRuntime(WasmType::I32))
31
18115
}
32

            
33
308473
pub(super) fn primary_entity_idx(_symbols: &mut SymbolTable, args: &[Expr]) -> Result<Expr> {
34
308473
    arity_check("PRIMARY-ENTITY-IDX", args, 0)?;
35
308473
    Ok(Expr::WasmRuntime(WasmType::I32))
36
308473
}
37

            
38
176342
pub(super) fn entity_type(symbols: &mut SymbolTable, args: &[Expr]) -> Result<Expr> {
39
176342
    arity_check("ENTITY-TYPE", args, 1)?;
40
176341
    eval_value(symbols, &args[0])?;
41
176341
    Ok(Expr::WasmRuntime(WasmType::I32))
42
176342
}
43

            
44
154052
pub(super) fn entity_parent_idx(symbols: &mut SymbolTable, args: &[Expr]) -> Result<Expr> {
45
154052
    arity_check("ENTITY-PARENT-IDX", args, 1)?;
46
154052
    eval_value(symbols, &args[0])?;
47
154052
    Ok(Expr::WasmRuntime(WasmType::I32))
48
154052
}
49

            
50
1664
pub(super) fn get_input_entities(_symbols: &mut SymbolTable, args: &[Expr]) -> Result<Expr> {
51
1664
    arity_check("GET-INPUT-ENTITIES", args, 0)?;
52
1664
    Ok(Expr::WasmRuntime(WasmType::PairRef(PairElement::I32)))
53
1664
}
54

            
55
147792
pub(super) fn compile_entity_count_to_stack(
56
147792
    ctx: &mut CompileContext,
57
147792
    emit: &mut FunctionEmitter,
58
147792
    _symbols: &mut SymbolTable,
59
147792
    args: &[Expr],
60
147792
) -> Result<WasmType> {
61
147792
    arity_check("ENTITY-COUNT", args, 0)?;
62
147792
    emit.call(ctx.ids.get_input_entities_count()?);
63
147567
    Ok(WasmType::I32)
64
147792
}
65

            
66
208
pub(super) fn compile_context_type_to_stack(
67
208
    ctx: &mut CompileContext,
68
208
    emit: &mut FunctionEmitter,
69
208
    _symbols: &mut SymbolTable,
70
208
    args: &[Expr],
71
208
) -> Result<WasmType> {
72
208
    arity_check("CONTEXT-TYPE", args, 0)?;
73
208
    emit.call(ctx.ids.get_input_offset()?);
74
208
    emit.i32_load8_u(6);
75
208
    Ok(WasmType::I32)
76
208
}
77

            
78
6246
pub(super) fn compile_primary_entity_type_to_stack(
79
6246
    ctx: &mut CompileContext,
80
6246
    emit: &mut FunctionEmitter,
81
6246
    _symbols: &mut SymbolTable,
82
6246
    args: &[Expr],
83
6246
) -> Result<WasmType> {
84
6246
    arity_check("PRIMARY-ENTITY-TYPE", args, 0)?;
85
6246
    emit.call(ctx.ids.get_input_offset()?);
86
6246
    emit.i32_load8_u(7);
87
6246
    Ok(WasmType::I32)
88
6246
}
89

            
90
50760
pub(super) fn compile_primary_entity_idx_to_stack(
91
50760
    ctx: &mut CompileContext,
92
50760
    emit: &mut FunctionEmitter,
93
50760
    _symbols: &mut SymbolTable,
94
50760
    args: &[Expr],
95
50760
) -> Result<WasmType> {
96
50760
    arity_check("PRIMARY-ENTITY-IDX", args, 0)?;
97
50760
    emit.call(ctx.ids.get_input_offset()?);
98
50760
    emit.i32_load(0x20);
99
50760
    Ok(WasmType::I32)
100
50760
}
101

            
102
27899
pub(super) fn compile_entity_type_to_stack(
103
27899
    ctx: &mut CompileContext,
104
27899
    emit: &mut FunctionEmitter,
105
27899
    symbols: &mut SymbolTable,
106
27899
    args: &[Expr],
107
27899
) -> Result<WasmType> {
108
27899
    arity_check("ENTITY-TYPE", args, 1)?;
109
27899
    compile_idx_to_stack(ctx, emit, symbols, &args[0])?;
110
27896
    emit_entity_header_offset(ctx, emit, LOCAL_TEMP_I32)?;
111
27896
    emit.i32_load8_u(0); // entity_type at offset 0 in header
112
27896
    Ok(WasmType::I32)
113
27899
}
114

            
115
24773
pub(super) fn compile_entity_parent_idx_to_stack(
116
24773
    ctx: &mut CompileContext,
117
24773
    emit: &mut FunctionEmitter,
118
24773
    symbols: &mut SymbolTable,
119
24773
    args: &[Expr],
120
24773
) -> Result<WasmType> {
121
24773
    arity_check("ENTITY-PARENT-IDX", args, 1)?;
122
24773
    compile_idx_to_stack(ctx, emit, symbols, &args[0])?;
123
24773
    emit_entity_header_offset(ctx, emit, LOCAL_TEMP_I32)?;
124
24773
    emit.i32_load(0x14); // parent_idx at offset 0x14 in header
125
24773
    Ok(WasmType::I32)
126
24773
}
127

            
128
1664
pub(super) fn compile_get_input_entities_to_stack(
129
1664
    ctx: &mut CompileContext,
130
1664
    emit: &mut FunctionEmitter,
131
1664
) -> Result<WasmType> {
132
    // Builds a `$pair<i32>` list: loop entity_count-1..=0, boxing each
133
    // index via `ref.i31` before pair_new so the values ride the anyref
134
    // car. Same loop as the legacy $cons builder; the difference is the
135
    // single `pair_new` call replacing the bespoke `struct.new cons`.
136
1664
    let i_local = ctx.alloc_local(WasmType::I32)?;
137
1664
    let result_local = ctx.alloc_local(WasmType::PairRef(PairElement::I32))?;
138
1664
    let pair_type = ctx.ids.ty_pair;
139
1664
    let pair_new = ctx.ids.pair_new;
140

            
141
    // result = nil
142
1664
    emit.ref_null(pair_type);
143
1664
    emit.local_set(result_local);
144

            
145
    // i = entity_count - 1
146
1664
    emit.call(ctx.ids.get_input_offset()?);
147
1664
    emit.i32_load(0x08); // entity_count at GlobalHeader offset 8
148
1664
    emit.i32_const(1);
149
1664
    emit.i32_sub();
150
1664
    emit.local_set(i_local);
151

            
152
1664
    emit.block_start();
153
1664
    emit.loop_start();
154

            
155
    // if i < 0 → exit
156
1664
    emit.local_get(i_local);
157
1664
    emit.i32_const(0);
158
1664
    emit.i32_lt_s();
159
1664
    emit.br_if(1);
160

            
161
    // result = pair_new(box_index i, result)
162
1664
    emit.local_get(i_local);
163
1664
    emit.call(ctx.ids.box_index);
164
1664
    emit.local_get(result_local);
165
1664
    emit.call(pair_new);
166
1664
    emit.local_set(result_local);
167

            
168
    // i = i - 1
169
1664
    emit.local_get(i_local);
170
1664
    emit.i32_const(1);
171
1664
    emit.i32_sub();
172
1664
    emit.local_set(i_local);
173

            
174
1664
    emit.br(0);
175
1664
    emit.block_end()?;
176
1664
    emit.block_end()?;
177

            
178
1664
    emit.local_get(result_local);
179
1664
    Ok(WasmType::PairRef(PairElement::I32))
180
1664
}