vile / scheme / native_bytes_d_vile_native.c.v
31 lines · 30 sloc · 1.3 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3// Bytevector storage is fixed for its lifetime. Validate through the VM type,
4// and retain only its owner handle in the existing C-stack undo journal.
5@[direct_array_access]
6fn native_bytevector_io(context voidptr, journal &C.VileNativeVectorJournal, operation i64, args &C.VileNativeValue, argc i64, out &C.VileNativeValue) {
7 if argc != (if operation == 3 { 2 } else if operation == 4 { 1 } else { 3 }) { return }
8 target := unsafe { args[0] }
9 if target.k !in [i64(10), i64(11)] { return }
10 content := unsafe { &Bytevector(voidptr(target.x)) }
11 if operation == 4 {
12 unsafe { *out = C.VileNativeValue{x: content.bytes.len, k: 1} }
13 return
14 }
15 index := unsafe { args[1] }
16 if index.k != 1 || index.x < 0 || index.x >= content.bytes.len { return }
17 if operation == 3 {
18 unsafe { *out = C.VileNativeValue{x: (&u8(content.bytes.data))[index.x], k: 1} }
19 return
20 }
21 if operation != 5 || target.k != 10 { return }
22 value := unsafe { args[2] }
23 if value.k != 1 || value.x < 0 || value.x > 255 { return }
24 mut scope := unsafe { &NativeScope(context) }
25 address := unsafe { voidptr(&(&u8(content.bytes.data))[index.x]) }
26 if !scope.record_sequence_write(journal, voidptr(content), -index.x - 1, address, 1) { return }
27 unsafe {
28 (&u8(content.bytes.data))[index.x] = u8(value.x)
29 *out = C.VileNativeValue{k: 5}
30 }
31}
32