module scheme // Only callback-backed binary resources use this adapter. Native host Readers // keep their synchronous adapter. Raw decoder state belongs to the physical // port and is never rolled back merely by resuming a Scheme continuation. @[heap] struct CustomTranscodedInput { resource &CustomPortResource mut: raw InputPort cursor TranscoderDecoder decoded []u8 finished bool step_input CodecInput position &CustomAttachedPosition = unsafe { nil } } fn (_ &CustomTranscodedInput) read(mut _bytes []u8) !int { return failure('Scheme port operation needs the resumable I/O driver') } fn (mut vm VM) attach_custom_input(value Value, codec CodecKind, eol EolStyle, mode ErrorHandlingMode, code u8) !Value { mut original := value.input_pointer() mut resource := custom_input_resource(value) adapter := &CustomTranscodedInput{ resource: resource raw: InputPort{ ...original, buffer: unsafe { original.buffer } } cursor: TranscoderDecoder{ eol: eol, mode: mode, decoder: CodecDecoder{ codec: codec } } } result := vm.input_port(adapter, read_size: original.read_size, fold_case: !original.no_fold)! result.input_pointer().transcoder_code = code resource.input_adapter = adapter original.closed = true original.owns_file = false original.file = unsafe { nil } original.stream = unsafe { nil } original.source = &closed_port original.buffer = []u8{} original.location = unsafe { nil } return result } // Decode only bytes already received. A whole UTF8 scalar always fits, including // when the binary callback requests one byte at a time. Returning an available // prefix avoids another callback merely to fill the decoded buffer. fn (mut source CustomTranscodedInput) decode_available(value Value) !bool { if !isnil(source.position) { return source.decode_available_positioned(value) } mut port := value.input_pointer() if port.position < port.buffer.len || port.ended { return true } commit_custom_text_cursor(value) port.buffer = []u8{} port.position = 0 if source.decoded.len == 0 { source.decoded = []u8{len: int_max(4, int_min(4096, port.read_size))} } mut produced := 0 for !source.finished && produced <= source.decoded.len - 4 { copied := copy_transcoded_ascii(mut source.raw, &source.cursor, mut source.decoded, produced) if copied > 0 { produced += copied continue } previous := source.cursor source.step_input = codec_input(unsafe { source.raw.buffer[source.raw.position..] }, source.raw.ended) step := source.cursor.next(mut source.step_input) or { if produced > 0 { source.cursor = previous source.step_input = CodecInput{} break } source.raw.position += source.step_input.position source.step_input = CodecInput{} return err } source.raw.position += source.step_input.position source.step_input = CodecInput{} match step.kind { .scalar { produced += unsafe { utf32_decode_to_buffer(u32(step.scalar), mut &source.decoded[produced]) } } .need_input { break } .finished { source.finished = true } .malformed { return failure('unhandled transcoder decoding step') } } } port.ended = source.finished port.buffer = unsafe { source.decoded[..produced] } return produced > 0 || source.finished } fn (mut m Machine) request_custom_decoded_input(mut vm VM) ! { mut state := custom_port_operation(m.env.slots[0]) mut resource := state.resource if resource.input_adapter.decode_available(state.target)! { m.ip = 1 return } m.request_custom_raw_input(mut vm)! } fn (mut m Machine) request_custom_raw_input(mut vm VM) ! { mut state := custom_port_operation(m.env.slots[0]) mut resource := state.resource if resource.read_buffer.tag != .bytevector { resource.read_buffer = owned_bytevector([]u8{len: resource.input_adapter.raw.read_size}) } state.pending = resource.read_buffer state.request_start = 0 state.request_count = state.pending.bytevector_pointer().bytes.len if !isnil(resource.input_adapter.position) && m.prepare_custom_attached_read(mut vm)! { return } m.push(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) collect_custom_decoded_input(result Value) ! { state := custom_port_operation(m.env.slots[0]) count := custom_port_single_result(result)!.as_integer()! if count < 0 || count > state.request_count { return failure('custom reader returned an invalid count') } if !isnil(state.position_cursor) { acknowledge_custom_attached_io(state, int(count), false)! } mut current := m.detach_custom_port_state() mut resource := current.resource mut adapter := resource.input_adapter data := current.pending.bytevector_pointer() adapter.raw.buffer = unsafe { data.bytes[..int(count)] } adapter.raw.position = 0 adapter.raw.ended = count == 0 adapter.finished = false // New raw publication supersedes any decoded lookahead left by an // intervening operation; codec carry itself remains physical shared state. resource.replay_active = false resource.replay_tail = []u8{} resource.text_view = unspecified() mut port := current.target.input_pointer() port.buffer = []u8{} port.position = 0 port.ended = false if !isnil(current.position_cursor) { publish_custom_attached_read(current, int(count)) } current.pending = unspecified() }