1
use std::collections::{HashMap, HashSet};
2

            
3
use super::*;
4
use crate::ast::{ClosureSigId, Number, PairElement};
5
use crate::error::Error;
6

            
7
#[path = "homes_binder_tests.rs"]
8
mod binder_tests;
9
#[path = "homes_field_tests.rs"]
10
mod field_tests;
11

            
12
14
fn table_with_home(name: &str, base: &[Arm], datum: Option<Expr>, ty: WasmType) -> SymbolTable {
13
14
    let mut symbols = SymbolTable::new();
14
14
    symbols.begin_home_attempt(HashMap::new());
15
14
    symbols.note_home(name, base.to_vec(), datum, ty);
16
14
    symbols
17
14
}
18

            
19
11
fn int(n: i64) -> Expr {
20
11
    Expr::Number(Number::Index(n))
21
11
}
22

            
23
#[test]
24
1
fn a_home_that_is_the_join_of_its_stores_is_not_widened() {
25
1
    let symbols = table_with_home(
26
1
        "G",
27
1
        &[Arm::NumberLiteral(WasmType::I32)],
28
1
        None,
29
1
        WasmType::I32,
30
1
    );
31
1
    symbols.note_store(&HomeKey::global("G"), Arm::NumberLiteral(WasmType::I32));
32
1
    assert_eq!(symbols.widened_homes(), None);
33
1
}
34

            
35
#[test]
36
1
fn a_store_wider_than_the_home_widens_it() {
37
1
    let symbols = table_with_home(
38
1
        "G",
39
1
        &[Arm::NumberLiteral(WasmType::I32)],
40
1
        None,
41
1
        WasmType::I32,
42
1
    );
43
1
    symbols.note_store(&HomeKey::global("G"), Arm::Value(WasmType::Ratio));
44
1
    assert_eq!(
45
1
        symbols.widened_homes(),
46
1
        Some(HashMap::from([(
47
1
            HomeKey::Global("G".to_string()),
48
1
            WasmType::Ratio
49
1
        )]))
50
    );
51
1
}
52

            
53
#[test]
54
1
fn a_nil_init_joins_a_list_store_to_the_list() {
55
1
    let symbols = table_with_home("L", &[Arm::Nil], Some(Expr::Nil), WasmType::Bool);
56
1
    symbols.note_store(
57
1
        &HomeKey::global("L"),
58
1
        Arm::Value(WasmType::PairRef(PairElement::I32)),
59
1
    );
60
1
    assert_eq!(
61
1
        symbols.widened_homes(),
62
1
        Some(HashMap::from([(
63
1
            HomeKey::Global("L".to_string()),
64
1
            WasmType::PairRef(PairElement::I32)
65
1
        )]))
66
    );
67
1
}
68

            
69
#[test]
70
1
fn a_store_to_a_home_of_another_unit_records_nothing() {
71
1
    let symbols = table_with_home(
72
1
        "G",
73
1
        &[Arm::NumberLiteral(WasmType::I32)],
74
1
        None,
75
1
        WasmType::I32,
76
1
    );
77
1
    symbols.note_store(&HomeKey::global("OTHER"), Arm::Value(WasmType::Ratio));
78
1
    assert_eq!(symbols.widened_homes(), None);
79
1
}
80

            
81
#[test]
82
1
fn a_closure_store_widens_its_home_to_any_so_no_signature_is_carried() {
83
1
    let sig = ClosureSigId(0);
84
1
    let cases = [
85
1
        (WasmType::Closure(sig), WasmType::AnyRef),
86
1
        (
87
1
            WasmType::PairRef(PairElement::Closure(sig)),
88
1
            WasmType::PairRef(PairElement::AnyRef),
89
1
        ),
90
1
    ];
91
1
    for (stored, widened) in cases {
92
2
        let symbols = table_with_home("F", &[Arm::Nil], None, WasmType::Bool);
93
2
        symbols.note_store(&HomeKey::global("F"), Arm::Value(stored));
94
2
        assert_eq!(
95
2
            symbols.widened_homes(),
96
2
            Some(HashMap::from([(HomeKey::Global("F".to_string()), widened)]))
97
        );
98
    }
99
1
}
100

            
101
#[test]
102
1
fn a_closure_home_is_not_recorded() {
103
1
    let symbols = table_with_home(
104
1
        "F",
105
1
        &[Arm::Value(WasmType::Closure(ClosureSigId(0)))],
106
1
        None,
107
1
        WasmType::Closure(ClosureSigId(0)),
108
1
    );
109
1
    symbols.note_store(&HomeKey::global("F"), Arm::Value(WasmType::Ratio));
110
1
    assert_eq!(symbols.widened_homes(), None);
111
1
}
112

            
113
#[test]
114
1
fn the_hint_is_the_one_the_attempt_began_with() {
115
1
    let mut symbols = SymbolTable::new();
116
1
    symbols.begin_home_attempt(HashMap::from([(
117
1
        HomeKey::Global("G".to_string()),
118
1
        WasmType::Ratio,
119
1
    )]));
120
1
    assert_eq!(symbols.hint(&HomeKey::global("G")), Some(WasmType::Ratio));
121
1
    symbols.end_home_attempt();
122
1
    assert_eq!(symbols.hint(&HomeKey::global("G")), None);
123
1
}
124

            
125
#[test]
126
1
fn a_clone_shares_the_book_of_its_table() {
127
1
    let symbols = table_with_home(
128
1
        "G",
129
1
        &[Arm::NumberLiteral(WasmType::I32)],
130
1
        None,
131
1
        WasmType::I32,
132
1
    );
133
1
    symbols
134
1
        .clone()
135
1
        .note_store(&HomeKey::global("G"), Arm::Value(WasmType::Ratio));
136
1
    assert!(symbols.widened_homes().is_some());
137
1
}
138

            
139
#[test]
140
1
fn a_rolled_back_trial_forgets_its_stores() {
141
1
    let mut symbols = table_with_home(
142
1
        "G",
143
1
        &[Arm::NumberLiteral(WasmType::I32)],
144
1
        None,
145
1
        WasmType::I32,
146
1
    );
147
1
    let journal = symbols.journal();
148
1
    symbols.note_store(&HomeKey::global("G"), Arm::Value(WasmType::Ratio));
149
1
    symbols.restore_journal(journal);
150
1
    assert_eq!(symbols.widened_homes(), None);
151
1
}
152

            
153
#[test]
154
1
fn a_probe_at_init_runs_no_trial_for_any_other_stored_cell() {
155
1
    let mut symbols = table_with_home("G", &[Arm::Nil], Some(int(1)), WasmType::I32);
156
1
    symbols.note_home("H", vec![Arm::Nil], Some(int(2)), WasmType::I32);
157
1
    for name in ["G", "H"] {
158
2
        symbols.define(crate::runtime::Symbol::new(
159
2
            name,
160
2
            crate::runtime::SymbolKind::Variable,
161
2
        ));
162
    }
163
1
    symbols.set_stored_globals(HashSet::from(["G".to_string(), "H".to_string()]));
164
1
    let probe = symbols
165
1
        .probe_at_init(&HomeKey::global("G"), "G")
166
1
        .expect("probe");
167
1
    assert!(probe.probe_at_init(&HomeKey::global("H"), "H").is_none());
168
1
}
169

            
170
#[test]
171
1
fn a_probe_at_init_reads_the_constant_the_home_started_with() {
172
1
    let mut symbols = table_with_home(
173
1
        "G",
174
1
        &[Arm::NumberLiteral(WasmType::I32)],
175
1
        Some(int(1)),
176
1
        WasmType::I32,
177
1
    );
178
1
    symbols.define(crate::runtime::Symbol::new(
179
1
        "G",
180
1
        crate::runtime::SymbolKind::Variable,
181
1
    ));
182
1
    let probe = symbols
183
1
        .probe_at_init(&HomeKey::global("G"), "G")
184
1
        .expect("probe");
185
1
    assert_eq!(probe.lookup("G").and_then(|s| s.value()), Some(&int(1)));
186
1
    assert!(probe.probe_at_init(&HomeKey::global("G"), "G").is_none());
187
1
    assert!(
188
1
        table_with_home("H", &[Arm::Nil], None, WasmType::Bool)
189
1
            .probe_at_init(&HomeKey::global("H"), "H")
190
1
            .is_none()
191
    );
192
1
}
193

            
194
3
fn table_with_global_g() -> SymbolTable {
195
3
    let mut symbols = SymbolTable::new();
196
3
    symbols.begin_home_attempt(HashMap::new());
197
3
    symbols.define(
198
3
        crate::runtime::Symbol::new("G", crate::runtime::SymbolKind::Variable)
199
3
            .with_value(Expr::WasmGlobal(0, WasmType::I32)),
200
3
    );
201
3
    symbols.define(
202
3
        crate::runtime::Symbol::new("K", crate::runtime::SymbolKind::Variable).with_value(int(1)),
203
3
    );
204
3
    symbols
205
3
}
206

            
207
#[test]
208
1
fn expiring_the_homes_drops_only_the_wasm_globals() {
209
1
    let mut symbols = table_with_global_g();
210
1
    symbols.expire_homes();
211
1
    assert!(symbols.lookup("G").is_some_and(|s| s.value().is_none()));
212
1
    assert_eq!(symbols.lookup("K").and_then(|s| s.value()), Some(&int(1)));
213
1
}
214

            
215
#[test]
216
1
fn a_read_of_an_expired_global_says_its_value_did_not_survive() {
217
1
    let mut symbols = table_with_global_g();
218
1
    symbols.expire_homes();
219
1
    for err in [
220
1
        Error::UndefinedSymbol("G".to_string()),
221
1
        Error::Compile("symbol 'G' has no value".to_string()),
222
1
    ] {
223
2
        assert_eq!(
224
2
            symbols.explain_expired(err).to_string(),
225
2
            "Compile error: G: its runtime value does not survive the form"
226
        );
227
    }
228
1
    for tail in ["", " for stack value"] {
229
2
        assert_eq!(
230
2
            symbols.not_callable("G", tail).to_string(),
231
2
            "Compile error: G: its runtime value does not survive the form"
232
        );
233
    }
234
1
    assert_eq!(
235
1
        symbols.not_callable("K", " for stack value").to_string(),
236
1
        "Compile error: symbol 'K' is not callable for stack value"
237
    );
238
1
    let other = Error::Compile("symbol 'K' has no value".to_string());
239
1
    assert_eq!(
240
1
        symbols.explain_expired(other).to_string(),
241
1
        "Compile error: symbol 'K' has no value"
242
    );
243
1
}
244

            
245
#[test]
246
1
fn a_call_of_a_closure_variable_says_undefined_function() {
247
1
    let mut symbols = SymbolTable::new();
248
1
    symbols.define(
249
1
        crate::runtime::Symbol::new("F", crate::runtime::SymbolKind::Variable).with_value(
250
1
            Expr::WasmLocal(0, WasmType::Closure(crate::ast::ClosureSigId(0))),
251
1
        ),
252
1
    );
253
1
    assert_eq!(
254
1
        symbols.not_callable("F", "").to_string(),
255
1
        "Compile error: undefined function F; use (funcall f …)"
256
    );
257
1
}
258

            
259
4
fn table_with_provisional_g() -> SymbolTable {
260
4
    let mut symbols = SymbolTable::new();
261
4
    symbols.begin_home_attempt(HashMap::new());
262
4
    symbols.note_provisional_home("G");
263
4
    symbols
264
4
}
265

            
266
#[test]
267
1
fn a_provisional_home_no_store_typed_stays_as_it_is() {
268
1
    let symbols = table_with_provisional_g();
269
1
    assert_eq!(symbols.widened_homes(), None);
270
1
    symbols.note_store(&HomeKey::global("G"), Arm::Value(WasmType::AnyRef));
271
1
    assert_eq!(symbols.widened_homes(), None);
272
1
}
273

            
274
#[test]
275
1
fn a_provisional_home_widens_to_the_join_of_its_stores() {
276
1
    let symbols = table_with_provisional_g();
277
1
    symbols.note_store(&HomeKey::global("G"), Arm::NumberLiteral(WasmType::I32));
278
1
    symbols.note_store(&HomeKey::global("G"), Arm::Value(WasmType::Ratio));
279
1
    assert_eq!(
280
1
        symbols.widened_homes(),
281
1
        Some(HashMap::from([(
282
1
            HomeKey::Global("G".to_string()),
283
1
            WasmType::Ratio
284
1
        )]))
285
    );
286
1
}
287

            
288
#[test]
289
1
fn a_hint_the_attempt_began_with_outlives_a_home_without_stores() {
290
1
    let mut symbols = SymbolTable::new();
291
1
    symbols.begin_home_attempt(HashMap::from([(
292
1
        HomeKey::Global("H".to_string()),
293
1
        WasmType::Ratio,
294
1
    )]));
295
1
    symbols.note_home(
296
1
        "G",
297
1
        vec![Arm::NumberLiteral(WasmType::I32)],
298
1
        None,
299
1
        WasmType::I32,
300
1
    );
301
1
    symbols.note_store(&HomeKey::global("G"), Arm::Value(WasmType::Ratio));
302
1
    assert_eq!(
303
1
        symbols.widened_homes(),
304
1
        Some(HashMap::from([
305
1
            (HomeKey::Global("G".to_string()), WasmType::Ratio),
306
1
            (HomeKey::Global("H".to_string()), WasmType::Ratio)
307
1
        ]))
308
    );
309
1
}
310

            
311
#[test]
312
1
fn a_home_that_holds_its_stores_leaves_no_hint() {
313
1
    let symbols = table_with_home("G", &[Arm::Nil], Some(Expr::Nil), WasmType::Bool);
314
1
    symbols.note_store(&HomeKey::global("G"), Arm::Nil);
315
1
    assert_eq!(symbols.widened_homes(), None);
316
1
}
317

            
318
#[test]
319
1
fn a_closure_in_a_provisional_home_asks_for_a_cell_of_its_shape() {
320
1
    let symbols = table_with_provisional_g();
321
1
    let shape = ClosureShape::of(&[WasmType::Ratio], WasmType::I32).expect("flat");
322
1
    symbols.note_closure_shape("G", shape.clone());
323
1
    symbols.note_closure_shape("G", ClosureShape::of(&[], WasmType::Ratio).expect("flat"));
324
1
    assert_eq!(symbols.widened_homes(), Some(HashMap::new()));
325
1
    assert_eq!(
326
1
        symbols.closure_shapes(),
327
1
        HashMap::from([("G".to_string(), shape.clone())])
328
    );
329
1
    let mut next = SymbolTable::new();
330
1
    next.begin_home_attempt(HashMap::new());
331
1
    next.hint_closures(symbols.closure_shapes());
332
1
    assert_eq!(next.closure_hint("G"), Some(shape));
333
1
    assert_eq!(next.closure_hint("H"), None);
334
1
}
335

            
336
#[test]
337
1
fn a_closure_shape_naming_another_closure_is_not_carried() {
338
1
    let nested = [WasmType::Closure(ClosureSigId(0))];
339
1
    assert_eq!(ClosureShape::of(&nested, WasmType::I32), None);
340
1
    assert_eq!(ClosureShape::of(&[], nested[0]), None);
341
1
    assert!(ClosureShape::of(&[WasmType::I32], WasmType::I32).is_some());
342
1
}
343

            
344
#[test]
345
1
fn a_closure_is_noted_only_in_a_provisional_home() {
346
1
    let symbols = table_with_home("G", &[Arm::Nil], None, WasmType::Bool);
347
1
    symbols.note_closure_shape("G", ClosureShape::of(&[], WasmType::I32).expect("flat"));
348
1
    assert_eq!(symbols.closure_shapes(), HashMap::new());
349
1
}
350

            
351
#[test]
352
1
fn the_first_constant_stored_is_the_init_a_store_is_typed_against() {
353
1
    let symbols = table_with_provisional_g();
354
1
    symbols.note_home_datum("G", &int(1));
355
1
    symbols.note_home_datum("G", &int(2));
356
1
    let mut table = symbols.clone();
357
1
    table.define(crate::runtime::Symbol::new(
358
1
        "G",
359
1
        crate::runtime::SymbolKind::Variable,
360
1
    ));
361
1
    let probe = table
362
1
        .probe_at_init(&HomeKey::global("G"), "G")
363
1
        .expect("probe");
364
1
    assert_eq!(probe.lookup("G").and_then(|s| s.value()), Some(&int(1)));
365
1
}
366

            
367
#[test]
368
1
fn a_cell_is_registered_with_its_flag_and_whether_it_started_bound() {
369
1
    let mut symbols = SymbolTable::new();
370
1
    symbols.begin_home_attempt(HashMap::new());
371
1
    symbols.note_cell("NEW", 3, Seed::Unbound);
372
1
    symbols.note_cell("OLD", 5, Seed::Constant);
373
1
    symbols.note_cell("LOST", 6, Seed::Lost);
374
1
    assert!(symbols.is_cell("NEW") && symbols.is_cell("OLD") && !symbols.is_cell("X"));
375
1
    assert_eq!(symbols.cell_flag("NEW"), Some(3));
376
1
    assert_eq!(symbols.cell_flag("X"), None);
377
1
    assert_eq!(symbols.cell_binding("NEW"), CellBinding::Unbound);
378
1
    assert_eq!(symbols.cell_binding("OLD"), CellBinding::Bound);
379
1
    assert_eq!(symbols.cell_binding("LOST"), CellBinding::Bound);
380
1
    assert!(symbols.is_lost_cell("LOST") && !symbols.is_lost_cell("OLD"));
381
1
}
382

            
383
#[test]
384
1
fn a_cell_global_is_a_registered_cell_whose_value_is_a_global() {
385
1
    let symbols = table_with_global_g();
386
1
    assert_eq!(symbols.cell_global("G"), None);
387
1
    symbols.note_cell("G", 1, Seed::Unbound);
388
1
    symbols.note_cell("K", 2, Seed::Unbound);
389
1
    assert_eq!(symbols.cell_global("G"), Some((0, WasmType::I32)));
390
1
    assert_eq!(symbols.cell_global("K"), None);
391
1
}
392

            
393
#[test]
394
1
fn a_sure_definition_of_a_cell_outranks_a_skippable_one() {
395
1
    let mut symbols = SymbolTable::new();
396
1
    symbols.begin_home_attempt(HashMap::new());
397
1
    symbols.note_cell("G", 0, Seed::Unbound);
398
1
    assert_eq!(symbols.cell_binding("G"), CellBinding::Unbound);
399
1
    symbols.note_cell_definition("G", false);
400
1
    assert_eq!(symbols.cell_binding("G"), CellBinding::Maybe);
401
1
    symbols.note_cell_definition("G", true);
402
1
    symbols.note_cell_definition("G", false);
403
1
    assert_eq!(symbols.cell_binding("G"), CellBinding::Bound);
404
1
}
405

            
406
#[test]
407
1
fn a_seeded_cell_is_bound() {
408
1
    let mut symbols = SymbolTable::new();
409
1
    symbols.begin_home_attempt(HashMap::new());
410
1
    symbols.note_cell("G", 0, Seed::Constant);
411
1
    assert_eq!(symbols.cell_binding("G"), CellBinding::Bound);
412
1
    assert_eq!(symbols.cell_binding("H"), CellBinding::Unbound);
413
1
}
414

            
415
#[test]
416
1
fn a_cell_asked_for_widens_the_attempt_with_its_type() {
417
1
    let mut symbols = SymbolTable::new();
418
1
    symbols.begin_home_attempt(HashMap::new());
419
1
    symbols.ask_cell("Z", Asked::Typed(WasmType::I32));
420
1
    symbols.ask_cell("W", Asked::Untyped);
421
1
    let shape = ClosureShape::of(&[WasmType::I32], WasmType::I32).expect("flat");
422
1
    symbols.ask_cell("F", Asked::Closure(shape.clone()));
423
1
    assert_eq!(
424
1
        symbols.closure_shapes(),
425
1
        HashMap::from([("F".to_string(), shape)])
426
    );
427
1
    assert_eq!(
428
1
        symbols.widened_homes(),
429
1
        Some(HashMap::from([(
430
1
            HomeKey::Global("Z".to_string()),
431
1
            WasmType::I32
432
1
        )]))
433
    );
434
1
    let forced = symbols.forced_cells();
435
1
    assert_eq!(
436
1
        forced,
437
1
        BTreeSet::from(["F".to_string(), "W".to_string(), "Z".to_string()])
438
    );
439
1
    symbols.begin_home_attempt(HashMap::new());
440
1
    symbols.force_cells(forced.clone());
441
1
    assert_eq!(symbols.forced_cells(), forced);
442
1
    assert_eq!(symbols.widened_homes(), None);
443
1
}
444

            
445
#[test]
446
1
fn a_refused_closure_seed_widens_the_attempt_and_the_next_one_starts_without_it() {
447
1
    let mut symbols = SymbolTable::new();
448
1
    symbols.begin_home_attempt(HashMap::new());
449
1
    symbols.note_cell("K", 0, Seed::Closure);
450
1
    symbols.note_cell("N", 1, Seed::Constant);
451
1
    symbols.refuse_seed("N");
452
1
    assert_eq!(symbols.widened_homes(), None);
453
1
    symbols.refuse_seed("K");
454
1
    assert!(symbols.widened_homes().is_some());
455
1
    let refused = symbols.refused_seeds();
456
1
    assert_eq!(refused, BTreeSet::from(["K".to_string()]));
457
1
    symbols.begin_home_attempt(HashMap::new());
458
1
    symbols.unseed_cells(refused);
459
1
    assert!(symbols.is_seed_refused("K"));
460
1
    assert!(!symbols.is_seed_refused("N"));
461
1
    assert_eq!(symbols.widened_homes(), None);
462
1
}