1
//! Closure-capture, multi-invocation, and HOF-with-capture coverage.
2
//!
3
//! The lambda-emit path lifts free-variable captures into a per-scope
4
//! `$env_<id>` GC struct that the helper-fn unpacks in its prologue.
5
//! These tests pin compile + wasm-validate for the most common shapes:
6
//! capture of a runtime ratio, repeat invocation through `call_ref`,
7
//! and a capturing lambda passed to MAP / FOLD.
8

            
9
use super::common::{compile_and_validate, wrap_with_runtime_ratio};
10

            
11
#[test]
12
1
fn capturing_closure_with_runtime_ratio_compiles() {
13
1
    compile_and_validate(&wrap_with_runtime_ratio(
14
1
        "(let ((adder (lambda (n) (+ n X)))) (funcall adder 5))",
15
1
    ));
16
1
}
17

            
18
#[test]
19
1
fn closure_invoked_multiple_times_compiles() {
20
1
    compile_and_validate(
21
1
        "(let ((double (lambda (x) (* x 2)))) \
22
1
           (+ (funcall double 1) (funcall double 2)))",
23
    );
24
1
}
25

            
26
#[test]
27
1
fn capturing_closure_passed_to_map_compiles() {
28
1
    compile_and_validate(&wrap_with_runtime_ratio(
29
1
        "(let ((adder (lambda (n) (+ n X)))) \
30
1
           (map adder (cons 1 (cons 2 nil))))",
31
1
    ));
32
1
}
33

            
34
#[test]
35
1
fn capturing_closure_passed_to_fold_compiles() {
36
1
    compile_and_validate(&wrap_with_runtime_ratio(
37
1
        "(let ((acc-add-x (lambda (acc n) (+ acc (+ n X))))) \
38
1
           (fold acc-add-x 0 (cons 1 (cons 2 nil))))",
39
1
    ));
40
1
}
41

            
42
#[test]
43
1
fn labels_mutual_recursion_with_runtime_arg_compiles() {
44
1
    compile_and_validate(
45
1
        "(labels ((even? (n) (if (= n 0) #t (odd? (- n 1)))) \
46
1
                  (odd?  (n) (if (= n 0) #f (even? (- n 1))))) \
47
1
            (let* ((X (transaction-post-date 0))) (even? X)))",
48
    );
49
1
}
50

            
51
#[test]
52
1
fn function_designator_passed_through_let_compiles() {
53
1
    compile_and_validate(
54
1
        "(defun fact (n) (if (<= n 1) 1 (* n (fact (- n 1))))) \
55
1
         (let ((f (function fact))) (funcall f 5))",
56
    );
57
1
}