| 1 | module scheme |
| 2 | |
| 3 | import math |
| 4 | |
| 5 | // The bridge keeps original Value handles in its C stack frame until commit or |
| 6 | // rollback. The reusable table contains only indices, so arena metadata never |
| 7 | // becomes the sole conservative-GC root of an overwritten payload. |
| 8 | fn (mut scope NativeScope) begin_vector_transaction() { |
| 9 | scope.vector_epoch++ |
| 10 | if scope.vector_epoch == 0 { |
| 11 | for i in 0 .. scope.vector_buckets.len { scope.vector_buckets[i] = NativeVectorBucket{} } |
| 12 | scope.vector_epoch = 1 |
| 13 | } |
| 14 | } |
| 15 | |
| 16 | // An uninitialized handle cannot be a native result. Keep unsupported packets |
| 17 | // private with that tag; callers validate it before storing or committing. |
| 18 | fn native_host_value(value C.VileNativeValue) Value { |
| 19 | return match value.k { |
| 20 | 1 { integer(value.x) } |
| 21 | 2 { boolean(value.x != 0) } |
| 22 | 3 { if value.x == 0 { nil_value() } else { Value{tag: .uninitialized} } } |
| 23 | 5 { unspecified() } |
| 24 | 7 { real(math.f64_from_bits(u64(value.x))) } |
| 25 | 8, 9 { |
| 26 | Value{ tag: .vector, read_only: value.k == 9, |
| 27 | bits: ValueBits{ payload: unsafe { voidptr(value.x) } } } |
| 28 | } |
| 29 | 10, 11 { |
| 30 | Value{tag: .bytevector, read_only: value.k == 11, |
| 31 | bits: ValueBits{payload: unsafe { voidptr(value.x) }}} |
| 32 | } |
| 33 | 12, 13 { |
| 34 | Value{tag: .string, read_only: value.k == 13, |
| 35 | bits: ValueBits{payload: unsafe { voidptr(value.x) }}} |
| 36 | } |
| 37 | 14 { |
| 38 | // Validate the full-width packet before narrowing it to a rune. |
| 39 | if value.x < 0 || value.x > 0x10ffff || (value.x >= 0xd800 && value.x <= 0xdfff) { |
| 40 | return Value{tag: .uninitialized} |
| 41 | } |
| 42 | character(rune(value.x)) or { Value{tag: .uninitialized} } |
| 43 | } |
| 44 | else { Value{tag: .uninitialized} } |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | // Called only from generated code, with handles introduced by native_scalar. |
| 49 | // The vector callback allocates nothing and never reenters Scheme. Captured |
| 50 | // scalar changes are published only by finish after all effects validate. |
| 51 | @[direct_array_access] |
| 52 | fn native_vector_io(context voidptr, journal &C.VileNativeVectorJournal, operation i64, args &C.VileNativeValue, argc i64, out &C.VileNativeValue) { |
| 53 | unsafe { *out = C.VileNativeValue{ k: 6 } } |
| 54 | if operation in [i64(6), i64(7)] { |
| 55 | native_text_io(operation, args, argc, out) |
| 56 | return |
| 57 | } |
| 58 | if operation in [i64(3), i64(4), i64(5)] { |
| 59 | native_bytevector_io(context, journal, operation, args, argc, out) |
| 60 | return |
| 61 | } |
| 62 | if argc != (if operation == 0 { 2 } else if operation == 1 { 1 } else { 3 }) { return } |
| 63 | target := unsafe { args[0] } |
| 64 | if target.k !in [i64(8), i64(9)] { return } |
| 65 | mut content := unsafe { &Vector(voidptr(target.x)) } |
| 66 | if operation == 1 { |
| 67 | unsafe { *out = C.VileNativeValue{ x: content.items.len, k: 1 } } |
| 68 | return |
| 69 | } |
| 70 | index := unsafe { args[1] } |
| 71 | if index.k != 1 || index.x < 0 || index.x >= content.items.len { return } |
| 72 | i := int(index.x) |
| 73 | if operation == 0 { |
| 74 | scalar := native_scalar(content.items[i]) |
| 75 | if scalar.k == 0 { return } |
| 76 | unsafe { *out = C.VileNativeValue{ x: scalar.x, k: scalar.k } } |
| 77 | return |
| 78 | } |
| 79 | if operation != 2 || target.k != 8 { return } |
| 80 | value := native_host_value(unsafe { args[2] }) |
| 81 | // String publication keeps the VM's complete retention path. This native |
| 82 | // extension only borrows existing text for reads and synchronous returns. |
| 83 | if value.tag in [.uninitialized, .string] { return } |
| 84 | mut scope := unsafe { &NativeScope(context) } |
| 85 | if !scope.record_vector_write(journal, content, i) { return } |
| 86 | container_store_validated(mut content.items, i, value) |
| 87 | unsafe { *out = C.VileNativeValue{ k: 5 } } |
| 88 | } |
| 89 | |
| 90 | @[direct_array_access] |
| 91 | fn (mut scope NativeScope) record_vector_write(journal &C.VileNativeVectorJournal, vector &Vector, index int) bool { |
| 92 | return scope.record_sequence_write(journal, voidptr(vector), i64(index), |
| 93 | unsafe { voidptr(&vector.items[index]) }, int(sizeof(Value))) |
| 94 | } |
| 95 | |
| 96 | // Negative indexes encode byte cells; ordinary vector indexes remain positive. |
| 97 | // Both sequence kinds share the same first-touch table and transaction budget. |
| 98 | @[direct_array_access; inline] |
| 99 | fn (mut scope NativeScope) record_sequence_write(journal &C.VileNativeVectorJournal, owner voidptr, index i64, original voidptr, width int) bool { |
| 100 | if journal.capacity <= 0 { return false } |
| 101 | mut transaction := unsafe { journal } |
| 102 | key := (u64(owner) >> 4) ^ (u64(index) * u64(0x9e3779b97f4a7c15)) |
| 103 | mut slot := int(key & 2047) |
| 104 | for _ in 0 .. scope.vector_buckets.len { |
| 105 | bucket := scope.vector_buckets[slot] |
| 106 | if bucket.epoch != scope.vector_epoch { |
| 107 | if transaction.count >= transaction.capacity { return false } |
| 108 | entry := unsafe { &transaction.entries[transaction.count] } |
| 109 | unsafe { |
| 110 | *entry = C.VileNativeVectorWrite{ vector: owner, index: index } |
| 111 | if width == 1 { entry.original[0] = u64(*(&u8(original))) } |
| 112 | else { vmemcpy(&entry.original[0], original, width) } |
| 113 | } |
| 114 | scope.vector_buckets[slot] = NativeVectorBucket{ epoch: scope.vector_epoch, index: int(transaction.count) } |
| 115 | transaction.count++ |
| 116 | return true |
| 117 | } |
| 118 | entry := unsafe { &transaction.entries[bucket.index] } |
| 119 | if entry.vector == owner && entry.index == index { return true } |
| 120 | slot = (slot + 1) & 2047 |
| 121 | } |
| 122 | return false |
| 123 | } |
| 124 | |
| 125 | @[direct_array_access] |
| 126 | fn native_vector_finish(_context voidptr, journal &C.VileNativeVectorJournal, out &C.VileNativeValue) { |
| 127 | mut scope := unsafe { &NativeScope(_context) } |
| 128 | if native_host_value(unsafe { *out }).tag != .uninitialized { |
| 129 | if scope.capture_writes_valid() { |
| 130 | scope.commit_capture_writes() |
| 131 | return |
| 132 | } |
| 133 | } |
| 134 | mut i := journal.count |
| 135 | for i > 0 { |
| 136 | i-- |
| 137 | entry := unsafe { &journal.entries[i] } |
| 138 | if entry.index < 0 { |
| 139 | content := unsafe { &Bytevector(entry.vector) } |
| 140 | unsafe { (&u8(content.bytes.data))[-entry.index - 1] = u8(entry.original[0]) } |
| 141 | continue |
| 142 | } |
| 143 | mut original := Value{} |
| 144 | unsafe { vmemcpy(&original, &entry.original[0], int(sizeof(Value))) } |
| 145 | mut vector := unsafe { &Vector(entry.vector) } |
| 146 | container_store_validated(mut vector.items, int(entry.index), original) |
| 147 | } |
| 148 | unsafe { *out = C.VileNativeValue{ k: 6 } } |
| 149 | } |
| 150 | |