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

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

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

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