vile / scheme / native_captures_d_vile_native_test.v
231 lines · 217 sloc · 10.38 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3fn test_native_captured_counter_loop_stages_and_commits_in_one_entry() {
4 mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})!
5 vm.eval('(define cells (let ((n 0))
6 (list (lambda()(set! n (+ n 1)) n) (lambda()n))))
7 (define counter (car cells)) (define peek (cadr cells))
8 (define (run f n) (let loop ((i 0)(sum 0))
9 (if (= i n) sum (loop (+ i 1) (+ sum (f))))))')!
10 vm.with_native_execution(fn (mut vm VM) !Value {
11 assert vm.call(vm.lookup('run')!,[vm.lookup('counter')!,integer(4096)])!.whole() == 8390656
12 hits,deopts := vm.native_execution_stats()
13 assert hits == 1 && deopts == 0
14 return unspecified()
15 })!
16 assert vm.call(vm.lookup('peek')!,[])!.whole() == 4096
17}
18
19fn test_native_capture_reads_share_writes_across_closures_and_lexical_depths() {
20 mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})!
21 vm.eval('(define cells (let ((n 0))
22 (list (lambda(d)(set! n (+ n d)) n)
23 (let ((unused 0)) (lambda()n)))))
24 (define inc (car cells)) (define read (cadr cells))
25 (define (work f g d)(f d) (+ (g) (g)))')!
26 vm.with_native_execution(fn (mut vm VM) !Value {
27 assert vm.call(vm.lookup('work')!,[vm.lookup('inc')!,vm.lookup('read')!,integer(5)])!.whole() == 10
28 assert vm.call(vm.lookup('work')!,[vm.lookup('inc')!,vm.lookup('read')!,integer(7)])!.whole() == 24
29 hits,deopts := vm.native_execution_stats()
30 assert hits == 2 && deopts == 0
31 return unspecified()
32 })!
33 assert vm.call(vm.lookup('read')!,[])!.whole() == 12
34}
35
36fn test_native_capture_alias_guards_follow_same_code_provider_environment_changes() {
37 mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})!
38 vm.eval('(define (make n)(list (lambda(d)(set! n (+ n d)) n) (lambda()n)))
39 (define a (make 1)) (define b (make 100))
40 (define (work f g)(f 1)(g)) (define (other f g)(f 1)(g))')!
41 vm.with_native_execution(fn (mut vm VM) !Value {
42 a,b := vm.lookup('a')!,vm.lookup('b')!
43 ai,ar := a.pair_pointer().car,a.pair_pointer().cdr.pair_pointer().car
44 bi,br := b.pair_pointer().car,b.pair_pointer().cdr.pair_pointer().car
45 work,other := vm.lookup('work')!,vm.lookup('other')!
46 assert vm.call(work,[ai,ar])!.whole() == 2
47 assert vm.call(work,[ai,br])!.whole() == 100
48 assert vm.call(work,[bi,br])!.whole() == 101
49 assert vm.call(work,[bi,ar])!.whole() == 3
50 assert vm.call(other,[ai,br])!.whole() == 102
51 assert vm.call(other,[ai,ar])!.whole() == 5
52 return unspecified()
53 })!
54}
55
56fn test_native_capture_alias_guards_distinguish_two_writers_from_one_shared_cell() {
57 mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})!
58 vm.eval('(define (make n)(lambda(d)(set! n (+ n d)) n))
59 (define a (make 1)) (define b (make 100))
60 (define (work f g)(+ (f 1) (g 10))) (define (other f g)(+ (f 1) (g 10)))')!
61 vm.with_native_execution(fn (mut vm VM) !Value {
62 a,b := vm.lookup('a')!,vm.lookup('b')!
63 assert vm.call(vm.lookup('work')!,[a,b])!.whole() == 112
64 assert vm.call(vm.lookup('work')!,[a,a])!.whole() == 16
65 assert vm.call(vm.lookup('other')!,[a,a])!.whole() == 38
66 assert vm.call(vm.lookup('other')!,[a,b])!.whole() == 145
67 return unspecified()
68 })!
69}
70
71fn test_native_capture_scalar_types_and_external_updates_remain_live() {
72 mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})!
73 vm.eval('(define cells (let ((n 0)) (list (lambda(x)(set! n x) n) (lambda()n))))
74 (define put (car cells)) (define peek (cadr cells)) (define (forward x)(put x))')!
75 vm.with_native_execution(fn (mut vm VM) !Value {
76 for x in [integer(7),real(0.5),boolean(false),nil_value(),unspecified()] {
77 actual := vm.call(vm.lookup('forward')!,[x])!
78 assert actual.tag == x.tag && write(actual)! == write(x)!
79 assert write(vm.call(vm.lookup('peek')!,[])!)! == write(x)!
80 }
81 hits,deopts := vm.native_execution_stats()
82 assert hits == 10 && deopts == 0
83 return unspecified()
84 })!
85 vm.call(vm.lookup('put')!,[integer(41)])!
86 vm.with_native_execution(fn (mut vm VM) !Value {
87 assert vm.call(vm.lookup('peek')!,[])!.whole() == 41
88 return unspecified()
89 })!
90}
91
92fn test_native_capture_and_vector_effects_replay_once_after_errors_and_overflow() {
93 mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})!
94 vm.eval('(define cells (let ((n 0))
95 (list (lambda(v)(set! n (+ n 1))(vector-set! v 0 (+ (vector-ref v 0) 1))(car (quote ())))
96 (lambda(v)(set! n (+ n 1))(vector-set! v 0 (+ (vector-ref v 0) 1))(+ 9223372036854775807 1))
97 (lambda()n))))
98 (define bad (car cells)) (define overflow (cadr cells)) (define peek (caddr cells))')!
99 vm.with_native_execution(fn (mut vm VM) !Value {
100 v := vector([integer(0)])
101 if _ := vm.call(vm.lookup('bad')!,[v]) { return error('invalid pair read succeeded') }
102 assert v.vector_pointer().items[0].whole() == 1
103 assert vm.call(vm.lookup('overflow')!,[v])!.tag == .big_integer
104 assert v.vector_pointer().items[0].whole() == 2
105 _,deopts := vm.native_execution_stats()
106 assert deopts >= 2
107 return unspecified()
108 })!
109 assert vm.call(vm.lookup('peek')!,[])!.whole() == 2
110}
111
112fn test_native_capture_rejects_unsupported_publication_before_any_commit() {
113 mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})!
114 vm.eval('(define (callback x)(+ x 1))
115 (define cells (let ((n 0)(m 0))
116 (list (lambda(v f)(set! n (+ n 1))(vector-set! v 0 (+ 1 (vector-ref v 0)))(set! m f)n)
117 (lambda()n) (lambda()m))))
118 (define work (car cells)) (define peek (cadr cells)) (define stored (caddr cells))')!
119 vm.with_native_execution(fn (mut vm VM) !Value {
120 v := vector([integer(0)])
121 callback := vm.lookup('callback')!
122 assert vm.call(vm.lookup('work')!,[v,callback])!.whole() == 1
123 assert v.vector_pointer().items[0].whole() == 1
124 _,deopts := vm.native_execution_stats()
125 assert deopts >= 1
126 return unspecified()
127 })!
128 assert vm.call(vm.lookup('peek')!,[])!.whole() == 1
129 assert vm.call(vm.lookup('stored')!,[])!.pointer() == vm.lookup('callback')!.pointer()
130}
131
132fn test_native_capture_private_pair_store_replays_with_real_vm_locations() {
133 mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})!
134 vm.eval('(define cells (let ((n 0)(m 0))
135 (list (lambda(v)(set! n (+ n 1))(vector-set! v 0 (+ 1 (vector-ref v 0)))
136 (set! m (cons 7 (quote ()))) (car m)) (lambda()n) (lambda()m))))
137 (define work (car cells)) (define peek (cadr cells)) (define stored (caddr cells))')!
138 vm.with_native_execution(fn (mut vm VM) !Value {
139 v := vector([integer(0)])
140 assert vm.call(vm.lookup('work')!,[v])!.whole() == 7
141 assert v.vector_pointer().items[0].whole() == 1
142 _,deopts := vm.native_execution_stats()
143 assert deopts >= 1
144 return unspecified()
145 })!
146 assert vm.call(vm.lookup('peek')!,[])!.whole() == 1
147 assert write(vm.call(vm.lookup('stored')!,[])!)! == '(7)'
148}
149
150fn test_native_capture_procedure_replacement_never_uses_a_stale_code_id() {
151 mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})!
152 vm.eval('(define (make bias)(lambda(x)(+ x bias)))
153 (define a (make 1)) (define b (make 41)) (define c (lambda(x)(* x 10)))
154 (define work (let ((f a)) (lambda(next x)(set! f next)(f x))))')!
155 vm.with_native_execution(fn (mut vm VM) !Value {
156 assert vm.call(vm.lookup('work')!,[vm.lookup('b')!,integer(1)])!.whole() == 42
157 assert vm.call(vm.lookup('work')!,[vm.lookup('c')!,integer(1)])!.whole() == 10
158 assert vm.call(vm.lookup('work')!,[vm.lookup('a')!,integer(1)])!.whole() == 2
159 return unspecified()
160 })!
161}
162
163fn test_native_capture_mutation_keeps_continuation_reentry_effects() {
164 mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})!
165 vm.eval('(define saved #f)
166 (define cells (let ((n 0)) (list
167 (lambda()(set! n (+ n 1))(call/cc (lambda(k)(set! saved k)n))) (lambda()n))))
168 (define work (car cells)) (define peek (cadr cells))')!
169 vm.with_native_execution(fn (mut vm VM) !Value {
170 assert vm.call(vm.lookup('work')!,[])!.whole() == 1
171 assert vm.call(vm.lookup('saved')!,[integer(5)])!.whole() == 5
172 assert vm.call(vm.lookup('saved')!,[integer(9)])!.whole() == 9
173 return unspecified()
174 })!
175 assert vm.call(vm.lookup('peek')!,[])!.whole() == 1
176}
177
178fn test_native_capture_mutation_works_in_vile_syntax_and_clears_borrowed_cells() {
179 mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})!
180 vm.eval_alt('make := fn(n) { fn(d) { n = n + d\n n } }\ncounter := make(0)\nforward := fn(d) { counter(d) }')!
181 vm.with_native_execution(fn (mut vm VM) !Value {
182 assert vm.call(vm.lookup('forward')!,[integer(7)])!.whole() == 7
183 assert vm.call(vm.lookup('forward')!,[integer(9)])!.whole() == 16
184 scope := unsafe { &NativeScope(vm.native_execution_scope) }
185 assert scope.hits == 2 && scope.deopts == 0 && isnil(scope.capture_plan)
186 for cell in scope.capture_cells { assert isnil(cell.env) }
187 return unspecified()
188 })!
189}
190
191fn test_native_capture_mutation_preserves_vile_implicit_callback_and_scope_results() {
192 mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})!
193 vm.eval('(define v (vector 0)) (define (callback)(vector-set! v 0 (+ 1 (vector-ref v 0))) 42)')!
194 vm.eval_alt('cells := let((n(0),), list(fn(f) { n = n + 1\n f }, fn() { n + 0 }))\nwork := car(cells)\npeek := cadr(cells)\nscoped := fn(f) { 1 + let((unused(0),), { f }) }')!
195 assert vm.call(vm.lookup('scoped')!,[vm.lookup('callback')!])!.whole() == 43
196 vm.eval('(vector-set! v 0 0)')!
197 vm.with_native_execution(fn (mut vm VM) !Value {
198 callback := vm.lookup('callback')!
199 assert vm.call(vm.lookup('work')!,[callback])!.whole() == 42
200 assert vm.call(vm.lookup('scoped')!,[callback])!.whole() == 43
201 hits,deopts := vm.native_execution_stats()
202 assert hits == 2 && deopts == 0
203 return unspecified()
204 })!
205 assert vm.call(vm.lookup('peek')!,[])!.whole() == 1
206 assert vm.lookup('v')!.vector_pointer().items[0].whole() == 2
207}
208
209fn test_native_capture_mutation_keeps_vile_implicit_tail_loops_bounded() {
210 mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})!
211 vm.eval_alt('work := fn(n) { let(loop, (), { n = n - 1\n if n == 0 { 42 } else { loop } }) }')!
212 vm.with_native_execution(fn (mut vm VM) !Value {
213 assert vm.call(vm.lookup('work')!,[integer(4096)])!.whole() == 42
214 hits,deopts := vm.native_execution_stats()
215 assert hits == 1 && deopts == 0
216 return unspecified()
217 })!
218}
219
220fn test_native_tiny_captured_mutator_roots_keep_the_vm_path() {
221 mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})!
222 vm.eval('(define cells (let ((n 0)) (list (lambda()(set! n (+ n 1))) (lambda()n))))
223 (define tick (car cells)) (define peek (cadr cells))')!
224 vm.with_native_execution(fn (mut vm VM) !Value {
225 for _ in 0 .. 100 { vm.call(vm.lookup('tick')!,[])! }
226 hits,deopts := vm.native_execution_stats()
227 assert hits == 0 && deopts == 0
228 return unspecified()
229 })!
230 assert vm.call(vm.lookup('peek')!,[])!.whole() == 100
231}
232