vile / scheme / goops_bootstrap_budget_test.v
96 lines · 89 sloc · 4.14 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3import arena
4
5fn test_goops_bootstrap_does_not_consume_the_callers_step_limit() {
6 mut vm := new_with_options(Options{ max_steps: 1 }) or { panic(err) }
7 initialize := vm.lookup('initialize') or { panic(err) }
8 assert initialize.is_callable()
9 if _ := vm.eval('42') {
10 panic('bootstrap replaced the caller step limit')
11 } else {
12 assert err.msg().contains('evaluation step limit exceeded'), err.msg()
13 }
14 // Management remains usable after the deliberately exhausted evaluation.
15 assert vm.lookup('initialize') or { panic(err) } == initialize
16}
17
18fn test_goops_bootstrap_restores_the_callers_frame_limit() {
19 mut vm := new_with_options(Options{ max_frames: 1 }) or { panic(err) }
20 if _ := vm.eval('(define (f n) (if (= n 0) 0 (+ 1 (f (- n 1))))) (f 4)') {
21 panic('bootstrap replaced the caller frame limit')
22 } else {
23 assert err.msg().contains('continuation frame limit exceeded'), err.msg()
24 }
25 assert (vm.eval('7') or { panic(err) }).as_integer() or { panic(err) } == 7
26}
27
28fn test_goops_bootstrap_restores_the_callers_expansion_limit() {
29 mut vm := new_with_options(Options{ max_expansions: 1 }) or { panic(err) }
30 if _ := vm.eval('(define-syntax first (syntax-rules () ((_ x) (second x))))
31 (define-syntax second (syntax-rules () ((_ x) x)))
32 (first 42)') {
33 panic('bootstrap replaced the caller expansion limit')
34 } else {
35 assert err.msg().contains('macro expansion limit exceeded'), err.msg()
36 }
37 assert (vm.eval('9') or { panic(err) }).as_integer() or { panic(err) } == 9
38}
39
40fn test_built_in_protocols_remain_vm_local_and_user_syntax_is_staged() {
41 mut first := new()
42 mut second := new()
43 program := '(define-class <test-class> () (x #:init-value 7))
44 (define original initialize)
45 (define probe (make <test-class>))
46 (define-syntax read-test-slot (syntax-rules () ((_ obj) (slot-ref obj (quote x)))))
47 (list (eq? make make-instance) (read-test-slot probe) (generic? original))'
48 first_value := first.eval(program) or { panic(err) }
49 second_value := second.eval(program) or { panic(err) }
50 for value in [first_value, second_value] {
51 text := write(value) or { panic(err) }
52 if text != '(#t 7 #t)' { panic('incorrect protocol installation: ${text}') }
53 }
54 first.eval('(slot-set! probe (quote x) 11) (set! initialize #f)') or { panic(err) }
55 value := second.eval('(list (eq? original initialize) (read-test-slot probe))') or { panic(err) }
56 text := write(value) or { panic(err) }
57 if text != '(#t 7)' { panic('VM protocol state crossed instances: ${text}') }
58}
59
60fn test_protocol_source_outlives_each_vm_arena() {
61 mut temporary := arena.new()
62 temporary.push()
63 mut first := new()
64 first_value := first.lookup('class-slot-ref') or { panic(err) }
65 first_function := first_value.procedure_pointer().function
66 temporary.pop()
67
68 mut second := new()
69 second_value := second.lookup('class-slot-ref') or { panic(err) }
70 second_function := second_value.procedure_pointer().function
71 if voidptr(first_function) == voidptr(second_function) {
72 panic('protocol function shared executable state across VMs')
73 }
74 if first_function.instructions.data != second_function.instructions.data {
75 panic('read-only protocol instructions were copied into each VM')
76 }
77 if first_function.global_slots.len == 0 || first_function.global_slots.data == second_function.global_slots.data {
78 panic('protocol global lookup caches must belong to each VM')
79 }
80 if !temporary.owns(first_function.global_slots.data) || temporary.owns(first_function.instructions.data) {
81 panic('protocol code or binding cache has the wrong allocation owner')
82 }
83 if first_function.code.doc.forms.data != second_function.code.doc.forms.data
84 || first_function.code.doc.arena.source_text().str != second_function.code.doc.arena.source_text().str {
85 panic('built-in protocol source was copied per VM')
86 }
87 if temporary.owns(second_function.code.doc.forms.data)
88 || temporary.owns(second_function.code.doc.arena.source_text().str) {
89 panic('protocol source retained a temporary VM arena')
90 }
91 temporary.free()
92 probe := second.eval("(define-class <source-test> () (x #:allocation #:class #:init-value 73)) (class-slot-ref <source-test> 'x)") or { panic(err) }
93 if (probe.as_integer() or { panic(err) }) != 73 {
94 panic('surviving VM lost its protocol state')
95 }
96}
97