module scheme import math // The bridge keeps original Value handles in its C stack frame until commit or // rollback. The reusable table contains only indices, so arena metadata never // becomes the sole conservative-GC root of an overwritten payload. fn (mut scope NativeScope) begin_vector_transaction() { scope.vector_epoch++ if scope.vector_epoch == 0 { for i in 0 .. scope.vector_buckets.len { scope.vector_buckets[i] = NativeVectorBucket{} } scope.vector_epoch = 1 } } // An uninitialized handle cannot be a native result. Keep unsupported packets // private with that tag; callers validate it before storing or committing. fn native_host_value(value C.VileNativeValue) Value { return match value.k { 1 { integer(value.x) } 2 { boolean(value.x != 0) } 3 { if value.x == 0 { nil_value() } else { Value{tag: .uninitialized} } } 5 { unspecified() } 7 { real(math.f64_from_bits(u64(value.x))) } 8, 9 { Value{ tag: .vector, read_only: value.k == 9, bits: ValueBits{ payload: unsafe { voidptr(value.x) } } } } 10, 11 { Value{tag: .bytevector, read_only: value.k == 11, bits: ValueBits{payload: unsafe { voidptr(value.x) }}} } 12, 13 { Value{tag: .string, read_only: value.k == 13, bits: ValueBits{payload: unsafe { voidptr(value.x) }}} } 14 { // Validate the full-width packet before narrowing it to a rune. if value.x < 0 || value.x > 0x10ffff || (value.x >= 0xd800 && value.x <= 0xdfff) { return Value{tag: .uninitialized} } character(rune(value.x)) or { Value{tag: .uninitialized} } } else { Value{tag: .uninitialized} } } } // Called only from generated code, with handles introduced by native_scalar. // The vector callback allocates nothing and never reenters Scheme. Captured // scalar changes are published only by finish after all effects validate. @[direct_array_access] fn native_vector_io(context voidptr, journal &C.VileNativeVectorJournal, operation i64, args &C.VileNativeValue, argc i64, out &C.VileNativeValue) { unsafe { *out = C.VileNativeValue{ k: 6 } } if operation in [i64(6), i64(7)] { native_text_io(operation, args, argc, out) return } if operation in [i64(3), i64(4), i64(5)] { native_bytevector_io(context, journal, operation, args, argc, out) return } if argc != (if operation == 0 { 2 } else if operation == 1 { 1 } else { 3 }) { return } target := unsafe { args[0] } if target.k !in [i64(8), i64(9)] { return } mut content := unsafe { &Vector(voidptr(target.x)) } if operation == 1 { unsafe { *out = C.VileNativeValue{ x: content.items.len, k: 1 } } return } index := unsafe { args[1] } if index.k != 1 || index.x < 0 || index.x >= content.items.len { return } i := int(index.x) if operation == 0 { scalar := native_scalar(content.items[i]) if scalar.k == 0 { return } unsafe { *out = C.VileNativeValue{ x: scalar.x, k: scalar.k } } return } if operation != 2 || target.k != 8 { return } value := native_host_value(unsafe { args[2] }) // String publication keeps the VM's complete retention path. This native // extension only borrows existing text for reads and synchronous returns. if value.tag in [.uninitialized, .string] { return } mut scope := unsafe { &NativeScope(context) } if !scope.record_vector_write(journal, content, i) { return } container_store_validated(mut content.items, i, value) unsafe { *out = C.VileNativeValue{ k: 5 } } } @[direct_array_access] fn (mut scope NativeScope) record_vector_write(journal &C.VileNativeVectorJournal, vector &Vector, index int) bool { return scope.record_sequence_write(journal, voidptr(vector), i64(index), unsafe { voidptr(&vector.items[index]) }, int(sizeof(Value))) } // Negative indexes encode byte cells; ordinary vector indexes remain positive. // Both sequence kinds share the same first-touch table and transaction budget. @[direct_array_access; inline] fn (mut scope NativeScope) record_sequence_write(journal &C.VileNativeVectorJournal, owner voidptr, index i64, original voidptr, width int) bool { if journal.capacity <= 0 { return false } mut transaction := unsafe { journal } key := (u64(owner) >> 4) ^ (u64(index) * u64(0x9e3779b97f4a7c15)) mut slot := int(key & 2047) for _ in 0 .. scope.vector_buckets.len { bucket := scope.vector_buckets[slot] if bucket.epoch != scope.vector_epoch { if transaction.count >= transaction.capacity { return false } entry := unsafe { &transaction.entries[transaction.count] } unsafe { *entry = C.VileNativeVectorWrite{ vector: owner, index: index } if width == 1 { entry.original[0] = u64(*(&u8(original))) } else { vmemcpy(&entry.original[0], original, width) } } scope.vector_buckets[slot] = NativeVectorBucket{ epoch: scope.vector_epoch, index: int(transaction.count) } transaction.count++ return true } entry := unsafe { &transaction.entries[bucket.index] } if entry.vector == owner && entry.index == index { return true } slot = (slot + 1) & 2047 } return false } @[direct_array_access] fn native_vector_finish(_context voidptr, journal &C.VileNativeVectorJournal, out &C.VileNativeValue) { mut scope := unsafe { &NativeScope(_context) } if native_host_value(unsafe { *out }).tag != .uninitialized { if scope.capture_writes_valid() { scope.commit_capture_writes() return } } mut i := journal.count for i > 0 { i-- entry := unsafe { &journal.entries[i] } if entry.index < 0 { content := unsafe { &Bytevector(entry.vector) } unsafe { (&u8(content.bytes.data))[-entry.index - 1] = u8(entry.original[0]) } continue } mut original := Value{} unsafe { vmemcpy(&original, &entry.original[0], int(sizeof(Value))) } mut vector := unsafe { &Vector(entry.vector) } container_store_validated(mut vector.items, int(entry.index), original) } unsafe { *out = C.VileNativeValue{ k: 6 } } }