Lines
100 %
Functions
Branches
//! `DebugValueData` writers for the compound value kinds — money and server
//! entities — so every kind the guest can hold has a debug serialisation
//! (plan 0014 Stage 1, ISS-018 / ISS-058).
use std::mem::offset_of;
use scripting_format::{DEBUG_VALUE_DATA_SIZE, DebugValueData, EntityHeader, ValueType};
use wasm_encoder::BlockType;
use crate::ast::EntityKind;
use crate::compiler::emit::FunctionEmitter;
use crate::compiler::expr::LOCAL_ENTITY_BASE;
use crate::error::Result;
use super::debug_values::{DEBUG_ENTITY_BYTES, Payload, store_i64_field};
use super::headers::OutputSerializer;
/// The commodity id's two i64 halves, inline after the fixed entity.
const COMMODITY_ID_BYTES: u32 = 16;
/// The guest handles a money writer needs.
#[derive(Clone, Copy)]
pub struct MoneyIds {
pub ty_commodity: u32,
pub ty_unit_term: u32,
/// `commodity_assert_atomic`: throws `NON-ATOMIC-COMMODITY` for a
/// compound unit term, which has no wire form.
pub assert_atomic: u32,
}
impl OutputSerializer {
/// The `$commodity` on the stack, by its unit term as the host decodes
/// it: null is atomic money (`Commodity`), an empty term is a
/// dimensionless `Number`, a compound term raises `NON-ATOMIC-COMMODITY`.
pub fn write_debug_commodity_from_stack(
&mut self,
emit: &mut FunctionEmitter,
ids: MoneyIds,
local: u32,
) -> Result<()> {
emit.local_set(local);
emit.local_get(local);
emit.struct_get(ids.ty_commodity, 4);
emit.ref_is_null();
emit.if_block(BlockType::Empty);
self.write_atomic_money(emit, ids.ty_commodity, local)?;
emit.else_block();
emit.ref_cast(ids.ty_unit_term);
emit.array_len();
emit.i32_eqz();
self.write_debug_number_from_fields(emit, ids.ty_commodity, local)?;
emit.call(ids.assert_atomic);
emit.drop_value();
emit.block_end()?;
emit.block_end()
fn write_atomic_money(
ty_commodity: u32,
let data = self.begin_debug_value(emit, Payload::Bytes(COMMODITY_ID_BYTES))?;
emit.store_u8_dynamic(
LOCAL_ENTITY_BASE,
data + offset_of!(DebugValueData, value_type) as u32,
ValueType::Commodity as u8,
);
let id_at = data + DEBUG_VALUE_DATA_SIZE as u32;
for (field, at) in [
(0, data + offset_of!(DebugValueData, data1) as u32),
(1, data + offset_of!(DebugValueData, data2) as u32),
(2, id_at),
(3, id_at + COMMODITY_ID_BYTES / 2),
] {
store_i64_field(emit, at, ty_commodity, local, field);
// The inline id bytes count in `data_size` so the entity walk skips
// them, as a string's inline bytes do.
emit.store_i32_dynamic(
offset_of!(EntityHeader, data_size) as u32,
(DEBUG_VALUE_DATA_SIZE as u32 + COMMODITY_ID_BYTES) as i32,
self.advance_past(emit, DEBUG_ENTITY_BYTES + COMMODITY_ID_BYTES);
Ok(())
/// An entity reference of `kind`, by its wire code alone; the reference
/// itself is not on the wire (the caller drops it).
pub fn write_debug_entity_tag(
kind: EntityKind,
let data = self.begin_debug_value(emit, Payload::None)?;
ValueType::Entity as u8,
emit.store_i64_dynamic(
data + offset_of!(DebugValueData, data1) as u32,
kind.wire_code(),
self.advance_past(emit, DEBUG_ENTITY_BYTES);