module scheme import encoding.binary // Endian windows borrow the fixed byte storage synchronously. No callback runs // between validation and a store; all failing checks precede the first write. fn bytevector_little(value Value) !bool { if value.tag != .symbol { return failure('expected big or little endianness symbol') } return match value.text() { 'big' { false } 'little' { true } else { return failure('expected big or little endianness symbol') } } } fn bytevector_native_little() bool { $if little_endian { return true } $else { return false } } fn bytevector_window(value Value, index Value, width int, aligned bool) !int { data := bytevector_data(value)! k := index.as_integer()! if width < 1 || width > data.bytes.len || k < 0 || k > data.bytes.len - width { return failure('bytevector index out of range') } if aligned && k % width != 0 { return failure('native bytevector index must be aligned') } return int(k) } @[direct_array_access] fn bytevector_word(bytes []u8, at int, width int, little bool) u64 { return match width { 1 { u64(bytes[at]) } 2 { if little { u64(binary.little_endian_u16_at(bytes, at)) } else { u64(binary.big_endian_u16_at(bytes, at)) } } 4 { if little { u64(binary.little_endian_u32_at(bytes, at)) } else { u64(binary.big_endian_u32_at(bytes, at)) } } else { if little { binary.little_endian_u64_at(bytes, at) } else { binary.big_endian_u64_at(bytes, at) } } } } // Every caller validated [at, at+width). Tail slicing avoids the compiler's // max_i32 end sentinel; the borrowed header gets its exact length explicitly. // No mutable slice clone or escaping view is introduced. @[inline] fn bytevector_borrow_bytes(bytes []u8, at int, width int) []u8 { mut view := unsafe { bytes[at..] } unsafe { view.len = width view.cap = width } return view } @[direct_array_access] fn bytevector_put_word(mut bytes []u8, at int, width int, little bool, word u64) { // A synchronous bounded view also keeps stdlib store indices small. The // current compiler narrows array-store index temporaries to C int. mut window := bytevector_borrow_bytes(bytes, at, width) match width { 1 { unsafe { (&u8(window.data))[0] = u8(word) } } 2 { if little { binary.little_endian_put_u16_at(mut window, u16(word), 0) } else { binary.big_endian_put_u16_at(mut window, u16(word), 0) } } 4 { if little { binary.little_endian_put_u32_at(mut window, u32(word), 0) } else { binary.big_endian_put_u32_at(mut window, u32(word), 0) } } else { if little { binary.little_endian_put_u64_at(mut window, word, 0) } else { binary.big_endian_put_u64_at(mut window, word, 0) } } } } fn bytevector_signed_word(word u64, width int) i64 { bits := width * 8 if word & (u64(1) << (bits - 1)) == 0 { return i64(word) } mask := if bits == 64 { ~u64(0) } else { (u64(1) << bits) - 1 } return -1 - i64((~word) & mask) } @[direct_array_access] fn bytevector_fixed_integer(args []Value, name string, width int, signed bool, native bool, store bool) !Value { count := 2 + int(!native && width > 1) + int(store) check_arity(name, args.len, count, count)! at := bytevector_window(args[0], args[1], width, native)! little := if native || width == 1 { bytevector_native_little() } else { bytevector_little(args[args.len - 1])! } if !store { bytes := args[0].bytevector_pointer().bytes word := bytevector_word(bytes, at, width, little) if signed { return integer(bytevector_signed_word(word, width)) } if word <= u64(max_i64) { return integer(i64(word)) } return bytevector_integer_read(bytes, at, width, little, false) } args[0].require_mutable(.bytevector)! bytevector_integer_range(args[2], width, signed)! word := bytevector_low_word(args[2]) mut data := args[0].bytevector_pointer() bytevector_put_word(mut data.bytes, at, width, little, word) return unspecified() } @[direct_array_access] fn bytevector_fixed_float(args []Value, name string, width int, native bool, store bool) !Value { count := 2 + int(!native) + int(store) check_arity(name, args.len, count, count)! at := bytevector_window(args[0], args[1], width, native)! little := if native { bytevector_native_little() } else { bytevector_little(args[args.len - 1])! } mut data := args[0].bytevector_pointer() if !store { if width == 4 { return real(f64(if little { binary.little_endian_f32_at(data.bytes, at) } else { binary.big_endian_f32_at(data.bytes, at) })) } return real(if little { binary.little_endian_f64_at(data.bytes, at) } else { binary.big_endian_f64_at(data.bytes, at) }) } args[0].require_mutable(.bytevector)! number := args[2].as_real()! mut window := bytevector_borrow_bytes(data.bytes, at, width) // Exact values retain the runtime's checked finite conversion policy. A finite // binary64 value may round to binary32 infinity, as IEEE conversion requires. if width == 4 { narrowed := f32(number) if little { binary.little_endian_put_f32_at(mut window, narrowed, 0) } else { binary.big_endian_put_f32_at(mut window, narrowed, 0) } } else { if little { binary.little_endian_put_f64_at(mut window, number, 0) } else { binary.big_endian_put_f64_at(mut window, number, 0) } } return unspecified() } fn bytevector_native_endianness(mut _vm VM, args []Value) !Value { check_arity('native-endianness', args.len, 0, 0)! return text_value(.symbol, if bytevector_native_little() { 'little' } else { 'big' }) } @[direct_array_access] fn bytevector_equal_args(mut _vm VM, args []Value) !Value { check_arity('bytevector=?', args.len, 2, 2)! a, b := bytevector_data(args[0])!, bytevector_data(args[1])! return boolean(a.bytes == b.bytes) } @[direct_array_access] fn bytevector_fill_args(mut _vm VM, args []Value) !Value { check_arity('bytevector-fill!', args.len, 2, 2)! args[0].require_mutable(.bytevector)! fill := args[1].as_integer()! if fill < -128 || fill > 255 { return failure('bytevector fill must be an exact integer in -128..255') } mut data := args[0].bytevector_pointer() if data.bytes.len > 0 { unsafe { vmemset(data.bytes.data, int(u8(fill)), data.bytes.len) } } return unspecified() }