| 1 | module scheme |
| 2 | |
| 3 | import io |
| 4 | |
| 5 | // The input raw facet is first all the way to this allocation's base. Shared |
| 6 | // file state can therefore retain it as a GC root while its output raw facet |
| 7 | // points into the same allocation. Both public endpoint interfaces own this base. |
| 8 | @[heap] |
| 9 | struct CombinedTranscodedPort { |
| 10 | mut: |
| 11 | input TranscodedInput |
| 12 | output TranscodedOutput |
| 13 | facade Value |
| 14 | file &CombinedFileState = unsafe { nil } |
| 15 | boundaries TranscodedBoundaries[i64] |
| 16 | track TranscodedBoundaryDecoder |
| 17 | emitted i64 |
| 18 | initialized bool |
| 19 | reading bool |
| 20 | } |
| 21 | |
| 22 | fn combined_transcoded_resource(value Value) &CombinedTranscodedPort { |
| 23 | if value.tag != .input_output_port { return unsafe { nil } } |
| 24 | input := value.input_pointer() |
| 25 | if input.source is CombinedTranscodedPort { return input.source } |
| 26 | output := value.output_pointer() |
| 27 | if output.sink is CombinedTranscodedPort { return output.sink } |
| 28 | return unsafe { nil } |
| 29 | } |
| 30 | |
| 31 | fn (mut vm VM) attach_combined_transcoder(value Value, codec CodecKind, eol EolStyle, mode ErrorHandlingMode, code u8) !Value { |
| 32 | mut input := vm.input_owner(value)! |
| 33 | mut output := vm.output_owner(value)! |
| 34 | check_combined_busy(value)! |
| 35 | if !input.binary || !output.binary { return failure('expected a binary input/output port') } |
| 36 | if input.closed && output.closed { return failure('input/output port is closed') } |
| 37 | if !isnil(custom_input_resource(value)) || !isnil(custom_output_resource(value)) { |
| 38 | return vm.attach_custom_combined(value, codec, eol, mode, code) |
| 39 | } |
| 40 | mut group := &CombinedTranscodedPort{ |
| 41 | input: TranscodedInput{ raw: InputPort{ ...input, buffer: unsafe { input.buffer } }, cursor: TranscoderDecoder{ eol: eol, mode: mode, decoder: CodecDecoder{ codec: codec } } } |
| 42 | output: TranscodedOutput{ raw: OutputPort{ ...output }, codec: codec, eol: eol, mode: mode } |
| 43 | file: combined_file_resource(value) |
| 44 | } |
| 45 | if input.closed { |
| 46 | // Binary close discarded its buffer after reconciling the file cursor, |
| 47 | // but its old byte index is intentionally no longer meaningful. |
| 48 | group.input.raw.position = 0 |
| 49 | group.input.raw.mark = -1 |
| 50 | group.input.raw.ended = false |
| 51 | } |
| 52 | result := vm.input_output_port(group, group, |
| 53 | read_size: input.read_size |
| 54 | fold_case: !input.no_fold |
| 55 | )! |
| 56 | group.facade = result |
| 57 | mut new_input := result.input_pointer() |
| 58 | mut new_output := result.output_pointer() |
| 59 | new_input.file_backed = input.file_backed |
| 60 | new_output.file_backed = output.file_backed |
| 61 | new_input.transcoder_code = code |
| 62 | new_output.transcoder_code = code |
| 63 | new_input.closed = input.closed |
| 64 | new_output.closed = output.closed |
| 65 | new_output.buffer_mode = output.buffer_mode |
| 66 | if !isnil(group.file) { |
| 67 | group.file.input = &group.input.raw |
| 68 | group.file.output = &group.output.raw |
| 69 | // A half-closed binary facade has dropped its endpoint interface. The |
| 70 | // owned shared backend still needs that internal capability for pending |
| 71 | // output cleanup and lazy native BOM inspection; it is not public access. |
| 72 | group.input.raw.source = group.file |
| 73 | group.output.raw.sink = group.file |
| 74 | } |
| 75 | input.closed = true |
| 76 | input.owns_file = false |
| 77 | input.file = unsafe { nil } |
| 78 | input.stream = unsafe { nil } |
| 79 | input.source = &closed_port |
| 80 | input.buffer = []u8{} |
| 81 | input.location = unsafe { nil } |
| 82 | output.closed = true |
| 83 | output.owns_file = false |
| 84 | output.file = unsafe { nil } |
| 85 | output.stream = unsafe { nil } |
| 86 | output.sink = &closed_port |
| 87 | if new_input.closed { new_input.source = &closed_port } |
| 88 | if new_output.closed { new_output.sink = &closed_port } |
| 89 | return result |
| 90 | } |
| 91 | |
| 92 | fn (group &CombinedTranscodedPort) shared_cursor() bool { |
| 93 | return !isnil(group.file) && group.file.seekable |
| 94 | } |
| 95 | |
| 96 | // The existing binary read buffer is sufficient for header detection; no new |
| 97 | // positional I/O API or temporary byte allocation is needed. On first write, |
| 98 | // sync_output rewinds that read-ahead just as it does after a user peek. This |
| 99 | // only probes native seekable files; independent host duplex never probes TX. |
| 100 | fn (mut group CombinedTranscodedPort) initialize(nonblocking bool) !bool { |
| 101 | if group.initialized { return true } |
| 102 | origin := group.file.position()! |
| 103 | mut header := 0 |
| 104 | if group.input.cursor.decoder.codec == .utf16 { |
| 105 | for group.input.raw.buffer.len - group.input.raw.position < 2 && !group.input.raw.ended { |
| 106 | if nonblocking { |
| 107 | if !group.input.raw.fill_buffer(true)! { return false } |
| 108 | } else { |
| 109 | group.input.raw.refill()! |
| 110 | } |
| 111 | } |
| 112 | raw := &group.input.raw |
| 113 | if raw.buffer.len - raw.position >= 2 { |
| 114 | a, b := raw.buffer[raw.position], raw.buffer[raw.position + 1] |
| 115 | if (a == 0xfe && b == 0xff) || (a == 0xff && b == 0xfe) { |
| 116 | header = 2 |
| 117 | group.input.raw.position += 2 |
| 118 | group.input.cursor.decoder.little = a == 0xff |
| 119 | } |
| 120 | } |
| 121 | group.input.cursor.decoder.started = true |
| 122 | group.output.little = group.input.cursor.decoder.little |
| 123 | } |
| 124 | group.boundaries.reset(origin + header) |
| 125 | group.track = TranscodedBoundaryDecoder{ consumed: origin + header } |
| 126 | group.reading = true |
| 127 | group.initialized = true |
| 128 | return true |
| 129 | } |
| 130 | |
| 131 | fn (mut group CombinedTranscodedPort) reset_decoder(raw i64) { |
| 132 | old := group.input.cursor |
| 133 | group.input.cursor = TranscoderDecoder{ eol: old.eol, mode: old.mode, decoder: CodecDecoder{ codec: old.decoder.codec, started: old.decoder.started, little: old.decoder.little } } |
| 134 | group.input.pending = 0 |
| 135 | group.input.pending_count = 0 |
| 136 | group.input.finished = false |
| 137 | group.boundaries.reset(raw) |
| 138 | group.track = TranscodedBoundaryDecoder{ consumed: raw } |
| 139 | group.emitted = 0 |
| 140 | } |
| 141 | |
| 142 | fn (mut group CombinedTranscodedPort) sync_output() ! { |
| 143 | if !group.shared_cursor() { return } |
| 144 | group.initialize(false)! |
| 145 | if !group.reading { return } |
| 146 | outer := group.facade.input_pointer() |
| 147 | unread := outer.buffer.len - outer.position |
| 148 | decoded := group.emitted - unread |
| 149 | raw := group.boundaries.at(decoded)! |
| 150 | // Prefer a cached byte boundary and one relative rewind. Older decoded |
| 151 | // lookahead can outlive the raw refill that supplied it; seek once then. |
| 152 | buffer_start := group.track.consumed - group.input.raw.position |
| 153 | buffer_end := buffer_start + group.input.raw.buffer.len |
| 154 | if raw >= buffer_start && raw <= buffer_end { |
| 155 | group.input.raw.position = int(raw - buffer_start) |
| 156 | group.file.before_write()! |
| 157 | } else { |
| 158 | group.file.seek(raw)! |
| 159 | group.file.before_write()! |
| 160 | } |
| 161 | mut input := group.facade.input_pointer() |
| 162 | input.checkpoint_input() |
| 163 | unsafe { input.buffer.len = 0 } |
| 164 | input.position = 0 |
| 165 | input.mark = -1 |
| 166 | input.ended = false |
| 167 | group.reset_decoder(raw) |
| 168 | group.reading = false |
| 169 | } |
| 170 | |
| 171 | fn (mut group CombinedTranscodedPort) read(mut bytes []u8) !int { |
| 172 | return group.read_impl(mut bytes, false) |
| 173 | } |
| 174 | |
| 175 | fn (mut group CombinedTranscodedPort) read_ready(mut bytes []u8) !int { |
| 176 | return group.read_impl(mut bytes, true) |
| 177 | } |
| 178 | |
| 179 | fn (mut group CombinedTranscodedPort) read_impl(mut destination []u8, nonblocking bool) !int { |
| 180 | if destination.len == 0 { return 0 } |
| 181 | if !group.shared_cursor() { return group.input.read_impl(mut destination, nonblocking) } |
| 182 | if !group.initialize(nonblocking)! { return -1 } |
| 183 | // Encoded retry bytes survive an output half-close failure. A shared file |
| 184 | // must finish them before RX, while independent duplex leaves RX usable. |
| 185 | group.output.flush_pending()! |
| 186 | if !group.reading { |
| 187 | group.file.before_read()! |
| 188 | group.reset_decoder(group.file.position()!) |
| 189 | group.reading = true |
| 190 | } |
| 191 | outer := group.facade.input_pointer() |
| 192 | // fill_buffer has temporarily extended outer.buffer.len for this call. |
| 193 | // Its destination starts at the end of the retained, published prefix. |
| 194 | retained := outer.buffer.len - destination.len |
| 195 | group.boundaries.prune(group.emitted - (retained - outer.position))! |
| 196 | mut produced := 0 |
| 197 | defer { group.emitted += produced } |
| 198 | for produced < destination.len { |
| 199 | if group.input.pending_count > 0 { |
| 200 | count := int_min(group.input.pending_count, destination.len - produced) |
| 201 | unsafe { vmemcpy(&u8(destination.data) + produced, &group.input.pending, count) } |
| 202 | group.input.pending_count -= count |
| 203 | if group.input.pending_count > 0 { |
| 204 | unsafe { vmemmove(&group.input.pending, &u8(&group.input.pending) + count, group.input.pending_count) } |
| 205 | } |
| 206 | produced += count |
| 207 | } |
| 208 | if produced == destination.len || group.input.finished { break } |
| 209 | before_ascii := group.track.consumed |
| 210 | copied := group.input.copy_ascii(mut destination, produced) |
| 211 | if copied > 0 { |
| 212 | group.track.consumed += copied |
| 213 | if group.boundaries.raw_end != before_ascii { |
| 214 | group.boundaries.append(1, before_ascii + 1) |
| 215 | if copied > 1 { group.boundaries.append(copied - 1, group.track.consumed) } |
| 216 | } else { |
| 217 | group.boundaries.append(copied, group.track.consumed) |
| 218 | } |
| 219 | produced += copied |
| 220 | continue |
| 221 | } |
| 222 | previous := group.input.cursor |
| 223 | previous_track := group.track |
| 224 | // A generic mut-argument chain can promote a local cursor in V. Reuse |
| 225 | // this adapter-owned scalar view, clearing it before any refill/callback. |
| 226 | group.input.step_input = codec_input(unsafe { group.input.raw.buffer[group.input.raw.position..] }, group.input.raw.ended) |
| 227 | step := group.track.next(mut group.input.cursor, mut group.input.step_input) or { |
| 228 | if produced > 0 { |
| 229 | group.input.cursor = previous |
| 230 | group.track = previous_track |
| 231 | group.input.step_input = CodecInput{} |
| 232 | return produced |
| 233 | } |
| 234 | group.input.raw.position += group.input.step_input.position |
| 235 | group.input.step_input = CodecInput{} |
| 236 | group.boundaries.commit(group.track.scalar_end) |
| 237 | return err |
| 238 | } |
| 239 | group.input.raw.position += group.input.step_input.position |
| 240 | group.input.step_input = CodecInput{} |
| 241 | match step.kind { |
| 242 | .scalar { |
| 243 | width := step.scalar.length_in_bytes() |
| 244 | group.boundaries.append(width, group.track.scalar_end) |
| 245 | if destination.len - produced >= width { |
| 246 | produced += unsafe { utf32_decode_to_buffer(u32(step.scalar), mut &destination[produced]) } |
| 247 | } else { |
| 248 | group.input.pending = 0 |
| 249 | group.input.pending_count = unsafe { utf32_decode_to_buffer(u32(step.scalar), mut &u8(&group.input.pending)) } |
| 250 | } |
| 251 | } |
| 252 | .need_input { |
| 253 | if produced > 0 { return produced } |
| 254 | if nonblocking { |
| 255 | if !group.input.raw.fill_buffer(true)! { return -1 } |
| 256 | } else { |
| 257 | group.input.raw.ensure_bytes(1)! |
| 258 | } |
| 259 | } |
| 260 | .finished { group.input.finished = true } |
| 261 | .malformed { return failure('unhandled transcoder decoding step') } |
| 262 | } |
| 263 | } |
| 264 | if produced == 0 && group.input.finished { |
| 265 | group.boundaries.commit(group.track.consumed) |
| 266 | return io.Eof{} |
| 267 | } |
| 268 | return produced |
| 269 | } |
| 270 | |
| 271 | fn (mut group CombinedTranscodedPort) write(bytes []u8) !int { |
| 272 | if bytes.len == 0 { return group.output.write(bytes) } |
| 273 | group.sync_output()! |
| 274 | return group.output.write(bytes) |
| 275 | } |
| 276 | |
| 277 | fn (mut group CombinedTranscodedPort) flush() ! { group.output.flush()! } |
| 278 | |
| 279 | fn (mut group CombinedTranscodedPort) close_half(input bool) ! { |
| 280 | outer_input := group.facade.input_pointer() |
| 281 | outer_output := group.facade.output_pointer() |
| 282 | if outer_input.closed && outer_output.closed { |
| 283 | group.close_all()! |
| 284 | return |
| 285 | } |
| 286 | if input { |
| 287 | group.sync_output()! |
| 288 | group.input.raw.closed = true |
| 289 | group.input.raw.buffer = []u8{} |
| 290 | group.input.raw.source = &closed_port |
| 291 | group.input.raw.location = unsafe { nil } |
| 292 | } else { |
| 293 | group.output.raw.closed = true |
| 294 | group.flush()! |
| 295 | group.output.raw.sink = &closed_port |
| 296 | group.output.pending = []u8{} |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | fn (mut group CombinedTranscodedPort) close_all() ! { |
| 301 | mut input := group.facade.input_pointer() |
| 302 | mut output := group.facade.output_pointer() |
| 303 | input.closed = true |
| 304 | output.closed = true |
| 305 | group.input.raw.closed = true |
| 306 | group.output.raw.closed = true |
| 307 | defer { |
| 308 | input.buffer = []u8{} |
| 309 | input.source = &closed_port |
| 310 | output.sink = &closed_port |
| 311 | group.input.raw.buffer = []u8{} |
| 312 | group.input.raw.source = &closed_port |
| 313 | group.input.raw.location = unsafe { nil } |
| 314 | group.output.raw.sink = &closed_port |
| 315 | group.output.pending = []u8{} |
| 316 | group.boundaries.spans = []TranscodedBoundarySpan[i64]{} |
| 317 | } |
| 318 | group.flush() or { |
| 319 | if !isnil(group.file) { group.file.close_all() or {} } |
| 320 | return err |
| 321 | } |
| 322 | if !isnil(group.file) { group.file.close_all()! } |
| 323 | } |
| 324 | |