vile / scheme / native_text_d_vile_native_test.v
182 lines · 170 sloc · 8.25 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3import arena
4
5fn native_text_root_enabled(vm &VM, name string) !bool {
6 if isnil(vm.native_execution_scope) { return false }
7 scope := unsafe { &NativeScope(vm.native_execution_scope) }
8 root := vm.lookup(name)!.procedure_pointer().function
9 return scope.plans.any(voidptr(it.root) == voidptr(root) && !it.disabled)
10}
11
12fn test_native_text_scans_have_an_enabled_outer_plan_and_unicode_oracles() {
13 mut vm := new_with_options(Options{max_steps: 0, max_frames: 0})!
14 vm.eval('(define(scan s) (let loop((i 0)(a 0))
15 (if (= i(string-length s)) a
16 (loop(+ i 1)(+ a(char->integer(string-ref s i)))))))')!
17 texts := [string_value(''), string_value('abc'), string_value('a\x00λ😀'),
18 string_value('é'), string_value(rune(0x10ffff).str())]
19 vm.with_native_execution(fn [texts] (mut vm VM) !Value {
20 for i, expected in [i64(0), 294, 129564, 870, 1114111] {
21 assert vm.call(vm.lookup('scan')!, [texts[i]])!.whole() == expected
22 assert native_text_root_enabled(vm, 'scan')!
23 }
24 hits, deopts := vm.native_execution_stats()
25 assert hits == 5 && deopts == 0
26 return unspecified()
27 })!
28}
29
30fn test_native_text_returns_preserve_handles_characters_literals_and_real_arithmetic() {
31 mut vm := new_with_options(Options{max_steps: 0, max_frames: 0})!
32 vm.eval('(define(keep s)s)(define(at s i)(string-ref s i))
33 (define(code c)(char->integer c))(define(literal)#\\λ)
34 (define(mixed s)(+ 0.5(char->integer(string-ref s 0))))
35 (define(child v)(vector-ref v 0))')!
36 text := vm.eval('"λ\\x0;😀"')!
37 vm.with_native_execution(fn [text] (mut vm VM) !Value {
38 kept := vm.call(vm.lookup('keep')!, [text])!
39 assert kept.pointer() == text.pointer() && kept.read_only == text.read_only
40 for i, code in [i64(955), 0, 128512] {
41 ch := vm.call(vm.lookup('at')!, [text, integer(i)])!
42 assert ch.as_character()! == rune(code)
43 assert vm.call(vm.lookup('code')!, [ch])!.whole() == code
44 }
45 assert vm.call(vm.lookup('literal')!, [])!.as_character()! == `λ`
46 assert vm.call(vm.lookup('mixed')!, [text])!.floating() == 955.5
47 assert vm.call(vm.lookup('child')!, [vector([text])])!.pointer() == text.pointer()
48 for name in ['keep', 'at', 'code', 'literal', 'mixed', 'child'] { assert native_text_root_enabled(vm, name)! }
49 return unspecified()
50 })!
51}
52
53fn test_native_text_refreshes_mutated_rebound_and_same_code_captured_handles() {
54 mut vm := new_with_options(Options{max_steps: 0, max_frames: 0})!
55 vm.eval('(define text(string-copy "aλ"))
56 (define(read-global i)(char->integer(string-ref text i)))
57 (define(make s)(lambda(i)(char->integer(string-ref s i))))
58 (define a(make(string-copy "aλ")))(define b(make(string-copy "b😀")))')!
59 vm.with_native_execution(fn (mut vm VM) !Value {
60 assert vm.call(vm.lookup('read-global')!, [integer(1)])!.whole() == 955
61 vm.eval('(string-set! text 0 #\\😀)')!
62 assert vm.call(vm.lookup('read-global')!, [integer(1)])!.whole() == 955
63 assert vm.call(vm.lookup('read-global')!, [integer(0)])!.whole() == 128512
64 vm.eval('(set! text(string-copy "xy"))')!
65 assert vm.call(vm.lookup('read-global')!, [integer(1)])!.whole() == 121
66 assert vm.call(vm.lookup('a')!, [integer(1)])!.whole() == 955
67 assert vm.call(vm.lookup('b')!, [integer(1)])!.whole() == 128512
68 vm.eval('(set! a(make(string-copy "z𐐷")))')!
69 gc_collect()
70 assert vm.call(vm.lookup('a')!, [integer(1)])!.whole() == 66615
71 assert native_text_root_enabled(vm, 'read-global')! && native_text_root_enabled(vm, 'a')!
72 _, deopts := vm.native_execution_stats()
73 assert deopts == 0
74 return unspecified()
75 })!
76}
77
78fn test_native_text_aliases_and_rebinding_use_actual_builtin_identity() {
79 mut vm := new_with_options(Options{max_steps: 0, max_frames: 0})!
80 vm.eval('(define old-ref string-ref)(define old-length string-length)(define old-code char->integer)
81 (define(alias s)(+ (old-length s)(old-code(old-ref s 0))))
82 (define(changed s)(char->integer(string-ref s 0)))')!
83 vm.with_native_execution(fn (mut vm VM) !Value {
84 text := string_value('abc')
85 assert vm.call(vm.lookup('alias')!, [text])!.whole() == 100
86 assert vm.call(vm.lookup('changed')!, [text])!.whole() == 97
87 assert native_text_root_enabled(vm, 'alias')! && native_text_root_enabled(vm, 'changed')!
88 vm.eval('(set! string-ref(lambda(s i)#\\λ))')!
89 assert vm.call(vm.lookup('changed')!, [text])!.whole() == 955
90 assert vm.call(vm.lookup('alias')!, [text])!.whole() == 100
91 vm.eval('(set! string-length(lambda(s)99))(set! char->integer(lambda(c)77))')!
92 assert vm.call(vm.lookup('changed')!, [text])!.whole() == 77
93 assert vm.call(vm.lookup('alias')!, [text])!.whole() == 100
94 return unspecified()
95 })!
96}
97
98fn test_native_text_failures_restore_capture_vector_and_byte_effects_before_replay() {
99 for bad in [integer(-1), integer(3), integer(exact_max), real(0.5), boolean(false)] {
100 mut vm := new_with_options(Options{max_steps: 0, max_frames: 0})!
101 vm.eval('(define ticks #f)(define work(let((n 0))
102 (set! ticks(lambda()n))
103 (lambda(v b s i)(set! n(+ n 1))
104 (vector-set! v 0(+ (vector-ref v 0) 1))
105 (bytevector-u8-set! b 0(+ (bytevector-u8-ref b 0) 1))
106 (char->integer(string-ref s i)))))')!
107 vm.with_native_execution(fn [bad] (mut vm VM) !Value {
108 v := vector([integer(0)]);b := bytevector([u8(0)]);s := string_value('aλ😀')
109 assert vm.call(vm.lookup('work')!, [v,b,s,integer(0)])!.whole() == 97
110 assert native_text_root_enabled(vm, 'work')!
111 if _ := vm.call(vm.lookup('work')!, [v,b,s,bad]) { return error('invalid native text index accepted') }
112 assert v.vector_pointer().items[0].whole() == 2 && b.bytevector_pointer().bytes[0] == 2
113 assert vm.call(vm.lookup('ticks')!, [])!.whole() == 2
114 _, deopts := vm.native_execution_stats()
115 assert deopts >= 1
116 return unspecified()
117 })!
118 }
119}
120
121fn test_native_text_string_publication_declines_before_replaying_vector_effects() {
122 mut vm := new_with_options(Options{max_steps: 0, max_frames: 0})!
123 vm.eval('(define(place v s)(vector-set! v 1(+ (vector-ref v 1) 1))
124 (vector-set! v 0 s)(string-length s))')!
125 vm.with_native_execution(fn (mut vm VM) !Value {
126 v := vector([integer(0),integer(0)]);s := string_value('λ😀')
127 assert vm.call(vm.lookup('place')!, [v,s])!.whole() == 2
128 assert v.vector_pointer().items[0].pointer() == s.pointer()
129 assert v.vector_pointer().items[1].whole() == 1
130 _, deopts := vm.native_execution_stats()
131 assert deopts == 1
132 return unspecified()
133 })!
134}
135
136fn test_native_character_packets_reject_full_width_non_scalars_before_narrowing() {
137 for n in [i64(-1), 0xd800, 0xdfff, 0x110000, (i64(1)<<32)+97] {
138 assert native_host_value(C.VileNativeValue{x:n,k:14}).tag == .uninitialized
139 }
140 for n in [i64(0), 97, 0x10ffff] {
141 assert native_host_value(C.VileNativeValue{x:n,k:14}).as_character()! == rune(n)
142 }
143}
144
145fn native_text_borrowed_entry(mut vm VM, text Value) !Value {
146 mut borrowed := arena.new();borrowed.push()
147 defer { borrowed.pop(); borrowed.free() }
148 return vm.call(vm.lookup('scan')!, [text])
149}
150
151fn test_native_text_borrowed_and_bounded_entries_keep_vm_control() {
152 mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})!
153 vm.eval('(define(scan s)(char->integer(string-ref s 0)))')!
154 vm.with_native_execution(fn (mut vm VM) !Value {
155 text := string_value('λ')
156 assert native_text_borrowed_entry(mut vm,text)!.whole() == 955
157 hits,_ := vm.native_execution_stats();assert hits == 0
158 assert vm.call(vm.lookup('scan')!,[text])!.whole() == 955
159 assert native_text_root_enabled(vm,'scan')!
160 return unspecified()
161 })!
162 mut bounded := new_with_options(Options{max_steps: 30,max_frames: 0})!
163 bounded.eval('(define(scan s)(let loop((i 0)) (if (= i 100) 0 (loop(+ i 1)))))')!
164 bounded.with_native_execution(fn (mut vm VM) !Value {
165 if _ := vm.call(vm.lookup('scan')!,[string_value('abc')]) { return error('native bypassed instruction budget') }
166 assert isnil(vm.native_execution_scope)
167 return unspecified()
168 })!
169}
170
171fn test_native_text_literal_strings_keep_vm_fallback_and_shared_frontend() {
172 mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})!
173 vm.eval('(define(literal-read)(string-ref "λ" 0))')!
174 vm.eval_alt('text_code := fn(s) { char->integer(string-ref(s, 0)) }')!
175 vm.with_native_execution(fn (mut vm VM) !Value {
176 assert vm.call(vm.lookup('literal-read')!,[])!.as_character()! == `λ`
177 assert !native_text_root_enabled(vm,'literal-read')!
178 assert vm.call(vm.lookup('text_code')!,[string_value('😀')])!.whole() == 128512
179 assert native_text_root_enabled(vm,'text_code')!
180 return unspecified()
181 })!
182}
183