1
//! `DebugValueData` writers for the compound value kinds — money and server
2
//! entities — so every kind the guest can hold has a debug serialisation
3
//! (plan 0014 Stage 1, ISS-018 / ISS-058).
4

            
5
use std::mem::offset_of;
6

            
7
use scripting_format::{DEBUG_VALUE_DATA_SIZE, DebugValueData, EntityHeader, ValueType};
8
use wasm_encoder::BlockType;
9

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

            
15
use super::debug_values::{DEBUG_ENTITY_BYTES, Payload, store_i64_field};
16
use super::headers::OutputSerializer;
17

            
18
/// The commodity id's two i64 halves, inline after the fixed entity.
19
const COMMODITY_ID_BYTES: u32 = 16;
20

            
21
/// The guest handles a money writer needs.
22
#[derive(Clone, Copy)]
23
pub struct MoneyIds {
24
    pub ty_commodity: u32,
25
    pub ty_unit_term: u32,
26
    /// `commodity_assert_atomic`: throws `NON-ATOMIC-COMMODITY` for a
27
    /// compound unit term, which has no wire form.
28
    pub assert_atomic: u32,
29
}
30

            
31
impl OutputSerializer {
32
    /// The `$commodity` on the stack, by its unit term as the host decodes
33
    /// it: null is atomic money (`Commodity`), an empty term is a
34
    /// dimensionless `Number`, a compound term raises `NON-ATOMIC-COMMODITY`.
35
198615
    pub fn write_debug_commodity_from_stack(
36
198615
        &mut self,
37
198615
        emit: &mut FunctionEmitter,
38
198615
        ids: MoneyIds,
39
198615
        local: u32,
40
198615
    ) -> Result<()> {
41
198615
        emit.local_set(local);
42
198615
        emit.local_get(local);
43
198615
        emit.struct_get(ids.ty_commodity, 4);
44
198615
        emit.ref_is_null();
45
198615
        emit.if_block(BlockType::Empty);
46
198615
        self.write_atomic_money(emit, ids.ty_commodity, local)?;
47
198615
        emit.else_block();
48
198615
        emit.local_get(local);
49
198615
        emit.struct_get(ids.ty_commodity, 4);
50
198615
        emit.ref_cast(ids.ty_unit_term);
51
198615
        emit.array_len();
52
198615
        emit.i32_eqz();
53
198615
        emit.if_block(BlockType::Empty);
54
198615
        self.write_debug_number_from_fields(emit, ids.ty_commodity, local)?;
55
198615
        emit.else_block();
56
198615
        emit.local_get(local);
57
198615
        emit.call(ids.assert_atomic);
58
198615
        emit.drop_value();
59
198615
        emit.block_end()?;
60
198615
        emit.block_end()
61
198615
    }
62

            
63
198615
    fn write_atomic_money(
64
198615
        &mut self,
65
198615
        emit: &mut FunctionEmitter,
66
198615
        ty_commodity: u32,
67
198615
        local: u32,
68
198615
    ) -> Result<()> {
69
198615
        let data = self.begin_debug_value(emit, Payload::Bytes(COMMODITY_ID_BYTES))?;
70
198615
        emit.store_u8_dynamic(
71
198615
            LOCAL_ENTITY_BASE,
72
198615
            data + offset_of!(DebugValueData, value_type) as u32,
73
198615
            ValueType::Commodity as u8,
74
198615
        );
75
198615
        let id_at = data + DEBUG_VALUE_DATA_SIZE as u32;
76
198615
        for (field, at) in [
77
198615
            (0, data + offset_of!(DebugValueData, data1) as u32),
78
198615
            (1, data + offset_of!(DebugValueData, data2) as u32),
79
198615
            (2, id_at),
80
198615
            (3, id_at + COMMODITY_ID_BYTES / 2),
81
198615
        ] {
82
794460
            store_i64_field(emit, at, ty_commodity, local, field);
83
        }
84
        // The inline id bytes count in `data_size` so the entity walk skips
85
        // them, as a string's inline bytes do.
86
198615
        emit.store_i32_dynamic(
87
198615
            LOCAL_ENTITY_BASE,
88
198615
            offset_of!(EntityHeader, data_size) as u32,
89
198615
            (DEBUG_VALUE_DATA_SIZE as u32 + COMMODITY_ID_BYTES) as i32,
90
198615
        );
91
198615
        self.advance_past(emit, DEBUG_ENTITY_BYTES + COMMODITY_ID_BYTES);
92
198615
        Ok(())
93
198615
    }
94

            
95
    /// An entity reference of `kind`, by its wire code alone; the reference
96
    /// itself is not on the wire (the caller drops it).
97
1590058
    pub fn write_debug_entity_tag(
98
1590058
        &mut self,
99
1590058
        emit: &mut FunctionEmitter,
100
1590058
        kind: EntityKind,
101
1590058
    ) -> Result<()> {
102
1590058
        let data = self.begin_debug_value(emit, Payload::None)?;
103
1590058
        emit.store_u8_dynamic(
104
1590058
            LOCAL_ENTITY_BASE,
105
1590058
            data + offset_of!(DebugValueData, value_type) as u32,
106
1590058
            ValueType::Entity as u8,
107
1590058
        );
108
1590058
        emit.store_i64_dynamic(
109
1590058
            LOCAL_ENTITY_BASE,
110
1590058
            data + offset_of!(DebugValueData, data1) as u32,
111
1590058
            kind.wire_code(),
112
1590058
        );
113
1590058
        self.advance_past(emit, DEBUG_ENTITY_BYTES);
114
1590058
        Ok(())
115
1590058
    }
116
}