module scheme #include "native_bytes_test.h" fn C.vile_native_test_fault_invoke(voidptr, &C.VileNativeValue, &C.VileNativeHostApi, &C.VileNativeValue) fn C.vile_native_bytes_map(i64) voidptr fn C.vile_native_bytes_unmap(voidptr, i64) fn test_native_bytevector_result_preserves_identity_and_commits_once() { mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! vm.eval('(define (work b)(bytevector-u8-set! b 0 (+ (bytevector-u8-ref b 0) 1)) b) (define (pass b)b)')! vm.with_native_execution(fn (mut vm VM) !Value { b := bytevector([u8(41)]) assert vm.call(vm.lookup('work')!,[b])!.pointer() == b.pointer() assert b.bytevector_items()! == [u8(42)] literal := vm.eval('#u8(7 8)')! returned := vm.call(vm.lookup('pass')!,[literal])! assert returned.pointer() == literal.pointer() && returned.read_only hits,deopts := vm.native_execution_stats() assert hits == 2 && deopts == 0 return unspecified() })! } fn test_native_bytevector_repeated_writes_share_first_touch_entries() { mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! vm.eval('(define (fill b n limit size)(if (= n limit) n (begin (bytevector-u8-set! b (modulo n size) (modulo n 256)) (fill b (+ n 1) limit size))))')! vm.with_native_execution(fn (mut vm VM) !Value { b := bytevector([]u8{len: 518}) assert vm.call(vm.lookup('fill')!,[b,integer(0),integer(4099),integer(518)])!.whole() == 4099 for i, value in b.bytevector_items()! { last := i + ((4098 - i) / 518) * 518 assert value == u8(last % 256) } hits,deopts := vm.native_execution_stats() assert hits == 1 && deopts == 0 return unspecified() })! } fn test_native_bytevector_journal_boundary_and_exhaustion_replay_once() { for size in [1024,1025] { mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! vm.eval('(define (fill b i)(if (= i (bytevector-length b)) 0 (begin (bytevector-u8-set! b i (+ (bytevector-u8-ref b i) 1)) (fill b (+ i 1)))))')! vm.with_native_execution(fn [size] (mut vm VM) !Value { b := bytevector([]u8{len: size}) assert vm.call(vm.lookup('fill')!,[b,integer(0)])!.whole() == 0 for value in b.bytevector_items()! { assert value == 1 } hits,deopts := vm.native_execution_stats() if size == 1024 { assert hits == 1 && deopts == 0 } else { assert deopts == 1 } return unspecified() })! } } fn test_native_bytevector_and_vector_effects_rollback_with_scalar_captures() { mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! vm.eval('(define cells (let ((count 0)) (list (lambda(b v)(set! count (+ count 1)) (bytevector-u8-set! b 0 (+ (bytevector-u8-ref b 0) 1)) (vector-set! v 0 (+ (vector-ref v 0) 1)) (+ 9223372036854775807 1)) (lambda()count)))) (define work (car cells)) (define get (cadr cells))')! vm.with_native_execution(fn (mut vm VM) !Value { b := bytevector([u8(1)]);v := vector([integer(0)]) assert vm.call(vm.lookup('work')!,[b,v])!.tag == .big_integer assert b.bytevector_items()! == [u8(2)] assert v.vector_pointer().items[0].whole() == 1 assert vm.call(vm.lookup('get')!,[])!.whole() == 1 _,deopts := vm.native_execution_stats() assert deopts == 1 return unspecified() })! } fn test_native_bytevector_globals_captures_and_nested_vector_handles_refresh() { mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! vm.eval('(define global (bytevector 4)) (define (read-global)(bytevector-u8-ref global 0)) (define (make b)(lambda(n)(bytevector-u8-set! b 0 (+ (bytevector-u8-ref b 0) n)) (bytevector-u8-ref b 0))) (define a (make (bytevector 1))) (define b (make (bytevector 100))) (define (nested v)(let ((b (vector-ref v 0))) (bytevector-u8-set! b 0 (+ (bytevector-u8-ref b 0) 2)) b))')! vm.with_native_execution(fn (mut vm VM) !Value { assert vm.call(vm.lookup('read-global')!,[])!.whole() == 4 assert vm.call(vm.lookup('a')!,[integer(7)])!.whole() == 8 assert vm.call(vm.lookup('b')!,[integer(10)])!.whole() == 110 vm.eval('(set! global (bytevector 42)) (set! a (make (bytevector 20)))')! $if gcboehm ? { gc_collect() } assert vm.call(vm.lookup('read-global')!,[])!.whole() == 42 assert vm.call(vm.lookup('a')!,[integer(2)])!.whole() == 22 bytes := bytevector([u8(10)]) v := vector([bytes]) assert vm.call(vm.lookup('nested')!,[v])!.pointer() == bytes.pointer() assert bytes.bytevector_items()! == [u8(12)] _,deopts := vm.native_execution_stats() assert deopts == 0 return unspecified() })! } fn test_native_bytevector_literals_keep_readonly_flags_and_errors() { mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! vm.eval('(define literal #u8(4 5)) (define (read)(+ (bytevector-length literal) (bytevector-u8-ref literal 1))) (define (write b v)(vector-set! v 0 (+ (vector-ref v 0) 1)) (bytevector-u8-set! b 0 42))')! vm.with_native_execution(fn (mut vm VM) !Value { assert vm.call(vm.lookup('read')!,[])!.whole() == 7 literal := vm.lookup('literal')! v := vector([integer(0)]) if _ := vm.call(vm.lookup('write')!,[literal,v]) { return error('modified bytevector literal') } assert literal.bytevector_items()! == [u8(4),5] assert v.vector_pointer().items[0].whole() == 1 _,deopts := vm.native_execution_stats() assert deopts == 1 return unspecified() })! } struct NativeByteErrorCase { index Value value Value } fn native_bytes_test_error(mut vm VM, work Value, args []Value) string { if _ := vm.call(work,args) { return 'unexpectedly accepted' } else { return err.msg() } } fn test_native_bytevector_bad_types_ranges_and_bytes_preserve_vm_errors() { for bad in [ NativeByteErrorCase{integer(-1),integer(0)},NativeByteErrorCase{integer(2),integer(0)}, NativeByteErrorCase{real(0.0),integer(0)},NativeByteErrorCase{integer(0),integer(-1)}, NativeByteErrorCase{integer(0),integer(256)},NativeByteErrorCase{integer(0),real(1.0)}, NativeByteErrorCase{integer(0),boolean(false)},NativeByteErrorCase{integer(0),list([integer(1)])} ] { mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! vm.eval('(define (work b i x)(bytevector-u8-set! b 0 (+ (bytevector-u8-ref b 0) 1)) (bytevector-u8-set! b i x))')! work := vm.lookup('work')! expected := native_bytes_test_error(mut vm,work,[bytevector([u8(0)]),bad.index,bad.value]) assert expected != 'unexpectedly accepted' vm.with_native_execution(fn [bad,expected] (mut vm VM) !Value { b := bytevector([u8(0)]) actual := native_bytes_test_error(mut vm,vm.lookup('work')!,[b,bad.index,bad.value]) assert actual == expected assert b.bytevector_items()! == [u8(1)] return unspecified() })! } } fn test_native_bytevector_handles_stay_distinct_from_vectors_and_imported_pairs() { mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! vm.eval('(define (work xs b)(+ (car xs) (bytevector-u8-ref b 0))) (define (as-vector x)(vector-ref x 0)) (define (as-bytes x)(bytevector-u8-ref x 0))')! vm.with_native_execution(fn (mut vm VM) !Value { assert vm.call(vm.lookup('work')!,[list([integer(20)]),bytevector([u8(22)])])!.whole() == 42 if _ := vm.call(vm.lookup('as-vector')!,[bytevector([u8(1)])]) { return error('bytevector used as vector') } if _ := vm.call(vm.lookup('as-bytes')!,[vector([integer(1)])]) { return error('vector used as bytevector') } return unspecified() })! } fn test_native_bytevector_primitive_rebinding_invalidates_cached_guards() { mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! vm.eval('(define (work b)(bytevector-u8-set! b 0 (+ (bytevector-u8-ref b 0) 1)) (bytevector-u8-ref b 0))')! vm.with_native_execution(fn (mut vm VM) !Value { b := bytevector([u8(0)]) assert vm.call(vm.lookup('work')!,[b])!.whole() == 1 vm.eval('(set! bytevector-u8-set! (lambda(b i x)42))')! assert vm.call(vm.lookup('work')!,[b])!.whole() == 1 vm.eval('(set! bytevector-u8-ref (lambda(b i)99))')! assert vm.call(vm.lookup('work')!,[b])!.whole() == 99 assert b.bytevector_items()! == [u8(1)] return unspecified() })! } fn native_bytes_gc_fault(args &C.VileNativeValue, api &C.VileNativeHostApi, journal &C.VileNativeVectorJournal, out &C.VileNativeValue) { mut values := [3]C.VileNativeValue{} values[0] = unsafe { args[0] };values[1] = C.VileNativeValue{x: 0,k: 1} api.vector(api.context,journal,0,&values[0],2,out) values[0] = unsafe { *out };values[2] = C.VileNativeValue{x: 77,k: 1} api.vector(api.context,journal,5,&values[0],3,out) values[0] = unsafe { args[0] };values[2] = C.VileNativeValue{x: 0,k: 1} api.vector(api.context,journal,2,&values[0],3,out) $if gcboehm ? { gc_collect() } unsafe { *out = C.VileNativeValue{k: 6} } } fn native_bytes_gc_container() Value { return vector([bytevector([u8(33)])]) } fn test_native_bytevector_journal_keeps_owners_alive_across_collection() { v := native_bytes_gc_container() mut scope := NativeScope{} scope.begin_vector_transaction() api := C.VileNativeHostApi{context: voidptr(&scope),vector: native_vector_io,finish: native_vector_finish} scalar := native_scalar(v) assert scalar.k != 0 args := [C.VileNativeValue{x: scalar.x,k: scalar.k}] mut out := C.VileNativeValue{} unsafe { C.vile_native_test_fault_invoke(voidptr(native_bytes_gc_fault),&args[0],&api,&out) } assert out.k == 6 assert v.vector_pointer().items[0].bytevector_items()! == [u8(33)] } fn test_native_bytevector_callbacks_preserve_large_offsets_without_large_resident_buffers() { length := i64(2147483648) + 8192 raw := C.vile_native_bytes_map(length) assert raw != unsafe { nil } defer { C.vile_native_bytes_unmap(raw,length) } mut storage := []u8{} unsafe { storage.data = raw;storage.len = int(length);storage.cap = int(length);storage.flags.clear(.managed) } bytes := owned_bytevector(storage) mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! vm.eval('(define (work b i)(bytevector-u8-set! b i 42) (bytevector-u8-ref b i)) (define (fail b i)(bytevector-u8-set! b i (+ (bytevector-u8-ref b i) 1)) (+ 9223372036854775807 1))')! vm.with_native_execution(fn [bytes] (mut vm VM) !Value { for i in [i64(2147483647),2147483648,2147483664] { assert vm.call(vm.lookup('work')!,[bytes,integer(i)])!.whole() == 42 assert unsafe { (&u8(bytes.bytevector_pointer().bytes.data))[i] } == 42 } assert vm.call(vm.lookup('fail')!,[bytes,integer(2147483664)])!.tag == .big_integer assert unsafe { (&u8(bytes.bytevector_pointer().bytes.data))[i64(2147483664)] } == 43 return unspecified() })! } fn test_native_bytevector_operations_work_with_vile_frontend() { mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! vm.eval_alt('work := fn(b) { bytevector-u8-set!(b,0,42)\n bytevector-u8-ref(b,0) }')! vm.with_native_execution(fn (mut vm VM) !Value { assert vm.call(vm.lookup('work')!,[bytevector([u8(0)])])!.whole() == 42 hits,deopts := vm.native_execution_stats() assert hits == 1 && deopts == 0 return unspecified() })! } fn test_native_bytevector_loops_obey_vm_instruction_budgets() { mut vm := new_with_options(Options{max_steps: 200,max_frames: 0})! vm.eval('(define (fill b n)(bytevector-u8-set! b 0 (modulo n 256)) (fill b (+ n 1)))')! vm.with_native_execution(fn (mut vm VM) !Value { if _ := vm.call(vm.lookup('fill')!,[bytevector([u8(0)]),integer(0)]) { return error('bytevector loop bypassed its budget') } hits,_ := vm.native_execution_stats() assert hits == 0 return unspecified() })! } fn test_native_bytevector_and_vector_writes_share_the_transaction_budget() { mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! vm.eval('(define (fill b i)(if (= i (bytevector-length b)) 0 (begin (bytevector-u8-set! b i (+ (bytevector-u8-ref b i) 1)) (fill b (+ i 1))))) (define (work b v)(vector-set! v 0 (+ (vector-ref v 0) 1)) (fill b 0))')! vm.with_native_execution(fn (mut vm VM) !Value { b := bytevector([]u8{len: 1024});v := vector([integer(0)]) assert vm.call(vm.lookup('work')!,[b,v])!.whole() == 0 for value in b.bytevector_items()! { assert value == 1 } assert v.vector_pointer().items[0].whole() == 1 _,deopts := vm.native_execution_stats() assert deopts == 1 return unspecified() })! } fn test_native_bytevector_publication_keeps_flags_identity_and_replay_effects() { mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})! vm.eval('(define (store b v)(bytevector-u8-set! b 0 (+ (bytevector-u8-ref b 0) 1)) (vector-set! v 0 b) b) (define (pass b v)(vector-set! v 0 b) b) (define cells (let ((held #f))(list (lambda(b v)(vector-set! v 0 (+ (vector-ref v 0) 1)) (set! held b) 42) (lambda()held)))) (define capture (car cells)) (define get (cadr cells))')! vm.with_native_execution(fn (mut vm VM) !Value { b := bytevector([u8(1)]);v := vector([integer(0)]) assert vm.call(vm.lookup('store')!,[b,v])!.pointer() == b.pointer() assert b.bytevector_items()! == [u8(2)] assert v.vector_pointer().items[0].pointer() == b.pointer() literal := vm.eval('#u8(7)')! returned := vm.call(vm.lookup('pass')!,[literal,v])! assert returned.pointer() == literal.pointer() && returned.read_only assert v.vector_pointer().items[0].read_only counter := vector([integer(0)]) assert vm.call(vm.lookup('capture')!,[b,counter])!.whole() == 42 assert counter.vector_pointer().items[0].whole() == 1 assert vm.call(vm.lookup('get')!,[])!.pointer() == b.pointer() _,deopts := vm.native_execution_stats() assert deopts == 1 return unspecified() })! }