vile / scheme / native_argument_calls_d_vile_native_test.v
48 lines · 45 sloc · 2.0 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3fn test_native_argument_callback_loop_refreshes_same_code_environments() {
4 mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})!
5 vm.eval('(define (make bias)(lambda(x)(+ x bias)))
6 (define a (make 7)) (define b (make 19))
7 (define (work f n)(do ((i n (- i 1))(sum 0 (+ sum (f 0)))) ((= i 0) sum)))')!
8 vm.with_native_execution(fn (mut vm VM) !Value {
9 work := vm.lookup('work')!
10 for name, expected in {'a':28672,'b':77824} {
11 assert vm.call(work,[vm.lookup(name)!,integer(4096)])!.whole() == expected
12 }
13 scope := unsafe { &NativeScope(vm.native_execution_scope) }
14 assert scope.hits == 2 && scope.deopts == 0
15 return unspecified()
16 })!
17}
18
19fn test_native_argument_callback_assignment_keeps_dynamic_dispatch() {
20 mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})!
21 vm.eval('(define (a x)(+ x 7)) (define (b x)(+ x 19))
22 (define (work f g n)(let () (set! f g)) (+ (f n) (g n)))')!
23 vm.with_native_execution(fn (mut vm VM) !Value {
24 assert vm.call(vm.lookup('work')!,[vm.lookup('a')!,vm.lookup('b')!,integer(100)])!.whole() == 238
25 hits,deopts := vm.native_execution_stats()
26 assert hits == 1 && deopts == 0
27 return unspecified()
28 })!
29}
30
31fn test_native_argument_callback_internal_entries_recheck_before_effects() {
32 for recursive in ['(work g f (- n 1) v)','(+ 100 (work g f (- n 1) v))'] {
33 mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})!
34 vm.eval('(define (make bias)(lambda(x)(+ x bias)))
35 (define a (make 7)) (define b (make 19))
36 (define work (letrec ((work (lambda(f g n v)(vector-set! v 0 (+ (vector-ref v 0) 1))
37 (if (= n 0) (f 1) '+recursive+')))) work))')!
38 vm.with_native_execution(fn [recursive] (mut vm VM) !Value {
39 v := vector([integer(0)])
40 result := vm.call(vm.lookup('work')!,[vm.lookup('a')!,vm.lookup('b')!,integer(1),v])!
41 assert result.whole() == if recursive.starts_with('(+') { 120 } else { 20 }
42 assert v.vector_pointer().items[0].whole() == 2
43 _,deopts := vm.native_execution_stats()
44 assert deopts > 0
45 return unspecified()
46 })!
47 }
48}
49