module main import os import scheme import time fn test_native_report_closure_keeps_pristine_bindings_after_root_redefinition() { mut vm := scheme.new_with_options(scheme.Options{ max_steps: 0, max_frames: 0 })! report := vm.report_environment(5)! root := vm.eval_in('(lambda(a b)(+ a b))', report)! vm.with_native_execution(fn [root] (mut vm scheme.VM) !scheme.Value { assert vm.call(root, [scheme.integer(20), scheme.integer(22)])!.as_integer()! == 42 vm.eval('(set! + (lambda xs 99))')! assert vm.call(root, [scheme.integer(20), scheme.integer(22)])!.as_integer()! == 42 // Report bindings embed procedure constants, so this graph safely falls back. return scheme.unspecified() })! } fn native_cli_restore_env(key string, value string) { if value == '' { os.unsetenv(key) } else { os.setenv(key, value, true) } } struct NativeCliCase { args []string input string expected string status int error_text string } fn test_native_cli_preserves_results_budgets_exit_status_and_scope_cleanup() { dir := os.join_path(os.temp_dir(), 'vile-native-cli-${os.getpid()}-${time.sys_mono_now()}') os.mkdir_all(dir)! defer { os.rmdir_all(dir) or {} } binary := os.join_path(dir, 'vile') build := os.exec([@VEXE, '-no-parallel', '-new-compiler', '-no-retry-compilation', '-cc', 'clang', '-prod', '-gc', 'boehm', '-d', 'use_bundled_libgc', '-d', 'vile_native', '-path', @VMODROOT + '|@vlib', '-o', binary, @VMODROOT]) assert build.exit_code == 0, build.output cache := os.join_path(dir, 'cache') wrapper := os.join_path(dir, 'bin') os.mkdir_all(wrapper)! counter := os.join_path(dir, 'compiler-calls') actual_clang := os.find_abs_path_of_executable('clang')! old_path := os.getenv('PATH').clone() old_cache := os.getenv('VILE_NATIVE_CACHE').clone() old_real := os.getenv('VILE_TEST_REAL_CLANG').clone() old_counter := os.getenv('VILE_TEST_COMPILATIONS').clone() mut old_headers := map[string]string{} for key in ['CPATH', 'C_INCLUDE_PATH', 'CPLUS_INCLUDE_PATH', 'OBJC_INCLUDE_PATH', 'SDKROOT'] { old_headers[key] = os.getenv(key).clone() os.unsetenv(key) } os.write_file(os.join_path(wrapper, 'clang'), '#!/bin/sh\nif [ "$1" != "--version" ]; then printf "%s\\n" build >> "$VILE_TEST_COMPILATIONS"; fi\nexec "$VILE_TEST_REAL_CLANG" "$@"\n')! os.chmod(os.join_path(wrapper, 'clang'), 0o700)! os.setenv('PATH', wrapper + ':' + old_path, true) os.setenv('VILE_NATIVE_CACHE', cache, true) os.setenv('VILE_TEST_REAL_CLANG', actual_clang, true) os.setenv('VILE_TEST_COMPILATIONS', counter, true) defer { native_cli_restore_env('PATH', old_path) native_cli_restore_env('VILE_NATIVE_CACHE', old_cache) native_cli_restore_env('VILE_TEST_REAL_CLANG', old_real) native_cli_restore_env('VILE_TEST_COMPILATIONS', old_counter) for key, value in old_headers { native_cli_restore_env(key, value) } } for _ in 0 .. 2 { probe := os.exec([binary, '--native', '-s', '-e', '(define (cache-probe x)(+ x 73)) (cache-probe 5)']) assert probe.exit_code == 0 && probe.output.trim_space() == '78', probe.output assert os.read_file(counter)!.split_into_lines().len == 1 } for case in [ NativeCliCase{ args: ['--native', '-s', '-e', '(define (work n)(call-with-values (lambda()(values n 2)) (lambda(a b)(+ a b)))) (work 40)'], expected: '42' }, NativeCliCase{ args: ['--native', '-v', '-e', 'work := fn(n) { call-with-values(fn() { values(n,2) }, fn(a,b) { a + b }) }\nwork(40)'], expected: '42' }, NativeCliCase{ args: ['--native', '-s', '-e', '(define (work)(call-with-values (lambda()(values 1 2)) (lambda(a)a))) (work)'], status: 1, error_text: 'argument' }, NativeCliCase{ args: ['--native', '-s', '-e', '(define (sum n a)(if (= n 0) a (sum (- n 1) (+ a n)))) (sum 1000 0)'], expected: '500500' }, NativeCliCase{ args: ['--native', '-v', '-e', 'work := fn(n) { n + 1 }\nwork(41)'], expected: '42' }, NativeCliCase{ args: ['--native', '-s', '-e', '(define v (vector 0)) (define (work) (vector-set! v 0 (+ (vector-ref v 0) 1)) (+ 9223372036854775807 1)) (work) (vector-ref v 0)'], expected: '1' }, NativeCliCase{ args: ['--native', '-s', '-e', '(define v (vector (vector 2.5))) (define (work v) (vector-set! (vector-ref v 0) 0 (+ (vector-ref (vector-ref v 0) 0) 0.25)) (vector-ref (vector-ref v 0) 0)) (work v)'], expected: '2.75' }, NativeCliCase{ args: ['--native', '-s', '-e', '(define (helper n)(+ n 1)) (define (work n)(helper n)) (work 1) (set! helper (lambda(n)42)) (work 1)'], expected: '42' }, NativeCliCase{ args: ['--native', '-s', '-e', '(define (through f x)(f x)) (define (make n)(lambda(x)(+ x n))) (through (make 41) 1)'], expected: '42' }, NativeCliCase{ args: ['--native', '-s', '-e', '(define (make-run callback)(lambda(n)(let loop ((i 0)(sum 0))(if (= i n) sum (loop (+ i 1) (+ sum (callback i))))))) (define run (make-run (lambda(x)x))) (run 100)'], expected: '4950' }, NativeCliCase{ args: ['--native', '-s', '-e', '(define (run n)(let loop ((i n)(sum 0))(if (= i 0) sum (let ((x (+ i 3))) (let ((y (+ x 1))) (loop (- i 1) (+ sum x y))))))) (run 4096)'], expected: '16809984' }, NativeCliCase{ args: ['--native', '-s', '-e', '(define (work n)(let outer ((i n)(sum 0))(if (= i 0) sum (outer (- i 1) (+ sum (let inner ((j i)(s 0))(if (= j 0) s (inner (- j 1) (+ s j))))))))) (work 10)'], expected: '220' }, NativeCliCase{ args: ['--native', '-s', '-e', '(define (work n)(let loop ((i n))(if (= i 0) 100 (+ i (loop (- i 1)))))) (work 10)'], expected: '155' }, NativeCliCase{ args: ['--native', '-v', '-e', 'work := fn(n) { let(loop, (), { n = n - 1\n if n == 0 { 42 } else { loop } }) }\nwork(4096)'], expected: '42' }, NativeCliCase{ args: ['--native', '-v', '-e', 'work := fn(x) { y := x + 1\n let((z(y + 2),), { x = x + z })\n x + y }\nwork(10)'], expected: '34' }, NativeCliCase{ args: ['--native', '-s', '-e', '(define (make n)(lambda(d)(set! n (+ n d)) n)) (define a (make 1)) (define b (make 100)) (define (work f g)(+ (f 1) (g 10))) (list (work a b) (work a a))'], expected: '(112 16)' }, NativeCliCase{ args: ['--native', '-s', '-e', '(define xs (list 1 2 3)) (define (fold xs a)(if (null? xs) a (fold (cdr xs) (+ a (car xs))))) (fold xs 0) (set-car! xs 10) (fold xs 0)'], expected: '15' }, NativeCliCase{ args: ['--native', '-s', '-e', '(define xs (list 1 2 3)) (define v (vector 0)) (define (work xs)(vector-set! v 0 (+ (vector-ref v 0) 1)) (cdr xs)) (list (eq? (work xs) (cdr xs)) (vector-ref v 0))'], expected: '(#t 1)' }, NativeCliCase{ args: ['--native', '-v', '-e', 'work := fn(xs) { car(xs) + car(cdr(xs)) }\nwork(list(20, 22))'], expected: '42' }, NativeCliCase{ args: ['--native', '-s', '-e', '(define b (bytevector 1)) (define (work b)(bytevector-u8-set! b 0 (+ (bytevector-u8-ref b 0) 1)) b) (list (eq? (work b) b) (bytevector-u8-ref b 0))'], expected: '(#t 2)' }, NativeCliCase{ args: ['--native', '-v', '-e', 'work := fn(b) { bytevector-u8-set!(b,0,42)\nbytevector-u8-ref(b,0) }\nwork(bytevector(0))'], expected: '42' }, NativeCliCase{ args: ['--native', '-s', '-e', '(define b (bytevector 0)) (define (work b)(bytevector-u8-set! b 0 256)) (work b)'], status: 1, error_text: '0..255' }, NativeCliCase{ args: ['--native', '-v', '-e', 'make := fn(n) { fn(d) { n = n + d\n n } }\ncounter := make(0)\ncounter(7)\ncounter(9)'], expected: '16' }, NativeCliCase{ args: ['--native', '-s', '-e', '((lambda()(if (inexact->exact #f) 1 2)))'], status: 1, error_text: 'expected a number' }, NativeCliCase{ args: ['--native', '--max-steps', '40', '-s', '-e', '(let loop () (loop))'], status: 1, error_text: 'step limit exceeded' }, NativeCliCase{ args: ['--native', '-s', '-'], input: '((lambda () (+ 20 22)))\n' }, NativeCliCase{ args: ['--native', '-s'], input: '((lambda () (+ 20 22)))\n', expected: '42' }, NativeCliCase{ args: ['--native', '-s', '-e', '(define (f x)(+ x 1)) (f 41) (exit 7)'], status: 7 }, NativeCliCase{ args: ['--native', '-s', '-e', '(define (f x)(+ x 1)) (f 41) (emergency-exit 9)'], status: 9 }, NativeCliCase{ args: ['--native', '-s'], input: '(define (f x)(+ x 1))\n(f 41)\n(exit 7)\n(display 99)\n', expected: '42', status: 7 }, NativeCliCase{ args: ['-s', '-e', '(+ 20 22)'], expected: '42' }, ] { before := os.ls(os.temp_dir())! mut process := os.new_process(binary) process.set_args(case.args) process.set_redirect_stdio() input_path := os.join_path(dir, 'stdin.scm') os.write_file(input_path, case.input)! process.set_stdin_path(input_path) process.run() child_pid := process.pid process.wait() output := process.stdout_slurp().trim_space() errors := process.stderr_slurp() status := process.code process.close() assert status == case.status, '${case.args}: ${status}: ${errors}' assert output == case.expected, '${case.args}: ${output}' if case.error_text != '' { assert errors.contains(case.error_text), errors } assert os.ls(os.temp_dir())!.filter(it.starts_with('vile-native.${child_pid}.') && it !in before).len == 0 } }