1
//! `DELETE-ENTITY` codegen. Takes an entity index, reads the
2
//! `entity_type` byte at the entity's header offset, and routes
3
//! through the layout serializer to emit a delete-record entry in the
4
//! output stream.
5

            
6
use crate::ast::{Expr, WasmType};
7
use crate::compiler::context::CompileContext;
8
use crate::compiler::emit::FunctionEmitter;
9
use crate::compiler::expr::LOCAL_TEMP_I32;
10
use crate::error::Result;
11
use crate::runtime::SymbolTable;
12

            
13
use super::{arity_check, compile_idx_to_stack, emit_entity_header_offset};
14

            
15
pub(super) fn delete_entity(_symbols: &mut SymbolTable, args: &[Expr]) -> Result<Expr> {
16
    arity_check("DELETE-ENTITY", args, 1)?;
17
    Ok(Expr::Nil)
18
}
19

            
20
272
pub(super) fn compile_delete_entity(
21
272
    ctx: &mut CompileContext,
22
272
    emit: &mut FunctionEmitter,
23
272
    symbols: &mut SymbolTable,
24
272
    args: &[Expr],
25
272
) -> Result<()> {
26
272
    arity_check("DELETE-ENTITY", args, 1)?;
27

            
28
272
    compile_idx_to_stack(ctx, emit, symbols, &args[0])?;
29
272
    let temp = LOCAL_TEMP_I32;
30
272
    emit_entity_header_offset(ctx, emit, temp)?;
31
272
    let header_local = ctx.alloc_local(WasmType::I32)?;
32
272
    emit.local_set(header_local);
33

            
34
    // Read entity_type from input header
35
272
    let type_local = ctx.alloc_local(WasmType::I32)?;
36
272
    emit.local_get(header_local);
37
272
    emit.i32_load8_u(0);
38
272
    emit.local_set(type_local);
39

            
40
272
    ctx.serializer()
41
272
        .write_delete_entity(emit, type_local, header_local);
42
272
    Ok(())
43
272
}