| 1 | module main |
| 2 | |
| 3 | import os |
| 4 | import scheme |
| 5 | import time |
| 6 | |
| 7 | fn test_native_report_closure_keeps_pristine_bindings_after_root_redefinition() { |
| 8 | mut vm := scheme.new_with_options(scheme.Options{ max_steps: 0, max_frames: 0 })! |
| 9 | report := vm.report_environment(5)! |
| 10 | root := vm.eval_in('(lambda(a b)(+ a b))', report)! |
| 11 | vm.with_native_execution(fn [root] (mut vm scheme.VM) !scheme.Value { |
| 12 | assert vm.call(root, [scheme.integer(20), scheme.integer(22)])!.as_integer()! == 42 |
| 13 | vm.eval('(set! + (lambda xs 99))')! |
| 14 | assert vm.call(root, [scheme.integer(20), scheme.integer(22)])!.as_integer()! == 42 |
| 15 | // Report bindings embed procedure constants, so this graph safely falls back. |
| 16 | return scheme.unspecified() |
| 17 | })! |
| 18 | } |
| 19 | |
| 20 | fn native_cli_restore_env(key string, value string) { |
| 21 | if value == '' { os.unsetenv(key) } else { os.setenv(key, value, true) } |
| 22 | } |
| 23 | |
| 24 | struct NativeCliCase { |
| 25 | args []string |
| 26 | input string |
| 27 | expected string |
| 28 | status int |
| 29 | error_text string |
| 30 | } |
| 31 | |
| 32 | fn test_native_cli_preserves_results_budgets_exit_status_and_scope_cleanup() { |
| 33 | dir := os.join_path(os.temp_dir(), 'vile-native-cli-${os.getpid()}-${time.sys_mono_now()}') |
| 34 | os.mkdir_all(dir)! |
| 35 | defer { os.rmdir_all(dir) or {} } |
| 36 | binary := os.join_path(dir, 'vile') |
| 37 | build := os.exec([@VEXE, '-no-parallel', '-new-compiler', '-no-retry-compilation', '-cc', 'clang', |
| 38 | '-prod', '-gc', 'boehm', '-d', 'use_bundled_libgc', '-d', 'vile_native', '-path', |
| 39 | @VMODROOT + '|@vlib', '-o', binary, @VMODROOT]) |
| 40 | assert build.exit_code == 0, build.output |
| 41 | |
| 42 | cache := os.join_path(dir, 'cache') |
| 43 | wrapper := os.join_path(dir, 'bin') |
| 44 | os.mkdir_all(wrapper)! |
| 45 | counter := os.join_path(dir, 'compiler-calls') |
| 46 | actual_clang := os.find_abs_path_of_executable('clang')! |
| 47 | old_path := os.getenv('PATH').clone() |
| 48 | old_cache := os.getenv('VILE_NATIVE_CACHE').clone() |
| 49 | old_real := os.getenv('VILE_TEST_REAL_CLANG').clone() |
| 50 | old_counter := os.getenv('VILE_TEST_COMPILATIONS').clone() |
| 51 | mut old_headers := map[string]string{} |
| 52 | for key in ['CPATH', 'C_INCLUDE_PATH', 'CPLUS_INCLUDE_PATH', 'OBJC_INCLUDE_PATH', 'SDKROOT'] { |
| 53 | old_headers[key] = os.getenv(key).clone() |
| 54 | os.unsetenv(key) |
| 55 | } |
| 56 | 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')! |
| 57 | os.chmod(os.join_path(wrapper, 'clang'), 0o700)! |
| 58 | os.setenv('PATH', wrapper + ':' + old_path, true) |
| 59 | os.setenv('VILE_NATIVE_CACHE', cache, true) |
| 60 | os.setenv('VILE_TEST_REAL_CLANG', actual_clang, true) |
| 61 | os.setenv('VILE_TEST_COMPILATIONS', counter, true) |
| 62 | defer { |
| 63 | native_cli_restore_env('PATH', old_path) |
| 64 | native_cli_restore_env('VILE_NATIVE_CACHE', old_cache) |
| 65 | native_cli_restore_env('VILE_TEST_REAL_CLANG', old_real) |
| 66 | native_cli_restore_env('VILE_TEST_COMPILATIONS', old_counter) |
| 67 | for key, value in old_headers { native_cli_restore_env(key, value) } |
| 68 | } |
| 69 | for _ in 0 .. 2 { |
| 70 | probe := os.exec([binary, '--native', '-s', '-e', '(define (cache-probe x)(+ x 73)) (cache-probe 5)']) |
| 71 | assert probe.exit_code == 0 && probe.output.trim_space() == '78', probe.output |
| 72 | assert os.read_file(counter)!.split_into_lines().len == 1 |
| 73 | } |
| 74 | |
| 75 | for case in [ |
| 76 | NativeCliCase{ args: ['--native', '-s', '-e', '(define (work n)(call-with-values (lambda()(values n 2)) (lambda(a b)(+ a b)))) (work 40)'], expected: '42' }, |
| 77 | NativeCliCase{ args: ['--native', '-v', '-e', 'work := fn(n) { call-with-values(fn() { values(n,2) }, fn(a,b) { a + b }) }\nwork(40)'], expected: '42' }, |
| 78 | NativeCliCase{ args: ['--native', '-s', '-e', '(define (work)(call-with-values (lambda()(values 1 2)) (lambda(a)a))) (work)'], status: 1, error_text: 'argument' }, |
| 79 | NativeCliCase{ args: ['--native', '-s', '-e', |
| 80 | '(define (sum n a)(if (= n 0) a (sum (- n 1) (+ a n)))) (sum 1000 0)'], expected: '500500' }, |
| 81 | NativeCliCase{ args: ['--native', '-v', '-e', 'work := fn(n) { n + 1 }\nwork(41)'], expected: '42' }, |
| 82 | 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' }, |
| 83 | 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' }, |
| 84 | 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' }, |
| 85 | NativeCliCase{ args: ['--native', '-s', '-e', '(define (through f x)(f x)) (define (make n)(lambda(x)(+ x n))) (through (make 41) 1)'], expected: '42' }, |
| 86 | 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' }, |
| 87 | 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' }, |
| 88 | 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' }, |
| 89 | NativeCliCase{ args: ['--native', '-s', '-e', '(define (work n)(let loop ((i n))(if (= i 0) 100 (+ i (loop (- i 1)))))) (work 10)'], expected: '155' }, |
| 90 | NativeCliCase{ args: ['--native', '-v', '-e', 'work := fn(n) { let(loop, (), { n = n - 1\n if n == 0 { 42 } else { loop } }) }\nwork(4096)'], expected: '42' }, |
| 91 | 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' }, |
| 92 | 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)' }, |
| 93 | 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' }, |
| 94 | 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)' }, |
| 95 | NativeCliCase{ args: ['--native', '-v', '-e', 'work := fn(xs) { car(xs) + car(cdr(xs)) }\nwork(list(20, 22))'], expected: '42' }, |
| 96 | 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)' }, |
| 97 | NativeCliCase{ args: ['--native', '-v', '-e', 'work := fn(b) { bytevector-u8-set!(b,0,42)\nbytevector-u8-ref(b,0) }\nwork(bytevector(0))'], expected: '42' }, |
| 98 | 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' }, |
| 99 | NativeCliCase{ args: ['--native', '-v', '-e', 'make := fn(n) { fn(d) { n = n + d\n n } }\ncounter := make(0)\ncounter(7)\ncounter(9)'], expected: '16' }, |
| 100 | NativeCliCase{ args: ['--native', '-s', '-e', '((lambda()(if (inexact->exact #f) 1 2)))'], status: 1, error_text: 'expected a number' }, |
| 101 | NativeCliCase{ args: ['--native', '--max-steps', '40', '-s', '-e', '(let loop () (loop))'], status: 1, error_text: 'step limit exceeded' }, |
| 102 | NativeCliCase{ args: ['--native', '-s', '-'], input: '((lambda () (+ 20 22)))\n' }, |
| 103 | NativeCliCase{ args: ['--native', '-s'], input: '((lambda () (+ 20 22)))\n', expected: '42' }, |
| 104 | NativeCliCase{ args: ['--native', '-s', '-e', '(define (f x)(+ x 1)) (f 41) (exit 7)'], status: 7 }, |
| 105 | NativeCliCase{ args: ['--native', '-s', '-e', '(define (f x)(+ x 1)) (f 41) (emergency-exit 9)'], status: 9 }, |
| 106 | NativeCliCase{ args: ['--native', '-s'], input: '(define (f x)(+ x 1))\n(f 41)\n(exit 7)\n(display 99)\n', expected: '42', status: 7 }, |
| 107 | NativeCliCase{ args: ['-s', '-e', '(+ 20 22)'], expected: '42' }, |
| 108 | ] { |
| 109 | before := os.ls(os.temp_dir())! |
| 110 | mut process := os.new_process(binary) |
| 111 | process.set_args(case.args) |
| 112 | process.set_redirect_stdio() |
| 113 | input_path := os.join_path(dir, 'stdin.scm') |
| 114 | os.write_file(input_path, case.input)! |
| 115 | process.set_stdin_path(input_path) |
| 116 | process.run() |
| 117 | child_pid := process.pid |
| 118 | process.wait() |
| 119 | output := process.stdout_slurp().trim_space() |
| 120 | errors := process.stderr_slurp() |
| 121 | status := process.code |
| 122 | process.close() |
| 123 | assert status == case.status, '${case.args}: ${status}: ${errors}' |
| 124 | assert output == case.expected, '${case.args}: ${output}' |
| 125 | if case.error_text != '' { |
| 126 | assert errors.contains(case.error_text), errors |
| 127 | } |
| 128 | assert os.ls(os.temp_dir())!.filter(it.starts_with('vile-native.${child_pid}.') && it !in before).len == 0 |
| 129 | } |
| 130 | } |
| 131 | |