1
//! Closure registry behavioural tests.
2
//!
3
//! The registry is the foundation of Tier 1.5 lambdas-as-real-wasm-fns:
4
//! `intern_closure_signature` dedupes per `(arg-types, ret-type)` so two
5
//! distinct lambdas with the same shape share `$fn_<sig>` and
6
//! `$closure_<sig>`. These tests lock the dedup invariant in.
7

            
8
use super::super::CompileContext;
9
use super::HelperSlot;
10
use crate::ast::{ClosureSigId, WasmType};
11
use wasm_encoder::{HeapType, ValType};
12

            
13
#[test]
14
1
fn intern_signature_deduplicates_by_shape() {
15
1
    let mut ctx = CompileContext::new_with_host_fns(&[]).unwrap();
16
1
    let sig_a = ctx
17
1
        .intern_closure_signature(&[WasmType::Ratio, WasmType::Ratio], WasmType::Ratio)
18
1
        .unwrap();
19
1
    let sig_b = ctx
20
1
        .intern_closure_signature(&[WasmType::Ratio, WasmType::Ratio], WasmType::Ratio)
21
1
        .unwrap();
22
1
    assert_eq!(sig_a, sig_b, "same signature must collapse to one id");
23
1
}
24

            
25
#[test]
26
1
fn intern_signature_distinguishes_arg_types() {
27
1
    let mut ctx = CompileContext::new_with_host_fns(&[]).unwrap();
28
1
    let r = ctx
29
1
        .intern_closure_signature(&[WasmType::Ratio], WasmType::Ratio)
30
1
        .unwrap();
31
1
    let i = ctx
32
1
        .intern_closure_signature(&[WasmType::I32], WasmType::Ratio)
33
1
        .unwrap();
34
1
    assert_ne!(r, i, "different param types must produce distinct ids");
35
1
}
36

            
37
#[test]
38
1
fn intern_signature_distinguishes_return_types() {
39
1
    let mut ctx = CompileContext::new_with_host_fns(&[]).unwrap();
40
1
    let to_ratio = ctx
41
1
        .intern_closure_signature(&[WasmType::Ratio], WasmType::Ratio)
42
1
        .unwrap();
43
1
    let to_i32 = ctx
44
1
        .intern_closure_signature(&[WasmType::Ratio], WasmType::I32)
45
1
        .unwrap();
46
1
    assert_ne!(to_ratio, to_i32, "different return types are distinct sigs");
47
1
}
48

            
49
#[test]
50
1
fn closure_ref_resolves_to_concrete_struct() {
51
1
    let mut ctx = CompileContext::new_with_host_fns(&[]).unwrap();
52
1
    let sig = ctx
53
1
        .intern_closure_signature(&[WasmType::Ratio], WasmType::Ratio)
54
1
        .unwrap();
55
1
    let val = ctx.closure_ref(sig).unwrap();
56
1
    match val {
57
        ValType::Ref(r) => {
58
1
            assert!(r.nullable, "closure_ref must be nullable");
59
1
            assert!(matches!(r.heap_type, HeapType::Concrete(_)));
60
        }
61
        _ => panic!("closure_ref must produce a ref ValType"),
62
    }
63
1
}
64

            
65
#[test]
66
1
fn intern_then_closure_ref_via_wasm_val_type() {
67
1
    let mut ctx = CompileContext::new_with_host_fns(&[]).unwrap();
68
1
    let sig = ctx
69
1
        .intern_closure_signature(&[WasmType::Ratio], WasmType::I32)
70
1
        .unwrap();
71
1
    let direct = ctx.closure_ref(sig).unwrap();
72
1
    let via_dispatch = ctx.wasm_val_type(WasmType::Closure(sig)).unwrap();
73
1
    assert_eq!(direct, via_dispatch);
74
1
}
75

            
76
4
fn encoded(helper: &wasm_encoder::Function) -> Vec<u8> {
77
4
    let mut bytes = Vec::new();
78
4
    wasm_encoder::Encode::encode(helper, &mut bytes);
79
4
    bytes
80
4
}
81

            
82
5
fn body(value: i32) -> wasm_encoder::Function {
83
5
    let mut helper = wasm_encoder::Function::new([]);
84
5
    helper.instruction(&wasm_encoder::Instruction::I32Const(value));
85
5
    helper.instruction(&wasm_encoder::Instruction::End);
86
5
    helper
87
5
}
88

            
89
#[test]
90
1
fn a_filled_helper_keeps_the_slot_reserved_before_a_later_queue() {
91
1
    let mut ctx = CompileContext::new_with_host_fns(&[]).unwrap();
92
1
    let slot = ctx.reserve_helper();
93
1
    let first = slot.0;
94
1
    ctx.queue_helper(body(2));
95
1
    ctx.fill_helper(slot, body(1)).unwrap();
96
1
    let queued: Vec<Vec<u8>> = ctx.pending_helpers[first..].iter().map(encoded).collect();
97
1
    assert_eq!(queued, vec![encoded(&body(1)), encoded(&body(2))]);
98
1
}
99

            
100
#[test]
101
1
fn filling_an_unreserved_slot_is_an_error() {
102
1
    let mut ctx = CompileContext::new_with_host_fns(&[]).unwrap();
103
1
    let slot = HelperSlot(ctx.pending_helper_count());
104
1
    let err = ctx.fill_helper(slot, body(1)).unwrap_err();
105
1
    assert!(err.to_string().contains("no reserved helper slot"));
106
1
}
107

            
108
/// A helper compiled inside an inlined body that hides labels starts with no
109
/// hidden labels: its own first BLOCK must resolve, and the hiding comes back
110
/// when the pool does.
111
#[test]
112
1
fn a_helper_pool_starts_with_no_hidden_labels() {
113
1
    let mut ctx = CompileContext::new_with_host_fns(&[]).unwrap();
114
1
    ctx.push_block_label("outer", 1, None);
115
1
    let seen = ctx
116
1
        .with_hidden_labels(0, |ctx| {
117
1
            let snapshot = ctx.take_local_pool(0);
118
1
            ctx.push_block_label("inner", 1, None);
119
1
            let inside = ctx.lookup_block_label("inner").is_some();
120
1
            ctx.pop_block_label("inner")?;
121
1
            ctx.restore_local_pool(snapshot);
122
1
            Ok((inside, ctx.lookup_block_label("outer").is_some()))
123
1
        })
124
        .unwrap();
125
1
    assert_eq!(seen, (true, false));
126
1
    assert!(ctx.lookup_block_label("outer").is_some());
127
1
}
128

            
129
#[test]
130
1
fn an_unknown_closure_signature_is_an_error_not_a_panic() {
131
1
    let ctx = CompileContext::new_with_host_fns(&[]).unwrap();
132
1
    let unknown = ClosureSigId(0);
133
1
    assert!(ctx.closure_sig(unknown).is_err());
134
1
    assert!(ctx.closure_ref(unknown).is_err());
135
1
    assert!(ctx.wasm_val_type(WasmType::Closure(unknown)).is_err());
136
1
    assert!(
137
1
        ctx.shape_compare(WasmType::Closure(unknown), false)
138
1
            .is_err()
139
    );
140
1
}
141

            
142
#[test]
143
1
fn an_any_call_helper_is_reserved_once_per_shape() {
144
1
    let mut ctx = CompileContext::new_with_host_fns(&[]).unwrap();
145
1
    let shape = [WasmType::I32, WasmType::Ratio];
146
1
    let first = ctx.any_call_helper(&shape).unwrap();
147
1
    assert_eq!(ctx.any_call_helper(&shape).unwrap(), first);
148
1
    let other = ctx.any_call_helper(&[WasmType::I32]).unwrap();
149
1
    assert_ne!(other, first);
150
1
    assert_eq!(ctx.take_any_call_helpers().len(), 2);
151
1
}
152

            
153
#[test]
154
1
fn a_rolled_back_attempt_forgets_its_any_call_helper() {
155
1
    let mut ctx = CompileContext::new_with_host_fns(&[]).unwrap();
156
1
    let snapshot = ctx.snapshot();
157
1
    let pending = ctx.pending_helper_count();
158
1
    ctx.any_call_helper(&[WasmType::I32]).unwrap();
159
1
    ctx.restore(snapshot);
160
1
    assert!(ctx.take_any_call_helpers().is_empty());
161
1
    assert_eq!(ctx.pending_helper_count(), pending);
162
1
}
163

            
164
#[test]
165
1
fn closure_sigs_of_arity_lists_only_that_arity() {
166
1
    let mut ctx = CompileContext::new_with_host_fns(&[]).unwrap();
167
1
    let nullary = ctx.intern_closure_signature(&[], WasmType::I32).unwrap();
168
1
    ctx.intern_closure_signature(&[WasmType::Ratio], WasmType::Ratio)
169
1
        .unwrap();
170
1
    let found = ctx.closure_sigs_of_arity(0);
171
1
    assert_eq!(found.len(), 1);
172
1
    assert_eq!(
173
1
        found[0].closure_type_idx,
174
1
        ctx.closure_sig(nullary).unwrap().closure_type_idx
175
    );
176
1
}