module scheme fn test_native_foreign_captured_callback_loop_runs_in_one_entry() { mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! vm.eval('(define (forward f v)(f v)) (define (make-callback bias) (lambda(v)(vector-set! v 0 (+ (vector-ref v 0) bias)))) (define (make-run callback) (lambda(v n) (let loop ((i 0)) (if (< i n) (begin (forward callback v) (loop (+ i 1))) (vector-ref v 0))))) (define run (make-run (make-callback 3)))')! vm.with_native_execution(fn (mut vm VM) !Value { v := vector([integer(0)]) assert vm.call(vm.lookup('run')!, [v,integer(4096)])!.whole() == 12288 scope := unsafe { &NativeScope(vm.native_execution_scope) } assert scope.hits == 1 && scope.deopts == 0 return unspecified() })! } fn test_native_argument_providers_keep_same_code_environments_distinct_and_live() { mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! vm.eval('(define (make bias)(lambda(x)(+ x bias))) (define a (make 7)) (define b (make 19)) (define r (make 0.5)) (define (work f g x)(+ (f x) (g x)))')! vm.with_native_execution(fn (mut vm VM) !Value { root := vm.lookup('work')! a,b := vm.lookup('a')!,vm.lookup('b')! assert a.procedure_pointer().function == b.procedure_pointer().function assert vm.call(root,[a,a,integer(3)])!.whole() == 20 assert vm.call(root,[a,b,integer(3)])!.whole() == 32 assert vm.call(root,[b,a,integer(3)])!.whole() == 32 assert vm.call(root,[vm.lookup('r')!,b,integer(3)])!.floating() == 25.5 hits,deopts := vm.native_execution_stats() assert hits == 4 && deopts == 0 return unspecified() })! } fn test_native_capture_providers_follow_callback_cell_changes_without_global_revision() { mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! vm.eval('(define (make bias)(lambda(x)(+ x bias))) (define a (make 1)) (define b (make 41)) (define c (lambda(x)(* x 10))) (define cells (let ((f a)) (list (lambda(x)(f x)) (lambda(next)(set! f next))))) (define run (car cells)) (define replace (cadr cells))')! vm.with_native_execution(fn (mut vm VM) !Value { scope := unsafe { &NativeScope(vm.native_execution_scope) } revision := scope.binding_revision result := vm.eval('(list (run 1) (begin (replace b) (run 1)) (begin (replace c) (run 1)))')! assert write(result)! == '(2 42 10)' assert scope.binding_revision == revision return unspecified() })! } fn test_native_procedure_result_and_store_restore_vectors_before_replay() { mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! vm.eval('(define (callback x)(+ x 1)) (define (return-it f v)(vector-set! v 0 (+ (vector-ref v 0) 1)) f) (define (store-it f v)(vector-set! v 0 (+ (vector-ref v 0) 1)) (vector-set! v 1 f) (vector-ref v 0))')! vm.with_native_execution(fn (mut vm VM) !Value { callback := vm.lookup('callback')! v := vector([integer(0),boolean(false)]) returned := vm.call(vm.lookup('return-it')!,[callback,v])! assert returned.pointer() == callback.pointer() && v.vector_pointer().items[0].whole() == 1 assert vm.call(vm.lookup('store-it')!,[callback,v])!.whole() == 2 assert v.vector_pointer().items[1].pointer() == callback.pointer() assert vm.call(returned,[integer(41)])!.whole() == 42 _,deopts := vm.native_execution_stats() assert deopts >= 2 return unspecified() })! } fn test_native_mutually_captured_callbacks_have_bounded_graphs_and_depth_fallback() { mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! vm.eval('(define callbacks (letrec ((even (lambda(n)(if (= n 0) #t (odd (- n 1))))) (odd (lambda(n)(if (= n 0) #f (even (- n 1)))))) (list even odd))) (define even (car callbacks))')! vm.with_native_execution(fn (mut vm VM) !Value { assert vm.call(vm.lookup('even')!,[integer(100)])!.is_true() scope := unsafe { &NativeScope(vm.native_execution_scope) } assert scope.plans.len == 1 && scope.plans[0].nodes.len == 2 && scope.hits == 1 assert vm.call(vm.lookup('even')!,[integer(2000)])!.is_true() assert scope.deopts > 0 return unspecified() })! } fn test_native_unsupported_or_foreign_procedure_arguments_retain_vm_semantics() { mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! mut other := new_with_options(Options{max_steps: 0,max_frames: 0})! foreign := other.eval('(lambda(x)(+ x 1))')! vm.eval('(define (ignore f)42) (define (use f x)(f x)) (define (own x)(+ x 1))')! vm.with_native_execution(fn [foreign] (mut vm VM) !Value { assert vm.call(vm.lookup('ignore')!,[foreign])!.whole() == 42 assert vm.call(vm.lookup('ignore')!,[vm.lookup('+')!])!.whole() == 42 assert vm.call(vm.lookup('ignore')!,[vm.lookup('own')!])!.whole() == 42 assert vm.call(vm.lookup('use')!,[vm.lookup('own')!,integer(41)])!.whole() == 42 assert vm.call(vm.lookup('use')!,[vm.lookup('+')!,integer(3)])!.whole() == 3 assert vm.eval('(call/cc (lambda(k)(ignore k)))')!.whole() == 42 assert vm.eval('(call/cc (lambda(k)(use k 17)))')!.whole() == 17 return unspecified() })! } fn test_native_inexact_to_exact_rejects_nonnumbers_before_branches_and_calls() { mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! vm.eval('(define (integer-path)(if (inexact->exact #f) 1 2)) (define (mixed-path)(if (inexact->exact #f) 1.0 2.0)) (define (callback v)(vector-set! v 0 (+ (vector-ref v 0) 10)) 42) (define (bad-call f v)(vector-set! v 0 (+ (vector-ref v 0) 1)) ((inexact->exact f) v))')! vm.with_native_execution(fn (mut vm VM) !Value { for name in ['integer-path','mixed-path'] { if _ := vm.call(vm.lookup(name)!,[]) { return error('non-number conversion bypassed its error') } } v := vector([integer(0)]) if _ := vm.call(vm.lookup('bad-call')!,[vm.lookup('callback')!,v]) { return error('conversion called its non-number argument') } assert v.vector_pointer().items[0].whole() == 1 _,deopts := vm.native_execution_stats() assert deopts >= 3 return unspecified() })! }