module scheme import io import os fn (vm &VM) checked_binary_input(value Value) !&InputPort { port := vm.checked_input(value)! if !port.binary { return failure('expected a binary input port') } return port } fn (vm &VM) checked_binary_output(value Value) !&OutputPort { port := vm.checked_output(value)! if !port.binary { return failure('expected a binary output port') } return port } pub fn (mut vm VM) open_input_bytevector(value Value) !Value { vm.ready()! bytes := bytevector_data(value)!.bytes return vm.memory_input_port(copy_byte_range(bytes, 0, bytes.len), true) } pub fn (mut vm VM) open_output_bytevector() !Value { return vm.memory_output_port(true) } pub fn (mut vm VM) get_output_bytevector(value Value) !Value { vm.ready()! port := vm.output_owner(value)! if isnil(port.memory_output) || !port.binary { return failure('expected a bytevector output port') } if port.busy { return failure('output port operation is already active') } return bytevector(port.memory_output.bytes) } pub fn (mut vm VM) open_binary_input_file(path string) !Value { value := vm.open_input_file(path)! value.input_pointer().binary = true return value } pub fn (mut vm VM) open_binary_output_file(path string) !Value { value := vm.open_output_file(path)! value.output_pointer().binary = true return value } pub fn (mut vm VM) read_u8(value Value) !Value { vm.ready()! return vm.port_byte(value, true) } pub fn (mut vm VM) peek_u8(value Value) !Value { vm.ready()! return vm.port_byte(value, false) } @[direct_array_access] fn (mut vm VM) port_byte(value Value, advance bool) !Value { mut port := vm.checked_binary_input(value)! if !isnil(custom_input_resource(value)) { return vm.custom_input_host_operation(if advance { PortIoOperation.read_u8 } else { PortIoOperation.peek_u8 }, value, unspecified(), 0, 0, unspecified()) } port.busy = true defer { port.busy = false } if !port.ensure_bytes(1)! { return eof_value() } byte := port.buffer[port.position] if advance { port.position++ } return integer(byte) } pub fn (mut vm VM) u8_ready(value Value) !bool { vm.ready()! mut port := vm.checked_binary_input(value)! if !isnil(custom_input_resource(value)) { if port.position < port.buffer.len || port.ended { return true } return failure('custom port does not support nonblocking readiness') } port.busy = true defer { port.busy = false } for port.position == port.buffer.len && !port.ended { if !port.fill_buffer(true)! { return false } } return true } pub fn (mut vm VM) read_bytevector(value Value, count int) !Value { vm.ready()! if count < 0 { return failure('read-bytevector count must be nonnegative') } mut port := vm.checked_binary_input(value)! if !isnil(custom_input_resource(value)) { return vm.custom_input_host_operation(.get_bytes, value, unspecified(), 0, count, unspecified()) } port.busy = true defer { port.busy = false port.release_large_frame() } available := port.buffer.len - port.position if count == 0 { return owned_bytevector([]u8{}) } if available >= count || port.ended { length := int_min(count, available) if length == 0 { return eof_value() } bytes := copy_byte_range(port.buffer, port.position, port.position + length) port.position += length return owned_bytevector(bytes) } // Never reserve the caller's entire count up front: an enormous count on // a short stream still needs only the storage for bytes actually received. mut bytes := []u8{cap: int_min(count, 4096)} unsafe { bytes.flags.set(.noslices) } for bytes.len < count && port.ensure_bytes(1)! { length := int_min(count - bytes.len, port.buffer.len - port.position) bytes << unsafe { port.buffer[port.position..port.position + length] } port.position += length } if bytes.len == 0 { return eof_value() } return owned_bytevector(bytes) } // read_bytevector_into drains lookahead, then lets the Reader fill the final // bytevector directly. Its fixed storage stays rooted through callbacks/GC. pub fn (mut vm VM) read_bytevector_into(value Value, target Value, start int, end int) !Value { vm.ready()! target.require_mutable(.bytevector)! mut data := target.bytevector_pointer() if start < 0 || end < start || end > data.bytes.len { return failure('bytevector range out of bounds') } mut port := vm.checked_binary_input(value)! if !isnil(custom_input_resource(value)) { return vm.custom_input_host_operation(.get_into, value, target, start, end, unspecified()) } port.busy = true defer { port.busy = false port.release_large_frame() } mut offset := start for offset < end { available := port.buffer.len - port.position if available > 0 { length := int_min(end - offset, available) unsafe { vmemmove(&u8(data.bytes.data) + offset, &u8(port.buffer.data) + port.position, length) } port.position += length offset += length } else { if port.ended { break } length := int_min(end - offset, port.read_size) mut destination := unsafe { (&u8(data.bytes.data) + offset).vbytes(length) } count := port.read_binary_chunk(mut destination)! if count == 0 { break } offset += count } } if offset == start && end != start { return eof_value() } return integer(offset - start) } fn (mut port InputPort) read_binary_chunk(mut destination []u8) !int { count := port.source.read(mut destination) or { if err is io.Eof || err is os.Eof { port.ended = true return 0 } return err } if count < 0 || count > destination.len { return failure('reader returned an invalid byte count') } if count == 0 { port.ended = true } return count } pub fn (mut vm VM) write_u8(value Value, byte u8) ! { vm.ready()! mut port := vm.checked_binary_output(value)! if !isnil(custom_output_resource(value)) { vm.custom_output_host_operation(.write_bytes, value, owned_bytevector([byte]), 0, 1, unspecified())! return } // write_bytes consumes this stack byte synchronously, including retries. port.write_bytes(unsafe { (&byte).vbytes(1) })! } pub fn (mut vm VM) write_bytevector(value Value, source Value, start int, end int) ! { vm.ready()! data := bytevector_data(source)! if start < 0 || end < start || end > data.bytes.len { return failure('bytevector range out of bounds') } mut port := vm.checked_binary_output(value)! if !isnil(custom_output_resource(value)) { vm.custom_output_host_operation(.write_bytes, value, source, start, end, unspecified())! return } port.write_bytes(unsafe { data.bytes[start..end] })! } const binary_port_native_descriptors = [ NativeDescriptor{'open-input-bytevector', NativeFn(port_open_input_bytevector)} NativeDescriptor{'open-output-bytevector', port_open_output_bytevector} NativeDescriptor{'get-output-bytevector', port_get_output_bytevector} NativeDescriptor{'open-binary-input-file', port_open_binary_input} NativeDescriptor{'open-binary-output-file', port_open_binary_output} NativeDescriptor{'read-u8', port_read_u8} NativeDescriptor{'peek-u8', port_peek_u8} NativeDescriptor{'u8-ready?', port_u8_ready} NativeDescriptor{'read-bytevector', port_read_bytevector} NativeDescriptor{'read-bytevector!', port_read_bytevector_into} NativeDescriptor{'write-u8', port_write_u8} NativeDescriptor{'write-bytevector', port_write_bytevector}, ]! fn (mut vm VM) install_binary_port_primitives() { for index in 0 .. binary_port_native_descriptors.len { entry := &binary_port_native_descriptors[index] vm.bind_global(entry.name, vm.port_native_procedure(entry)) } } @[direct_array_access] fn port_open_input_bytevector(mut vm VM, args []Value) !Value { check_arity('open-input-bytevector', args.len, 1, 1)! return vm.open_input_bytevector(args[0]) } @[direct_array_access] fn port_open_output_bytevector(mut vm VM, args []Value) !Value { check_arity('open-output-bytevector', args.len, 0, 0)! return vm.open_output_bytevector() } @[direct_array_access] fn port_get_output_bytevector(mut vm VM, args []Value) !Value { check_arity('get-output-bytevector', args.len, 1, 1)! return vm.get_output_bytevector(args[0]) } @[direct_array_access] fn port_open_binary_input(mut vm VM, args []Value) !Value { check_arity('open-binary-input-file', args.len, 1, 1)! return vm.open_binary_input_file(string_data(args[0])!) } @[direct_array_access] fn port_open_binary_output(mut vm VM, args []Value) !Value { check_arity('open-binary-output-file', args.len, 1, 1)! return vm.open_binary_output_file(string_data(args[0])!) } @[direct_array_access] fn port_read_u8(mut vm VM, args []Value) !Value { check_arity('read-u8', args.len, 0, 1)! return vm.port_byte(if args.len == 0 { vm.current_input_value()! } else { args[0] }, true) } @[direct_array_access] fn port_peek_u8(mut vm VM, args []Value) !Value { check_arity('peek-u8', args.len, 0, 1)! return vm.port_byte(if args.len == 0 { vm.current_input_value()! } else { args[0] }, false) } @[direct_array_access] fn port_u8_ready(mut vm VM, args []Value) !Value { check_arity('u8-ready?', args.len, 0, 1)! return boolean(vm.u8_ready(if args.len == 0 { vm.current_input_value()! } else { args[0] })!) } @[direct_array_access] fn port_read_bytevector(mut vm VM, args []Value) !Value { check_arity('read-bytevector', args.len, 1, 2)! count := allocation_count(args[0], 1)! return vm.read_bytevector(if args.len == 1 { vm.current_input_value()! } else { args[1] }, count) } @[direct_array_access] fn port_read_bytevector_into(mut vm VM, args []Value) !Value { check_arity('read-bytevector!', args.len, 1, 4)! data := bytevector_data(args[0])! start, end := sequence_range(args, 2, data.bytes.len)! return vm.read_bytevector_into(if args.len == 1 { vm.current_input_value()! } else { args[1] }, args[0], start, end) } @[direct_array_access] fn port_write_u8(mut vm VM, args []Value) !Value { check_arity('write-u8', args.len, 1, 2)! byte := byte_value(args[0])! vm.write_u8(if args.len == 1 { vm.current_output_value()! } else { args[1] }, byte)! return unspecified() } @[direct_array_access] fn port_write_bytevector(mut vm VM, args []Value) !Value { check_arity('write-bytevector', args.len, 1, 4)! data := bytevector_data(args[0])! start, end := sequence_range(args, 2, data.bytes.len)! vm.write_bytevector(if args.len == 1 { vm.current_output_value()! } else { args[1] }, args[0], start, end)! return unspecified() }