| 1 | module scheme |
| 2 | |
| 3 | import arena |
| 4 | import reader |
| 5 | |
| 6 | struct MapEntryObservation { |
| 7 | text string |
| 8 | message string |
| 9 | steps int |
| 10 | } |
| 11 | |
| 12 | fn map_entry_observe_machine(mut vm VM, source string, limit int, frames int) !Value { |
| 13 | vm.options = Options{...vm.options, max_steps: limit, max_frames: frames} |
| 14 | context := vm.next_context() |
| 15 | function := vm.new_source_program(reader.read_all(source)!, context, unsafe { nil }, '', false)! |
| 16 | mut machine := Machine{function: function, context: context, logical_context: context} |
| 17 | entry := vm.enter_program(mut machine)! |
| 18 | defer { vm.leave_entry(entry) } |
| 19 | mut message := '' |
| 20 | value := machine.run(mut vm) or { message = err.msg(); unspecified() } |
| 21 | return vector([integer(machine.steps), string_value(message), string_value(if message == '' { write(value)! } else { '' })]) |
| 22 | } |
| 23 | |
| 24 | fn map_entry_observe(source string, bounded_reference bool, limit int, frames int) !MapEntryObservation { |
| 25 | if bounded_reference { |
| 26 | mut bounded := new_bounded(Options{}, arena.ChunkLimits{max_live_bytes: 32 * 1024 * 1024})! |
| 27 | defer { bounded.close() or { panic(err) } } |
| 28 | result := bounded.run(fn [source, limit, frames] (mut vm VM) !Value { |
| 29 | return map_entry_observe_machine(mut vm, source, limit, frames) |
| 30 | })! |
| 31 | items := result.vector_pointer().items |
| 32 | return MapEntryObservation{steps: int(items[0].whole()), message: items[1].as_string()!, text: items[2].as_string()!} |
| 33 | } |
| 34 | mut vm := new_with_options(Options{max_steps: 0, max_frames: 0})! |
| 35 | items := map_entry_observe_machine(mut vm, source, limit, frames)!.vector_pointer().items |
| 36 | return MapEntryObservation{steps: int(items[0].whole()), message: items[1].as_string()!, text: items[2].as_string()!} |
| 37 | } |
| 38 | |
| 39 | fn test_map_callback_entry_matches_full_application_results_errors_and_budgets() { |
| 40 | for source in ["(map(lambda(x)(+ x 1)) '(1 2 3))", |
| 41 | "(let((bias 10))(map(lambda(x)(+ bias x)) '(1 2 3)))", |
| 42 | "(map(lambda(x)(map(lambda(y)(+ x y)) '(1 2))) '(10 20))", |
| 43 | "(map(lambda(x y)(+ x y)) '(1 2) '(10 20))", |
| 44 | "(map(lambda(x y)(+ x y)) '(1))"] { |
| 45 | for frames in [0, 1, 2] { |
| 46 | for limit in 0 .. 40 { |
| 47 | actual := map_entry_observe(source, false, limit, frames)! |
| 48 | reference := map_entry_observe(source, true, limit, frames)! |
| 49 | assert actual.text == reference.text && actual.message == reference.message, '${source}/${limit}/${frames}' |
| 50 | assert actual.steps == reference.steps, '${source}/${limit}/${frames}' |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | fn test_map_callback_entry_keeps_multishot_bridge_state_and_fresh_locations() { |
| 57 | mut vm := new_with_options(Options{max_steps: 0, max_frames: 0})! |
| 58 | vm.eval('(define saved #f)(define(work) |
| 59 | (map(lambda(x)(if (= x 2)(call/cc(lambda(k)(set! saved k) x)) x)) (list 1 2 3)))')! |
| 60 | assert write(vm.call(vm.lookup('work')!, [])!)! == '(1 2 3)' |
| 61 | gc_collect() |
| 62 | assert write(vm.call(vm.lookup('saved')!, [integer(20)])!)! == '(1 20 3)' |
| 63 | gc_collect() |
| 64 | assert write(vm.call(vm.lookup('saved')!, [integer(30)])!)! == '(1 30 3)' |
| 65 | vm.eval('(define cells(map(lambda(x)(lambda(d)(set! x(+ x d)) x)) (list 1 10 100)))')! |
| 66 | gc_collect() |
| 67 | assert write(vm.eval('(map(lambda(f)(f 2)) cells)')!)! == '(3 12 102)' |
| 68 | assert write(vm.eval('(map(lambda(f)(f 3)) cells)')!)! == '(6 15 105)' |
| 69 | } |
| 70 | |
| 71 | fn test_map_callback_entry_preserves_builtin_rest_and_foreign_procedure_paths() { |
| 72 | mut vm := new_with_options(Options{max_steps: 0, max_frames: 0})! |
| 73 | assert write(vm.eval("(list(map + '(1 2 3))(map(lambda xs xs) '(1 2)))")!)! == '((1 2 3) ((1) (2)))' |
| 74 | mut other := new() |
| 75 | vm.define('foreign', other.eval('(lambda(x)x)')!)! |
| 76 | mut message := '' |
| 77 | vm.eval("(map foreign '(1))") or { message = err.msg(); unspecified() } |
| 78 | assert message.contains('another VM') |
| 79 | } |
| 80 | |