| 1 | module scheme |
| 2 | |
| 3 | fn test_native_foreign_captured_callback_loop_runs_in_one_entry() { |
| 4 | mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! |
| 5 | vm.eval('(define (forward f v)(f v)) |
| 6 | (define (make-callback bias) (lambda(v)(vector-set! v 0 (+ (vector-ref v 0) bias)))) |
| 7 | (define (make-run callback) (lambda(v n) |
| 8 | (let loop ((i 0)) (if (< i n) |
| 9 | (begin (forward callback v) (loop (+ i 1))) (vector-ref v 0))))) |
| 10 | (define run (make-run (make-callback 3)))')! |
| 11 | vm.with_native_execution(fn (mut vm VM) !Value { |
| 12 | v := vector([integer(0)]) |
| 13 | assert vm.call(vm.lookup('run')!, [v,integer(4096)])!.whole() == 12288 |
| 14 | scope := unsafe { &NativeScope(vm.native_execution_scope) } |
| 15 | assert scope.hits == 1 && scope.deopts == 0 |
| 16 | return unspecified() |
| 17 | })! |
| 18 | } |
| 19 | |
| 20 | fn test_native_argument_providers_keep_same_code_environments_distinct_and_live() { |
| 21 | mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! |
| 22 | vm.eval('(define (make bias)(lambda(x)(+ x bias))) |
| 23 | (define a (make 7)) (define b (make 19)) (define r (make 0.5)) |
| 24 | (define (work f g x)(+ (f x) (g x)))')! |
| 25 | vm.with_native_execution(fn (mut vm VM) !Value { |
| 26 | root := vm.lookup('work')! |
| 27 | a,b := vm.lookup('a')!,vm.lookup('b')! |
| 28 | assert a.procedure_pointer().function == b.procedure_pointer().function |
| 29 | assert vm.call(root,[a,a,integer(3)])!.whole() == 20 |
| 30 | assert vm.call(root,[a,b,integer(3)])!.whole() == 32 |
| 31 | assert vm.call(root,[b,a,integer(3)])!.whole() == 32 |
| 32 | assert vm.call(root,[vm.lookup('r')!,b,integer(3)])!.floating() == 25.5 |
| 33 | hits,deopts := vm.native_execution_stats() |
| 34 | assert hits == 4 && deopts == 0 |
| 35 | return unspecified() |
| 36 | })! |
| 37 | } |
| 38 | |
| 39 | fn test_native_capture_providers_follow_callback_cell_changes_without_global_revision() { |
| 40 | mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! |
| 41 | vm.eval('(define (make bias)(lambda(x)(+ x bias))) |
| 42 | (define a (make 1)) (define b (make 41)) (define c (lambda(x)(* x 10))) |
| 43 | (define cells (let ((f a)) (list (lambda(x)(f x)) (lambda(next)(set! f next))))) |
| 44 | (define run (car cells)) (define replace (cadr cells))')! |
| 45 | vm.with_native_execution(fn (mut vm VM) !Value { |
| 46 | scope := unsafe { &NativeScope(vm.native_execution_scope) } |
| 47 | revision := scope.binding_revision |
| 48 | result := vm.eval('(list (run 1) (begin (replace b) (run 1)) (begin (replace c) (run 1)))')! |
| 49 | assert write(result)! == '(2 42 10)' |
| 50 | assert scope.binding_revision == revision |
| 51 | return unspecified() |
| 52 | })! |
| 53 | } |
| 54 | |
| 55 | fn test_native_procedure_result_and_store_restore_vectors_before_replay() { |
| 56 | mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! |
| 57 | vm.eval('(define (callback x)(+ x 1)) |
| 58 | (define (return-it f v)(vector-set! v 0 (+ (vector-ref v 0) 1)) f) |
| 59 | (define (store-it f v)(vector-set! v 0 (+ (vector-ref v 0) 1)) |
| 60 | (vector-set! v 1 f) (vector-ref v 0))')! |
| 61 | vm.with_native_execution(fn (mut vm VM) !Value { |
| 62 | callback := vm.lookup('callback')! |
| 63 | v := vector([integer(0),boolean(false)]) |
| 64 | returned := vm.call(vm.lookup('return-it')!,[callback,v])! |
| 65 | assert returned.pointer() == callback.pointer() && v.vector_pointer().items[0].whole() == 1 |
| 66 | assert vm.call(vm.lookup('store-it')!,[callback,v])!.whole() == 2 |
| 67 | assert v.vector_pointer().items[1].pointer() == callback.pointer() |
| 68 | assert vm.call(returned,[integer(41)])!.whole() == 42 |
| 69 | _,deopts := vm.native_execution_stats() |
| 70 | assert deopts >= 2 |
| 71 | return unspecified() |
| 72 | })! |
| 73 | } |
| 74 | |
| 75 | fn test_native_mutually_captured_callbacks_have_bounded_graphs_and_depth_fallback() { |
| 76 | mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! |
| 77 | vm.eval('(define callbacks (letrec |
| 78 | ((even (lambda(n)(if (= n 0) #t (odd (- n 1))))) |
| 79 | (odd (lambda(n)(if (= n 0) #f (even (- n 1)))))) (list even odd))) |
| 80 | (define even (car callbacks))')! |
| 81 | vm.with_native_execution(fn (mut vm VM) !Value { |
| 82 | assert vm.call(vm.lookup('even')!,[integer(100)])!.is_true() |
| 83 | scope := unsafe { &NativeScope(vm.native_execution_scope) } |
| 84 | assert scope.plans.len == 1 && scope.plans[0].nodes.len == 2 && scope.hits == 1 |
| 85 | assert vm.call(vm.lookup('even')!,[integer(2000)])!.is_true() |
| 86 | assert scope.deopts > 0 |
| 87 | return unspecified() |
| 88 | })! |
| 89 | } |
| 90 | |
| 91 | fn test_native_unsupported_or_foreign_procedure_arguments_retain_vm_semantics() { |
| 92 | mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! |
| 93 | mut other := new_with_options(Options{max_steps: 0,max_frames: 0})! |
| 94 | foreign := other.eval('(lambda(x)(+ x 1))')! |
| 95 | vm.eval('(define (ignore f)42) (define (use f x)(f x)) (define (own x)(+ x 1))')! |
| 96 | vm.with_native_execution(fn [foreign] (mut vm VM) !Value { |
| 97 | assert vm.call(vm.lookup('ignore')!,[foreign])!.whole() == 42 |
| 98 | assert vm.call(vm.lookup('ignore')!,[vm.lookup('+')!])!.whole() == 42 |
| 99 | assert vm.call(vm.lookup('ignore')!,[vm.lookup('own')!])!.whole() == 42 |
| 100 | assert vm.call(vm.lookup('use')!,[vm.lookup('own')!,integer(41)])!.whole() == 42 |
| 101 | assert vm.call(vm.lookup('use')!,[vm.lookup('+')!,integer(3)])!.whole() == 3 |
| 102 | assert vm.eval('(call/cc (lambda(k)(ignore k)))')!.whole() == 42 |
| 103 | assert vm.eval('(call/cc (lambda(k)(use k 17)))')!.whole() == 17 |
| 104 | return unspecified() |
| 105 | })! |
| 106 | } |
| 107 | |
| 108 | fn test_native_inexact_to_exact_rejects_nonnumbers_before_branches_and_calls() { |
| 109 | mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! |
| 110 | vm.eval('(define (integer-path)(if (inexact->exact #f) 1 2)) |
| 111 | (define (mixed-path)(if (inexact->exact #f) 1.0 2.0)) |
| 112 | (define (callback v)(vector-set! v 0 (+ (vector-ref v 0) 10)) 42) |
| 113 | (define (bad-call f v)(vector-set! v 0 (+ (vector-ref v 0) 1)) ((inexact->exact f) v))')! |
| 114 | vm.with_native_execution(fn (mut vm VM) !Value { |
| 115 | for name in ['integer-path','mixed-path'] { |
| 116 | if _ := vm.call(vm.lookup(name)!,[]) { return error('non-number conversion bypassed its error') } |
| 117 | } |
| 118 | v := vector([integer(0)]) |
| 119 | if _ := vm.call(vm.lookup('bad-call')!,[vm.lookup('callback')!,v]) { |
| 120 | return error('conversion called its non-number argument') |
| 121 | } |
| 122 | assert v.vector_pointer().items[0].whole() == 1 |
| 123 | _,deopts := vm.native_execution_stats() |
| 124 | assert deopts >= 3 |
| 125 | return unspecified() |
| 126 | })! |
| 127 | } |
| 128 | |