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

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

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

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