submilli-engine 0.3.0

Compiles Submilli programs, a TypeScript subset, to WebAssembly and runs them under capability policy
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
//! Per-module canonical-shape field-names registry.

use std::collections::BTreeSet;

use wasm_encoder::{
    ConstExpr, GlobalSection, GlobalType, HeapType, Instruction, RefType, StorageType, ValType,
};

use crate::Type;
use crate::codegen::intrinsics::IntrinsicTypeIndices;
use crate::codegen::symbol_table::SymbolTable;
use crate::codegen::{internal_failure, next_index, wasm_u32};
use crate::compiler_error::CompilerFailure;

/// Optional field names use a nominal string subtype. The name's text stays
/// unchanged, while `in` can recover optionality after receiver-type erasure.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct FieldName {
    pub name: String,
    pub optional: bool,
    pub is_accessor: bool,
    pub is_private: bool,
}

pub fn declare_optional_name_type(
    types: &mut wasm_encoder::TypeSection,
    symbols: &mut SymbolTable,
    next_type_idx: &mut u32,
    intrinsics: IntrinsicTypeIndices,
) -> Result<(), CompilerFailure> {
    let type_idx = next_index(next_type_idx)?;
    let mut fields = [intrinsics.vtable, intrinsics.raw_string]
        .map(|index| wasm_encoder::FieldType {
            element_type: StorageType::Val(ValType::Ref(RefType {
                nullable: false,
                heap_type: HeapType::Concrete(index),
            })),
            mutable: false,
        })
        .to_vec();
    fields.push(wasm_encoder::FieldType {
        element_type: StorageType::Val(ValType::I64),
        mutable: true,
    });
    fields.push(wasm_encoder::FieldType {
        element_type: StorageType::Val(ValType::I32),
        mutable: true,
    });
    fields.push(wasm_encoder::FieldType {
        element_type: StorageType::Val(ValType::I32),
        mutable: false,
    });
    types.ty().subtype(&wasm_encoder::SubType {
        is_final: true,
        supertype_idx: Some(intrinsics.string),
        composite_type: wasm_encoder::CompositeType {
            inner: wasm_encoder::CompositeInnerType::Struct(wasm_encoder::StructType {
                fields: fields.into(),
            }),
            shared: false,
            descriptor: None,
            describes: None,
        },
    });
    symbols.record_optional_field_name_type(type_idx);
    Ok(())
}

pub fn collect(shapes: &[Type]) -> Vec<Vec<FieldName>> {
    let mut keys: BTreeSet<Vec<FieldName>> = BTreeSet::new();
    for shape in shapes {
        if let Type::Object { fields, .. } = shape {
            keys.insert(
                fields
                    .iter()
                    .map(|(name, field)| FieldName {
                        name: name.clone(),
                        optional: field.optional,
                        is_accessor: false,
                        is_private: false,
                    })
                    .collect(),
            );
        }
    }
    keys.into_iter().collect()
}

pub fn emit(
    shapes: &[Vec<FieldName>],
    globals: &mut GlobalSection,
    symbols: &mut SymbolTable,
    next_global_idx: &mut u32,
    intrinsics: IntrinsicTypeIndices,
    string_vtable_global_idx: u32,
) -> Result<(), CompilerFailure> {
    let field_names_val_type = ValType::Ref(RefType {
        nullable: false,
        heap_type: HeapType::Concrete(intrinsics.field_names),
    });
    let optional_name_type = symbols.optional_field_name_type()?;
    for shape in shapes {
        let init = build_init_expr(
            shape,
            intrinsics,
            string_vtable_global_idx,
            optional_name_type,
        )?;
        let global_idx = next_index(next_global_idx)?;
        globals.global(
            GlobalType {
                val_type: field_names_val_type,
                mutable: false,
                shared: false,
            },
            &init,
        );
        symbols.record_field_names_global(shape.clone(), global_idx);
    }
    Ok(())
}

fn build_init_expr(
    shape: &[FieldName],
    intrinsics: IntrinsicTypeIndices,
    string_vtable_global_idx: u32,
    optional_name_type: u32,
) -> Result<ConstExpr, CompilerFailure> {
    let mut instrs: Vec<Instruction<'_>> = Vec::new();
    for name in shape {
        let code_units = crate::literal_units::literal_units(&name.name);
        instrs.push(Instruction::GlobalGet(string_vtable_global_idx));
        for unit in &code_units {
            instrs.push(Instruction::I32Const(i32::from(*unit)));
        }
        instrs.push(Instruction::ArrayNewFixed {
            array_type_index: intrinsics.raw_string,
            array_size: wasm_u32(code_units.len())?,
        });
        instrs.push(Instruction::I64Const(0));
        if name.optional || name.is_accessor || name.is_private {
            instrs.push(Instruction::I32Const(if name.is_accessor {
                -1
            } else if name.optional {
                0
            } else {
                1
            }));
            instrs.push(Instruction::I32Const(i32::from(name.is_private)));
        }
        instrs.push(Instruction::StructNew(
            if name.optional || name.is_accessor || name.is_private {
                optional_name_type
            } else {
                intrinsics.string
            },
        ));
    }
    instrs.push(Instruction::ArrayNewFixed {
        array_type_index: intrinsics.field_names,
        array_size: wasm_u32(shape.len())?,
    });
    Ok(ConstExpr::extended(instrs))
}

/// Optional names carry per-instance presence separately from the value slot.
/// Required names remain shared immutable strings.
pub(crate) fn emit_instance_names(
    emitter: &mut super::function_emitter::FunctionEmitter,
    ctx: &super::CodegenCtx,
    names: &[FieldName],
    present: impl Fn(&str) -> bool,
) -> Result<(), CompilerFailure> {
    let global = ctx
        .symbols
        .field_names_global_idx(names)
        .ok_or_else(|| internal_failure("object field names were not collected"))?;
    if !names.iter().any(|name| name.optional) {
        emitter.instruction(Instruction::GlobalGet(global));
        return Ok(());
    }
    let intrinsics = intrinsics(ctx)?;
    let optional_name_type = ctx.symbols.optional_field_name_type()?;
    for (index, name) in names.iter().enumerate() {
        let index = wasm_u32(index)?.cast_signed();
        if name.optional {
            for field_index in [0, 1] {
                emitter.instruction(Instruction::GlobalGet(global));
                emitter.instruction(Instruction::I32Const(index));
                emitter.instruction(Instruction::ArrayGet(intrinsics.field_names));
                emitter.instruction(Instruction::StructGet {
                    struct_type_index: intrinsics.string,
                    field_index,
                });
            }
            emitter.instruction(Instruction::I64Const(0));
            emitter.instruction(Instruction::I32Const(i32::from(present(&name.name))));
            emitter.instruction(Instruction::I32Const(i32::from(name.is_private)));
            emitter.instruction(Instruction::StructNew(optional_name_type));
        } else {
            emitter.instruction(Instruction::GlobalGet(global));
            emitter.instruction(Instruction::I32Const(index));
            emitter.instruction(Instruction::ArrayGet(intrinsics.field_names));
        }
    }
    emitter.instruction(Instruction::ArrayNewFixed {
        array_type_index: intrinsics.field_names,
        array_size: wasm_u32(names.len())?,
    });
    Ok(())
}

/// Push whether the name on the stack marks an internal accessor payload slot.
pub(crate) fn emit_name_is_accessor(
    emitter: &mut super::function_emitter::FunctionEmitter,
    ctx: &super::CodegenCtx,
) -> Result<(), crate::compiler_error::CompilerFailure> {
    let name = emitter.add_anonymous_local(ctx.symbols.value_type(&Type::String)?)?;
    let marked = ctx.symbols.optional_field_name_type()?;
    emitter.instruction(Instruction::LocalTee(name));
    emitter.instruction(Instruction::RefTestNonNull(HeapType::Concrete(marked)));
    emitter.emit_if(wasm_encoder::BlockType::Result(ValType::I32));
    emitter.instruction(Instruction::LocalGet(name));
    emitter.instruction(Instruction::RefCastNonNull(HeapType::Concrete(marked)));
    emitter.instruction(Instruction::StructGet {
        struct_type_index: marked,
        field_index: 3,
    });
    emitter.instruction(Instruction::I32Const(-1));
    emitter.instruction(Instruction::I32Eq);
    emitter.emit_else();
    emitter.instruction(Instruction::I32Const(0));
    emitter.emit_end();

    Ok(())
}

/// Push the presence flag for a field name already on the stack.
pub(crate) fn emit_name_presence(
    emitter: &mut super::function_emitter::FunctionEmitter,
    ctx: &super::CodegenCtx,
) -> Result<(), CompilerFailure> {
    let intrinsics = intrinsics(ctx)?;
    let optional = ctx.symbols.optional_field_name_type()?;
    let name = emitter.add_anonymous_local(ValType::Ref(RefType {
        nullable: false,
        heap_type: HeapType::Concrete(intrinsics.string),
    }))?;
    emitter.instruction(Instruction::LocalTee(name));
    emitter.instruction(Instruction::RefTestNonNull(HeapType::Concrete(optional)));
    emitter.emit_if(wasm_encoder::BlockType::Result(ValType::I32));
    emitter.instruction(Instruction::LocalGet(name));
    emitter.instruction(Instruction::RefCastNonNull(HeapType::Concrete(optional)));
    emitter.instruction(Instruction::StructGet {
        struct_type_index: optional,
        field_index: 3,
    });
    emitter.instruction(Instruction::I32Eqz);
    emitter.instruction(Instruction::I32Eqz);
    emitter.emit_else();
    emitter.instruction(Instruction::I32Const(1));
    emitter.emit_end();
    Ok(())
}

/// The object and index are locals; callers mark a successful store as present.
pub(crate) fn emit_mark_present(
    emitter: &mut super::function_emitter::FunctionEmitter,
    ctx: &super::CodegenCtx,
    object: u32,
    index: u32,
) -> Result<(), CompilerFailure> {
    let intrinsics = intrinsics(ctx)?;
    let optional = ctx.symbols.optional_field_name_type()?;
    let name = emitter.add_anonymous_local(ValType::Ref(RefType {
        nullable: false,
        heap_type: HeapType::Concrete(intrinsics.string),
    }))?;
    emitter.instruction(Instruction::LocalGet(object));
    emitter.instruction(Instruction::StructGet {
        struct_type_index: intrinsics.object_shape,
        field_index: 1,
    });
    emitter.instruction(Instruction::LocalGet(index));
    emitter.instruction(Instruction::ArrayGet(intrinsics.field_names));
    emitter.instruction(Instruction::LocalTee(name));
    emitter.instruction(Instruction::RefTestNonNull(HeapType::Concrete(optional)));
    emitter.emit_if(wasm_encoder::BlockType::Empty);
    emitter.instruction(Instruction::LocalGet(name));
    emitter.instruction(Instruction::RefCastNonNull(HeapType::Concrete(optional)));
    emitter.instruction(Instruction::StructGet {
        struct_type_index: optional,
        field_index: 3,
    });
    emitter.instruction(Instruction::I32Eqz);
    emitter.emit_if(wasm_encoder::BlockType::Empty);
    emitter.instruction(Instruction::LocalGet(name));
    emitter.instruction(Instruction::RefCastNonNull(HeapType::Concrete(optional)));
    emitter.instruction(Instruction::I32Const(1));
    emitter.instruction(Instruction::StructSet {
        struct_type_index: optional,
        field_index: 3,
    });
    emitter.emit_end();
    emitter.emit_end();
    Ok(())
}

fn intrinsics(ctx: &super::CodegenCtx) -> Result<IntrinsicTypeIndices, CompilerFailure> {
    ctx.symbols
        .intrinsic_type_indices()
        .ok_or_else(|| internal_failure("intrinsic types are not declared"))
}

/// Serializer fast path for an optional slot in its own declared layout.
pub(crate) fn emit_optional_presence(
    function: &mut wasm_encoder::Function,
    intrinsics: IntrinsicTypeIndices,
    optional_name_type: u32,
    object: u32,
    index: u32,
    value: u32,
) {
    for instruction in [
        Instruction::LocalGet(object),
        Instruction::RefCastNonNull(HeapType::Concrete(intrinsics.object_shape)),
        Instruction::StructGet {
            struct_type_index: intrinsics.object_shape,
            field_index: 1,
        },
        Instruction::I32Const(index.cast_signed()),
        Instruction::ArrayGet(intrinsics.field_names),
        Instruction::RefCastNonNull(HeapType::Concrete(optional_name_type)),
        Instruction::StructGet {
            struct_type_index: optional_name_type,
            field_index: 3,
        },
    ]
    .into_iter()
    .chain(super::nullish::is_nullish(
        &[Instruction::LocalGet(value)],
        intrinsics,
    ))
    .chain([Instruction::I32Eqz, Instruction::I32Or])
    {
        function.instruction(&instruction);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::collections::BTreeMap;

    #[test]
    fn collect_dedupes_same_shape_with_different_field_types() {
        use crate::ObjectField;
        let mut a = BTreeMap::new();
        a.insert("x".to_string(), ObjectField::required(Type::Number));
        let mut b = BTreeMap::new();
        b.insert("x".to_string(), ObjectField::required(Type::String));
        let shapes = vec![
            Type::Object {
                index: None,
                fields: a,
            },
            Type::Object {
                index: None,
                fields: b,
            },
        ];
        let keys = collect(&shapes);
        assert_eq!(keys.len(), 1);
        assert_eq!(
            keys[0],
            vec![FieldName {
                name: "x".to_string(),
                optional: false,
                is_accessor: false,
                is_private: false,
            }]
        );
    }

    #[test]
    fn collect_separates_shapes_with_different_field_names() {
        use crate::ObjectField;
        let mut a = BTreeMap::new();
        a.insert("x".to_string(), ObjectField::required(Type::Number));
        let mut b = BTreeMap::new();
        b.insert("y".to_string(), ObjectField::required(Type::Number));
        let shapes = vec![
            Type::Object {
                index: None,
                fields: a,
            },
            Type::Object {
                index: None,
                fields: b,
            },
        ];
        let keys = collect(&shapes);
        assert_eq!(keys.len(), 2);
        assert_eq!(
            keys[0],
            vec![FieldName {
                name: "x".to_string(),
                optional: false,
                is_accessor: false,
                is_private: false,
            }]
        );
        assert_eq!(
            keys[1],
            vec![FieldName {
                name: "y".to_string(),
                optional: false,
                is_accessor: false,
                is_private: false,
            }]
        );
    }
}