module scheme import encoding.utf8 fn (mut vm VM) prepare_custom_text_input(operation PortIoOperation, target Value, args []Value) !&CustomPortOperation { port := if custom_input_closing(operation) { vm.input_owner(target)! } else { vm.checked_text_input(target)! } if port.busy { return failure('input port operation is already active') } mut source := unspecified() mut start := 0 mut end := 0 mut position := unspecified() match operation { .read_char, .peek_char, .get_char, .lookahead_char, .input_eof, .get_position, .close_input, .close_any, .read_datum, .get_datum { } .read_text, .get_text { count := args[if operation == .read_text { 0 } else { 1 }].as_integer()! if count < 0 || count > max_int { return failure('string read count out of supported range') } end = int(count) } .text_all, .read_line, .get_line { end = max_int } .text_into { source = args[1] source.require_mutable(.string)! data := source.string_pointer() first, last := port_get_put_range(args, data.length)! start = first end = last - first } .set_position { position = args[1] } else { return failure('expected a binary input port') } } return new_custom_text_input_operation(operation, target, source, start, end, position) } fn new_custom_text_input_operation(operation PortIoOperation, target Value, source Value, start int, count int, position Value) !&CustomPortOperation { mut state := new_custom_input_operation(operation, target, source, 0, count, position)! state.start = start if operation in [.read_datum, .get_datum] { state.frame = &CustomDatumFrame{} } if operation == .text_into { data := source.string_pointer() state.snapshot = data.value state.revision = data.revision state.first_byte = text_offset(data, start)! } return state } fn (mut vm VM) custom_text_input_host_operation(operation PortIoOperation, target Value, source Value, start int, count int, position Value) !Value { state := new_custom_text_input_operation(operation, custom_combined_facet(operation, target), source, start, count, position)! return vm.call(procedure_value(&Procedure{ mode: .closure, name: 'custom text input', owner: vm.owner, function: &custom_port_io_bridges[int(operation)] }), [payload_value(.port_operation, state)]) } fn (state &CustomPortOperation) copy_text_bytes(mut destination []u8, start int) { if state.bytes.len > 0 { unsafe { vmemcpy(&u8(destination.data) + start + state.offset - state.bytes.len, state.bytes.data, state.bytes.len) } } mut cursor := state.chunks for !isnil(cursor) { unsafe { vmemcpy(&u8(destination.data) + start + cursor.offset, cursor.bytes.data, cursor.bytes.len) } cursor = cursor.previous } } fn (state &CustomPortOperation) input_text_result() Value { if state.characters == 0 && !state.line_done && state.end != 0 { return eof_value() } if state.offset == 0 { return owned_string('', 0) } if isnil(state.chunks) && state.bytes.cap > state.bytes.len { unsafe { (&u8(state.bytes.data))[state.bytes.len] = 0 } return owned_string(unsafe { tos(state.bytes.data, state.bytes.len) }, state.characters) } mut bytes := []u8{len: state.offset + 1} state.copy_text_bytes(mut bytes, 0) return owned_string(unsafe { tos(bytes.data, state.offset) }, state.characters) } fn (state &CustomPortOperation) finish_text_into() !Value { if state.characters == 0 { return if state.end == 0 { integer(0) } else { eof_value() } } mut data := state.source.string_pointer() if data.revision != state.revision { return failure('string target changed during input') } mut suffix := state.first_byte for _ in 0 .. state.characters { _, width := utf8.decode_rune_at(state.snapshot, suffix) suffix += width } remaining := state.snapshot.len - suffix if state.offset > max_int - 1 - state.first_byte - remaining { return failure('string length exceeds supported range') } length := state.first_byte + state.offset + remaining mut bytes := []u8{len: length + 1} unsafe { if state.first_byte > 0 { vmemcpy(bytes.data, state.snapshot.str, state.first_byte) } if remaining > 0 { vmemcpy(&u8(bytes.data) + state.first_byte + state.offset, state.snapshot.str + suffix, remaining) } } state.copy_text_bytes(mut bytes, state.first_byte) data.value = unsafe { tos(bytes.data, length) } data.revision++ return integer(state.characters) } fn (mut m Machine) request_custom_text_input(mut vm VM) ! { mut state := custom_port_operation(m.env.slots[0]) // A bounded speculative prefix can precede the current live callback view. // Moving to its tail needs no Scheme call and must skip the collect opcode. refresh_custom_text_view(state.target)! mut port := state.target.input_pointer() if port.position < port.buffer.len { m.ip = 1 return } // Checkpoint consumed bytes before the callback can overwrite its buffer. commit_custom_text_cursor(state.target) port.buffer = []u8{} port.position = 0 mut resource := state.resource resource.text_view = unspecified() if !isnil(resource.input_adapter) { m.request_custom_decoded_input(mut vm)! return } if positioned_custom_text(resource) { m.request_positioned_text_input(mut vm)! return } if resource.read_buffer.tag != .string || (state.operation == .text_into && resource.read_buffer.pointer() == state.source.pointer()) { resource.read_buffer = owned_string('\x00'.repeat(port.read_size), port.read_size) } state.pending = resource.read_buffer state.request_start = 0 state.request_count = state.pending.string_pointer().length m.push(state.resource.read) m.push(state.pending) m.push(integer(0)) m.push(integer(state.request_count)) m.apply(mut vm, 3, false)! } fn (mut m Machine) finish_custom_text_read() ! { state := custom_port_operation(m.env.slots[0]) result := if state.operation == .text_into { state.finish_text_into()! } else { state.input_text_result() } m.finish_custom_port_operation(result) } fn (mut m Machine) step_custom_text_input(mut vm VM) ! { mut state := custom_port_operation(m.env.slots[0]) refresh_custom_text_view(state.target)! if state.operation in [.read_datum, .get_datum] { m.step_custom_datum_input(mut vm) or { return custom_datum_operation_error(state.operation, err) } return } mut port := state.target.input_pointer() match state.operation { .get_position { m.push(state.resource.get_position) m.apply(mut vm, 0, false)! return } .set_position { m.push(state.resource.set_position) m.push(m.custom_text_set_argument()!) m.apply(mut vm, 1, false)! return } .close_input, .close_any { if !claim_custom_combined_close(state.lease) { m.finish_custom_port_operation(unspecified()) return } m.push(state.resource.close) m.apply(mut vm, 0, false)! return } else {} } if state.operation in [.read_char, .get_char, .peek_char, .lookahead_char, .input_eof] { if port.position < port.buffer.len { ch, width := port.peek_character()! if state.operation in [.read_char, .get_char] { port.position += width } m.finish_custom_port_operation(if state.operation == .input_eof { boolean(false) } else { character(ch)! }) } else if port.ended { observe_custom_attached_eof(state.target) m.finish_custom_port_operation(if state.operation == .input_eof { boolean(true) } else { eof_value() }) } else { m.request_custom_text_input(mut vm)! } return } if state.line_cr { if port.position == port.buffer.len && !port.ended { m.request_custom_text_input(mut vm)! return } if port.position < port.buffer.len { ch, width := port.peek_character()! if ch in [`\n`, rune(0x85)] { port.position += width } } if port.ended && port.position == port.buffer.len { observe_custom_attached_eof(state.target) } state.line_cr = false m.finish_custom_text_read()! return } start := port.position mut last := start for port.position < port.buffer.len && state.characters < state.end { ch, width := port.peek_character()! if state.operation in [.read_line, .get_line] && (ch == `\n` || (state.operation == .read_line && ch in [ `\r`, rune(0x85), rune(0x2028), ])) { state.line_done = true state.line_cr = ch == `\r` port.position += width break } port.position += width last = port.position state.characters++ } if last > start { length := last - start if length > max_int - 1 - state.offset { return failure('string length exceeds supported range') } state.bytes << unsafe { port.buffer[start..last] } state.offset += length } if state.line_cr { if port.position == port.buffer.len && !port.ended { m.request_custom_text_input(mut vm)! return } if port.position < port.buffer.len { ch, width := port.peek_character()! if ch in [`\n`, rune(0x85)] { port.position += width } } if port.ended && port.position == port.buffer.len { observe_custom_attached_eof(state.target) } state.line_cr = false } if state.line_done || state.characters == state.end || port.ended { if port.ended && port.position == port.buffer.len && state.characters < state.end && !state.line_done { observe_custom_attached_eof(state.target) } m.finish_custom_text_read()! } else { m.request_custom_text_input(mut vm)! } } fn (mut m Machine) collect_custom_text_input(result Value) ! { state := custom_port_operation(m.env.slots[0]) match state.operation { .get_position { m.finish_custom_port_operation(custom_text_position(state.target, custom_port_single_result(result)!)!) } .set_position { m.restore_custom_text_position()! m.finish_custom_port_operation(unspecified()) } .close_input, .close_any { m.finish_custom_port_operation(unspecified()) } else { if !isnil(state.resource.input_adapter) { m.collect_custom_decoded_input(result)! return } count := custom_port_single_result(result)!.as_integer()! if count < 0 || count > state.request_count { return failure('custom textual reader returned an invalid count') } mut current := m.detach_custom_port_state() data := current.pending.string_pointer() end := text_offset(data, count)! mut port := current.target.input_pointer() mut resource := current.resource // A resumed callback publishes a new physical read-ahead view; // replay metadata from an intervening operation belongs to the old one. resource.replay_active = false resource.replay_tail = []u8{} resource.text_view = current.pending resource.view_start = 0 resource.view_count = int(count) resource.view_revision = data.revision port.buffer = unsafe { data.value.str.vbytes(end) } port.position = 0 port.ended = count == 0 publish_custom_text_origin(current) current.pending = unspecified() } } } // Consume by adjusting the byte view, without moving or copying callback data. // Freeze diagnostic history before caller code may mutate an already-read prefix. fn commit_custom_text_cursor(value Value) { mut port := value.input_pointer() if port.position == 0 { return } mut resource := custom_input_resource(value) if !resource.replay_active && resource.text_view.tag == .string { resource.view_start += unicode_length(unsafe { tos(port.buffer.data, port.position) }) } trim_custom_attached_coordinates(resource, port.position, resource.replay_active) trim_custom_text_coordinates(resource, unsafe { port.buffer[..port.position] }, resource.replay_active) port.checkpoint_input() port.buffer = unsafe { port.buffer[port.position..] } port.position = 0 } // A retained Scheme string may replace its UTF8 allocation. Keep the live input // cursor in characters and reacquire the new header only when revision changes. // Explicit position checkpoints use detached unread bytes instead of this view. fn refresh_custom_text_view(value Value) ! { mut resource := custom_input_resource(value) mut port := value.input_pointer() mut restore := false if resource.replay_active { if port.position < port.buffer.len { return } commit_custom_text_cursor(value) resource.replay_active = false port.buffer = unsafe { resource.replay_tail } resource.replay_tail = []u8{} restore = true } if resource.text_view.tag != .string { return } data := resource.text_view.string_pointer() if !restore && data.revision == resource.view_revision { return } commit_custom_text_cursor(value) first := text_offset(data, resource.view_start)! last := text_offset(data, resource.view_count)! port.buffer = unsafe { (data.value.str + first).vbytes(last - first) } resource.view_revision = data.revision }