module scheme import arena fn native_text_root_enabled(vm &VM, name string) !bool { if isnil(vm.native_execution_scope) { return false } scope := unsafe { &NativeScope(vm.native_execution_scope) } root := vm.lookup(name)!.procedure_pointer().function return scope.plans.any(voidptr(it.root) == voidptr(root) && !it.disabled) } fn test_native_text_scans_have_an_enabled_outer_plan_and_unicode_oracles() { mut vm := new_with_options(Options{max_steps: 0, max_frames: 0})! vm.eval('(define(scan s) (let loop((i 0)(a 0)) (if (= i(string-length s)) a (loop(+ i 1)(+ a(char->integer(string-ref s i)))))))')! texts := [string_value(''), string_value('abc'), string_value('a\x00位馃榾'), string_value('e虂'), string_value(rune(0x10ffff).str())] vm.with_native_execution(fn [texts] (mut vm VM) !Value { for i, expected in [i64(0), 294, 129564, 870, 1114111] { assert vm.call(vm.lookup('scan')!, [texts[i]])!.whole() == expected assert native_text_root_enabled(vm, 'scan')! } hits, deopts := vm.native_execution_stats() assert hits == 5 && deopts == 0 return unspecified() })! } fn test_native_text_returns_preserve_handles_characters_literals_and_real_arithmetic() { mut vm := new_with_options(Options{max_steps: 0, max_frames: 0})! vm.eval('(define(keep s)s)(define(at s i)(string-ref s i)) (define(code c)(char->integer c))(define(literal)#\\位) (define(mixed s)(+ 0.5(char->integer(string-ref s 0)))) (define(child v)(vector-ref v 0))')! text := vm.eval('"位\\x0;馃榾"')! vm.with_native_execution(fn [text] (mut vm VM) !Value { kept := vm.call(vm.lookup('keep')!, [text])! assert kept.pointer() == text.pointer() && kept.read_only == text.read_only for i, code in [i64(955), 0, 128512] { ch := vm.call(vm.lookup('at')!, [text, integer(i)])! assert ch.as_character()! == rune(code) assert vm.call(vm.lookup('code')!, [ch])!.whole() == code } assert vm.call(vm.lookup('literal')!, [])!.as_character()! == `位` assert vm.call(vm.lookup('mixed')!, [text])!.floating() == 955.5 assert vm.call(vm.lookup('child')!, [vector([text])])!.pointer() == text.pointer() for name in ['keep', 'at', 'code', 'literal', 'mixed', 'child'] { assert native_text_root_enabled(vm, name)! } return unspecified() })! } fn test_native_text_refreshes_mutated_rebound_and_same_code_captured_handles() { mut vm := new_with_options(Options{max_steps: 0, max_frames: 0})! vm.eval('(define text(string-copy "a位")) (define(read-global i)(char->integer(string-ref text i))) (define(make s)(lambda(i)(char->integer(string-ref s i)))) (define a(make(string-copy "a位")))(define b(make(string-copy "b馃榾")))')! vm.with_native_execution(fn (mut vm VM) !Value { assert vm.call(vm.lookup('read-global')!, [integer(1)])!.whole() == 955 vm.eval('(string-set! text 0 #\\馃榾)')! assert vm.call(vm.lookup('read-global')!, [integer(1)])!.whole() == 955 assert vm.call(vm.lookup('read-global')!, [integer(0)])!.whole() == 128512 vm.eval('(set! text(string-copy "xy"))')! assert vm.call(vm.lookup('read-global')!, [integer(1)])!.whole() == 121 assert vm.call(vm.lookup('a')!, [integer(1)])!.whole() == 955 assert vm.call(vm.lookup('b')!, [integer(1)])!.whole() == 128512 vm.eval('(set! a(make(string-copy "z饜惙")))')! gc_collect() assert vm.call(vm.lookup('a')!, [integer(1)])!.whole() == 66615 assert native_text_root_enabled(vm, 'read-global')! && native_text_root_enabled(vm, 'a')! _, deopts := vm.native_execution_stats() assert deopts == 0 return unspecified() })! } fn test_native_text_aliases_and_rebinding_use_actual_builtin_identity() { mut vm := new_with_options(Options{max_steps: 0, max_frames: 0})! vm.eval('(define old-ref string-ref)(define old-length string-length)(define old-code char->integer) (define(alias s)(+ (old-length s)(old-code(old-ref s 0)))) (define(changed s)(char->integer(string-ref s 0)))')! vm.with_native_execution(fn (mut vm VM) !Value { text := string_value('abc') assert vm.call(vm.lookup('alias')!, [text])!.whole() == 100 assert vm.call(vm.lookup('changed')!, [text])!.whole() == 97 assert native_text_root_enabled(vm, 'alias')! && native_text_root_enabled(vm, 'changed')! vm.eval('(set! string-ref(lambda(s i)#\\位))')! assert vm.call(vm.lookup('changed')!, [text])!.whole() == 955 assert vm.call(vm.lookup('alias')!, [text])!.whole() == 100 vm.eval('(set! string-length(lambda(s)99))(set! char->integer(lambda(c)77))')! assert vm.call(vm.lookup('changed')!, [text])!.whole() == 77 assert vm.call(vm.lookup('alias')!, [text])!.whole() == 100 return unspecified() })! } fn test_native_text_failures_restore_capture_vector_and_byte_effects_before_replay() { for bad in [integer(-1), integer(3), integer(exact_max), real(0.5), boolean(false)] { mut vm := new_with_options(Options{max_steps: 0, max_frames: 0})! vm.eval('(define ticks #f)(define work(let((n 0)) (set! ticks(lambda()n)) (lambda(v b s i)(set! n(+ n 1)) (vector-set! v 0(+ (vector-ref v 0) 1)) (bytevector-u8-set! b 0(+ (bytevector-u8-ref b 0) 1)) (char->integer(string-ref s i)))))')! vm.with_native_execution(fn [bad] (mut vm VM) !Value { v := vector([integer(0)]);b := bytevector([u8(0)]);s := string_value('a位馃榾') assert vm.call(vm.lookup('work')!, [v,b,s,integer(0)])!.whole() == 97 assert native_text_root_enabled(vm, 'work')! if _ := vm.call(vm.lookup('work')!, [v,b,s,bad]) { return error('invalid native text index accepted') } assert v.vector_pointer().items[0].whole() == 2 && b.bytevector_pointer().bytes[0] == 2 assert vm.call(vm.lookup('ticks')!, [])!.whole() == 2 _, deopts := vm.native_execution_stats() assert deopts >= 1 return unspecified() })! } } fn test_native_text_string_publication_declines_before_replaying_vector_effects() { mut vm := new_with_options(Options{max_steps: 0, max_frames: 0})! vm.eval('(define(place v s)(vector-set! v 1(+ (vector-ref v 1) 1)) (vector-set! v 0 s)(string-length s))')! vm.with_native_execution(fn (mut vm VM) !Value { v := vector([integer(0),integer(0)]);s := string_value('位馃榾') assert vm.call(vm.lookup('place')!, [v,s])!.whole() == 2 assert v.vector_pointer().items[0].pointer() == s.pointer() assert v.vector_pointer().items[1].whole() == 1 _, deopts := vm.native_execution_stats() assert deopts == 1 return unspecified() })! } fn test_native_character_packets_reject_full_width_non_scalars_before_narrowing() { for n in [i64(-1), 0xd800, 0xdfff, 0x110000, (i64(1)<<32)+97] { assert native_host_value(C.VileNativeValue{x:n,k:14}).tag == .uninitialized } for n in [i64(0), 97, 0x10ffff] { assert native_host_value(C.VileNativeValue{x:n,k:14}).as_character()! == rune(n) } } fn native_text_borrowed_entry(mut vm VM, text Value) !Value { mut borrowed := arena.new();borrowed.push() defer { borrowed.pop(); borrowed.free() } return vm.call(vm.lookup('scan')!, [text]) } fn test_native_text_borrowed_and_bounded_entries_keep_vm_control() { mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! vm.eval('(define(scan s)(char->integer(string-ref s 0)))')! vm.with_native_execution(fn (mut vm VM) !Value { text := string_value('位') assert native_text_borrowed_entry(mut vm,text)!.whole() == 955 hits,_ := vm.native_execution_stats();assert hits == 0 assert vm.call(vm.lookup('scan')!,[text])!.whole() == 955 assert native_text_root_enabled(vm,'scan')! return unspecified() })! mut bounded := new_with_options(Options{max_steps: 30,max_frames: 0})! bounded.eval('(define(scan s)(let loop((i 0)) (if (= i 100) 0 (loop(+ i 1)))))')! bounded.with_native_execution(fn (mut vm VM) !Value { if _ := vm.call(vm.lookup('scan')!,[string_value('abc')]) { return error('native bypassed instruction budget') } assert isnil(vm.native_execution_scope) return unspecified() })! } fn test_native_text_literal_strings_keep_vm_fallback_and_shared_frontend() { mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! vm.eval('(define(literal-read)(string-ref "位" 0))')! vm.eval_alt('text_code := fn(s) { char->integer(string-ref(s, 0)) }')! vm.with_native_execution(fn (mut vm VM) !Value { assert vm.call(vm.lookup('literal-read')!,[])!.as_character()! == `位` assert !native_text_root_enabled(vm,'literal-read')! assert vm.call(vm.lookup('text_code')!,[string_value('馃榾')])!.whole() == 128512 assert native_text_root_enabled(vm,'text_code')! return unspecified() })! }