Lines
100 %
Functions
Branches
//! Closure-capture, multi-invocation, and HOF-with-capture coverage.
//!
//! The lambda-emit path lifts free-variable captures into a per-scope
//! `$env_<id>` GC struct that the helper-fn unpacks in its prologue.
//! These tests pin compile + wasm-validate for the most common shapes:
//! capture of a runtime ratio, repeat invocation through `call_ref`,
//! and a capturing lambda passed to MAP / FOLD.
use super::common::{compile_and_validate, wrap_with_runtime_ratio};
#[test]
fn capturing_closure_with_runtime_ratio_compiles() {
compile_and_validate(&wrap_with_runtime_ratio(
"(let ((adder (lambda (n) (+ n X)))) (funcall adder 5))",
));
}
fn closure_invoked_multiple_times_compiles() {
compile_and_validate(
"(let ((double (lambda (x) (* x 2)))) \
(+ (funcall double 1) (funcall double 2)))",
);
fn capturing_closure_passed_to_map_compiles() {
"(let ((adder (lambda (n) (+ n X)))) \
(map adder (cons 1 (cons 2 nil))))",
fn capturing_closure_passed_to_fold_compiles() {
"(let ((acc-add-x (lambda (acc n) (+ acc (+ n X))))) \
(fold acc-add-x 0 (cons 1 (cons 2 nil))))",
fn labels_mutual_recursion_with_runtime_arg_compiles() {
"(labels ((even? (n) (if (= n 0) #t (odd? (- n 1)))) \
(odd? (n) (if (= n 0) #f (even? (- n 1))))) \
(let* ((X (transaction-post-date 0))) (even? X)))",
fn function_designator_passed_through_let_compiles() {
"(defun fact (n) (if (<= n 1) 1 (* n (fact (- n 1))))) \
(let ((f (function fact))) (funcall f 5))",