1
//! A list holding closures, carried on as the source that rebuilds it (plan
2
//! 0014 PERSIST Q4-2): `(LET ((<hidden> <lambda>) …) (LIST <hidden> …))`, each
3
//! closure member read like a closure global, so the next unit makes its own
4
//! closures from the source.
5

            
6
use nomiscript::{Carried, Expr, SymbolTable, WasmType};
7
use wasmtime::{AnyRef, AsContextMut, Rooted, Val};
8

            
9
use crate::checked_ref::CheckedAnyRef;
10

            
11
use super::carry::{opaque, read_closure, read_ref};
12
use super::structure::check_depth;
13
use super::type_handles::{AnyRefKind, TypeHandles, classify_anyref};
14

            
15
type Context<'a> = (&'a SymbolTable, &'a TypeHandles);
16

            
17
/// The source of the list `head`; opaque when a member has no source form.
18
11389
pub(super) fn read_list<S: AsContextMut>(
19
11389
    store: &mut S,
20
11389
    context: Context<'_>,
21
11389
    head: &Rooted<AnyRef>,
22
11389
) -> wasmtime::Result<Carried> {
23
11389
    let mut closures = Vec::new();
24
11389
    let Some(list) = list_source(store, context, head, &mut closures, 0)? else {
25
2
        return Ok(opaque("list"));
26
    };
27
11387
    let bindings = closures
28
11387
        .into_iter()
29
13457
        .map(|(name, lambda)| Expr::List(vec![Expr::Symbol(name), lambda]))
30
        .collect();
31
11387
    Ok(Carried::Constant(Expr::List(vec![
32
11387
        Expr::symbol("LET"),
33
11387
        Expr::List(bindings),
34
11387
        list,
35
11387
    ])))
36
11389
}
37

            
38
12769
fn list_source<S: AsContextMut>(
39
12769
    store: &mut S,
40
12769
    context: Context<'_>,
41
12769
    head: &Rooted<AnyRef>,
42
12769
    closures: &mut Vec<(String, Expr)>,
43
12769
    depth: usize,
44
12769
) -> wasmtime::Result<Option<Expr>> {
45
12769
    check_depth(depth)?;
46
12769
    let mut forms = vec![Expr::symbol("LIST")];
47
12769
    let mut cur = Some(head.checked_struct(&mut *store)?);
48
30481
    while let Some(node) = cur {
49
17714
        let car = node.field(&mut *store, 0)?;
50
17714
        let Some(member) = member_source(store, context, car, closures, depth + 1)? else {
51
2
            return Ok(None);
52
        };
53
17712
        forms.push(member);
54
17712
        cur = match node.field(&mut *store, 1)? {
55
4945
            Val::AnyRef(Some(next)) => Some(next.checked_struct(&mut *store)?),
56
12767
            _ => None,
57
        };
58
    }
59
12767
    Ok(Some(Expr::List(forms)))
60
12769
}
61

            
62
17714
fn member_source<S: AsContextMut>(
63
17714
    store: &mut S,
64
17714
    context: Context<'_>,
65
17714
    car: Val,
66
17714
    closures: &mut Vec<(String, Expr)>,
67
17714
    depth: usize,
68
17714
) -> wasmtime::Result<Option<Expr>> {
69
17714
    let Val::AnyRef(Some(car)) = car else {
70
230
        return Ok(Some(Expr::Nil));
71
    };
72
17484
    match classify_anyref(&mut *store, car, context.1)? {
73
        AnyRefKind::Closure(_) => {
74
13458
            let Some(lambda) = read_closure(store, context, &car)?.into_constant() else {
75
1
                return Ok(None);
76
            };
77
13457
            let name = context.0.hidden_name("CARRY", &closures.len().to_string());
78
13457
            closures.push((name.clone(), lambda));
79
13457
            Ok(Some(Expr::Symbol(name)))
80
        }
81
1380
        AnyRefKind::Pair => list_source(store, context, &car, closures, depth),
82
2646
        _ => Ok(read_ref(store, context, car, WasmType::AnyRef).into_constant()),
83
    }
84
17714
}