| 1 | module scheme |
| 2 | |
| 3 | const custom_binary_input_name = 'make-custom-binary-input-port' |
| 4 | |
| 5 | struct CustomPortInput { |
| 6 | resource &CustomPortResource |
| 7 | } |
| 8 | |
| 9 | fn (_ &CustomPortInput) read(mut _bytes []u8) !int { |
| 10 | return failure('Scheme port operation needs the resumable I/O driver') |
| 11 | } |
| 12 | |
| 13 | fn custom_input_resource(value Value) &CustomPortResource { |
| 14 | if value.tag in [.input_port, .input_output_port] { |
| 15 | port := value.input_pointer() |
| 16 | if port.source is CustomPortInput { return port.source.resource } |
| 17 | if port.source is CustomTranscodedInput { return port.source.resource } |
| 18 | } |
| 19 | return unsafe { nil } |
| 20 | } |
| 21 | |
| 22 | fn custom_port_resource(value Value) &CustomPortResource { |
| 23 | if value.tag == .input_output_port { |
| 24 | input := custom_input_resource(value) |
| 25 | return if isnil(input) { custom_output_resource(value) } else { input } |
| 26 | } |
| 27 | return if value.tag == .input_port { |
| 28 | custom_input_resource(value) |
| 29 | } else { |
| 30 | custom_output_resource(value) |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | fn custom_binary_input(mut vm VM, args []Value) !Value { |
| 35 | check_arity(custom_binary_input_name, args.len, 5, 5)! |
| 36 | return vm.make_custom_binary_input_port(args[0].as_string()!, args[1], args[2], args[3], args[4]) |
| 37 | } |
| 38 | |
| 39 | pub fn (mut vm VM) make_custom_binary_input_port(id string, read_callback Value, get_position Value, set_position Value, close Value, options InputOptions) !Value { |
| 40 | vm.ready()! |
| 41 | vm.check_custom_port_callback(read_callback, false)! |
| 42 | vm.check_custom_port_callback(get_position, true)! |
| 43 | vm.check_custom_port_callback(set_position, true)! |
| 44 | vm.check_custom_port_callback(close, true)! |
| 45 | if options.read_size <= 0 { return failure('input read size must be positive') } |
| 46 | resource := &CustomPortResource{ id: id, read: read_callback, get_position: get_position, set_position: set_position, close: close } |
| 47 | return vm.input_port(&CustomPortInput{ resource: resource }, |
| 48 | binary: true |
| 49 | read_size: options.read_size |
| 50 | ) |
| 51 | } |
| 52 | |
| 53 | fn custom_input_closing(operation PortIoOperation) bool { |
| 54 | return operation in [.close_input, .close_any] |
| 55 | } |
| 56 | |
| 57 | fn (mut vm VM) prepare_custom_input_operation(operation PortIoOperation, target Value, args []Value) !&CustomPortOperation { |
| 58 | if custom_output_only(operation) { |
| 59 | return failure('expected an output port') |
| 60 | } |
| 61 | if !target.input_pointer().binary { |
| 62 | return vm.prepare_custom_text_input(operation, target, args) |
| 63 | } |
| 64 | port := if custom_input_closing(operation) { |
| 65 | vm.input_owner(target)! |
| 66 | } else { |
| 67 | vm.checked_binary_input(target)! |
| 68 | } |
| 69 | if port.busy { return failure('input port operation is already active') } |
| 70 | mut destination := unspecified() |
| 71 | mut start := 0 |
| 72 | mut end := 0 |
| 73 | mut position := unspecified() |
| 74 | match operation { |
| 75 | .read_bytes { end = allocation_count(args[0], 1)! } |
| 76 | .get_bytes { |
| 77 | count := args[1].as_integer()! |
| 78 | if count < 0 || count > max_int { |
| 79 | return failure('bytevector read count out of bounds') |
| 80 | } |
| 81 | end = int(count) |
| 82 | } |
| 83 | .read_into, .get_into { |
| 84 | destination = args[if operation == .read_into { 0 } else { 1 }] |
| 85 | destination.require_mutable(.bytevector)! |
| 86 | data := destination.bytevector_pointer() |
| 87 | start, end = if operation == .read_into { |
| 88 | sequence_range(args, 2, data.bytes.len)! |
| 89 | } else { |
| 90 | port_get_put_range(args, data.bytes.len)! |
| 91 | } |
| 92 | } |
| 93 | .read_all, .read_some { end = max_int } |
| 94 | .set_position { position = args[1] } |
| 95 | .get_u8, .lookahead_u8, .read_u8, .peek_u8, .input_eof, .get_position, .close_input, |
| 96 | .close_any { |
| 97 | } |
| 98 | else { return failure('expected an output port') } |
| 99 | } |
| 100 | return new_custom_input_operation(operation, target, destination, start, end, position) |
| 101 | } |
| 102 | |
| 103 | fn new_custom_input_operation(operation PortIoOperation, target Value, destination Value, start int, end int, position Value) !&CustomPortOperation { |
| 104 | resource := custom_input_resource(target) |
| 105 | if !isnil(resource.input_adapter) && operation in [.get_position, .set_position] { |
| 106 | return failure('transcoded input port does not support positions') |
| 107 | } |
| 108 | if operation == .get_position && !resource.get_position.is_callable() { |
| 109 | return failure('input port does not support positions') |
| 110 | } |
| 111 | if operation == .set_position { |
| 112 | if target.input_pointer().binary { |
| 113 | check_custom_binary_position(position)! |
| 114 | } else { |
| 115 | checked_custom_text_position(target, position)! |
| 116 | } |
| 117 | if !resource.set_position.is_callable() { |
| 118 | return failure('input port does not support setting positions') |
| 119 | } |
| 120 | } |
| 121 | mut state := &CustomPortOperation{ |
| 122 | operation: operation |
| 123 | target: target |
| 124 | resource: resource |
| 125 | source: destination |
| 126 | start: start |
| 127 | offset: start |
| 128 | end: end |
| 129 | position: position |
| 130 | lease: &CustomPortLease{ port: target, resource: resource, closing: custom_input_closing(operation), input_cursor: operation != .get_position && !custom_input_closing(operation) && !zero_custom_text_read(operation, end) } |
| 131 | } |
| 132 | unsafe { state.bytes.flags.set(.noslices) } |
| 133 | return state |
| 134 | } |
| 135 | |
| 136 | fn (mut vm VM) custom_input_host_operation(operation PortIoOperation, target Value, destination Value, start int, end int, position Value) !Value { |
| 137 | state := new_custom_input_operation(operation, custom_combined_facet(operation, target), destination, start, end, position)! |
| 138 | return vm.call(procedure_value(&Procedure{ mode: .closure, name: 'custom input operation', owner: vm.owner, function: &custom_port_io_bridges[int(operation)] }), [payload_value(.port_operation, state)]) |
| 139 | } |
| 140 | |
| 141 | // The prefix is immutable once a captured bridge freezes its current buffer. |
| 142 | // New branches append to a fresh suffix; repeated capture never copies all |
| 143 | // previous input. Materialization places each chunk at its known offset once. |
| 144 | @[heap] |
| 145 | struct CustomInputChunk { |
| 146 | previous &CustomInputChunk = unsafe { nil } |
| 147 | bytes []u8 |
| 148 | offset int |
| 149 | } |
| 150 | |
| 151 | fn (mut m Machine) detach_custom_port_state() &CustomPortOperation { |
| 152 | mut state := custom_port_operation(m.env.slots[0]) |
| 153 | if !m.env.escaped { return state } |
| 154 | mut scope := m.detach_bridge() |
| 155 | prefix := if state.bytes.len == 0 { |
| 156 | state.chunks |
| 157 | } else { |
| 158 | &CustomInputChunk{ previous: state.chunks, bytes: unsafe { state.bytes }, offset: state.offset - state.bytes.len } |
| 159 | } |
| 160 | state = &CustomPortOperation{ ...state, bytes: []u8{}, chunks: prefix } |
| 161 | if !isnil(state.frame) { |
| 162 | state.frame = &CustomDatumFrame{ ...state.frame, stack: state.frame.stack.clone(), directive: state.frame.directive.clone() } |
| 163 | } |
| 164 | if !isnil(state.encoded) { state.encoded = &CustomEncodedState{ ...state.encoded } } |
| 165 | if !isnil(state.position_cursor) { |
| 166 | // Both records begin with this enum member. Access the compatible |
| 167 | // member type directly instead of aliasing an unrelated header struct. |
| 168 | kind := unsafe { *(&CustomCursorKind(state.position_cursor)) } |
| 169 | if kind == .binary { |
| 170 | cursor := state.binary_cursor() |
| 171 | state.position_cursor = &CustomBinaryCursor{ ...cursor } |
| 172 | } else if kind == .attached { |
| 173 | cursor := state.attached_operation() |
| 174 | state.position_cursor = &CustomAttachedOperation{ ...cursor } |
| 175 | mut next := state.attached_operation() |
| 176 | if !isnil(next.probe) { next.probe = &CustomAttachedProbePositions{ ...next.probe } } |
| 177 | } else { |
| 178 | cursor := state.text_operation() |
| 179 | state.position_cursor = &CustomTextOperation{ ...cursor } |
| 180 | mut next := state.text_operation() |
| 181 | if !isnil(next.probe) { next.probe = &CustomTextProbePositions{ ...next.probe } } |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | unsafe { state.bytes.flags.set(.noslices) } |
| 186 | scope.slots[0] = payload_value(.port_operation, state) |
| 187 | return state |
| 188 | } |
| 189 | |
| 190 | fn (state &CustomPortOperation) input_bytes_result() Value { |
| 191 | if state.offset == 0 && state.end != 0 { return eof_value() } |
| 192 | if isnil(state.chunks) { return owned_bytevector(unsafe { state.bytes }) } |
| 193 | mut result := []u8{len: state.offset} |
| 194 | if state.bytes.len > 0 { |
| 195 | unsafe { vmemcpy(&u8(result.data) + state.offset - state.bytes.len, state.bytes.data, state.bytes.len) } |
| 196 | } |
| 197 | mut cursor := state.chunks |
| 198 | for !isnil(cursor) { |
| 199 | unsafe { vmemcpy(&u8(result.data) + cursor.offset, cursor.bytes.data, cursor.bytes.len) } |
| 200 | cursor = cursor.previous |
| 201 | } |
| 202 | return owned_bytevector(result) |
| 203 | } |
| 204 | |
| 205 | fn (mut m Machine) request_custom_input(mut vm VM, direct bool) ! { |
| 206 | mut state := custom_port_operation(m.env.slots[0]) |
| 207 | port := state.target.input_pointer() |
| 208 | if direct { |
| 209 | state.pending = state.source |
| 210 | state.request_start = state.offset |
| 211 | state.request_count = int_min(port.read_size, state.end - state.offset) |
| 212 | } else { |
| 213 | mut resource := state.resource |
| 214 | // This is the resource's real read-ahead storage, not a VM cache. Hosts |
| 215 | // retain arenas backing published port mutations just like native ports. |
| 216 | if resource.read_buffer.tag != .bytevector { |
| 217 | resource.read_buffer = owned_bytevector([]u8{len: port.read_size}) |
| 218 | } |
| 219 | state.pending = resource.read_buffer |
| 220 | state.request_start = 0 |
| 221 | state.request_count = state.pending.bytevector_pointer().bytes.len |
| 222 | } |
| 223 | begin_custom_binary_callback(state, true) |
| 224 | m.push(state.resource.read) |
| 225 | m.push(state.pending) |
| 226 | m.push(integer(state.request_start)) |
| 227 | m.push(integer(state.request_count)) |
| 228 | m.apply(mut vm, 3, false)! |
| 229 | } |
| 230 | |
| 231 | fn (mut m Machine) step_custom_input_operation(mut vm VM) ! { |
| 232 | mut state := custom_port_operation(m.env.slots[0]) |
| 233 | mut port := state.target.input_pointer() |
| 234 | match state.operation { |
| 235 | .read_u8, .get_u8, .peek_u8, .lookahead_u8, .input_eof { |
| 236 | if port.position < port.buffer.len { |
| 237 | byte := port.buffer[port.position] |
| 238 | if state.operation in [.read_u8, .get_u8] { port.position++ } |
| 239 | m.finish_custom_port_operation(if state.operation == .input_eof { |
| 240 | boolean(false) |
| 241 | } else { |
| 242 | integer(byte) |
| 243 | }) |
| 244 | } else if port.ended { |
| 245 | m.finish_custom_port_operation(if state.operation == .input_eof { |
| 246 | boolean(true) |
| 247 | } else { |
| 248 | eof_value() |
| 249 | }) |
| 250 | } else { |
| 251 | m.request_custom_input(mut vm, false)! |
| 252 | } |
| 253 | } |
| 254 | .read_bytes, .get_bytes, .read_all, .read_some { |
| 255 | available := port.buffer.len - port.position |
| 256 | length := int_min(state.end - state.offset, available) |
| 257 | if length > 0 { |
| 258 | state.bytes << unsafe { port.buffer[port.position..port.position + length] } |
| 259 | port.position += length |
| 260 | state.offset += length |
| 261 | } |
| 262 | if state.offset == state.end || port.ended || (state.operation == .read_some && state.offset > 0) { |
| 263 | m.finish_custom_port_operation(state.input_bytes_result()) |
| 264 | } else { |
| 265 | m.request_custom_input(mut vm, false)! |
| 266 | } |
| 267 | } |
| 268 | .read_into, .get_into { |
| 269 | available := port.buffer.len - port.position |
| 270 | length := int_min(state.end - state.offset, available) |
| 271 | if length > 0 { |
| 272 | data := state.source.bytevector_pointer() |
| 273 | unsafe { vmemmove(&u8(data.bytes.data) + state.offset, &u8(port.buffer.data) + port.position, length) } |
| 274 | port.position += length |
| 275 | state.offset += length |
| 276 | } |
| 277 | if state.offset == state.end || port.ended { |
| 278 | m.finish_custom_port_operation(if state.offset == state.start && state.end != state.start { |
| 279 | eof_value() |
| 280 | } else { |
| 281 | integer(state.offset - state.start) |
| 282 | }) |
| 283 | } else { |
| 284 | m.request_custom_input(mut vm, true)! |
| 285 | } |
| 286 | } |
| 287 | .close_input, .close_any { |
| 288 | if !claim_custom_combined_close(state.lease) { |
| 289 | m.finish_custom_port_operation(unspecified()) |
| 290 | return |
| 291 | } |
| 292 | m.push(state.resource.close) |
| 293 | m.apply(mut vm, 0, false)! |
| 294 | } |
| 295 | .get_position { |
| 296 | begin_custom_binary_callback(state, false) |
| 297 | m.push(state.resource.get_position) |
| 298 | m.apply(mut vm, 0, false)! |
| 299 | } |
| 300 | .set_position { |
| 301 | begin_custom_binary_callback(state, true) |
| 302 | m.push(state.resource.set_position) |
| 303 | m.push(state.position) |
| 304 | m.apply(mut vm, 1, false)! |
| 305 | } |
| 306 | else { return failure('invalid input port operation') } |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | fn (mut m Machine) collect_custom_input_operation(result Value) ! { |
| 311 | state := custom_port_operation(m.env.slots[0]) |
| 312 | match state.operation { |
| 313 | .get_position { |
| 314 | raw := custom_port_single_result(result)! |
| 315 | check_custom_binary_position(raw)! |
| 316 | port := state.target.input_pointer() |
| 317 | position := numeric_subtract(raw, integer(port.buffer.len - port.position))! |
| 318 | if exact_sign(position) < 0 { |
| 319 | return failure('source position precedes buffered input') |
| 320 | } |
| 321 | m.finish_custom_port_operation(position) |
| 322 | } |
| 323 | .set_position { |
| 324 | mut port := state.target.input_pointer() |
| 325 | port.buffer = []u8{} |
| 326 | port.position = 0 |
| 327 | port.mark = -1 |
| 328 | port.ended = false |
| 329 | port.location = unsafe { nil } |
| 330 | m.finish_custom_port_operation(unspecified()) |
| 331 | } |
| 332 | .close_input, .close_any { m.finish_custom_port_operation(unspecified()) } |
| 333 | else { |
| 334 | count := custom_port_single_result(result)!.as_integer()! |
| 335 | if count < 0 || count > state.request_count { |
| 336 | return failure('custom reader returned an invalid count') |
| 337 | } |
| 338 | mut current := m.detach_custom_port_state() |
| 339 | mut port := current.target.input_pointer() |
| 340 | port.ended = count == 0 |
| 341 | if current.operation in [.read_into, .get_into] { |
| 342 | current.offset += int(count) |
| 343 | } else { |
| 344 | data := current.pending.bytevector_pointer() |
| 345 | port.buffer = unsafe { data.bytes[..int(count)] } |
| 346 | port.position = 0 |
| 347 | } |
| 348 | current.pending = unspecified() |
| 349 | } |
| 350 | } |
| 351 | } |
| 352 | |