1
//! `Operation::Delete` entity record. No data payload — just an
2
//! entity header carrying the id of the row to remove. Runs after
3
//! a CRUD list is iterated, so the `entity_type` rides a wasm local
4
//! rather than being known at compile time.
5

            
6
use std::mem::offset_of;
7

            
8
use scripting_format::{ENTITY_HEADER_SIZE, EntityHeader, Operation};
9

            
10
use crate::compiler::emit::FunctionEmitter;
11
use crate::compiler::expr::LOCAL_ENTITY_BASE;
12
use crate::error::Result;
13

            
14
use super::headers::OutputSerializer;
15

            
16
impl OutputSerializer {
17
2080
    pub fn write_delete_entity(
18
2080
        &mut self,
19
2080
        emit: &mut FunctionEmitter,
20
2080
        entity_type_local: u32,
21
2080
        input_header_local: u32,
22
2080
    ) -> Result<()> {
23
2080
        emit.i32_const(ENTITY_HEADER_SIZE as i32);
24
2080
        self.ensure_room(emit)?;
25
2080
        self.load_entity_base(emit);
26
2080
        let ent = LOCAL_ENTITY_BASE;
27

            
28
        // entity_type from a runtime local.
29
2080
        emit.local_get(ent);
30
2080
        emit.i32_const(offset_of!(EntityHeader, entity_type) as i32);
31
2080
        emit.i32_add();
32
2080
        emit.local_get(entity_type_local);
33
2080
        emit.i32_store8_raw();
34

            
35
2080
        emit.store_u8_dynamic(
36
2080
            ent,
37
2080
            offset_of!(EntityHeader, operation) as u32,
38
2080
            Operation::Delete as u8,
39
2080
        );
40
2080
        emit.store_u8_dynamic(ent, offset_of!(EntityHeader, flags) as u32, 0);
41

            
42
        // Copy the 16-byte id from the input entity header.
43
2080
        for i in 0..4u32 {
44
8320
            emit.local_get(ent);
45
8320
            emit.i32_const((offset_of!(EntityHeader, id) as u32 + i * 4) as i32);
46
8320
            emit.i32_add();
47
8320
            emit.local_get(input_header_local);
48
8320
            emit.i32_load((offset_of!(EntityHeader, id) + (i as usize) * 4) as u64);
49
8320
            emit.i32_store_raw();
50
        }
51

            
52
2080
        emit.store_i32_dynamic(ent, offset_of!(EntityHeader, parent_idx) as u32, -1);
53

            
54
        // data_offset = (entity_base - output_base) + ENTITY_HEADER_SIZE.
55
2080
        emit.local_get(ent);
56
2080
        emit.i32_const(offset_of!(EntityHeader, data_offset) as i32);
57
2080
        emit.i32_add();
58
2080
        emit.local_get(ent);
59
2080
        emit.local_get(self.output_base_local);
60
2080
        emit.i32_sub();
61
2080
        emit.i32_const(ENTITY_HEADER_SIZE as i32);
62
2080
        emit.i32_add();
63
2080
        emit.i32_store_raw();
64

            
65
2080
        emit.store_i32_dynamic(ent, offset_of!(EntityHeader, data_size) as u32, 0);
66

            
67
2080
        self.bump_entity_count(emit);
68
2080
        self.advance_past(emit, ENTITY_HEADER_SIZE as u32);
69
2080
        Ok(())
70
2080
    }
71
}