module scheme import arena import reader struct MapEntryObservation { text string message string steps int } fn map_entry_observe_machine(mut vm VM, source string, limit int, frames int) !Value { vm.options = Options{...vm.options, max_steps: limit, max_frames: frames} context := vm.next_context() function := vm.new_source_program(reader.read_all(source)!, context, unsafe { nil }, '', false)! mut machine := Machine{function: function, context: context, logical_context: context} entry := vm.enter_program(mut machine)! defer { vm.leave_entry(entry) } mut message := '' value := machine.run(mut vm) or { message = err.msg(); unspecified() } return vector([integer(machine.steps), string_value(message), string_value(if message == '' { write(value)! } else { '' })]) } fn map_entry_observe(source string, bounded_reference bool, limit int, frames int) !MapEntryObservation { if bounded_reference { mut bounded := new_bounded(Options{}, arena.ChunkLimits{max_live_bytes: 32 * 1024 * 1024})! defer { bounded.close() or { panic(err) } } result := bounded.run(fn [source, limit, frames] (mut vm VM) !Value { return map_entry_observe_machine(mut vm, source, limit, frames) })! items := result.vector_pointer().items return MapEntryObservation{steps: int(items[0].whole()), message: items[1].as_string()!, text: items[2].as_string()!} } mut vm := new_with_options(Options{max_steps: 0, max_frames: 0})! items := map_entry_observe_machine(mut vm, source, limit, frames)!.vector_pointer().items return MapEntryObservation{steps: int(items[0].whole()), message: items[1].as_string()!, text: items[2].as_string()!} } fn test_map_callback_entry_matches_full_application_results_errors_and_budgets() { for source in ["(map(lambda(x)(+ x 1)) '(1 2 3))", "(let((bias 10))(map(lambda(x)(+ bias x)) '(1 2 3)))", "(map(lambda(x)(map(lambda(y)(+ x y)) '(1 2))) '(10 20))", "(map(lambda(x y)(+ x y)) '(1 2) '(10 20))", "(map(lambda(x y)(+ x y)) '(1))"] { for frames in [0, 1, 2] { for limit in 0 .. 40 { actual := map_entry_observe(source, false, limit, frames)! reference := map_entry_observe(source, true, limit, frames)! assert actual.text == reference.text && actual.message == reference.message, '${source}/${limit}/${frames}' assert actual.steps == reference.steps, '${source}/${limit}/${frames}' } } } } fn test_map_callback_entry_keeps_multishot_bridge_state_and_fresh_locations() { mut vm := new_with_options(Options{max_steps: 0, max_frames: 0})! vm.eval('(define saved #f)(define(work) (map(lambda(x)(if (= x 2)(call/cc(lambda(k)(set! saved k) x)) x)) (list 1 2 3)))')! assert write(vm.call(vm.lookup('work')!, [])!)! == '(1 2 3)' gc_collect() assert write(vm.call(vm.lookup('saved')!, [integer(20)])!)! == '(1 20 3)' gc_collect() assert write(vm.call(vm.lookup('saved')!, [integer(30)])!)! == '(1 30 3)' vm.eval('(define cells(map(lambda(x)(lambda(d)(set! x(+ x d)) x)) (list 1 10 100)))')! gc_collect() assert write(vm.eval('(map(lambda(f)(f 2)) cells)')!)! == '(3 12 102)' assert write(vm.eval('(map(lambda(f)(f 3)) cells)')!)! == '(6 15 105)' } fn test_map_callback_entry_preserves_builtin_rest_and_foreign_procedure_paths() { mut vm := new_with_options(Options{max_steps: 0, max_frames: 0})! assert write(vm.eval("(list(map + '(1 2 3))(map(lambda xs xs) '(1 2)))")!)! == '((1 2 3) ((1) (2)))' mut other := new() vm.define('foreign', other.eval('(lambda(x)x)')!)! mut message := '' vm.eval("(map foreign '(1))") or { message = err.msg(); unspecified() } assert message.contains('another VM') }