| 1 | module scheme |
| 2 | |
| 3 | import arena |
| 4 | import reader |
| 5 | |
| 6 | struct FusionObservation { |
| 7 | text string |
| 8 | steps int |
| 9 | error string |
| 10 | } |
| 11 | |
| 12 | fn test_primitive_branch_fusion_preserves_results_and_virtual_steps() { |
| 13 | for source, expected in { |
| 14 | '(if (< 1 2) 42 0)': '42' |
| 15 | '(if (< 2 1) 0 42)': '42' |
| 16 | '(let ((x 20)) (if (= x 20) (+ x 22) 0))': '42' |
| 17 | '(if (vector-ref #(#f) 0) 1 42)': '42' |
| 18 | '(if (vector-ref #(0) 0) 42 1)': '42' |
| 19 | '(if (quotient 10 3) 42 1)': '42' |
| 20 | '(define n 0)(if (< 1 0) (set! n 100) (set! n 42)) n': '42' |
| 21 | '(let loop ((n 30)(a 0)) (if (= n 0) a (loop (- n 1) (+ a n))))': '465' |
| 22 | } { |
| 23 | fast := fusion_observe(source, false)! |
| 24 | original := fusion_observe(source, true)! |
| 25 | assert fast.error == '' && fast.text == expected, source |
| 26 | assert fast.text == original.text && fast.steps == original.steps, source |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | fn test_primitive_branch_fusion_keeps_each_budget_boundary_and_error_origin() { |
| 31 | for source in ['(if (< 1 2) 42 0)', '(if (< 2 1) 0 42)', |
| 32 | '(define(f a)(if (< a 2) 42 0))(list(f 1)(f 3))', |
| 33 | '(if (vector-ref #(#f) 0) 1 42)', '(if (vector-ref #(0) 0) 42 1)', |
| 34 | '(let((a 1))(if (< a x) 1 2)(define x 2))', '(if (< #f 2) 1 2)', |
| 35 | '(if (vector-ref #(1) 1) 1 2)', '(if (quotient 1 0) 1 2)'] { |
| 36 | for limit in 0 .. 56 { |
| 37 | fast := fusion_observe_limit(source, false, limit)! |
| 38 | original := fusion_observe_limit(source, true, limit)! |
| 39 | assert fast.text == original.text && fast.error == original.error, '${source}/${limit}' |
| 40 | assert fast.steps == original.steps, '${source}/${limit}' |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | fn test_primitive_branch_fusion_observes_mutable_values_and_live_bindings() { |
| 46 | mut vm := new_with_options(Options{max_steps: 0, max_frames: 0})! |
| 47 | vm.eval('(define v(vector #f))(define old-ref vector-ref) |
| 48 | (define(test)(if (vector-ref v 0) 1 2))')! |
| 49 | assert vm.eval('(test)')!.whole() == 2 |
| 50 | vm.eval('(vector-set! v 0 0)')! |
| 51 | assert vm.eval('(test)')!.whole() == 1 |
| 52 | vm.eval('(set! vector-ref(lambda(v i)#f))')! |
| 53 | assert vm.eval('(test)')!.whole() == 2 |
| 54 | assert vm.eval('(if (old-ref v 0) 1 2)')!.whole() == 1 |
| 55 | } |
| 56 | |
| 57 | // The bounded owner disables optimized primitive reads. Observe its machine |
| 58 | // directly so result, located error and virtual charge counts remain comparable. |
| 59 | fn read_observe_bounded(source string, limit int) !FusionObservation { |
| 60 | mut bounded := new_bounded(Options{}, arena.ChunkLimits{max_live_bytes: 32 * 1024 * 1024})! |
| 61 | defer { bounded.close() or { panic(err) } } |
| 62 | observed := bounded.run(fn [source, limit] (mut vm VM) !Value { |
| 63 | vm.options = Options{...vm.options, max_steps: limit, max_frames: 0} |
| 64 | context := vm.next_context() |
| 65 | function := vm.new_source_program(reader.read_all(source)!, context, unsafe { nil }, '', false)! |
| 66 | mut machine := Machine{function: function, context: context, logical_context: context} |
| 67 | entry := vm.enter_program(mut machine)! |
| 68 | defer { vm.leave_entry(entry) } |
| 69 | mut message := '' |
| 70 | value := machine.run(mut vm) or { message = err.msg(); unspecified() } |
| 71 | return vector([integer(machine.steps), string_value(message), string_value(if message == '' { write(value)! } else { '' })]) |
| 72 | })! |
| 73 | items := observed.vector_pointer().items |
| 74 | return FusionObservation{steps: int(items[0].whole()), error: items[1].as_string()!, text: items[2].as_string()!} |
| 75 | } |
| 76 | |
| 77 | fn test_read_primitive_entry_matches_individual_instructions() { |
| 78 | for source in [ |
| 79 | "(list (car '(20 22)) (cdr '(20 22)) (null? '()) (pair? '(1)) (not #f))", |
| 80 | "(list (boolean? #t) (string? \"λ\") (symbol? 'a) (char? #\\λ) (vector? #(1)) (bytevector? #u8(1)))", |
| 81 | '(list (string-length "aλ😀") (vector-length #(1 2)) (bytevector-length #u8(1 2 3)) (char->integer #\\λ))', |
| 82 | '(list (string-ref "aλ😀" 0) (string-ref "aλ😀" 2) (string-ref "aλ😀" 1) (bytevector-u8-ref #u8(0 255) 1))', |
| 83 | "(define(f get x)(get x))(list(f car '(42)) (f cdr '(1 2)) (f string-length \"ab\"))", |
| 84 | '(define(f get x i)(get x i))(list(f string-ref "λ😀" 1)(f bytevector-u8-ref #u8(42) 0))', |
| 85 | '(list(car(cons(if #f #t) 1))(cdr(cons 1(if #f #t))))', |
| 86 | ] { |
| 87 | fast := fusion_observe(source, false)! |
| 88 | plain := read_observe_bounded(source, 0)! |
| 89 | assert fast.error == '' && fast.text == plain.text, source |
| 90 | assert fast.steps == plain.steps, source |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | fn test_read_primitive_entry_keeps_errors_and_each_step_boundary() { |
| 95 | for source in ['(string-ref "λ" 1)', '(string-ref "λ" -1)', '(string-ref "λ" 4294967296)', |
| 96 | '(string-ref "λ" 0.0)', '(string-ref #f 0)', '(string-ref "λ")', |
| 97 | '(bytevector-u8-ref #u8(1) 4294967296)', '(bytevector-u8-ref #u8(1) -1)', |
| 98 | '(bytevector-u8-ref #u8(1) 0.0)', '(bytevector-u8-ref #f 0)', '(bytevector-u8-ref #u8(1))', |
| 99 | '(car 1)', '(cdr 1)', '(char->integer 1)', '(vector-length #f)', |
| 100 | '(string-length #f)', '(bytevector-length #f)', '(not)', '(null? 1 2)', |
| 101 | '(define(f n)(car x)(define x n))(f 1)', |
| 102 | '(define(f n)(string-ref "aλ😀" n))(list(f 2)(f 0)(f 1))', |
| 103 | "(define(f x)(car x))(list 10(f '(42)) 20)"] { |
| 104 | for limit in 0 .. 48 { |
| 105 | fast := fusion_observe_limit(source, false, limit)! |
| 106 | plain := read_observe_bounded(source, limit)! |
| 107 | assert fast.text == plain.text && fast.error == plain.error, '${source}/${limit}' |
| 108 | assert fast.steps == plain.steps, '${source}/${limit}' |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | fn test_read_primitive_entry_observes_mutation_aliases_and_rebinding() { |
| 114 | mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! |
| 115 | vm.eval('(define s(string-copy "aλ😀"))(define b(bytevector 1 2))(define p(cons s b)) |
| 116 | (define old-car car)(define old-ref string-ref)(define(f i)(string-ref s i))')! |
| 117 | assert vm.eval('(f 2)')!.as_character()! == `😀` |
| 118 | vm.eval('(string-set! s 0 #\\😀)(string-set! s 1 #\\x)(bytevector-u8-set! b 0 255)')! |
| 119 | assert write(vm.eval('(list(f 0)(f 1)(f 2)(bytevector-u8-ref(cdr p)0))')!)! == '(#\\😀 #\\x #\\😀 255)' |
| 120 | vm.eval('(set! string-ref(lambda(s i)42))(set! car(lambda(p)99))')! |
| 121 | assert vm.eval('(f 1)')!.whole() == 42 |
| 122 | assert vm.eval('(car p)')!.whole() == 99 |
| 123 | assert vm.eval('(old-car p)')!.pointer() == vm.lookup('s')!.pointer() |
| 124 | assert vm.eval('(old-ref s 1)')!.as_character()! == `x` |
| 125 | gc_collect() |
| 126 | assert vm.eval('(old-car p)')!.pointer() == vm.lookup('s')!.pointer() |
| 127 | } |
| 128 | |
| 129 | fn test_read_primitive_entry_preserves_foreign_owners_and_shared_frontend() { |
| 130 | mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! |
| 131 | vm.eval_alt('work := fn(s,i) { char->integer(string-ref(s,i)) }')! |
| 132 | assert vm.call(vm.lookup('work')!,[string_value('aλ'),integer(1)])!.whole() == 955 |
| 133 | mut other := new_with_options(Options{max_steps: 0,max_frames: 0})! |
| 134 | vm.define('foreign',other.lookup('car')!)! |
| 135 | mut message := '' |
| 136 | vm.eval("(foreign '(42))") or { message = err.msg(); unspecified() } |
| 137 | assert message.contains('another VM') |
| 138 | } |
| 139 | |
| 140 | // SingleMachineStep executes the original instruction sequence and cannot |
| 141 | // enter fusion. Compare both its result and its exact virtual step count. |
| 142 | fn fusion_observe(source string, single bool) !FusionObservation { |
| 143 | return fusion_observe_limit(source, single, 0) |
| 144 | } |
| 145 | |
| 146 | fn fusion_observe_limit(source string, single bool, limit int) !FusionObservation { |
| 147 | return fusion_observe_controls(source, single, limit, 0) |
| 148 | } |
| 149 | |
| 150 | fn fusion_observe_controls(source string, single bool, limit int, frames int) !FusionObservation { |
| 151 | mut vm := new_with_options(Options{max_steps: limit, max_frames: frames})! |
| 152 | context := vm.next_context() |
| 153 | function := vm.new_source_program(reader.read_all(source)!, context, unsafe { nil }, '', false)! |
| 154 | original := function.instructions.clone() |
| 155 | mut machine := Machine{function: function, context: context, logical_context: context} |
| 156 | entry := vm.enter_program(mut machine)! |
| 157 | defer { vm.leave_entry(entry) } |
| 158 | mut message := '' |
| 159 | mut value := unspecified() |
| 160 | if single { |
| 161 | for !machine.finished { |
| 162 | assert machine.steps < 100000 |
| 163 | ins := machine.function.instructions[machine.ip] |
| 164 | machine.ip++ |
| 165 | machine.origin = ins.origin |
| 166 | machine.charge(limit) or { message = machine.located(err).msg(); break } |
| 167 | machine.execute(mut vm, ins) or { message = machine.located(err).msg(); break } |
| 168 | } |
| 169 | if message == '' { value = machine.stack[0] } |
| 170 | } else { |
| 171 | value = machine.run(mut vm) or { message = err.msg(); unspecified() } |
| 172 | } |
| 173 | assert function.instructions == original |
| 174 | return FusionObservation{text: if message == '' { write(value)! } else { '' }, steps: machine.steps, error: message} |
| 175 | } |
| 176 | |
| 177 | fn test_cached_closure_entry_matches_original_calls_and_virtual_steps() { |
| 178 | for source in [ |
| 179 | '(define(f n)n)(list (f 1)(f 2)(f 3))', |
| 180 | '(define(f)42)(list(f)(f)(f))', |
| 181 | '(define(f n a)(if (= n 0) a (f (- n 1) (+ a n))))(f 30 0)', |
| 182 | '(define(a n)(if (= n 0) 42 (b (- n 1) 7)))(define(b n x)(a n))(a 20)', |
| 183 | '(define(make n)(lambda(x)(+ x n)))(define a(make 10))(define b(make 100))(list(a 1)(b 2)(a 3))', |
| 184 | '(define(f n)(+ n 1))(list 100 (f 41) 200 (f 0))', |
| 185 | '(define(f a b c d e f g h)(list a b c d e f g h))(list(f 1 2 3 4 5 6 7 8)(f 8 7 6 5 4 3 2 1))', |
| 186 | '(define(f n)(if (= n 0) 1 (+ (f (- n 1)) (f (- n 1)))))(f 5)', |
| 187 | '(define(f n)(call-with-values(lambda()(values n 1))(lambda(a b)(+ a b))))(list(f 20)(f 30))', |
| 188 | '((lambda args args) 1 2 3)', |
| 189 | ] { |
| 190 | fast := fusion_observe(source,false)! |
| 191 | plain := fusion_observe(source,true)! |
| 192 | assert fast.error == '' && fast.text == plain.text, source |
| 193 | assert fast.steps == plain.steps, source |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | fn test_cached_closure_entry_preserves_step_and_frame_limit_errors() { |
| 198 | for source in ['(define(f n)(+ n 1))(list(f 20)(f 21))', |
| 199 | '(define(f n)(if (= n 0) 42 (+ 1 (f (- n 1)))))(f 4)', |
| 200 | '(define(f a b)(+ a b))(f 1)', |
| 201 | '(define(f n)(+ n x)(define x 1))(f 2)'] { |
| 202 | for steps in [0,1,5,10,20,30,40,60,100] { |
| 203 | for frames in [0,1,2,3] { |
| 204 | fast := fusion_observe_controls(source,false,steps,frames)! |
| 205 | plain := fusion_observe_controls(source,true,steps,frames)! |
| 206 | assert fast.text == plain.text && fast.error == plain.error, '${source}/${steps}/${frames}' |
| 207 | assert fast.steps == plain.steps, '${source}/${steps}/${frames}' |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | fn test_cached_closure_entry_keeps_escaped_activations_and_gc_roots() { |
| 214 | mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! |
| 215 | vm.eval('(define saved (list))(define(f n)(if (= n 0) 0 |
| 216 | (begin(set! saved(cons(lambda()n) saved))(f (- n 1)))))(f 8)')! |
| 217 | gc_collect() |
| 218 | assert write(vm.eval('(map (lambda(f)(f)) saved)')!)! == '(1 2 3 4 5 6 7 8)' |
| 219 | vm.eval('(define(make v)(lambda(x)(vector v x)))(define a(make 10))(define b(make 100))')! |
| 220 | for i in 0 .. 20 { |
| 221 | value := vm.call(vm.lookup(if i % 2 == 0 { 'a' } else { 'b' })!,[integer(i)])! |
| 222 | gc_collect() |
| 223 | assert value.vector_pointer().items[0].whole() == if i % 2 == 0 { 10 } else { 100 } |
| 224 | assert value.vector_pointer().items[1].whole() == i |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | fn test_cached_closure_entry_reinitializes_internal_definition_slots() { |
| 229 | for source in ['(define(f n)(define x (+ n 1))(if (= n 0) x (f (- n 1))))(f 10)', |
| 230 | '(define(f n)(if (= n 1) x 0)(define x n)(if (= n 0) x (f (- n 1))))(f 3)'] { |
| 231 | fast := fusion_observe(source,false)! |
| 232 | plain := fusion_observe(source,true)! |
| 233 | assert fast.text == plain.text && fast.error == plain.error |
| 234 | assert fast.steps == plain.steps |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | fn test_cached_closure_entry_handles_vile_calls_and_live_rebinding() { |
| 239 | mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! |
| 240 | vm.eval_alt('inc := fn(n) { n + 1 }\nwork := fn(n) { inc(n) + inc(n) }')! |
| 241 | assert vm.call(vm.lookup('work')!,[integer(20)])!.whole() == 42 |
| 242 | vm.eval_alt('inc = fn(n) { n + 10 }')! |
| 243 | assert vm.call(vm.lookup('work')!,[integer(20)])!.whole() == 60 |
| 244 | } |
| 245 | |
| 246 | fn test_primitive_fusion_matches_every_limited_instruction_boundary() { |
| 247 | for source in ['(define(f a b)(+ a b))(list (f 20 22) (f 1 2))', |
| 248 | '(define v (vector 42))(define(f i)(vector-ref v i))(f 0)', |
| 249 | '(define(f n)(+ n x)(define x 1))(f 2)', '(quotient 10 0)'] { |
| 250 | for limit in 1 .. 48 { |
| 251 | fast := fusion_observe_limit(source, false, limit)! |
| 252 | plain := fusion_observe_limit(source, true, limit)! |
| 253 | assert fast.text == plain.text && fast.error == plain.error, '${source}, limit ${limit}' |
| 254 | assert fast.steps == plain.steps, '${source}, limit ${limit}' |
| 255 | } |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | fn test_primitive_fusion_avoids_temporary_operand_stack_growth() { |
| 260 | for limit in [0, 100] { |
| 261 | mut vm := new_with_options(Options{max_steps: limit, max_frames: 0})! |
| 262 | vm.eval('(define plus +)')! |
| 263 | f := vm.eval('(lambda()(plus 20 22))')! |
| 264 | assert vm.call(f,[])!.whole() == 42 |
| 265 | context := vm.next_context() |
| 266 | mut machine := Machine{function: f.procedure_pointer().function, context: context, logical_context: context} |
| 267 | entry := vm.enter_program(mut machine)! |
| 268 | machine.stack = []Value{cap: 1} |
| 269 | storage := machine.stack.data |
| 270 | result := machine.run(mut vm)! |
| 271 | vm.leave_entry(entry) |
| 272 | assert result.whole() == 42 |
| 273 | assert machine.stack.cap == 1 && machine.stack.data == storage |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | fn test_primitive_fusion_matches_original_instruction_results_and_steps() { |
| 278 | for source in [ |
| 279 | '(+ 20 22)', |
| 280 | '(define x 20)(define y 22)(+ x y)', |
| 281 | '((lambda(a b)(+ a b)) 20 22)', |
| 282 | '((lambda(a)(lambda(b)(+ a b))) 20)', |
| 283 | '(let ((a 20)) ((lambda(b)(+ a b)) 22))', |
| 284 | '(list 1 (+ 20 22) (* 6 7) (< 1 2) (= 1 2))', |
| 285 | '(define(f n a)(if (= n 0) a (f (- n 1) (+ a n))))(f 30 0)', |
| 286 | '(list (quotient -10 3) (remainder -10 3) (modulo 10 -3))', |
| 287 | '(+ 9223372036854775807 1)', |
| 288 | '(quotient -9223372036854775808 -1)', |
| 289 | '(define v (vector 20 22))(list (vector-ref v 0) (vector-ref v 1))', |
| 290 | '(let ((v (vector 42))) ((lambda(i)(vector-ref v i)) 0))', |
| 291 | ] { |
| 292 | fast := fusion_observe(source, false)! |
| 293 | plain := fusion_observe(source, true)! |
| 294 | assert fast.error == '' && fast.text == plain.text, source |
| 295 | assert fast.steps == plain.steps, '${source}: ${fast.steps} != ${plain.steps}' |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | fn test_primitive_fusion_preserves_fallback_errors_and_source_locations() { |
| 300 | for source in [ |
| 301 | '(define(f n)(+ n x)(define x 1))\n(f 2)', |
| 302 | '(quotient 10 0)', '(remainder 10 0)', '(modulo 10 0)', |
| 303 | '(vector-ref #(1) -1)', '(vector-ref #(1) 1)', |
| 304 | '(vector-ref 1 0)', '(vector-ref #(1) 0.0)', |
| 305 | '(+ 1 #f)', '(define other #f)(other 1 2)', |
| 306 | ] { |
| 307 | fast := fusion_observe(source, false)! |
| 308 | plain := fusion_observe(source, true)! |
| 309 | assert fast.error != '' && fast.error == plain.error, source |
| 310 | assert fast.steps == plain.steps, source |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | fn test_primitive_fusion_observes_live_globals_and_saved_aliases() { |
| 315 | mut vm := new_with_options(Options{max_steps: 0, max_frames: 0})! |
| 316 | vm.eval('(define saved +)(define x 20)(define y 22)(define(f)(+ x y))')! |
| 317 | assert vm.eval('(f)')!.whole() == 42 |
| 318 | vm.eval('(set! y 30)')! |
| 319 | assert vm.eval('(f)')!.whole() == 50 |
| 320 | vm.eval('(set! + (lambda(a b)99))')! |
| 321 | assert vm.eval('(f)')!.whole() == 99 |
| 322 | assert vm.eval('(saved x y)')!.whole() == 50 |
| 323 | assert vm.eval('(let ((saved (lambda(a b)77)))(saved x y))')!.whole() == 77 |
| 324 | vm.eval('(set! + saved)')! |
| 325 | assert vm.eval('(f)')!.whole() == 50 |
| 326 | } |
| 327 | |
| 328 | fn test_primitive_fusion_preserves_vector_identity_values_and_tail_returns() { |
| 329 | mut vm := new_with_options(Options{max_steps: 0, max_frames: 0})! |
| 330 | vm.eval('(define original vector-ref)(define v (vector 0))(define(f i)(vector-ref v i))')! |
| 331 | for value in [unspecified(), nil_value(), boolean(false), integer(42), real(1.5), |
| 332 | string_value('kept λ'), cons(integer(1),nil_value()), vm.lookup('+')!] { |
| 333 | vm.define('item',value)! |
| 334 | vm.eval('(vector-set! v 0 item)')! |
| 335 | gc_collect() |
| 336 | actual := vm.eval('(list (f 0) (original v 0))')!.pair_pointer().car |
| 337 | assert actual.tag == value.tag |
| 338 | assert write(actual)! == write(value)! |
| 339 | if value.tag in [.pair,.string,.procedure] { assert actual.pointer() == value.pointer() } |
| 340 | } |
| 341 | vm.eval('(set! vector-ref (lambda(v i)99))')! |
| 342 | assert vm.eval('(f 0)')!.whole() == 99 |
| 343 | assert vm.eval('(original v 0)')!.pointer() == vm.lookup('+')!.pointer() |
| 344 | } |
| 345 | |
| 346 | fn test_primitive_fusion_keeps_side_effect_order_and_continuation_reentry() { |
| 347 | mut vm := new_with_options(Options{max_steps: 0, max_frames: 0})! |
| 348 | vm.eval('(define count 0)(define saved #f)(define(tick n)(set! count (+ count 1)) n)')! |
| 349 | assert vm.eval('(+ (tick 20) (tick 22))')!.whole() == 42 |
| 350 | assert vm.eval('count')!.whole() == 2 |
| 351 | assert vm.eval('(+ (call/cc (lambda(k)(set! saved k)20)) 22)')!.whole() == 42 |
| 352 | assert vm.call(vm.lookup('saved')!,[integer(30)])!.whole() == 52 |
| 353 | assert vm.eval('count')!.whole() == 2 |
| 354 | } |
| 355 | |
| 356 | fn test_primitive_fusion_uses_shared_vile_frontend_and_rejects_foreign_owners() { |
| 357 | mut vm := new_with_options(Options{max_steps: 0, max_frames: 0})! |
| 358 | vm.eval_alt('work := fn(v,i) { vector-ref(v,i) + i }')! |
| 359 | assert vm.call(vm.lookup('work')!,[vector([integer(41)]),integer(0)])!.whole() == 41 |
| 360 | mut other := new_with_options(Options{max_steps: 0, max_frames: 0})! |
| 361 | vm.define('foreign', other.lookup('+')!)! |
| 362 | vm.eval('(define(f a b)(foreign a b))')! |
| 363 | mut message := '' |
| 364 | vm.eval('(f 20 22)') or { message = err.msg(); unspecified() } |
| 365 | assert message.contains('another VM') |
| 366 | } |
| 367 | |
| 368 | fn test_primitive_fusion_leaves_limited_and_bounded_execution_equivalent() { |
| 369 | mut plain := new_with_options(Options{max_steps: 0, max_frames: 0})! |
| 370 | mut bounded := new_bounded(Options{},arena.ChunkLimits{max_live_bytes: 32 * 1024 * 1024})! |
| 371 | defer { bounded.close() or { panic(err) } } |
| 372 | for source in ['(+ 20 22)', '(let((a 20))(+ a 22))', |
| 373 | '(vector-ref #(42) 0)', '(quotient -10 3)', '(+ 9223372036854775807 1)', |
| 374 | '(define(f n)(if (= n 0) 42 (f (- n 1))))(f 3)'] { |
| 375 | for budget in 0 .. 40 { |
| 376 | plain.options = Options{...plain.options,max_steps: budget} |
| 377 | mut expected_error := '' |
| 378 | expected := plain.eval(source) or { expected_error = err.msg(); unspecified() } |
| 379 | mut actual_error := '' |
| 380 | actual := bounded.run(fn [source,budget] (mut vm VM) !Value { |
| 381 | vm.options = Options{...vm.options,max_steps: budget} |
| 382 | return vm.eval(source) |
| 383 | }) or { actual_error = err.msg(); unspecified() } |
| 384 | assert actual_error == expected_error |
| 385 | if expected_error == '' { assert write(actual)! == write(expected)! } |
| 386 | } |
| 387 | } |
| 388 | } |
| 389 | |