Lines
99.61 %
Functions
95.65 %
Branches
100 %
//! Script-mode output of runtime reference values. A list is written as a
//! pre-order `List` … `ListEnd` run of debug values, walked iteratively with
//! the rest of each enclosing list kept on a stack of `$pair` cells, so
//! nesting depth costs heap rather than wasm stack. Every element, and every
//! `AnyRef`, is classified by its runtime shape. A runtime struct instance is
//! walked the same way as a list opened by the `Struct` tag.
use scripting_format::ValueType;
use wasm_encoder::BlockType;
use crate::ast::{EntityKind, 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::compile::money_ids;
use super::serialize_struct::{emit_chain, emit_struct, guard_depth};
use super::{LOCAL_GC_ARR, LOCAL_IDX, LOCAL_TEMP_I32, LOCAL_TEMP_RATIO};
const PLACEHOLDER: &[u8] = b"<anyref>";
/// Writes the `AnyRef` on the stack.
pub(super) fn serialize_anyref_to_output(
ctx: &mut CompileContext,
emit: &mut FunctionEmitter,
) -> Result<()> {
let value = ctx.alloc_local(WasmType::AnyRef)?;
emit.local_set(value);
emit_value(ctx, emit, value)
}
/// Writes the list (a nullable `$pair`) on the stack.
pub(super) fn serialize_list_to_output(
let cursor = ctx.alloc_local(WasmType::PairRef(PairElement::AnyRef))?;
emit.local_set(cursor);
nil_or(ctx, emit, cursor, |ctx, emit| {
emit_list(ctx, emit, cursor, ValueType::List)
})
/// Writes the entity reference (nullable) on the stack: nil, else its kind tag.
pub(super) fn serialize_entity_to_output(
kind: EntityKind,
let entity = ctx.alloc_local(WasmType::EntityRef(kind))?;
emit.local_set(entity);
nil_or(ctx, emit, entity, |ctx, emit| {
ctx.serializer().write_debug_entity_tag(emit, kind)
/// Writes the symbol (nullable) on the stack: nil, else its name as a symbol.
pub(super) fn serialize_symbol_to_output(
let symbol = ctx.alloc_local(WasmType::SymbolRef)?;
emit.local_set(symbol);
nil_or(ctx, emit, symbol, |ctx, emit| {
let gc = GcLocals {
type_idx: ctx.ids.ty_i8_array,
arr: LOCAL_GC_ARR,
idx: LOCAL_IDX,
};
emit.local_get(symbol);
emit.struct_get(ctx.ids.ty_symbol, 0);
ctx.serializer()
.write_debug_string_from_stack(emit, ValueType::Symbol, &gc)
/// Writes nil when the reference in `local` is null, else what `write` emits.
pub(super) fn nil_or(
local: u32,
write: impl FnOnce(&mut CompileContext, &mut FunctionEmitter) -> Result<()>,
emit.local_get(local);
emit.ref_is_null();
emit.if_block(BlockType::Empty);
ctx.serializer().write_debug_tag(emit, ValueType::Nil)?;
emit.else_block();
write(ctx, emit)?;
emit.block_end()
/// The value in the `AnyRef` local `value`: nil, a list, or an atom.
fn emit_value(ctx: &mut CompileContext, emit: &mut FunctionEmitter, value: u32) -> Result<()> {
nil_or(ctx, emit, value, |ctx, emit| {
let pair = ctx.ids.ty_pair;
emit.local_get(value);
emit.ref_test(pair);
emit.ref_cast_nullable(pair);
emit_list(ctx, emit, cursor, ValueType::List)?;
emit.ref_test(ctx.ids.ty_struct);
let instance = ctx.alloc_local(WasmType::StructRef(StructId(0)))?;
emit.ref_cast_nullable(ctx.ids.ty_struct);
emit.local_set(instance);
emit_struct(ctx, emit, instance)?;
emit_atom(ctx, emit, value)?;
emit.block_end()?;
/// Walks the non-null list in `cursor`, opened by `open`. On a nested list or
/// struct the rest of the current one is pushed onto `pending`; on each
/// list's end it is popped. Nesting past `MAX_WIRE_LIST_DEPTH` is written as
/// a placeholder, so a struct holding itself ends.
pub(super) fn emit_list(
cursor: u32,
open: ValueType,
let pending = ctx.alloc_local(WasmType::PairRef(PairElement::AnyRef))?;
let car = ctx.alloc_local(WasmType::AnyRef)?;
let depth = ctx.alloc_local(WasmType::I32)?;
emit.i32_const(1);
emit.local_set(depth);
emit.ref_null(pair);
emit.local_set(pending);
ctx.serializer().write_debug_tag(emit, open)?;
emit.block_start();
emit.loop_start();
emit.local_get(cursor);
ctx.serializer().write_debug_tag(emit, ValueType::ListEnd)?;
emit.local_get(pending);
emit.br_if(2);
emit.local_get(depth);
emit.i32_sub();
emit.struct_get(pair, 0);
emit.struct_get(pair, 1);
emit.br(1);
emit.local_set(car);
nil_or(ctx, emit, car, |ctx, emit| {
emit.local_get(car);
guard_depth(ctx, emit, depth, |ctx, emit| {
push_rest(ctx, emit, cursor, pending, ValueType::List)?;
Ok(())
})?;
push_rest(ctx, emit, cursor, pending, ValueType::Struct)?;
emit_chain(ctx, emit, instance, cursor)
emit_atom(ctx, emit, car)?;
emit.br(0);
/// Pushes the rest of the current list, in `cursor`, onto `pending` and
/// writes the `open` tag of the nested list or struct.
fn push_rest(
pending: u32,
emit.struct_new(ctx.ids.ty_pair);
ctx.serializer().write_debug_tag(emit, open)
/// The non-null, non-list value in `value`: the `$t` singleton (by `ref.eq`,
/// never a type test, as WasmGC struct types are structural), an index, a
/// scalar, a string, money, an entity of each kind (the entity structs share
/// one recursion group, so same-shaped kinds are distinct types) or a
/// closure (by its `$closure_base` supertype). Every kind the guest can box
/// is tagged; the `<anyref>` placeholder remains only as the final arm no
/// value reaches.
fn emit_atom(ctx: &mut CompileContext, emit: &mut FunctionEmitter, value: u32) -> Result<()> {
let ratio = ctx.ids.ty_ratio;
let i8_array = ctx.ids.ty_i8_array;
type_idx: i8_array,
let mut open = 0;
emit.ref_cast_eq();
emit.global_get(ctx.ids.global_t);
emit.ref_eq();
open += 1;
ctx.serializer().write_debug_tag(emit, ValueType::T)?;
emit.ref_test_i31();
emit.ref_cast_i31();
emit.i31_get_s();
.write_debug_i32_from_stack(emit, LOCAL_TEMP_I32)?;
open += open_kind_test(emit, value, ratio);
emit.ref_cast(ratio);
.write_debug_number_from_stack(emit, ratio, LOCAL_TEMP_RATIO)?;
open += open_kind_test(emit, value, i8_array);
emit.ref_cast(i8_array);
.write_debug_string_from_stack(emit, ValueType::String, &gc)?;
open += open_kind_test(emit, value, ctx.ids.ty_symbol);
emit.ref_cast(ctx.ids.ty_symbol);
.write_debug_string_from_stack(emit, ValueType::Symbol, &gc)?;
let money = money_ids(ctx);
let money_local = ctx.alloc_local(WasmType::Commodity)?;
open += open_kind_test(emit, value, money.ty_commodity);
emit.ref_cast(money.ty_commodity);
.write_debug_commodity_from_stack(emit, money, money_local)?;
for kind in EntityKind::ALL {
open += open_kind_test(emit, value, ctx.ids.entity_type(kind));
ctx.serializer().write_debug_entity_tag(emit, kind)?;
let closure_base = ctx.ids.ty_closure_base;
open += open_kind_test(emit, value, closure_base);
emit.ref_cast(closure_base);
.write_debug_closure_from_stack(emit, closure_base, LOCAL_TEMP_I32)?;
let placeholder = ctx.add_data(PLACEHOLDER)?;
let len = u32::try_from(PLACEHOLDER.len())
.map_err(|_| Error::Compile("internal: placeholder length overflows".to_string()))?;
.write_debug_string_gc(emit, ValueType::String, placeholder, len, &gc)?;
for _ in 0..open {
/// Opens the `if` arm taken when `value` is of the struct/array `type_idx`;
/// the one block it opens, for the caller's count.
fn open_kind_test(emit: &mut FunctionEmitter, value: u32, type_idx: u32) -> usize {
emit.ref_test(type_idx);
1