vile / scheme / native_capture_alias_d_vile_native_test.v
41 lines · 39 sloc · 1.74 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3fn test_native_readonly_capture_aliases_can_change_while_another_cell_is_written() {
4 mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})!
5 vm.eval('(define (make bias)(lambda()bias))
6 (define a (make 7)) (define b (make 19))
7 (define cells (let ((n 0)) (list (lambda()(set! n (+ n 1)) n) (lambda()n))))
8 (define tick (car cells)) (define peek (cadr cells))
9 (define (work f g w)(w)(+ (f) (g)))')!
10 vm.with_native_execution(fn (mut vm VM) !Value {
11 a,b,w := vm.lookup('a')!,vm.lookup('b')!,vm.lookup('tick')!
12 root := vm.lookup('work')!
13 assert vm.call(root,[a,a,w])!.whole() == 14
14 assert vm.call(root,[a,b,w])!.whole() == 26
15 assert vm.call(root,[b,a,w])!.whole() == 26
16 hits,deopts := vm.native_execution_stats()
17 assert hits == 3 && deopts == 0
18 return unspecified()
19 })!
20 assert vm.call(vm.lookup('peek')!,[])!.whole() == 3
21}
22
23fn test_native_readonly_vector_bindings_keep_distinct_payloads_after_alias_changes() {
24 mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})!
25 vm.eval('(define va (vector 0)) (define vb (vector 100))
26 (define (make v)(lambda(d)(vector-set! v 0 (+ (vector-ref v 0) d))(vector-ref v 0)))
27 (define a (make va)) (define b (make vb))
28 (define tick (let ((n 0))(lambda()(set! n (+ n 1)) n)))
29 (define (work f g w)(w)(+ (f 1) (g 10)))')!
30 vm.with_native_execution(fn (mut vm VM) !Value {
31 a,b,w := vm.lookup('a')!,vm.lookup('b')!,vm.lookup('tick')!
32 root := vm.lookup('work')!
33 assert vm.call(root,[a,a,w])!.whole() == 12
34 assert vm.call(root,[a,b,w])!.whole() == 122
35 hits,deopts := vm.native_execution_stats()
36 assert hits == 2 && deopts == 0
37 return unspecified()
38 })!
39 assert vm.lookup('va')!.vector_pointer().items[0].whole() == 12
40 assert vm.lookup('vb')!.vector_pointer().items[0].whole() == 110
41}
42