| 1 | module scheme |
| 2 | |
| 3 | // Only callback-backed binary resources use this adapter. Native host Readers |
| 4 | // keep their synchronous adapter. Raw decoder state belongs to the physical |
| 5 | // port and is never rolled back merely by resuming a Scheme continuation. |
| 6 | @[heap] |
| 7 | struct CustomTranscodedInput { |
| 8 | resource &CustomPortResource |
| 9 | mut: |
| 10 | raw InputPort |
| 11 | cursor TranscoderDecoder |
| 12 | decoded []u8 |
| 13 | finished bool |
| 14 | step_input CodecInput |
| 15 | position &CustomAttachedPosition = unsafe { nil } |
| 16 | } |
| 17 | |
| 18 | fn (_ &CustomTranscodedInput) read(mut _bytes []u8) !int { |
| 19 | return failure('Scheme port operation needs the resumable I/O driver') |
| 20 | } |
| 21 | |
| 22 | fn (mut vm VM) attach_custom_input(value Value, codec CodecKind, eol EolStyle, mode ErrorHandlingMode, code u8) !Value { |
| 23 | mut original := value.input_pointer() |
| 24 | mut resource := custom_input_resource(value) |
| 25 | adapter := &CustomTranscodedInput{ |
| 26 | resource: resource |
| 27 | raw: InputPort{ ...original, buffer: unsafe { original.buffer } } |
| 28 | cursor: TranscoderDecoder{ eol: eol, mode: mode, decoder: CodecDecoder{ codec: codec } } |
| 29 | } |
| 30 | result := vm.input_port(adapter, read_size: original.read_size, fold_case: !original.no_fold)! |
| 31 | result.input_pointer().transcoder_code = code |
| 32 | resource.input_adapter = adapter |
| 33 | original.closed = true |
| 34 | original.owns_file = false |
| 35 | original.file = unsafe { nil } |
| 36 | original.stream = unsafe { nil } |
| 37 | original.source = &closed_port |
| 38 | original.buffer = []u8{} |
| 39 | original.location = unsafe { nil } |
| 40 | return result |
| 41 | } |
| 42 | |
| 43 | // Decode only bytes already received. A whole UTF8 scalar always fits, including |
| 44 | // when the binary callback requests one byte at a time. Returning an available |
| 45 | // prefix avoids another callback merely to fill the decoded buffer. |
| 46 | fn (mut source CustomTranscodedInput) decode_available(value Value) !bool { |
| 47 | if !isnil(source.position) { return source.decode_available_positioned(value) } |
| 48 | mut port := value.input_pointer() |
| 49 | if port.position < port.buffer.len || port.ended { return true } |
| 50 | commit_custom_text_cursor(value) |
| 51 | port.buffer = []u8{} |
| 52 | port.position = 0 |
| 53 | if source.decoded.len == 0 { |
| 54 | source.decoded = []u8{len: int_max(4, int_min(4096, port.read_size))} |
| 55 | } |
| 56 | mut produced := 0 |
| 57 | for !source.finished && produced <= source.decoded.len - 4 { |
| 58 | copied := copy_transcoded_ascii(mut source.raw, &source.cursor, mut source.decoded, produced) |
| 59 | if copied > 0 { |
| 60 | produced += copied |
| 61 | continue |
| 62 | } |
| 63 | previous := source.cursor |
| 64 | source.step_input = codec_input(unsafe { source.raw.buffer[source.raw.position..] }, source.raw.ended) |
| 65 | step := source.cursor.next(mut source.step_input) or { |
| 66 | if produced > 0 { |
| 67 | source.cursor = previous |
| 68 | source.step_input = CodecInput{} |
| 69 | break |
| 70 | } |
| 71 | source.raw.position += source.step_input.position |
| 72 | source.step_input = CodecInput{} |
| 73 | return err |
| 74 | } |
| 75 | source.raw.position += source.step_input.position |
| 76 | source.step_input = CodecInput{} |
| 77 | match step.kind { |
| 78 | .scalar { |
| 79 | produced += unsafe { utf32_decode_to_buffer(u32(step.scalar), mut &source.decoded[produced]) } |
| 80 | } |
| 81 | .need_input { break } |
| 82 | .finished { source.finished = true } |
| 83 | .malformed { return failure('unhandled transcoder decoding step') } |
| 84 | } |
| 85 | } |
| 86 | port.ended = source.finished |
| 87 | port.buffer = unsafe { source.decoded[..produced] } |
| 88 | return produced > 0 || source.finished |
| 89 | } |
| 90 | |
| 91 | fn (mut m Machine) request_custom_decoded_input(mut vm VM) ! { |
| 92 | mut state := custom_port_operation(m.env.slots[0]) |
| 93 | mut resource := state.resource |
| 94 | if resource.input_adapter.decode_available(state.target)! { |
| 95 | m.ip = 1 |
| 96 | return |
| 97 | } |
| 98 | m.request_custom_raw_input(mut vm)! |
| 99 | } |
| 100 | |
| 101 | fn (mut m Machine) request_custom_raw_input(mut vm VM) ! { |
| 102 | mut state := custom_port_operation(m.env.slots[0]) |
| 103 | mut resource := state.resource |
| 104 | if resource.read_buffer.tag != .bytevector { |
| 105 | resource.read_buffer = owned_bytevector([]u8{len: resource.input_adapter.raw.read_size}) |
| 106 | } |
| 107 | state.pending = resource.read_buffer |
| 108 | state.request_start = 0 |
| 109 | state.request_count = state.pending.bytevector_pointer().bytes.len |
| 110 | if !isnil(resource.input_adapter.position) && m.prepare_custom_attached_read(mut vm)! { return } |
| 111 | m.push(resource.read) |
| 112 | m.push(state.pending) |
| 113 | m.push(integer(0)) |
| 114 | m.push(integer(state.request_count)) |
| 115 | m.apply(mut vm, 3, false)! |
| 116 | } |
| 117 | |
| 118 | fn (mut m Machine) collect_custom_decoded_input(result Value) ! { |
| 119 | state := custom_port_operation(m.env.slots[0]) |
| 120 | count := custom_port_single_result(result)!.as_integer()! |
| 121 | if count < 0 || count > state.request_count { |
| 122 | return failure('custom reader returned an invalid count') |
| 123 | } |
| 124 | if !isnil(state.position_cursor) { acknowledge_custom_attached_io(state, int(count), false)! } |
| 125 | mut current := m.detach_custom_port_state() |
| 126 | mut resource := current.resource |
| 127 | mut adapter := resource.input_adapter |
| 128 | data := current.pending.bytevector_pointer() |
| 129 | adapter.raw.buffer = unsafe { data.bytes[..int(count)] } |
| 130 | adapter.raw.position = 0 |
| 131 | adapter.raw.ended = count == 0 |
| 132 | adapter.finished = false |
| 133 | // New raw publication supersedes any decoded lookahead left by an |
| 134 | // intervening operation; codec carry itself remains physical shared state. |
| 135 | resource.replay_active = false |
| 136 | resource.replay_tail = []u8{} |
| 137 | resource.text_view = unspecified() |
| 138 | mut port := current.target.input_pointer() |
| 139 | port.buffer = []u8{} |
| 140 | port.position = 0 |
| 141 | port.ended = false |
| 142 | if !isnil(current.position_cursor) { publish_custom_attached_read(current, int(count)) } |
| 143 | current.pending = unspecified() |
| 144 | } |
| 145 | |