1
//! Script-mode output of runtime struct instances. An instance is written as
2
//! a `Struct` … `ListEnd` run: the name, then each field's keyword and value.
3
//! The run is walked by [`emit_list`] over a temporary `$pair` chain, so
4
//! nesting costs no recursion.
5

            
6
use scripting_format::{MAX_WIRE_LIST_DEPTH, ValueType};
7
use wasm_encoder::BlockType;
8

            
9
use crate::ast::{PairElement, StructId, WasmType};
10
use crate::compiler::context::CompileContext;
11
use crate::compiler::emit::FunctionEmitter;
12
use crate::compiler::layout::GcLocals;
13
use crate::error::{Error, Result};
14

            
15
use super::serialize_value::{emit_list, nil_or};
16
use super::{LOCAL_GC_ARR, LOCAL_IDX};
17

            
18
const DEPTH_PLACEHOLDER: &[u8] = b"#<...>";
19
const LAYOUT_NAME_SLOT: i32 = 1;
20
const LAYOUT_FIELDS_AT: i32 = 2;
21
const LAYOUT_FIELD: u32 = 1;
22
const VALUES_FIELD: u32 = 2;
23

            
24
/// Writes the struct instance (nullable) on the stack: nil, else its fields.
25
8747
pub(super) fn serialize_struct_to_output(
26
8747
    ctx: &mut CompileContext,
27
8747
    emit: &mut FunctionEmitter,
28
8747
) -> Result<()> {
29
8747
    let instance = ctx.alloc_local(WasmType::StructRef(StructId(0)))?;
30
8747
    emit.local_set(instance);
31
8747
    nil_or(ctx, emit, instance, |ctx, emit| {
32
8747
        emit_struct(ctx, emit, instance)
33
8747
    })
34
8747
}
35

            
36
/// Writes the non-null instance in `instance`.
37
45074
pub(super) fn emit_struct(
38
45074
    ctx: &mut CompileContext,
39
45074
    emit: &mut FunctionEmitter,
40
45074
    instance: u32,
41
45074
) -> Result<()> {
42
45074
    let cursor = ctx.alloc_local(WasmType::PairRef(PairElement::AnyRef))?;
43
45074
    emit_chain(ctx, emit, instance, cursor)?;
44
45074
    emit_list(ctx, emit, cursor, ValueType::Struct)
45
45074
}
46

            
47
/// Sets `cursor` to the list `(NAME :F0 v0 :F1 v1 ...)` of the non-null
48
/// instance in `instance`, built from the last field back.
49
207344
pub(super) fn emit_chain(
50
207344
    ctx: &mut CompileContext,
51
207344
    emit: &mut FunctionEmitter,
52
207344
    instance: u32,
53
207344
    cursor: u32,
54
207344
) -> Result<()> {
55
207344
    let (pair, slots, ty_struct) = (ctx.ids.ty_pair, ctx.ids.ty_slots, ctx.ids.ty_struct);
56
207344
    let index = ctx.alloc_local(WasmType::I32)?;
57
207344
    emit.ref_null(pair);
58
207344
    emit.local_set(cursor);
59
207344
    emit.local_get(instance);
60
207344
    emit.struct_get(ty_struct, VALUES_FIELD);
61
207344
    emit.array_len();
62
207344
    emit.local_set(index);
63
207344
    emit.block_start();
64
207344
    emit.loop_start();
65
207344
    emit.local_get(index);
66
207344
    emit.i32_eqz();
67
207344
    emit.br_if(1);
68
207344
    emit.local_get(index);
69
207344
    emit.i32_const(1);
70
207344
    emit.i32_sub();
71
207344
    emit.local_set(index);
72
207344
    emit.local_get(instance);
73
207344
    emit.struct_get(ty_struct, VALUES_FIELD);
74
207344
    emit.local_get(index);
75
207344
    emit.array_get(slots);
76
207344
    emit.local_get(cursor);
77
207344
    emit.struct_new(pair);
78
207344
    emit.local_set(cursor);
79
207344
    emit.local_get(instance);
80
207344
    emit.struct_get(ty_struct, LAYOUT_FIELD);
81
207344
    emit.local_get(index);
82
207344
    emit.i32_const(LAYOUT_FIELDS_AT);
83
207344
    emit.i32_add();
84
207344
    emit.array_get(slots);
85
207344
    emit.local_get(cursor);
86
207344
    emit.struct_new(pair);
87
207344
    emit.local_set(cursor);
88
207344
    emit.br(0);
89
207344
    emit.block_end()?;
90
207344
    emit.block_end()?;
91
207344
    emit.local_get(instance);
92
207344
    emit.struct_get(ty_struct, LAYOUT_FIELD);
93
207344
    emit.i32_const(LAYOUT_NAME_SLOT);
94
207344
    emit.array_get(slots);
95
207344
    emit.local_get(cursor);
96
207344
    emit.struct_new(pair);
97
207344
    emit.local_set(cursor);
98
207344
    Ok(())
99
207344
}
100

            
101
/// Runs `descend` when the nesting `depth` (levels open so far) is below the
102
/// wire cap, counting the new level; else writes the placeholder instead of descending.
103
324540
pub(super) fn guard_depth(
104
324540
    ctx: &mut CompileContext,
105
324540
    emit: &mut FunctionEmitter,
106
324540
    depth: u32,
107
324540
    descend: impl FnOnce(&mut CompileContext, &mut FunctionEmitter) -> Result<()>,
108
324540
) -> Result<()> {
109
324540
    let cap = i32::try_from(MAX_WIRE_LIST_DEPTH)
110
        .map_err(|_| Error::Compile("internal: wire depth cap overflows".to_string()))?;
111
324540
    emit.local_get(depth);
112
324540
    emit.i32_const(cap);
113
324540
    emit.i32_ge_s();
114
324540
    emit.if_block(BlockType::Empty);
115
324540
    let gc = GcLocals {
116
324540
        type_idx: ctx.ids.ty_i8_array,
117
324540
        arr: LOCAL_GC_ARR,
118
324540
        idx: LOCAL_IDX,
119
324540
    };
120
324540
    let placeholder = ctx.add_data(DEPTH_PLACEHOLDER)?;
121
324540
    let len = u32::try_from(DEPTH_PLACEHOLDER.len())
122
        .map_err(|_| Error::Compile("internal: placeholder length overflows".to_string()))?;
123
324540
    ctx.serializer()
124
324540
        .write_debug_string_gc(emit, ValueType::Symbol, placeholder, len, &gc)?;
125
324540
    emit.else_block();
126
324540
    emit.local_get(depth);
127
324540
    emit.i32_const(1);
128
324540
    emit.i32_add();
129
324540
    emit.local_set(depth);
130
324540
    descend(ctx, emit)?;
131
324540
    emit.block_end()
132
324540
}