| 1 | module scheme |
| 2 | |
| 3 | @[heap] |
| 4 | struct CustomAttachedProbePositions { |
| 5 | mut: |
| 6 | points [4]CustomRawPoint |
| 7 | } |
| 8 | |
| 9 | @[heap] |
| 10 | struct CustomAttachedPosition { |
| 11 | mut: |
| 12 | physical CustomRawPoint |
| 13 | raw_origin CustomRawPoint |
| 14 | initial &CustomRawAnchor = unsafe { nil } |
| 15 | initial_size int |
| 16 | cohorts CustomRawCohorts |
| 17 | history TranscodedBoundaries[CustomRawPoint] |
| 18 | decoded_base int |
| 19 | track CustomRawBoundaryDecoder |
| 20 | prefix [4]CustomRawPoint |
| 21 | prefix_len int |
| 22 | transmitting bool |
| 23 | } |
| 24 | |
| 25 | enum CustomAttachedPhase { |
| 26 | idle |
| 27 | read_get |
| 28 | read_ready |
| 29 | read |
| 30 | align_needed |
| 31 | align_get |
| 32 | align_set |
| 33 | write_get |
| 34 | write_ready |
| 35 | write |
| 36 | } |
| 37 | |
| 38 | @[heap] |
| 39 | struct CustomAttachedOperation { |
| 40 | kind CustomCursorKind = .attached |
| 41 | mut: |
| 42 | phase CustomAttachedPhase |
| 43 | epoch u64 |
| 44 | done bool |
| 45 | aligned bool |
| 46 | draining bool |
| 47 | force_read bool |
| 48 | target CustomRawPoint |
| 49 | align Value |
| 50 | before CustomRawPoint |
| 51 | candidate CustomRawPoint |
| 52 | probe &CustomAttachedProbePositions = unsafe { nil } |
| 53 | } |
| 54 | |
| 55 | fn positioned_custom_attached(resource &CustomPortResource) bool { |
| 56 | return !isnil(resource.duplex) && !isnil(resource.input_adapter) && !isnil(resource.output_adapter) |
| 57 | && (resource.get_position.is_callable() || resource.set_position.is_callable()) |
| 58 | } |
| 59 | |
| 60 | fn custom_attached_pending(resource &CustomPortResource) bool { |
| 61 | return positioned_custom_attached(resource) && !isnil(resource.output_adapter.pending) |
| 62 | } |
| 63 | |
| 64 | fn (state &CustomPortOperation) attached_operation() &CustomAttachedOperation { |
| 65 | return unsafe { &CustomAttachedOperation(state.position_cursor) } |
| 66 | } |
| 67 | |
| 68 | fn (mut state CustomPortOperation) attached_operation_state() &CustomAttachedOperation { |
| 69 | if isnil(state.position_cursor) { state.position_cursor = &CustomAttachedOperation{} } |
| 70 | return state.attached_operation() |
| 71 | } |
| 72 | |
| 73 | fn initialize_custom_attached_position(mut source CustomTranscodedInput) ! { |
| 74 | resource := source.resource |
| 75 | if !positioned_custom_attached(resource) { return } |
| 76 | mut position := &CustomAttachedPosition{ initial_size: source.raw.buffer.len } |
| 77 | if is_exact_integer(resource.binary_position) { |
| 78 | base := if source.raw.buffer.len == 0 { |
| 79 | resource.binary_position |
| 80 | } else { |
| 81 | numeric_subtract(resource.binary_position, integer(source.raw.buffer.len))! |
| 82 | } |
| 83 | position.raw_origin = custom_raw_point(base, 0)! |
| 84 | } else if resource.get_position.is_callable() { |
| 85 | position.initial = &CustomRawAnchor{} |
| 86 | position.raw_origin = custom_initial_raw_point(position.initial, 0)! |
| 87 | } else { |
| 88 | position.raw_origin = position.cohorts.next() |
| 89 | } |
| 90 | position.physical = CustomRawPoint{ ...position.raw_origin, offset: source.raw.buffer.len } |
| 91 | start := CustomRawPoint{ ...position.raw_origin, offset: source.raw.position } |
| 92 | position.history.reset(start) |
| 93 | position.track.begin(start, 0)! |
| 94 | source.position = position |
| 95 | } |
| 96 | |
| 97 | fn defer_custom_attached_close(lease &CustomPortLease) bool { |
| 98 | return lease.closing && !lease.both && lease.port.tag == .input_port |
| 99 | && positioned_custom_attached(lease.resource) && !lease.resource.duplex.input.closed |
| 100 | && !lease.resource.duplex.output.closed |
| 101 | } |
| 102 | |
| 103 | fn custom_attached_visible(value Value) !CustomRawPoint { |
| 104 | resource := custom_input_resource(value) |
| 105 | if isnil(resource) { return failure('input port is closed') } |
| 106 | mut position := resource.input_adapter.position |
| 107 | if resource.duplex.input.closed { return position.physical } |
| 108 | refresh_custom_text_view(value)! |
| 109 | port := value.input_pointer() |
| 110 | if resource.replay_active && port.position < position.prefix_len { |
| 111 | return position.prefix[port.position] |
| 112 | } |
| 113 | return position.history.at(position.decoded_base + port.position) |
| 114 | } |
| 115 | |
| 116 | fn trim_custom_attached_coordinates(resource &CustomPortResource, count int, prefix bool) { |
| 117 | if !positioned_custom_attached(resource) { return } |
| 118 | mut position := resource.input_adapter.position |
| 119 | if prefix { |
| 120 | remaining := position.prefix_len - count |
| 121 | if remaining < 0 { panic('invalid attached replay prefix consumption') } |
| 122 | for i in 0 .. remaining { position.prefix[i] = position.prefix[i + count] } |
| 123 | for i in remaining .. 4 { position.prefix[i] = CustomRawPoint{} } |
| 124 | position.prefix_len = remaining |
| 125 | } else { |
| 126 | position.decoded_base += count |
| 127 | position.history.prune(position.decoded_base) or { panic(err.msg()) } |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | fn observe_custom_attached_eof(value Value) { |
| 132 | resource := custom_input_resource(value) |
| 133 | if !positioned_custom_attached(resource) { return } |
| 134 | mut position := resource.input_adapter.position |
| 135 | position.history.commit(position.track.frontier()) |
| 136 | } |
| 137 | |
| 138 | fn (mut position CustomAttachedPosition) accept_getter(raw Value) ! { |
| 139 | check_custom_binary_position(raw)! |
| 140 | if !isnil(position.initial) { |
| 141 | base := if position.initial_size == 0 { |
| 142 | raw |
| 143 | } else { |
| 144 | numeric_subtract(raw, integer(position.initial_size))! |
| 145 | } |
| 146 | position.initial.resolve(base)! |
| 147 | position.initial = unsafe { nil } |
| 148 | } |
| 149 | position.physical = custom_raw_point(raw, 0)! |
| 150 | } |
| 151 | |
| 152 | fn (mut position CustomAttachedPosition) request_candidate(limit int) !CustomRawPoint { |
| 153 | if !position.physical.valid { position.physical = position.cohorts.next() } |
| 154 | point := position.physical |
| 155 | if !isnil(point.initial) && point.initial.symbolic { return position.cohorts.next() } |
| 156 | if point.offset > max_int - limit { return custom_raw_point(point.absolute()!, 0) } |
| 157 | return point |
| 158 | } |
| 159 | |
| 160 | // A successful seek or first positive write acknowledgment invalidates RX. |
| 161 | // Keep the codec session's established endian/header state across switches. |
| 162 | fn reset_custom_attached_input(resource &CustomPortResource, point CustomRawPoint) { |
| 163 | mut source := resource.input_adapter |
| 164 | mut position := source.position |
| 165 | position.raw_origin = point |
| 166 | position.history.reset(point) |
| 167 | // Partial acknowledgments and subsequent writes only advance the empty |
| 168 | // input boundary. The first switch already retired all input-owned state. |
| 169 | if position.transmitting { return } |
| 170 | position.transmitting = true |
| 171 | old := source.cursor |
| 172 | source.cursor = TranscoderDecoder{ |
| 173 | eol: old.eol |
| 174 | mode: old.mode |
| 175 | decoder: CodecDecoder{ codec: old.decoder.codec, started: true, little: old.decoder.little } |
| 176 | } |
| 177 | source.raw.buffer = []u8{} |
| 178 | source.raw.position = 0 |
| 179 | source.raw.mark = -1 |
| 180 | source.raw.ended = false |
| 181 | source.finished = false |
| 182 | source.step_input = CodecInput{} |
| 183 | position.decoded_base = 0 |
| 184 | position.track = CustomRawBoundaryDecoder{} |
| 185 | position.prefix = [4]CustomRawPoint{} |
| 186 | position.prefix_len = 0 |
| 187 | mut current := unsafe { &CustomPortResource(resource) } |
| 188 | current.replay_active = false |
| 189 | current.replay_tail = []u8{} |
| 190 | current.text_view = unspecified() |
| 191 | mut port := ¤t.duplex.input |
| 192 | port.buffer = []u8{} |
| 193 | port.position = 0 |
| 194 | port.mark = -1 |
| 195 | port.ended = false |
| 196 | port.location = unsafe { nil } |
| 197 | } |
| 198 | |
| 199 | fn (mut m Machine) request_custom_attached_get(mut vm VM, phase CustomAttachedPhase) ! { |
| 200 | mut state := custom_port_operation(m.env.slots[0]) |
| 201 | mut operation := state.attached_operation_state() |
| 202 | operation.phase = phase |
| 203 | operation.epoch = state.resource.io_epoch |
| 204 | operation.done = false |
| 205 | m.push(state.resource.get_position) |
| 206 | m.apply(mut vm, 0, false)! |
| 207 | } |
| 208 | |
| 209 | fn (mut m Machine) align_custom_attached_output(mut vm VM) !bool { |
| 210 | mut state := custom_port_operation(m.env.slots[0]) |
| 211 | claim_custom_attached_epoch(state) |
| 212 | mut operation := state.attached_operation_state() |
| 213 | if operation.aligned { return false } |
| 214 | mut position := state.resource.input_adapter.position |
| 215 | if !operation.target.valid { |
| 216 | if state.resource.duplex.input.closed { |
| 217 | operation.target = position.physical |
| 218 | } else { |
| 219 | input := payload_value(.input_port, &state.resource.duplex.input) |
| 220 | operation.target = custom_attached_visible(input)! |
| 221 | } |
| 222 | } |
| 223 | if !isnil(position.initial) || (!position.physical.valid && state.resource.get_position.is_callable()) { |
| 224 | m.request_custom_attached_get(mut vm, .align_get)! |
| 225 | return true |
| 226 | } |
| 227 | mut equal := operation.target.same(position.physical) |
| 228 | if !equal && position.physical.valid { |
| 229 | if target := operation.target.absolute() { |
| 230 | if actual := position.physical.absolute() { equal = numeric_equal(target, actual) } |
| 231 | } |
| 232 | } |
| 233 | if equal { |
| 234 | operation.aligned = true |
| 235 | operation.phase = .idle |
| 236 | return false |
| 237 | } |
| 238 | if !state.resource.set_position.is_callable() { |
| 239 | return failure('cannot reconcile custom duplex input without a position setter') |
| 240 | } |
| 241 | operation.align = operation.target.absolute() or { |
| 242 | return failure('custom duplex position is unknown; cannot seek unread input') |
| 243 | } |
| 244 | operation.phase = .align_set |
| 245 | operation.epoch = state.resource.io_epoch |
| 246 | operation.done = false |
| 247 | position.physical = CustomRawPoint{} |
| 248 | m.push(state.resource.set_position) |
| 249 | m.push(operation.align) |
| 250 | m.apply(mut vm, 1, false)! |
| 251 | return true |
| 252 | } |
| 253 | |
| 254 | fn (mut m Machine) step_custom_attached_position(mut vm VM) !bool { |
| 255 | mut state := custom_port_operation(m.env.slots[0]) |
| 256 | if !positioned_custom_attached(state.resource) { return false } |
| 257 | if state.lease.closing && state.resource.close_attempted { |
| 258 | m.finish_custom_port_operation(unspecified()) |
| 259 | return true |
| 260 | } |
| 261 | if zero_custom_text_read(state.operation, state.end) { return false } |
| 262 | half_close := defer_custom_attached_close(state.lease) |
| 263 | // Cached reads and output that produces no packet need no private cursor |
| 264 | // record. Callback preparation/probe capture allocate it only when needed. |
| 265 | if isnil(state.position_cursor) && !half_close && !custom_attached_pending(state.resource) { |
| 266 | return false |
| 267 | } |
| 268 | mut operation := state.attached_operation_state() |
| 269 | if operation.draining { |
| 270 | if isnil(state.encoded) { |
| 271 | operation.draining = false |
| 272 | operation.phase = .idle |
| 273 | } else { |
| 274 | // The final acknowledgment can clear shared pending bytes while |
| 275 | // this private encoder still needs its drain-only completion step. |
| 276 | m.step_custom_encoded_output(mut vm)! |
| 277 | return true |
| 278 | } |
| 279 | } |
| 280 | reading := state.target.tag == .input_port && !state.lease.closing |
| 281 | if (reading || half_close) && custom_attached_pending(state.resource) { |
| 282 | operation.draining = true |
| 283 | if isnil(state.encoded) { |
| 284 | state.encoded = &CustomEncodedState{ adapter: state.resource.output_adapter, drain_only: true } |
| 285 | } |
| 286 | m.step_custom_encoded_output(mut vm)! |
| 287 | return true |
| 288 | } |
| 289 | if operation.force_read { |
| 290 | if operation.phase != .read_ready { |
| 291 | m.request_custom_attached_get(mut vm, .read_get)! |
| 292 | } else { |
| 293 | m.force_custom_attached_read(mut vm)! |
| 294 | } |
| 295 | return true |
| 296 | } |
| 297 | if operation.phase == .align_needed || half_close { |
| 298 | if m.align_custom_attached_output(mut vm)! { return true } |
| 299 | if half_close { state.lease.begin_close() } |
| 300 | } |
| 301 | return false |
| 302 | } |
| 303 | |
| 304 | fn (mut m Machine) collect_custom_attached_position(result Value) !bool { |
| 305 | state := custom_port_operation(m.env.slots[0]) |
| 306 | if !positioned_custom_attached(state.resource) || isnil(state.position_cursor) { return false } |
| 307 | operation := state.attached_operation() |
| 308 | if operation.phase !in [.read_get, .align_get, .align_set, .write_get] { return false } |
| 309 | if operation.epoch != state.resource.io_epoch { |
| 310 | return failure('custom duplex position changed while callback was suspended') |
| 311 | } |
| 312 | mut position := state.resource.input_adapter.position |
| 313 | replayed := operation.done |
| 314 | if operation.phase == .align_set { |
| 315 | if !replayed { |
| 316 | position.physical = custom_raw_point(operation.align, 0)! |
| 317 | reset_custom_attached_input(state.resource, position.physical) |
| 318 | } |
| 319 | } else { |
| 320 | raw := custom_port_single_result(result)! |
| 321 | check_custom_binary_position(raw)! |
| 322 | if !replayed { position.accept_getter(raw)! } |
| 323 | } |
| 324 | mut shared := state.attached_operation() |
| 325 | shared.done = true |
| 326 | mut current := m.detach_custom_port_state() |
| 327 | mut next := current.attached_operation_state() |
| 328 | match operation.phase { |
| 329 | .read_get { |
| 330 | if replayed { next.force_read = true } |
| 331 | next.phase = if replayed { |
| 332 | CustomAttachedPhase.idle |
| 333 | } else { |
| 334 | CustomAttachedPhase.read_ready |
| 335 | } |
| 336 | } |
| 337 | .align_get { next.phase = .align_needed } |
| 338 | .align_set { |
| 339 | next.aligned = true |
| 340 | next.phase = .idle |
| 341 | } |
| 342 | .write_get { |
| 343 | next.phase = if replayed { |
| 344 | CustomAttachedPhase.idle |
| 345 | } else { |
| 346 | CustomAttachedPhase.write_ready |
| 347 | } |
| 348 | } |
| 349 | else {} |
| 350 | } |
| 351 | return true |
| 352 | } |
| 353 | |
| 354 | // Called only when an actual raw reader request is needed, after decoding all |
| 355 | // currently available bytes. Getter callbacks remain on the same Machine. |
| 356 | fn (mut m Machine) prepare_custom_attached_read(mut vm VM) !bool { |
| 357 | mut state := custom_port_operation(m.env.slots[0]) |
| 358 | if !positioned_custom_attached(state.resource) { return false } |
| 359 | mut operation := state.attached_operation_state() |
| 360 | if state.resource.get_position.is_callable() && operation.phase != .read_ready { |
| 361 | m.request_custom_attached_get(mut vm, .read_get)! |
| 362 | return true |
| 363 | } |
| 364 | mut position := state.resource.input_adapter.position |
| 365 | operation.candidate = position.request_candidate(state.request_count)! |
| 366 | operation.before = position.physical |
| 367 | operation.phase = .read |
| 368 | operation.force_read = false |
| 369 | operation.epoch = state.resource.io_epoch |
| 370 | operation.done = false |
| 371 | position.physical = CustomRawPoint{} |
| 372 | return false |
| 373 | } |
| 374 | |
| 375 | fn (mut m Machine) prepare_custom_attached_write(mut vm VM) !bool { |
| 376 | mut state := custom_port_operation(m.env.slots[0]) |
| 377 | if !positioned_custom_attached(state.resource) { return false } |
| 378 | claim_custom_attached_epoch(state) |
| 379 | mut operation := state.attached_operation_state() |
| 380 | mut position := state.resource.input_adapter.position |
| 381 | if (!position.physical.valid || !isnil(position.initial)) && state.resource.get_position.is_callable() |
| 382 | && operation.phase != .write_ready { |
| 383 | m.request_custom_attached_get(mut vm, .write_get)! |
| 384 | return true |
| 385 | } |
| 386 | operation.candidate = position.request_candidate(state.request_count)! |
| 387 | operation.before = position.physical |
| 388 | operation.phase = .write |
| 389 | operation.epoch = state.resource.io_epoch |
| 390 | operation.done = false |
| 391 | position.physical = CustomRawPoint{} |
| 392 | return false |
| 393 | } |
| 394 | |
| 395 | fn acknowledge_custom_attached_io(state &CustomPortOperation, count int, writing bool) ! { |
| 396 | if !positioned_custom_attached(state.resource) { return } |
| 397 | mut operation := state.attached_operation() |
| 398 | if operation.epoch != state.resource.io_epoch { |
| 399 | return failure('custom duplex position changed while callback was suspended') |
| 400 | } |
| 401 | if !operation.done { |
| 402 | mut position := state.resource.input_adapter.position |
| 403 | position.physical = if count == 0 { |
| 404 | operation.before |
| 405 | } else { |
| 406 | CustomRawPoint{ ...operation.candidate, offset: operation.candidate.offset + count } |
| 407 | } |
| 408 | if writing && count > 0 { reset_custom_attached_input(state.resource, position.physical) } |
| 409 | } |
| 410 | operation.done = true |
| 411 | } |
| 412 | |
| 413 | fn publish_custom_attached_read(state &CustomPortOperation, count int) { |
| 414 | if !positioned_custom_attached(state.resource) { return } |
| 415 | mut operation := state.attached_operation() |
| 416 | mut position := state.resource.input_adapter.position |
| 417 | position.raw_origin = if count == 0 { operation.before } else { operation.candidate } |
| 418 | position.prefix = [4]CustomRawPoint{} |
| 419 | position.prefix_len = 0 |
| 420 | operation.phase = .idle |
| 421 | } |
| 422 | |
| 423 | fn finish_custom_attached_write(state &CustomPortOperation) { |
| 424 | if positioned_custom_attached(state.resource) { |
| 425 | mut operation := state.attached_operation() |
| 426 | operation.phase = .idle |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | // An ignored string has no encoded packet and must not invalidate input. |
| 431 | // Output claims its cursor epoch when real alignment or raw writing begins. |
| 432 | fn claim_custom_attached_epoch(state &CustomPortOperation) { |
| 433 | mut resource := state.resource |
| 434 | if voidptr(resource.io_lease) != voidptr(state.lease) { |
| 435 | resource.io_lease = state.lease |
| 436 | resource.io_epoch++ |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | fn (mut m Machine) force_custom_attached_read(mut vm VM) ! { |
| 441 | state := custom_port_operation(m.env.slots[0]) |
| 442 | mut resource := state.resource |
| 443 | mut source := resource.input_adapter |
| 444 | mut position := source.position |
| 445 | mut base := position.physical |
| 446 | if source.cursor.pending_cr || source.cursor.has_queued { |
| 447 | base = position.history.raw_end |
| 448 | } else if source.cursor.decoder.count > 0 { |
| 449 | base = position.track.trail.back(source.cursor.decoder.count) |
| 450 | } |
| 451 | source.raw.buffer = []u8{} |
| 452 | source.raw.position = 0 |
| 453 | source.raw.ended = false |
| 454 | source.finished = false |
| 455 | position.raw_origin = position.physical |
| 456 | position.history.reset(base) |
| 457 | position.decoded_base = 0 |
| 458 | if source.cursor.decoder.count == 0 { position.track.begin(position.physical, 0)! } |
| 459 | position.prefix = [4]CustomRawPoint{} |
| 460 | position.prefix_len = 0 |
| 461 | resource.replay_active = false |
| 462 | resource.replay_tail = []u8{} |
| 463 | resource.text_view = unspecified() |
| 464 | mut port := state.target.input_pointer() |
| 465 | port.buffer = []u8{} |
| 466 | port.position = 0 |
| 467 | port.ended = false |
| 468 | m.request_custom_raw_input(mut vm)! |
| 469 | } |
| 470 | |