Lines
99.23 %
Functions
100 %
Branches
//! Closure registry behavioural tests.
//!
//! The registry is the foundation of Tier 1.5 lambdas-as-real-wasm-fns:
//! `intern_closure_signature` dedupes per `(arg-types, ret-type)` so two
//! distinct lambdas with the same shape share `$fn_<sig>` and
//! `$closure_<sig>`. These tests lock the dedup invariant in.
use super::super::CompileContext;
use super::HelperSlot;
use crate::ast::{ClosureSigId, WasmType};
use wasm_encoder::{HeapType, ValType};
#[test]
fn intern_signature_deduplicates_by_shape() {
let mut ctx = CompileContext::new_with_host_fns(&[]).unwrap();
let sig_a = ctx
.intern_closure_signature(&[WasmType::Ratio, WasmType::Ratio], WasmType::Ratio)
.unwrap();
let sig_b = ctx
assert_eq!(sig_a, sig_b, "same signature must collapse to one id");
}
fn intern_signature_distinguishes_arg_types() {
let r = ctx
.intern_closure_signature(&[WasmType::Ratio], WasmType::Ratio)
let i = ctx
.intern_closure_signature(&[WasmType::I32], WasmType::Ratio)
assert_ne!(r, i, "different param types must produce distinct ids");
fn intern_signature_distinguishes_return_types() {
let to_ratio = ctx
let to_i32 = ctx
.intern_closure_signature(&[WasmType::Ratio], WasmType::I32)
assert_ne!(to_ratio, to_i32, "different return types are distinct sigs");
fn closure_ref_resolves_to_concrete_struct() {
let sig = ctx
let val = ctx.closure_ref(sig).unwrap();
match val {
ValType::Ref(r) => {
assert!(r.nullable, "closure_ref must be nullable");
assert!(matches!(r.heap_type, HeapType::Concrete(_)));
_ => panic!("closure_ref must produce a ref ValType"),
fn intern_then_closure_ref_via_wasm_val_type() {
let direct = ctx.closure_ref(sig).unwrap();
let via_dispatch = ctx.wasm_val_type(WasmType::Closure(sig)).unwrap();
assert_eq!(direct, via_dispatch);
fn encoded(helper: &wasm_encoder::Function) -> Vec<u8> {
let mut bytes = Vec::new();
wasm_encoder::Encode::encode(helper, &mut bytes);
bytes
fn body(value: i32) -> wasm_encoder::Function {
let mut helper = wasm_encoder::Function::new([]);
helper.instruction(&wasm_encoder::Instruction::I32Const(value));
helper.instruction(&wasm_encoder::Instruction::End);
helper
fn a_filled_helper_keeps_the_slot_reserved_before_a_later_queue() {
let slot = ctx.reserve_helper();
let first = slot.0;
ctx.queue_helper(body(2));
ctx.fill_helper(slot, body(1)).unwrap();
let queued: Vec<Vec<u8>> = ctx.pending_helpers[first..].iter().map(encoded).collect();
assert_eq!(queued, vec![encoded(&body(1)), encoded(&body(2))]);
fn filling_an_unreserved_slot_is_an_error() {
let slot = HelperSlot(ctx.pending_helper_count());
let err = ctx.fill_helper(slot, body(1)).unwrap_err();
assert!(err.to_string().contains("no reserved helper slot"));
/// A helper compiled inside an inlined body that hides labels starts with no
/// hidden labels: its own first BLOCK must resolve, and the hiding comes back
/// when the pool does.
fn a_helper_pool_starts_with_no_hidden_labels() {
ctx.push_block_label("outer", 1, None);
let seen = ctx
.with_hidden_labels(0, |ctx| {
let snapshot = ctx.take_local_pool(0);
ctx.push_block_label("inner", 1, None);
let inside = ctx.lookup_block_label("inner").is_some();
ctx.pop_block_label("inner")?;
ctx.restore_local_pool(snapshot);
Ok((inside, ctx.lookup_block_label("outer").is_some()))
})
assert_eq!(seen, (true, false));
assert!(ctx.lookup_block_label("outer").is_some());
fn an_unknown_closure_signature_is_an_error_not_a_panic() {
let ctx = CompileContext::new_with_host_fns(&[]).unwrap();
let unknown = ClosureSigId(0);
assert!(ctx.closure_sig(unknown).is_err());
assert!(ctx.closure_ref(unknown).is_err());
assert!(ctx.wasm_val_type(WasmType::Closure(unknown)).is_err());
assert!(
ctx.shape_compare(WasmType::Closure(unknown), false)
.is_err()
);
fn an_any_call_helper_is_reserved_once_per_shape() {
let shape = [WasmType::I32, WasmType::Ratio];
let first = ctx.any_call_helper(&shape).unwrap();
assert_eq!(ctx.any_call_helper(&shape).unwrap(), first);
let other = ctx.any_call_helper(&[WasmType::I32]).unwrap();
assert_ne!(other, first);
assert_eq!(ctx.take_any_call_helpers().len(), 2);
fn a_rolled_back_attempt_forgets_its_any_call_helper() {
let snapshot = ctx.snapshot();
let pending = ctx.pending_helper_count();
ctx.any_call_helper(&[WasmType::I32]).unwrap();
ctx.restore(snapshot);
assert!(ctx.take_any_call_helpers().is_empty());
assert_eq!(ctx.pending_helper_count(), pending);
fn closure_sigs_of_arity_lists_only_that_arity() {
let nullary = ctx.intern_closure_signature(&[], WasmType::I32).unwrap();
ctx.intern_closure_signature(&[WasmType::Ratio], WasmType::Ratio)
let found = ctx.closure_sigs_of_arity(0);
assert_eq!(found.len(), 1);
assert_eq!(
found[0].closure_type_idx,
ctx.closure_sig(nullary).unwrap().closure_type_idx