| 1 | module scheme |
| 2 | |
| 3 | fn native_values_root_enabled(vm &VM, name string) !bool { |
| 4 | scope := unsafe { &NativeScope(vm.native_execution_scope) } |
| 5 | root := vm.lookup(name)!.procedure_pointer().function |
| 6 | return scope.plans.any(voidptr(it.root) == voidptr(root) && !it.disabled) |
| 7 | } |
| 8 | |
| 9 | fn test_native_values_fixed_arities_and_scalar_producer() { |
| 10 | mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! |
| 11 | vm.eval('(define (zero)(call-with-values (lambda()(values)) (lambda()42))) |
| 12 | (define (one n)(call-with-values (lambda()(values n)) (lambda(a)(+ a 1)))) |
| 13 | (define (many n)(call-with-values (lambda()(values n 2 3 4 5 6 7 8)) (lambda(a b c d e f g h)(+ a b c d e f g h)))) |
| 14 | (define (scalar n)(call-with-values (lambda() (+ n 1)) (lambda(a)(+ a 1))))')! |
| 15 | vm.with_native_execution(fn (mut vm VM) !Value { |
| 16 | assert vm.call(vm.lookup('zero')!,[])!.whole() == 42 |
| 17 | assert vm.call(vm.lookup('one')!,[integer(40)])!.whole() == 41 |
| 18 | assert vm.call(vm.lookup('many')!,[integer(7)])!.whole() == 42 |
| 19 | assert vm.call(vm.lookup('scalar')!,[integer(40)])!.whole() == 42 |
| 20 | for name in ['zero','one','many','scalar'] { assert native_values_root_enabled(vm,name)! } |
| 21 | hits,deopts := vm.native_execution_stats() |
| 22 | assert hits == 4 && deopts == 0 |
| 23 | return unspecified() |
| 24 | })! |
| 25 | } |
| 26 | |
| 27 | fn test_native_values_order_and_shared_parent_locations() { |
| 28 | mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! |
| 29 | vm.eval('(define (work n) |
| 30 | (call-with-values |
| 31 | (lambda()(values (begin (set! n (+ n 1)) n) (begin (set! n (* n 10)) n))) |
| 32 | (lambda(a b)(+ (* a 100) b n))))')! |
| 33 | vm.with_native_execution(fn (mut vm VM) !Value { |
| 34 | assert vm.call(vm.lookup('work')!,[integer(3)])!.whole() == 480 |
| 35 | assert native_values_root_enabled(vm,'work')! |
| 36 | return unspecified() |
| 37 | })! |
| 38 | } |
| 39 | |
| 40 | fn test_native_values_nested_and_pending_operand_work() { |
| 41 | mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! |
| 42 | vm.eval('(define (work n)(+ 100 (* 2 |
| 43 | (call-with-values |
| 44 | (lambda()(call-with-values (lambda()(values n 3)) (lambda(a b)(values (+ a b) (* a b))))) |
| 45 | (lambda(a b)(+ a b)))) 7))')! |
| 46 | vm.with_native_execution(fn (mut vm VM) !Value { |
| 47 | assert vm.call(vm.lookup('work')!,[integer(5)])!.whole() == 153 |
| 48 | assert native_values_root_enabled(vm,'work')! |
| 49 | return unspecified() |
| 50 | })! |
| 51 | } |
| 52 | |
| 53 | fn test_native_values_tail_loop_and_private_helpers() { |
| 54 | mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! |
| 55 | vm.eval('(define (work n)(let loop ((i n)(acc 0)) (if (= i 0) acc |
| 56 | (loop (- i 1) (+ acc (call-with-values |
| 57 | (lambda() (let ((x (+ i 1))) (values i x))) |
| 58 | (lambda(a b)(let ((c (+ a b))) c))))))))')! |
| 59 | vm.with_native_execution(fn (mut vm VM) !Value { |
| 60 | assert vm.call(vm.lookup('work')!,[integer(1000)])!.whole() == 1002000 |
| 61 | hits,deopts := vm.native_execution_stats() |
| 62 | assert hits == 1 && deopts == 0 |
| 63 | return unspecified() |
| 64 | })! |
| 65 | } |
| 66 | |
| 67 | fn test_native_values_same_code_capture_providers_refresh_after_gc() { |
| 68 | mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! |
| 69 | vm.eval('(define (make offset)(lambda(n)(call-with-values (lambda()(values n offset)) (lambda(a b)(+ a b))))) |
| 70 | (define a (make 10)) (define b (make 100))')! |
| 71 | vm.with_native_execution(fn (mut vm VM) !Value { |
| 72 | assert vm.call(vm.lookup('a')!,[integer(2)])!.whole() == 12 |
| 73 | assert vm.call(vm.lookup('b')!,[integer(3)])!.whole() == 103 |
| 74 | vm.eval('(set! a (make 1000))')! |
| 75 | $if gcboehm ? { gc_collect() } |
| 76 | assert vm.call(vm.lookup('a')!,[integer(4)])!.whole() == 1004 |
| 77 | assert native_values_root_enabled(vm,'a')! |
| 78 | _,deopts := vm.native_execution_stats() |
| 79 | assert deopts == 0 |
| 80 | return unspecified() |
| 81 | })! |
| 82 | } |
| 83 | |
| 84 | fn test_native_values_aliases_and_builtin_rebinding_guards() { |
| 85 | mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! |
| 86 | vm.eval('(define bridge call-with-values) (define pack values) |
| 87 | (define (work n)(bridge (lambda()(pack n)) (lambda(a)(+ a 1))))')! |
| 88 | vm.with_native_execution(fn (mut vm VM) !Value { |
| 89 | assert vm.call(vm.lookup('work')!,[integer(4)])!.whole() == 5 |
| 90 | assert native_values_root_enabled(vm,'work')! |
| 91 | vm.eval('(set! pack (lambda(n)(+ n 10)))')! |
| 92 | assert vm.call(vm.lookup('work')!,[integer(4)])!.whole() == 15 |
| 93 | vm.eval('(set! bridge (lambda(p c)99))')! |
| 94 | assert vm.call(vm.lookup('work')!,[integer(4)])!.whole() == 99 |
| 95 | return unspecified() |
| 96 | })! |
| 97 | } |
| 98 | |
| 99 | fn test_native_values_lexical_shadowing_keeps_user_procedures() { |
| 100 | mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! |
| 101 | vm.eval('(define (work n)(let ((call-with-values (lambda(p c)77)) (values (lambda(x)(+ x 10)))) |
| 102 | (call-with-values (lambda()(values n)) (lambda(a)a))))')! |
| 103 | vm.with_native_execution(fn (mut vm VM) !Value { |
| 104 | assert vm.call(vm.lookup('work')!,[integer(4)])!.whole() == 77 |
| 105 | return unspecified() |
| 106 | })! |
| 107 | } |
| 108 | |
| 109 | fn test_native_values_observable_control_procedures_do_not_become_markers() { |
| 110 | mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! |
| 111 | vm.eval('(define (get) (let ((x 1)) values)) |
| 112 | (define (truth)(let ((x 1))(if values 42 0))) |
| 113 | (define (pass f) f) (define (observe)(let ((x 1))(pass call-with-values)))')! |
| 114 | vm.with_native_execution(fn (mut vm VM) !Value { |
| 115 | assert vm.call(vm.lookup('get')!,[])!.pointer() == vm.lookup('values')!.pointer() |
| 116 | assert vm.call(vm.lookup('truth')!,[])!.whole() == 42 |
| 117 | assert vm.call(vm.lookup('observe')!,[])!.pointer() == vm.lookup('call-with-values')!.pointer() |
| 118 | for name in ['get','truth','observe'] { assert !native_values_root_enabled(vm,name)! } |
| 119 | return unspecified() |
| 120 | })! |
| 121 | } |
| 122 | |
| 123 | fn test_native_values_rest_and_escaping_callbacks_use_vm_protocol() { |
| 124 | mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! |
| 125 | vm.eval('(define (rest n)(call-with-values (lambda()(values n 2 3)) (lambda args (car args)))) |
| 126 | (define (escape n)(call-with-values (lambda()(values n)) (lambda(a)(lambda()a)))) |
| 127 | (define (external p c)(call-with-values p c)) |
| 128 | (define producer (lambda()(values 20 22))) (define consumer (lambda(a b)(+ a b)))')! |
| 129 | vm.with_native_execution(fn (mut vm VM) !Value { |
| 130 | assert vm.call(vm.lookup('rest')!,[integer(42)])!.whole() == 42 |
| 131 | escaped := vm.call(vm.lookup('escape')!,[integer(42)])! |
| 132 | assert vm.call(escaped,[])!.whole() == 42 |
| 133 | assert vm.call(vm.lookup('external')!,[vm.lookup('producer')!,vm.lookup('consumer')!])!.whole() == 42 |
| 134 | for name in ['rest','escape','external'] { assert !native_values_root_enabled(vm,name)! } |
| 135 | return unspecified() |
| 136 | })! |
| 137 | } |
| 138 | |
| 139 | fn test_native_values_arity_errors_and_source_match_vm() { |
| 140 | for expression in [ |
| 141 | '(define (work)(call-with-values (lambda()(values 1 2)) (lambda(a)a)))', |
| 142 | '(define (work)(call-with-values (lambda()1) (lambda(a b)(+ a b))))', |
| 143 | '(define (work)(call-with-values (lambda(x)x) (lambda(a)a)))', |
| 144 | ] { |
| 145 | mut plain := new_with_options(Options{max_steps: 0,max_frames: 0})! |
| 146 | plain.eval(expression)! |
| 147 | mut expected := '' |
| 148 | plain.call(plain.lookup('work')!,[]) or { expected = err.msg(); unspecified() } |
| 149 | assert expected != '' |
| 150 | mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! |
| 151 | vm.eval(expression)! |
| 152 | vm.with_native_execution(fn [expected] (mut vm VM) !Value { |
| 153 | mut actual := '' |
| 154 | vm.call(vm.lookup('work')!,[]) or { actual = err.msg(); unspecified() } |
| 155 | assert actual == expected |
| 156 | return unspecified() |
| 157 | })! |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | fn test_native_values_overflow_rolls_back_all_effects_before_replay() { |
| 162 | mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! |
| 163 | vm.eval('(define cells (let ((count 0)) (list |
| 164 | (lambda(v b)(call-with-values (lambda() |
| 165 | (set! count (+ count 1)) (vector-set! v 0 (+ (vector-ref v 0) 1)) |
| 166 | (bytevector-u8-set! b 0 (+ (bytevector-u8-ref b 0) 1)) (values count 9223372036854775807)) |
| 167 | (lambda(a c)(+ a c)))) (lambda()count)))) |
| 168 | (define work (car cells)) (define get (cadr cells))')! |
| 169 | vm.with_native_execution(fn (mut vm VM) !Value { |
| 170 | v := vector([integer(10)]); b := bytevector([u8(20)]) |
| 171 | assert vm.call(vm.lookup('work')!,[v,b])!.tag == .big_integer |
| 172 | assert v.vector_pointer().items[0].whole() == 11 |
| 173 | assert b.bytevector_items()! == [u8(21)] |
| 174 | assert vm.call(vm.lookup('get')!,[])!.whole() == 1 |
| 175 | _,deopts := vm.native_execution_stats() |
| 176 | // The outer transaction fails, then its scalar consumer independently |
| 177 | // declines the overflowing addition during ordinary VM replay. |
| 178 | assert deopts == 2 |
| 179 | scope := unsafe { &NativeScope(vm.native_execution_scope) } |
| 180 | root := vm.lookup('work')!.procedure_pointer().function |
| 181 | assert scope.plans.any(voidptr(it.root) == voidptr(root) && it.disabled) |
| 182 | return unspecified() |
| 183 | })! |
| 184 | } |
| 185 | |
| 186 | fn test_native_values_internal_definitions_reset_on_each_activation() { |
| 187 | mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! |
| 188 | vm.eval('(define (work n)(let loop ((i n)(sum 0)) (if (= i 0) sum |
| 189 | (loop (- i 1) (+ sum (call-with-values (lambda()(define x (+ i 1))(values i x)) |
| 190 | (lambda(a b)(define x (+ a b)) x)))))))')! |
| 191 | vm.with_native_execution(fn (mut vm VM) !Value { |
| 192 | assert vm.call(vm.lookup('work')!,[integer(100)])!.whole() == 10200 |
| 193 | _,deopts := vm.native_execution_stats() |
| 194 | assert deopts == 0 |
| 195 | return unspecified() |
| 196 | })! |
| 197 | } |
| 198 | |
| 199 | fn test_native_values_continuations_keep_vm_dynamic_extent() { |
| 200 | mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! |
| 201 | vm.eval('(define saved #f) (define (work n)(call-with-values |
| 202 | (lambda()(call/cc (lambda(k)(set! saved k)(values n 2)))) (lambda(a b)(+ a b))))')! |
| 203 | vm.with_native_execution(fn (mut vm VM) !Value { |
| 204 | assert vm.call(vm.lookup('work')!,[integer(40)])!.whole() == 42 |
| 205 | assert vm.call(vm.lookup('saved')!,[integer(10),integer(20)])!.whole() == 30 |
| 206 | assert !native_values_root_enabled(vm,'work')! |
| 207 | return unspecified() |
| 208 | })! |
| 209 | } |
| 210 | |
| 211 | fn test_native_values_steps_budget_stays_on_vm() { |
| 212 | mut vm := new_with_options(Options{max_steps: 20,max_frames: 0})! |
| 213 | vm.eval('(define (work n)(call-with-values (lambda()(values n 2)) (lambda(a b)(+ a b))))')! |
| 214 | vm.with_native_execution(fn (mut vm VM) !Value { |
| 215 | vm.call(vm.lookup('work')!,[integer(40)]) or { |
| 216 | assert err.msg().contains('budget') || err.msg().contains('limit') |
| 217 | hits,_ := vm.native_execution_stats() |
| 218 | assert hits == 0 |
| 219 | return unspecified() |
| 220 | } |
| 221 | assert false |
| 222 | return unspecified() |
| 223 | })! |
| 224 | } |
| 225 | |
| 226 | fn test_native_values_vile_callbacks_share_the_same_lowering() { |
| 227 | mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! |
| 228 | vm.eval_alt('work := fn(n) { call-with-values(fn() { values(n, 2) }, fn(a,b) { a + b }) }')! |
| 229 | vm.with_native_execution(fn (mut vm VM) !Value { |
| 230 | assert vm.call(vm.lookup('work')!,[integer(40)])!.whole() == 42 |
| 231 | assert native_values_root_enabled(vm,'work')! |
| 232 | return unspecified() |
| 233 | })! |
| 234 | } |
| 235 | |
| 236 | fn test_native_values_producer_branches_scalar_and_explicit_values() { |
| 237 | mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! |
| 238 | vm.eval('(define (work n)(call-with-values (lambda()(if (= n 0) (values 41) (+ n 1))) (lambda(a)(+ a 1))))')! |
| 239 | vm.with_native_execution(fn (mut vm VM) !Value { |
| 240 | assert vm.call(vm.lookup('work')!,[integer(0)])!.whole() == 42 |
| 241 | assert vm.call(vm.lookup('work')!,[integer(40)])!.whole() == 42 |
| 242 | assert native_values_root_enabled(vm,'work')! |
| 243 | return unspecified() |
| 244 | })! |
| 245 | } |
| 246 | |
| 247 | fn test_native_values_hygienic_private_references_preserve_binding_identity() { |
| 248 | mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! |
| 249 | vm.eval('(define-syntax build-work (syntax-rules () ((_ name) |
| 250 | (define (name n)(call-with-values (lambda()(values n 2)) (lambda(a b)(+ a b))))))) |
| 251 | (build-work work)')! |
| 252 | vm.with_native_execution(fn (mut vm VM) !Value { |
| 253 | assert vm.call(vm.lookup('work')!,[integer(40)])!.whole() == 42 |
| 254 | assert native_values_root_enabled(vm,'work')! |
| 255 | vm.eval('(set! call-with-values (lambda(p c)99))')! |
| 256 | assert vm.call(vm.lookup('work')!,[integer(40)])!.whole() == 99 |
| 257 | return unspecified() |
| 258 | })! |
| 259 | } |
| 260 | |
| 261 | fn test_native_values_early_local_read_does_not_reuse_previous_iteration() { |
| 262 | source := '(define (work n)(let loop ((i n))(if (= i 0) 0 |
| 263 | (call-with-values (lambda() (if (= i 1) x 0) (define x i) (values x)) |
| 264 | (lambda(a)(loop (- a 1)))))))' |
| 265 | mut plain := new_with_options(Options{max_steps: 0,max_frames: 0})! |
| 266 | plain.eval(source)! |
| 267 | mut expected := '' |
| 268 | plain.call(plain.lookup('work')!,[integer(2)]) or { expected = err.msg(); unspecified() } |
| 269 | assert expected.contains('initializ') |
| 270 | mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! |
| 271 | vm.eval(source)! |
| 272 | vm.with_native_execution(fn [expected] (mut vm VM) !Value { |
| 273 | mut actual := '' |
| 274 | vm.call(vm.lookup('work')!,[integer(2)]) or { actual = err.msg(); unspecified() } |
| 275 | assert actual == expected |
| 276 | _,deopts := vm.native_execution_stats() |
| 277 | assert deopts > 0 |
| 278 | return unspecified() |
| 279 | })! |
| 280 | } |
| 281 | |