| 1 | module scheme |
| 2 | |
| 3 | fn test_native_binding_helpers_keep_parallel_initializers_and_nontail_results() { |
| 4 | mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! |
| 5 | vm.eval('(define (work x) |
| 6 | (+ (let ((a (begin (set! x (+ x 1)) x)) |
| 7 | (b (begin (set! x (* x 2)) x))) (+ (* a 100) b)) |
| 8 | (let* ((a (+ x 1)) (b (+ a x))) (+ a b))))')! |
| 9 | vm.with_native_execution(fn (mut vm VM) !Value { |
| 10 | assert vm.call(vm.lookup('work')!,[integer(1)])!.whole() == 218 |
| 11 | assert vm.call(vm.lookup('work')!,[integer(2)])!.whole() == 326 |
| 12 | hits,deopts := vm.native_execution_stats() |
| 13 | assert hits == 2 && deopts == 0 |
| 14 | return unspecified() |
| 15 | })! |
| 16 | } |
| 17 | |
| 18 | fn test_native_binding_helpers_keep_tail_calls_and_nested_lexical_depths() { |
| 19 | mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! |
| 20 | vm.eval('(define (make bias) (lambda(n) |
| 21 | (let loop ((i n) (sum 0)) |
| 22 | (if (= i 0) sum |
| 23 | (let ((x (+ i bias))) |
| 24 | (let ((y (+ x 1))) (loop (- i 1) (+ sum x y)))))))) |
| 25 | (define work (make 3))')! |
| 26 | vm.with_native_execution(fn (mut vm VM) !Value { |
| 27 | assert vm.call(vm.lookup('work')!,[integer(4096)])!.whole() == 16809984 |
| 28 | hits,deopts := vm.native_execution_stats() |
| 29 | assert hits == 1 && deopts == 0 |
| 30 | return unspecified() |
| 31 | })! |
| 32 | } |
| 33 | |
| 34 | fn test_native_binding_helpers_keep_empty_scopes_and_parent_assignments() { |
| 35 | mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! |
| 36 | vm.eval('(define (work x) |
| 37 | (let () (set! x (+ x 1)) (define y (+ x 2)) |
| 38 | (let ((z (+ y 3))) (set! y (+ z x))) (+ y x)))')! |
| 39 | vm.with_native_execution(fn (mut vm VM) !Value { |
| 40 | assert vm.call(vm.lookup('work')!,[integer(10)])!.whole() == 38 |
| 41 | hits,deopts := vm.native_execution_stats() |
| 42 | assert hits == 1 && deopts == 0 |
| 43 | return unspecified() |
| 44 | })! |
| 45 | } |
| 46 | |
| 47 | fn test_native_binding_helpers_preserve_escaped_locations_and_fresh_closures() { |
| 48 | mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! |
| 49 | vm.eval('(define (make x)(let ((n x))(lambda(d)(set! n (+ n d)) n)))')! |
| 50 | vm.with_native_execution(fn (mut vm VM) !Value { |
| 51 | a := vm.call(vm.lookup('make')!,[integer(1)])! |
| 52 | b := vm.call(vm.lookup('make')!,[integer(10)])! |
| 53 | assert a.pointer() != b.pointer() |
| 54 | before,_ := vm.native_execution_stats() |
| 55 | assert before == 0 |
| 56 | assert vm.call(a,[integer(2)])!.whole() == 3 |
| 57 | assert vm.call(b,[integer(4)])!.whole() == 14 |
| 58 | assert vm.call(a,[integer(5)])!.whole() == 8 |
| 59 | hits,_ := vm.native_execution_stats() |
| 60 | assert hits == 0 |
| 61 | return unspecified() |
| 62 | })! |
| 63 | } |
| 64 | |
| 65 | fn test_native_binding_helpers_preserve_continuation_reentry() { |
| 66 | mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! |
| 67 | vm.eval('(define saved #f) |
| 68 | (define (work)(let ((x 10)) (+ x (call/cc (lambda(k)(set! saved k) 0)))))')! |
| 69 | vm.with_native_execution(fn (mut vm VM) !Value { |
| 70 | assert vm.call(vm.lookup('work')!,[])!.whole() == 10 |
| 71 | assert vm.call(vm.lookup('saved')!,[integer(5)])!.whole() == 15 |
| 72 | assert vm.call(vm.lookup('saved')!,[integer(9)])!.whole() == 19 |
| 73 | return unspecified() |
| 74 | })! |
| 75 | } |
| 76 | |
| 77 | fn test_native_binding_helpers_reset_uninitialized_definitions_before_replay() { |
| 78 | mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! |
| 79 | vm.eval('(define (work v) |
| 80 | (let loop ((i 0)) |
| 81 | (let ((unused 0)) |
| 82 | (if (= i 0) 0 x) |
| 83 | (define x 99) |
| 84 | (vector-set! v 0 (+ 1 (vector-ref v 0))) |
| 85 | (loop (+ i 1)))))')! |
| 86 | vm.with_native_execution(fn (mut vm VM) !Value { |
| 87 | v := vector([integer(0)]) |
| 88 | if _ := vm.call(vm.lookup('work')!,[v]) { return error('uninitialized binding was reused') } |
| 89 | assert v.vector_pointer().items[0].whole() == 1 |
| 90 | _,deopts := vm.native_execution_stats() |
| 91 | assert deopts > 0 |
| 92 | return unspecified() |
| 93 | })! |
| 94 | } |
| 95 | |
| 96 | fn test_native_binding_helpers_refresh_inlined_global_guards_and_captures() { |
| 97 | mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! |
| 98 | vm.eval('(define offset 1) (define (helper x)(+ x 2)) |
| 99 | (define cells (let ((bias 3)) |
| 100 | (list (lambda(x)(let ((y (helper x))) (+ y bias offset))) |
| 101 | (lambda(n)(set! bias n))))) |
| 102 | (define work (car cells)) (define change (cadr cells))')! |
| 103 | vm.with_native_execution(fn (mut vm VM) !Value { |
| 104 | result := vm.eval('(list (work 10) (begin (change 20) (work 10)) |
| 105 | (begin (set! offset 30) (work 10)) |
| 106 | (begin (set! helper (lambda(x)(* x 2))) (work 10)))')! |
| 107 | assert write(result)! == '(16 33 62 70)' |
| 108 | return unspecified() |
| 109 | })! |
| 110 | } |
| 111 | |
| 112 | fn test_native_binding_helpers_run_actual_wm_callback_chain_in_one_entry() { |
| 113 | mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! |
| 114 | vm.eval($embed_file('../bench/wm/common.scm').to_string())! |
| 115 | vm.eval('(define (make-run callback)(lambda(s n) |
| 116 | (let loop ((i 0)) |
| 117 | (if (< i n) |
| 118 | (begin (wm-invoke callback s (modulo i 64) (modulo i 13)) (loop (+ i 1))) |
| 119 | (vector-ref s 4))))) |
| 120 | (define run (make-run (wm-callback 7)))')! |
| 121 | root := vm.lookup('run')! |
| 122 | before := vm.call(vm.lookup('wm-new')!,[integer(3)])! |
| 123 | expected := vm.call(root,[before,integer(4096)])! |
| 124 | actual := vm.call(vm.lookup('wm-new')!,[integer(3)])! |
| 125 | vm.with_native_execution(fn [actual,expected] (mut vm VM) !Value { |
| 126 | assert vm.call(vm.lookup('run')!,[actual,integer(4096)])!.whole() == expected.whole() |
| 127 | hits,deopts := vm.native_execution_stats() |
| 128 | assert hits == 1 && deopts == 0 |
| 129 | return unspecified() |
| 130 | })! |
| 131 | assert write(actual)! == write(before)! |
| 132 | } |
| 133 | |
| 134 | fn test_native_binding_helpers_work_in_vile_frontend() { |
| 135 | mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! |
| 136 | vm.eval_alt('work := fn(x) { y := x + 1\n let((z(y + 2),), { x = x + z })\n x + y }')! |
| 137 | vm.with_native_execution(fn (mut vm VM) !Value { |
| 138 | assert vm.call(vm.lookup('work')!,[integer(10)])!.whole() == 34 |
| 139 | hits,deopts := vm.native_execution_stats() |
| 140 | assert hits == 1 && deopts == 0 |
| 141 | return unspecified() |
| 142 | })! |
| 143 | } |
| 144 | |
| 145 | fn test_native_binding_helpers_revalidate_hygienic_private_global_references() { |
| 146 | mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! |
| 147 | vm.with_native_execution(fn (mut vm VM) !Value { |
| 148 | result := vm.eval('(define first #f) |
| 149 | (define-syntax emit-work (syntax-rules () ((_ exported) |
| 150 | (begin (define (exported x) (let ((y (+ x 1))) (+ y 2))) |
| 151 | (set! first (exported 20)) (define + *) (list first (exported 20)))))) |
| 152 | (emit-work compute)')! |
| 153 | assert write(result)! == '(23 40)' |
| 154 | assert vm.call(vm.lookup('compute')!,[integer(10)])!.whole() == 20 |
| 155 | assert vm.eval('(+ 20 22)')!.whole() == 42 |
| 156 | return unspecified() |
| 157 | })! |
| 158 | } |
| 159 | |