| 1 | module scheme |
| 2 | |
| 3 | import io |
| 4 | import os |
| 5 | |
| 6 | fn (vm &VM) checked_binary_input(value Value) !&InputPort { |
| 7 | port := vm.checked_input(value)! |
| 8 | if !port.binary { return failure('expected a binary input port') } |
| 9 | return port |
| 10 | } |
| 11 | |
| 12 | fn (vm &VM) checked_binary_output(value Value) !&OutputPort { |
| 13 | port := vm.checked_output(value)! |
| 14 | if !port.binary { return failure('expected a binary output port') } |
| 15 | return port |
| 16 | } |
| 17 | |
| 18 | pub fn (mut vm VM) open_input_bytevector(value Value) !Value { |
| 19 | vm.ready()! |
| 20 | bytes := bytevector_data(value)!.bytes |
| 21 | return vm.memory_input_port(copy_byte_range(bytes, 0, bytes.len), true) |
| 22 | } |
| 23 | |
| 24 | pub fn (mut vm VM) open_output_bytevector() !Value { |
| 25 | return vm.memory_output_port(true) |
| 26 | } |
| 27 | |
| 28 | pub fn (mut vm VM) get_output_bytevector(value Value) !Value { |
| 29 | vm.ready()! |
| 30 | port := vm.output_owner(value)! |
| 31 | if isnil(port.memory_output) || !port.binary { |
| 32 | return failure('expected a bytevector output port') |
| 33 | } |
| 34 | if port.busy { return failure('output port operation is already active') } |
| 35 | return bytevector(port.memory_output.bytes) |
| 36 | } |
| 37 | |
| 38 | pub fn (mut vm VM) open_binary_input_file(path string) !Value { |
| 39 | value := vm.open_input_file(path)! |
| 40 | value.input_pointer().binary = true |
| 41 | return value |
| 42 | } |
| 43 | |
| 44 | pub fn (mut vm VM) open_binary_output_file(path string) !Value { |
| 45 | value := vm.open_output_file(path)! |
| 46 | value.output_pointer().binary = true |
| 47 | return value |
| 48 | } |
| 49 | |
| 50 | pub fn (mut vm VM) read_u8(value Value) !Value { |
| 51 | vm.ready()! |
| 52 | return vm.port_byte(value, true) |
| 53 | } |
| 54 | |
| 55 | pub fn (mut vm VM) peek_u8(value Value) !Value { |
| 56 | vm.ready()! |
| 57 | return vm.port_byte(value, false) |
| 58 | } |
| 59 | |
| 60 | @[direct_array_access] |
| 61 | fn (mut vm VM) port_byte(value Value, advance bool) !Value { |
| 62 | mut port := vm.checked_binary_input(value)! |
| 63 | if !isnil(custom_input_resource(value)) { |
| 64 | return vm.custom_input_host_operation(if advance { |
| 65 | PortIoOperation.read_u8 |
| 66 | } else { |
| 67 | PortIoOperation.peek_u8 |
| 68 | }, value, unspecified(), 0, 0, unspecified()) |
| 69 | } |
| 70 | port.busy = true |
| 71 | defer { port.busy = false } |
| 72 | if !port.ensure_bytes(1)! { return eof_value() } |
| 73 | byte := port.buffer[port.position] |
| 74 | if advance { port.position++ } |
| 75 | return integer(byte) |
| 76 | } |
| 77 | |
| 78 | pub fn (mut vm VM) u8_ready(value Value) !bool { |
| 79 | vm.ready()! |
| 80 | mut port := vm.checked_binary_input(value)! |
| 81 | if !isnil(custom_input_resource(value)) { |
| 82 | if port.position < port.buffer.len || port.ended { return true } |
| 83 | return failure('custom port does not support nonblocking readiness') |
| 84 | } |
| 85 | port.busy = true |
| 86 | defer { port.busy = false } |
| 87 | for port.position == port.buffer.len && !port.ended { |
| 88 | if !port.fill_buffer(true)! { return false } |
| 89 | } |
| 90 | return true |
| 91 | } |
| 92 | |
| 93 | pub fn (mut vm VM) read_bytevector(value Value, count int) !Value { |
| 94 | vm.ready()! |
| 95 | if count < 0 { return failure('read-bytevector count must be nonnegative') } |
| 96 | mut port := vm.checked_binary_input(value)! |
| 97 | if !isnil(custom_input_resource(value)) { |
| 98 | return vm.custom_input_host_operation(.get_bytes, value, unspecified(), 0, count, unspecified()) |
| 99 | } |
| 100 | port.busy = true |
| 101 | defer { |
| 102 | port.busy = false |
| 103 | port.release_large_frame() |
| 104 | } |
| 105 | available := port.buffer.len - port.position |
| 106 | if count == 0 { return owned_bytevector([]u8{}) } |
| 107 | if available >= count || port.ended { |
| 108 | length := int_min(count, available) |
| 109 | if length == 0 { return eof_value() } |
| 110 | bytes := copy_byte_range(port.buffer, port.position, port.position + length) |
| 111 | port.position += length |
| 112 | return owned_bytevector(bytes) |
| 113 | } |
| 114 | // Never reserve the caller's entire count up front: an enormous count on |
| 115 | // a short stream still needs only the storage for bytes actually received. |
| 116 | mut bytes := []u8{cap: int_min(count, 4096)} |
| 117 | unsafe { bytes.flags.set(.noslices) } |
| 118 | for bytes.len < count && port.ensure_bytes(1)! { |
| 119 | length := int_min(count - bytes.len, port.buffer.len - port.position) |
| 120 | bytes << unsafe { port.buffer[port.position..port.position + length] } |
| 121 | port.position += length |
| 122 | } |
| 123 | if bytes.len == 0 { return eof_value() } |
| 124 | return owned_bytevector(bytes) |
| 125 | } |
| 126 | |
| 127 | // read_bytevector_into drains lookahead, then lets the Reader fill the final |
| 128 | // bytevector directly. Its fixed storage stays rooted through callbacks/GC. |
| 129 | pub fn (mut vm VM) read_bytevector_into(value Value, target Value, start int, end int) !Value { |
| 130 | vm.ready()! |
| 131 | target.require_mutable(.bytevector)! |
| 132 | mut data := target.bytevector_pointer() |
| 133 | if start < 0 || end < start || end > data.bytes.len { |
| 134 | return failure('bytevector range out of bounds') |
| 135 | } |
| 136 | mut port := vm.checked_binary_input(value)! |
| 137 | if !isnil(custom_input_resource(value)) { |
| 138 | return vm.custom_input_host_operation(.get_into, value, target, start, end, unspecified()) |
| 139 | } |
| 140 | port.busy = true |
| 141 | defer { |
| 142 | port.busy = false |
| 143 | port.release_large_frame() |
| 144 | } |
| 145 | mut offset := start |
| 146 | for offset < end { |
| 147 | available := port.buffer.len - port.position |
| 148 | if available > 0 { |
| 149 | length := int_min(end - offset, available) |
| 150 | unsafe { vmemmove(&u8(data.bytes.data) + offset, &u8(port.buffer.data) + port.position, length) } |
| 151 | port.position += length |
| 152 | offset += length |
| 153 | } else { |
| 154 | if port.ended { break } |
| 155 | length := int_min(end - offset, port.read_size) |
| 156 | mut destination := unsafe { (&u8(data.bytes.data) + offset).vbytes(length) } |
| 157 | count := port.read_binary_chunk(mut destination)! |
| 158 | if count == 0 { break } |
| 159 | offset += count |
| 160 | } |
| 161 | } |
| 162 | if offset == start && end != start { return eof_value() } |
| 163 | return integer(offset - start) |
| 164 | } |
| 165 | |
| 166 | fn (mut port InputPort) read_binary_chunk(mut destination []u8) !int { |
| 167 | count := port.source.read(mut destination) or { |
| 168 | if err is io.Eof || err is os.Eof { |
| 169 | port.ended = true |
| 170 | return 0 |
| 171 | } |
| 172 | return err |
| 173 | } |
| 174 | if count < 0 || count > destination.len { |
| 175 | return failure('reader returned an invalid byte count') |
| 176 | } |
| 177 | if count == 0 { port.ended = true } |
| 178 | return count |
| 179 | } |
| 180 | |
| 181 | pub fn (mut vm VM) write_u8(value Value, byte u8) ! { |
| 182 | vm.ready()! |
| 183 | mut port := vm.checked_binary_output(value)! |
| 184 | if !isnil(custom_output_resource(value)) { |
| 185 | vm.custom_output_host_operation(.write_bytes, value, owned_bytevector([byte]), 0, 1, unspecified())! |
| 186 | return |
| 187 | } |
| 188 | // write_bytes consumes this stack byte synchronously, including retries. |
| 189 | port.write_bytes(unsafe { (&byte).vbytes(1) })! |
| 190 | } |
| 191 | |
| 192 | pub fn (mut vm VM) write_bytevector(value Value, source Value, start int, end int) ! { |
| 193 | vm.ready()! |
| 194 | data := bytevector_data(source)! |
| 195 | if start < 0 || end < start || end > data.bytes.len { |
| 196 | return failure('bytevector range out of bounds') |
| 197 | } |
| 198 | mut port := vm.checked_binary_output(value)! |
| 199 | if !isnil(custom_output_resource(value)) { |
| 200 | vm.custom_output_host_operation(.write_bytes, value, source, start, end, unspecified())! |
| 201 | return |
| 202 | } |
| 203 | port.write_bytes(unsafe { data.bytes[start..end] })! |
| 204 | } |
| 205 | |
| 206 | const binary_port_native_descriptors = [ |
| 207 | NativeDescriptor{'open-input-bytevector', NativeFn(port_open_input_bytevector)} |
| 208 | NativeDescriptor{'open-output-bytevector', port_open_output_bytevector} |
| 209 | NativeDescriptor{'get-output-bytevector', port_get_output_bytevector} |
| 210 | NativeDescriptor{'open-binary-input-file', port_open_binary_input} |
| 211 | NativeDescriptor{'open-binary-output-file', port_open_binary_output} |
| 212 | NativeDescriptor{'read-u8', port_read_u8} |
| 213 | NativeDescriptor{'peek-u8', port_peek_u8} |
| 214 | NativeDescriptor{'u8-ready?', port_u8_ready} |
| 215 | NativeDescriptor{'read-bytevector', port_read_bytevector} |
| 216 | NativeDescriptor{'read-bytevector!', port_read_bytevector_into} |
| 217 | NativeDescriptor{'write-u8', port_write_u8} |
| 218 | NativeDescriptor{'write-bytevector', port_write_bytevector}, |
| 219 | ]! |
| 220 | |
| 221 | fn (mut vm VM) install_binary_port_primitives() { |
| 222 | for index in 0 .. binary_port_native_descriptors.len { |
| 223 | entry := &binary_port_native_descriptors[index] |
| 224 | vm.bind_global(entry.name, vm.port_native_procedure(entry)) |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | @[direct_array_access] |
| 229 | fn port_open_input_bytevector(mut vm VM, args []Value) !Value { |
| 230 | check_arity('open-input-bytevector', args.len, 1, 1)! |
| 231 | return vm.open_input_bytevector(args[0]) |
| 232 | } |
| 233 | |
| 234 | @[direct_array_access] |
| 235 | fn port_open_output_bytevector(mut vm VM, args []Value) !Value { |
| 236 | check_arity('open-output-bytevector', args.len, 0, 0)! |
| 237 | return vm.open_output_bytevector() |
| 238 | } |
| 239 | |
| 240 | @[direct_array_access] |
| 241 | fn port_get_output_bytevector(mut vm VM, args []Value) !Value { |
| 242 | check_arity('get-output-bytevector', args.len, 1, 1)! |
| 243 | return vm.get_output_bytevector(args[0]) |
| 244 | } |
| 245 | |
| 246 | @[direct_array_access] |
| 247 | fn port_open_binary_input(mut vm VM, args []Value) !Value { |
| 248 | check_arity('open-binary-input-file', args.len, 1, 1)! |
| 249 | return vm.open_binary_input_file(string_data(args[0])!) |
| 250 | } |
| 251 | |
| 252 | @[direct_array_access] |
| 253 | fn port_open_binary_output(mut vm VM, args []Value) !Value { |
| 254 | check_arity('open-binary-output-file', args.len, 1, 1)! |
| 255 | return vm.open_binary_output_file(string_data(args[0])!) |
| 256 | } |
| 257 | |
| 258 | @[direct_array_access] |
| 259 | fn port_read_u8(mut vm VM, args []Value) !Value { |
| 260 | check_arity('read-u8', args.len, 0, 1)! |
| 261 | return vm.port_byte(if args.len == 0 { vm.current_input_value()! } else { args[0] }, true) |
| 262 | } |
| 263 | |
| 264 | @[direct_array_access] |
| 265 | fn port_peek_u8(mut vm VM, args []Value) !Value { |
| 266 | check_arity('peek-u8', args.len, 0, 1)! |
| 267 | return vm.port_byte(if args.len == 0 { vm.current_input_value()! } else { args[0] }, false) |
| 268 | } |
| 269 | |
| 270 | @[direct_array_access] |
| 271 | fn port_u8_ready(mut vm VM, args []Value) !Value { |
| 272 | check_arity('u8-ready?', args.len, 0, 1)! |
| 273 | return boolean(vm.u8_ready(if args.len == 0 { vm.current_input_value()! } else { args[0] })!) |
| 274 | } |
| 275 | |
| 276 | @[direct_array_access] |
| 277 | fn port_read_bytevector(mut vm VM, args []Value) !Value { |
| 278 | check_arity('read-bytevector', args.len, 1, 2)! |
| 279 | count := allocation_count(args[0], 1)! |
| 280 | return vm.read_bytevector(if args.len == 1 { vm.current_input_value()! } else { args[1] }, count) |
| 281 | } |
| 282 | |
| 283 | @[direct_array_access] |
| 284 | fn port_read_bytevector_into(mut vm VM, args []Value) !Value { |
| 285 | check_arity('read-bytevector!', args.len, 1, 4)! |
| 286 | data := bytevector_data(args[0])! |
| 287 | start, end := sequence_range(args, 2, data.bytes.len)! |
| 288 | return vm.read_bytevector_into(if args.len == 1 { vm.current_input_value()! } else { args[1] }, args[0], start, end) |
| 289 | } |
| 290 | |
| 291 | @[direct_array_access] |
| 292 | fn port_write_u8(mut vm VM, args []Value) !Value { |
| 293 | check_arity('write-u8', args.len, 1, 2)! |
| 294 | byte := byte_value(args[0])! |
| 295 | vm.write_u8(if args.len == 1 { vm.current_output_value()! } else { args[1] }, byte)! |
| 296 | return unspecified() |
| 297 | } |
| 298 | |
| 299 | @[direct_array_access] |
| 300 | fn port_write_bytevector(mut vm VM, args []Value) !Value { |
| 301 | check_arity('write-bytevector', args.len, 1, 4)! |
| 302 | data := bytevector_data(args[0])! |
| 303 | start, end := sequence_range(args, 2, data.bytes.len)! |
| 304 | vm.write_bytevector(if args.len == 1 { vm.current_output_value()! } else { args[1] }, args[0], start, end)! |
| 305 | return unspecified() |
| 306 | } |
| 307 | |