Lines
100 %
Functions
Branches
//! The unit prologue of the session's struct instances.
use crate::compiler::CompileMode;
use wasmparser::{ExternalKind, Parser, Payload};
use super::allocate_instances;
use crate::ast::{Expr, Number, StructId, WasmType};
use crate::compiler::context::{CompileContext, instance_export_name};
use crate::compiler::emit::FunctionEmitter;
use crate::compiler::expr::prepare_unit;
use crate::reader::Reader;
use crate::runtime::{CellBinding, Found, StructInstance, Symbol, SymbolKind, SymbolTable};
use crate::{Compiler, Program};
fn number(n: i64) -> Expr {
Expr::Number(Number::Index(n))
}
/// A table whose global `P` holds instance 0 of `(defstruct pt x)`, with `x`
/// carried as `field`, and whose counter stands at `next_serial`.
fn holding(field: Expr, next_serial: u32) -> (SymbolTable, StructId) {
let mut symbols = SymbolTable::with_builtins_for_wasm();
let id = symbols.define_struct("PT", vec!["X".to_string()]).unwrap();
let leaf = Expr::Instance(0, id);
symbols.define(Symbol::new("P", SymbolKind::Variable).with_value(leaf));
let instance = StructInstance {
layout: id,
fields: vec![field],
};
symbols.settle_instances(Found::from([(0, Some(instance))]), next_serial);
(symbols, id)
fn prologue(symbols: &mut SymbolTable) -> CompileContext {
let mut ctx = CompileContext::new_with_host_fns(&[]).unwrap();
let mut emit = FunctionEmitter::new();
let scan = prepare_unit(symbols, &[], CompileMode::Script).unwrap();
allocate_instances(&mut ctx, &mut emit, symbols, &scan).unwrap();
ctx
fn exported_globals(bytes: &[u8]) -> Vec<String> {
Parser::new(0)
.parse_all(bytes)
.filter_map(Result::ok)
.filter_map(|payload| match payload {
Payload::ExportSection(reader) => Some(reader),
_ => None,
})
.flat_map(|reader| reader.into_iter().filter_map(Result::ok))
.filter(|export| export.kind == ExternalKind::Global)
.map(|export| export.name.to_string())
.collect()
#[test]
fn every_session_instance_gets_an_exported_global_that_validates() {
let (mut symbols, _) = holding(number(1), 1);
let mut ctx = prologue(&mut symbols);
assert!(ctx.instance_global(0).is_ok());
ctx.add_should_apply(CompileContext::default_should_apply(), usize::MAX);
let mut process = wasm_encoder::Function::new([]);
process.instruction(&wasm_encoder::Instruction::End);
ctx.add_process(process);
let bytes = ctx.finish();
wasmparser::validate(&bytes).expect("the instance globals validate");
assert!(exported_globals(&bytes).contains(&instance_export_name(0)));
fn a_serial_no_instance_holds_has_no_global() {
let ctx = prologue(&mut symbols);
assert!(ctx.instance_global(7).is_err());
fn a_carried_field_types_the_home_of_its_layout_field() {
let (mut symbols, id) = holding(number(1), 1);
prologue(&mut symbols);
assert_eq!(symbols.field_type(id, 0), WasmType::I32);
fn a_cell_of_a_carried_instance_is_seeded_by_the_prologue() {
let unit = Reader::parse("(setf p 2)").unwrap().exprs;
let scan = prepare_unit(&mut symbols, &unit, CompileMode::Script).unwrap();
allocate_instances(&mut ctx, &mut emit, &mut symbols, &scan).unwrap();
let cell = symbols.cell_global("P").map(|(_, ty)| ty);
assert_eq!(cell, Some(WasmType::StructRef(id)));
assert_eq!(symbols.cell_binding("P"), CellBinding::Bound);
fn the_serial_counter_starts_past_the_serials_the_session_used() {
let (mut symbols, _) = holding(Expr::Nil, 7);
assert!(ctx.state_digest().contains("struct_serial_base: 7"));
/// A table whose global `PS` holds `count` instances of `(defstruct pt x)`,
/// the `x` of instance `n` being `n`.
fn crowded(count: u32) -> SymbolTable {
let leaves = (0..count)
.map(|serial| Expr::Instance(serial, id))
.collect();
symbols.define(Symbol::new("PS", SymbolKind::Variable).with_value(Expr::List(leaves)));
let found = (0..count)
.map(|serial| {
fields: vec![number(i64::from(serial))],
(serial, Some(instance))
symbols.settle_instances(found, count);
symbols
fn largest_function_body(count: u32) -> u64 {
let mut symbols = crowded(count);
let (bytes, _) = Compiler::new()
.compile_eval_with_type(&Program::new(vec![number(1)]), &mut symbols)
.unwrap();
.parse_all(&bytes)
Payload::CodeSectionEntry(body) => Some(body.range().end - body.range().start),
.max()
.unwrap_or(0)
/// No function body grows with the carried-instance count: one start function
/// holding every instance took 18 s to compile at 8000.
fn no_function_grows_with_the_carried_instance_count() {
let small = largest_function_body(600);
let large = largest_function_body(2_400);
assert!(small > 0);
assert_eq!(small, large, "largest body {small} vs {large} bytes");
/// Compile time is linear in the carried instances: a per-field table clone
/// was quadratic and never finished at 100000. The nextest override in
/// `.config/nextest.toml` terminates this test past 120 s.
fn a_later_unit_compiles_over_a_hundred_thousand_carried_instances() {
let mut symbols = crowded(100_000);
wasmparser::validate(&bytes).expect("the crowded unit validates");
/// More instances than one helper holds still validate, each reaching its
/// serial's global.
fn a_crowd_past_one_helper_validates() {
let mut symbols = crowded(600);
wasmparser::validate(&bytes).expect("the chunked prologue validates");
let exported = exported_globals(&bytes);
assert!(exported.contains(&instance_export_name(0)));
assert!(exported.contains(&instance_export_name(599)));