module scheme import strings fn test_native_entry_argument_boundary_keeps_nine_argument_fallback() { mut vm := new_with_options(Options{max_steps: 0, max_frames: 0})! vm.eval('(define (eight a b c d e f g h) (+ a b c d e f g h)) (define (nine a b c d e f g h i) (+ a b c d e f g h i))')! vm.with_native_execution(fn (mut vm VM) !Value { assert vm.call(vm.lookup('eight')!, [integer(1), integer(2), integer(3), integer(4), integer(5), integer(6), integer(7), integer(8)])!.whole() == 36 assert vm.call(vm.lookup('nine')!, [integer(1), integer(2), integer(3), integer(4), integer(5), integer(6), integer(7), integer(8), integer(9)])!.whole() == 45 hits, deopts := vm.native_execution_stats() assert hits == 1 && deopts == 0 return unspecified() })! } fn test_native_entry_graph_boundary_keeps_oversized_root_on_vm() { for count in [32, 33] { mut vm := new_with_options(Options{max_steps: 0, max_frames: 0})! mut source := strings.new_builder(2048) for i in 0 .. count { body := if i + 1 == count { '(+ x 1)' } else { '(node${i + 1} x)' } source.writeln('(define (node${i} x) ${body})') } vm.eval(source.str())! root := vm.lookup('node0')! if graph := native_graph(mut vm, root, [integer(41)]) { assert count == 32 && graph.nodes.len == 32 } else { assert count == 33 && err.msg().contains('graph too large') } vm.with_native_execution(fn [root, count] (mut vm VM) !Value { assert vm.call(root, [integer(41)])!.whole() == 42 scope := unsafe { &NativeScope(vm.native_execution_scope) } assert scope.hits == 1 && scope.deopts == 0 assert scope.plans.len == 1 && scope.plans[0].nodes.len == 32 assert scope.rejected.len == count - 32 if count == 33 { assert voidptr(scope.rejected[0]) == voidptr(root.procedure_pointer().function) } return unspecified() })! } } fn test_native_entry_capture_boundary_clears_full_live_prefix() { for count in [1024, 1025] { mut vm := new_with_options(Options{max_steps: 0, max_frames: 0})! mut source := strings.new_builder(4096) source.write_string('(define work (let ((x 17)) (lambda () (begin ') for _ in 0 .. count { source.write_string('x ') } source.write_string('))))') vm.eval(source.str())! root := vm.lookup('work')! if graph := native_graph(mut vm, root, []Value{}) { assert count == 1024 && graph.captures == 1024 assert native_capture_layout(mut vm, graph)!.len == 1024 } else { assert count == 1025 && err.msg().contains('too many native captures') } vm.with_native_execution(fn [root, count] (mut vm VM) !Value { assert vm.call(root, []Value{})!.whole() == 17 scope := unsafe { &NativeScope(vm.native_execution_scope) } assert scope.hits == u64(if count == 1024 { 1 } else { 0 }) assert scope.deopts == 0 for value in scope.capture_values { assert value.k == 0 } for cell in scope.capture_cells { assert isnil(cell.env) } return unspecified() })! } } fn test_native_entry_highest_written_capture_commits_and_releases_cell() { mut vm := new_with_options(Options{max_steps: 0, max_frames: 0})! mut source := strings.new_builder(32768) source.write_string('(define (identity x) x) (define accessors (let (') for i in 0 .. 1024 { source.write_string('(x${i} ${i})') } source.write_string(') (list (lambda () (begin ') for i in 0 .. 1023 { source.write_string('x${i} ') } source.write_string('(set! x1023 42) (identity 42))) (lambda () x1023)))) (define work (car accessors)) (define last (cadr accessors))') vm.eval(source.str())! root := vm.lookup('work')! graph := native_graph(mut vm, root, []Value{})! layout := native_capture_layout(mut vm, graph)! assert graph.captures == 1024 && layout.len == 1024 assert layout[1023].written && layout[1023].alias == 1023 vm.with_native_execution(fn [root] (mut vm VM) !Value { assert vm.call(root, []Value{})!.whole() == 42 scope := unsafe { &NativeScope(vm.native_execution_scope) } assert scope.hits == 1 && scope.deopts == 0 assert scope.plans[0].capture_writes == [1023] for value in scope.capture_values { assert value.k == 0 } for cell in scope.capture_cells { assert isnil(cell.env) } return unspecified() })! gc_collect() assert vm.call(vm.lookup('last')!, []Value{})!.whole() == 42 }