Lines
98 %
Functions
60 %
Branches
100 %
//! Script-mode output of runtime struct instances. An instance is written as
//! a `Struct` … `ListEnd` run: the name, then each field's keyword and value.
//! The run is walked by [`emit_list`] over a temporary `$pair` chain, so
//! nesting costs no recursion.
use scripting_format::{MAX_WIRE_LIST_DEPTH, ValueType};
use wasm_encoder::BlockType;
use crate::ast::{PairElement, StructId, WasmType};
use crate::compiler::context::CompileContext;
use crate::compiler::emit::FunctionEmitter;
use crate::compiler::layout::GcLocals;
use crate::error::{Error, Result};
use super::serialize_value::{emit_list, nil_or};
use super::{LOCAL_GC_ARR, LOCAL_IDX};
const DEPTH_PLACEHOLDER: &[u8] = b"#<...>";
const LAYOUT_NAME_SLOT: i32 = 1;
const LAYOUT_FIELDS_AT: i32 = 2;
const LAYOUT_FIELD: u32 = 1;
const VALUES_FIELD: u32 = 2;
/// Writes the struct instance (nullable) on the stack: nil, else its fields.
pub(super) fn serialize_struct_to_output(
ctx: &mut CompileContext,
emit: &mut FunctionEmitter,
) -> Result<()> {
let instance = ctx.alloc_local(WasmType::StructRef(StructId(0)))?;
emit.local_set(instance);
nil_or(ctx, emit, instance, |ctx, emit| {
emit_struct(ctx, emit, instance)
})
}
/// Writes the non-null instance in `instance`.
pub(super) fn emit_struct(
instance: u32,
let cursor = ctx.alloc_local(WasmType::PairRef(PairElement::AnyRef))?;
emit_chain(ctx, emit, instance, cursor)?;
emit_list(ctx, emit, cursor, ValueType::Struct)
/// Sets `cursor` to the list `(NAME :F0 v0 :F1 v1 ...)` of the non-null
/// instance in `instance`, built from the last field back.
pub(super) fn emit_chain(
cursor: u32,
let (pair, slots, ty_struct) = (ctx.ids.ty_pair, ctx.ids.ty_slots, ctx.ids.ty_struct);
let index = ctx.alloc_local(WasmType::I32)?;
emit.ref_null(pair);
emit.local_set(cursor);
emit.local_get(instance);
emit.struct_get(ty_struct, VALUES_FIELD);
emit.array_len();
emit.local_set(index);
emit.block_start();
emit.loop_start();
emit.local_get(index);
emit.i32_eqz();
emit.br_if(1);
emit.i32_const(1);
emit.i32_sub();
emit.array_get(slots);
emit.local_get(cursor);
emit.struct_new(pair);
emit.struct_get(ty_struct, LAYOUT_FIELD);
emit.i32_const(LAYOUT_FIELDS_AT);
emit.i32_add();
emit.br(0);
emit.block_end()?;
emit.i32_const(LAYOUT_NAME_SLOT);
Ok(())
/// Runs `descend` when the nesting `depth` (levels open so far) is below the
/// wire cap, counting the new level; else writes the placeholder instead of descending.
pub(super) fn guard_depth(
depth: u32,
descend: impl FnOnce(&mut CompileContext, &mut FunctionEmitter) -> Result<()>,
let cap = i32::try_from(MAX_WIRE_LIST_DEPTH)
.map_err(|_| Error::Compile("internal: wire depth cap overflows".to_string()))?;
emit.local_get(depth);
emit.i32_const(cap);
emit.i32_ge_s();
emit.if_block(BlockType::Empty);
let gc = GcLocals {
type_idx: ctx.ids.ty_i8_array,
arr: LOCAL_GC_ARR,
idx: LOCAL_IDX,
};
let placeholder = ctx.add_data(DEPTH_PLACEHOLDER)?;
let len = u32::try_from(DEPTH_PLACEHOLDER.len())
.map_err(|_| Error::Compile("internal: placeholder length overflows".to_string()))?;
ctx.serializer()
.write_debug_string_gc(emit, ValueType::Symbol, placeholder, len, &gc)?;
emit.else_block();
emit.local_set(depth);
descend(ctx, emit)?;
emit.block_end()