1
use super::tests::{failure, num, run, script, value};
2
use super::{NEW, REF, SET};
3
use crate::ast::{Expr, Number, WasmType};
4
use crate::compiler::context::CompileContext;
5
use crate::compiler::emit::FunctionEmitter;
6
use crate::compiler::expr::{compile_for_stack, probe_value};
7
use crate::runtime::SymbolTable;
8

            
9
/// The static type the Eval-mode result names, `Some(<type>) [...]`.
10
22
fn eval_type(source: &str) -> String {
11
22
    let shown = value(run(source));
12
22
    let inner = shown.strip_prefix("Some(").unwrap_or(&shown);
13
22
    inner.split(") [").next().unwrap_or(inner).to_string()
14
22
}
15

            
16
4
fn compile_error(source: &str) -> String {
17
4
    failure(run(source))
18
4
}
19

            
20
#[test]
21
1
fn an_index_stored_field_reads_as_an_index() {
22
1
    let source = "(+ (s-ref (s-new {pt} 1) {pt} 0) 1)";
23
1
    assert_eq!(eval_type(source), "I32");
24
1
    assert_eq!(script(source), num(2));
25
1
}
26

            
27
#[test]
28
1
fn every_store_kind_types_the_read() {
29
1
    for (stored, ty, shown) in [
30
1
        (
31
1
            "1/2",
32
1
            "Ratio",
33
1
            "Number(Scalar(Ratio { numer: 1, denom: 2 }))",
34
1
        ),
35
1
        ("\"a\"", "StringRef", "String(\"a\")"),
36
1
        ("t", "Bool", "T"),
37
1
        ("nil", "Bool", "Nil"),
38
1
    ] {
39
4
        let source = format!("(let ((p (s-new {{pt}} {stored}))) (s-ref p {{pt}} 0))");
40
4
        assert_eq!(eval_type(&source), ty, "{stored}");
41
4
        assert_eq!(script(&source), shown, "{stored}");
42
    }
43
1
}
44

            
45
#[test]
46
1
fn a_string_store_widens_the_field_and_the_read_needs_the() {
47
1
    let widened = "(let ((p (s-new {pt} 1))) (s-set p {pt} 0 \"a\") ";
48
1
    let sum = format!("{widened}(+ (s-ref p {{pt}} 0) 1))");
49
1
    assert!(compile_error(&sum).contains("+ expects numeric arguments, got an any value"));
50
1
    let narrowed = format!("{widened}(the string (s-ref p {{pt}} 0)))");
51
1
    assert_eq!(script(&narrowed), "String(\"a\")");
52
1
    let wrong = format!("{widened}(+ (the index (s-ref p {{pt}} 0)) 1))");
53
1
    assert!(failure(run(&wrong)).contains("TYPE-ERROR: not an index"));
54
1
}
55

            
56
#[test]
57
1
fn a_nil_store_beside_an_index_widens_like_a_global() {
58
1
    let global = "(defvar *g* nil) (setf *g* 1) (+ *g* 1)";
59
1
    let field = "(let ((p (s-new {pt} nil))) (s-set p {pt} 0 1) (+ (s-ref p {pt} 0) 1))";
60
1
    assert!(compile_error(global).contains("got an any value"));
61
1
    assert!(compile_error(field).contains("got an any value"));
62
1
    let read = "(let ((p (s-new {pt} nil))) (s-set p {pt} 0 1) (s-ref p {pt} 0))";
63
1
    assert_eq!(eval_type(read), "AnyRef");
64
1
}
65

            
66
#[test]
67
1
fn an_index_and_a_scalar_store_join_to_any() {
68
1
    let read = "(let ((p (s-new {pt} 1))) (s-set p {pt} 0 1/2) (s-ref p {pt} 0))";
69
1
    assert_eq!(eval_type(read), "AnyRef");
70
1
    assert_eq!(script(read), "Number(Scalar(Ratio { numer: 1, denom: 2 }))");
71
1
    let sum = "(let ((p (s-new {pt} 1))) (s-set p {pt} 0 1/2) (+ (s-ref p {pt} 0) 1))";
72
1
    assert!(compile_error(sum).contains("got an any value"));
73
1
}
74

            
75
#[test]
76
1
fn a_read_compiled_before_any_store_settles_on_the_next_attempt() {
77
1
    for source in [
78
1
        "(defun rd (q) (s-ref q {pt} 0)) (+ (rd (s-new {pt} 1)) 1)",
79
1
        "(defun rd (q) (s-ref q {pt} 0)) (let ((v (rd (s-new {pt} 1)))) (+ v 1))",
80
1
    ] {
81
2
        assert_eq!(eval_type(source), "I32", "{source}");
82
2
        assert_eq!(script(source), num(2), "{source}");
83
    }
84
1
}
85

            
86
#[test]
87
1
fn a_store_not_compiled_leaves_the_field_narrow() {
88
1
    let source = "(let ((p (s-new {pt} 1))) (when nil (s-set p {pt} 0 \"a\")) (s-ref p {pt} 0))";
89
1
    assert_eq!(eval_type(source), "I32");
90
1
}
91

            
92
#[test]
93
1
fn a_store_inside_a_loop_a_closure_or_a_handler_types_the_field() {
94
1
    for (source, expected) in [
95
1
        (
96
1
            "(let ((p (s-new {pt} 1))) (dolist (i '(1 2 3)) \
97
1
             (s-set p {pt} 0 (+ (s-ref p {pt} 0) i))) (s-ref p {pt} 0))",
98
1
            7,
99
1
        ),
100
1
        (
101
1
            "(let ((p (s-new {pt} 1))) (let ((f (lambda () \
102
1
             (s-set p {pt} 0 (+ (s-ref p {pt} 0) 1))))) (funcall f) (funcall f) \
103
1
             (+ (s-ref p {pt} 0) 0)))",
104
1
            3,
105
1
        ),
106
1
        (
107
1
            "(let ((p (s-new {pt} 1))) (handler-case (s-set p {pt} 0 (error 'foo \"x\")) \
108
1
             (foo (e) (s-set p {pt} 0 9))) (+ (s-ref p {pt} 0) 1))",
109
1
            10,
110
1
        ),
111
1
    ] {
112
3
        assert_eq!(eval_type(source), "I32", "{source}");
113
3
        assert_eq!(script(source), num(expected), "{source}");
114
    }
115
1
}
116

            
117
#[test]
118
1
fn a_widened_field_read_widens_the_global_it_is_stored_into() {
119
1
    let narrow = "(defvar *g* 1) (let ((p (s-new {pt} 5))) (setf *g* (s-ref p {pt} 0)) \
120
1
                  (s-set p {pt} 0 \"x\") *g*)";
121
1
    assert_eq!(script(narrow), num(5));
122
1
    let init = "(let ((p (s-new {pt} nil))) (s-set p {pt} 0 3) \
123
1
                (defvar *c* (s-ref p {pt} 0)) *c*)";
124
1
    assert_eq!(script(init), num(3));
125
1
}
126

            
127
#[test]
128
1
fn a_set_returns_the_stored_value_with_its_own_type() {
129
1
    for (stored, ty) in [
130
1
        ("2", "I32"),
131
1
        ("1/2", "Ratio"),
132
1
        ("\"a\"", "StringRef"),
133
1
        ("nil", "Bool"),
134
1
        ("(list 1 2)", "AnyRef"),
135
1
    ] {
136
5
        let source = format!("(let ((p (s-new {{pt}} 1))) (s-set p {{pt}} 0 {stored}))");
137
5
        assert_eq!(eval_type(&source), ty, "{stored}");
138
    }
139
1
    let sum = "(let ((p (s-new {pt} 1))) (+ (s-set p {pt} 0 2) 1))";
140
1
    assert_eq!(script(sum), num(3));
141
1
    let half = "(let ((p (s-new {pt} 1))) (+ (s-set p {pt} 0 1/2) 1/1))";
142
1
    assert_eq!(script(half), "Number(Scalar(Ratio { numer: 3, denom: 2 }))");
143
1
}
144

            
145
#[test]
146
1
fn a_field_holding_a_struct_reads_as_that_struct() {
147
1
    let nested = "(let ((p (s-new {box} (s-new {pt} 4)))) (+ (s-ref (s-ref p {box} 0) {pt} 0) 1))";
148
1
    assert_eq!(eval_type(nested), "I32");
149
1
    assert_eq!(script(nested), num(5));
150
1
    let inner = "(let ((p (s-new {box} (s-new {pt} 4)))) (s-ref p {box} 0))";
151
1
    assert!(eval_type(inner).starts_with("StructRef("));
152
1
}
153

            
154
#[test]
155
1
fn a_field_holding_itself_reads_as_its_struct() {
156
1
    let source = "(let ((b (s-new {box} nil))) (s-set b {box} 0 b) (eq b (s-ref b {box} 0)))";
157
1
    assert_eq!(script(source), "T");
158
1
    let read = "(let ((b (s-new {box} nil))) (s-set b {box} 0 b) \
159
1
                (s-set (s-ref b {box} 0) {box} 0 nil) (s-ref b {box} 0))";
160
1
    assert_eq!(script(read), "Nil");
161
1
}
162

            
163
#[test]
164
1
fn a_field_holding_a_list_reads_as_a_list() {
165
1
    let source = "(let ((p (s-new {box} (list 1 2)))) (s-ref p {box} 0))";
166
1
    assert_eq!(eval_type(source), "PairRef(AnyRef)");
167
1
    let car = "(let ((p (s-new {box} (list 1 2)))) (the index (car (s-ref p {box} 0))))";
168
1
    assert_eq!(script(car), num(1));
169
1
}
170

            
171
#[test]
172
1
fn a_copy_shares_the_field_types_and_not_the_nested_instance() {
173
1
    let source = "(let* ((p (s-new {box} (s-new {pt} 4))) (c (s-copy p {box}))) \
174
1
                  (s-set (s-ref c {box} 0) {pt} 0 7) (+ (s-ref (s-ref p {box} 0) {pt} 0) 1))";
175
1
    assert_eq!(eval_type(source), "I32");
176
1
    assert_eq!(script(source), num(8));
177
1
}
178

            
179
5
fn call(name: &str, args: Vec<Expr>) -> Expr {
180
5
    let head = Expr::Symbol(name.to_string());
181
5
    Expr::List(std::iter::once(head).chain(args).collect())
182
5
}
183

            
184
10
fn index(n: u32) -> Expr {
185
10
    Expr::Number(Number::Index(i64::from(n)))
186
10
}
187

            
188
#[test]
189
1
fn a_money_store_types_the_read_as_money_in_probe_and_codegen() {
190
1
    let mut symbols = SymbolTable::with_builtins_for_wasm();
191
1
    let id = symbols
192
1
        .define_struct("PT", vec!["X".to_string()])
193
1
        .expect("PT defines");
194
1
    let made = call(
195
1
        NEW,
196
1
        vec![index(id.0), Expr::WasmLocal(0, WasmType::Commodity)],
197
1
    );
198
1
    let read = call(REF, vec![made, index(id.0), index(0)]);
199
1
    let mut ctx = CompileContext::new_with_host_fns(&[]).expect("ctx");
200
1
    let mut emit = FunctionEmitter::new();
201
1
    assert_eq!(
202
1
        compile_for_stack(&mut ctx, &mut emit, &mut symbols, &read).expect("compiles"),
203
1
        WasmType::Commodity
204
    );
205
1
    let probed = probe_value(&mut symbols.clone(), &read).expect("probes");
206
1
    assert_eq!(probed, Expr::WasmRuntime(WasmType::Commodity));
207
1
}
208

            
209
#[test]
210
1
fn a_read_probed_before_any_store_stays_provisional_and_asks_for_a_retry() {
211
1
    let mut symbols = SymbolTable::with_builtins_for_wasm();
212
1
    let id = symbols
213
1
        .define_struct("PT", vec!["X".to_string()])
214
1
        .expect("PT defines");
215
1
    let made = call(NEW, vec![index(id.0), index(1)]);
216
1
    let read = call(REF, vec![made.clone(), index(id.0), index(0)]);
217
1
    let before = probe_value(&mut symbols.clone(), &read).expect("probes");
218
1
    assert_eq!(before, Expr::WasmRuntime(WasmType::AnyRef));
219
1
    let mut ctx = CompileContext::new_with_host_fns(&[]).expect("ctx");
220
1
    let mut emit = FunctionEmitter::new();
221
1
    let set = call(SET, vec![made, index(id.0), index(0), index(2)]);
222
1
    compile_for_stack(&mut ctx, &mut emit, &mut symbols, &set).expect("compiles");
223
1
    let after = probe_value(&mut symbols.clone(), &read).expect("probes");
224
1
    assert_eq!(after, Expr::WasmRuntime(WasmType::AnyRef));
225
1
    assert_eq!(symbols.field_type(id, 0), WasmType::AnyRef);
226
1
    assert!(symbols.widened_homes().is_some());
227
1
}