module scheme // Positioned binary duplex callbacks describe one shared backend cursor. // Ports without either position callback keep independent RX/TX algorithms. // Attached transcoders use raw byte boundaries and are handled separately. enum CustomBinaryDuplexPhase { none get_origin set_origin ready } // Cold state is allocated only for positioned binary duplex operations that // need a callback or reconciliation. Ordinary custom ports retain one pointer, // rather than two Value payloads and request metadata in every operation. @[heap] struct CustomBinaryCursor { kind CustomCursorKind = .binary mut: duplex_phase CustomBinaryDuplexPhase io_epoch u64 raw_position Value align_position Value callback_done bool request_unread int } fn (state &CustomPortOperation) binary_cursor() &CustomBinaryCursor { return unsafe { &CustomBinaryCursor(state.position_cursor) } } fn (mut state CustomPortOperation) binary_cursor_state() &CustomBinaryCursor { if isnil(state.position_cursor) { state.position_cursor = &CustomBinaryCursor{} } return state.binary_cursor() } fn positioned_custom_binary(resource &CustomPortResource) bool { return !isnil(resource.duplex) && resource.duplex.input.binary && isnil(resource.input_adapter) && isnil(resource.output_adapter) && (resource.get_position.is_callable() || resource.set_position.is_callable()) } fn defer_custom_binary_close(lease &CustomPortLease) bool { return lease.closing && !lease.both && lease.port.tag == .input_port && positioned_custom_binary(lease.resource) && !lease.resource.duplex.input.closed && !lease.resource.duplex.output.closed } fn custom_binary_unread(resource &CustomPortResource) int { port := &resource.duplex.input return if port.closed { 0 } else { port.buffer.len - port.position } } fn clear_custom_binary_input(mut resource CustomPortResource) { mut port := &resource.duplex.input if port.closed { return } port.buffer = []u8{} port.position = 0 port.mark = -1 port.ended = false port.location = unsafe { nil } } // Counts are bounded buffer lengths; absolute coordinates remain Scheme exact // integers. In-flight callbacks invalidate the cached physical coordinate: an // escape can move the backend without ever returning an acknowledgment. fn begin_custom_binary_callback(state &CustomPortOperation, changes_position bool) { if !positioned_custom_binary(state.resource) { return } mut current := unsafe { &CustomPortOperation(state) } mut cursor := current.binary_cursor_state() mut resource := state.resource cursor.callback_done = false cursor.request_unread = custom_binary_unread(resource) cursor.io_epoch = resource.io_epoch cursor.raw_position = resource.binary_position if changes_position { resource.binary_position = unspecified() } } fn (mut vm VM) custom_combined_position_host(operation PortIoOperation, target Value, position Value) !Value { args := if operation == .set_position { [target, position] } else { [target] } return vm.custom_combined_host_operation(operation, target, args) } fn (mut vm VM) custom_combined_host_operation(operation PortIoOperation, target Value, args []Value) !Value { state := vm.prepare_custom_combined_operation(operation, target, args)! return vm.call(procedure_value(&Procedure{ mode: .closure, name: 'custom combined operation', owner: vm.owner, function: &custom_port_io_bridges[int(operation)] }), [payload_value(.port_operation, state)]) } fn (mut m Machine) step_custom_binary_duplex(mut vm VM) !bool { mut state := custom_port_operation(m.env.slots[0]) if !positioned_custom_binary(state.resource) { return false } writing := state.operation in [.write_u8, .put_u8, .write_bytes, .put_bytes] && state.offset < state.end half_close := defer_custom_binary_close(state.lease) if !writing && !half_close { return false } mut cursor := state.binary_cursor_state() mut resource := state.resource match cursor.duplex_phase { .none { if custom_binary_unread(resource) == 0 { cursor.duplex_phase = .ready } else { if !resource.set_position.is_callable() { return failure('cannot rewind unread custom duplex input without a position setter') } cursor.duplex_phase = if resource.get_position.is_callable() { .get_origin } else { .set_origin } if cursor.duplex_phase == .set_origin { if !is_exact_integer(resource.binary_position) { return failure('custom duplex position is unknown; set its position before switching to output') } cursor.align_position = custom_binary_logical_position(resource, resource.binary_position)! } } } else {} } match cursor.duplex_phase { .get_origin { begin_custom_binary_callback(state, false) m.push(resource.get_position) m.apply(mut vm, 0, false)! return true } .set_origin { begin_custom_binary_callback(state, true) m.push(resource.set_position) m.push(cursor.align_position) m.apply(mut vm, 1, false)! return true } .ready { if half_close { state.lease.begin_close() } } .none {} } return false } fn custom_binary_logical_position(resource &CustomPortResource, raw Value) !Value { check_custom_binary_position(raw)! return custom_binary_position_with_unread(raw, custom_binary_unread(resource)) } fn custom_binary_position_with_unread(raw Value, unread int) !Value { check_custom_binary_position(raw)! position := numeric_subtract(raw, integer(unread))! if exact_sign(position) < 0 { return failure('source position precedes buffered input') } return position } // Callback collection is the commit point. A continuation returning after a // different operation cannot publish a stale physical coordinate or discard // that operation's fresh read-ahead. Replaying without an intervening lease // retains the existing private-operation replay policy. fn (mut m Machine) collect_custom_binary_duplex(result Value) !bool { state := custom_port_operation(m.env.slots[0]) if !positioned_custom_binary(state.resource) { return false } mut resource := state.resource if isnil(state.position_cursor) { return false } mut cursor := state.binary_cursor() if cursor.duplex_phase !in [.get_origin, .set_origin] && state.operation in [ .close_input, .close_output, .close_any, .flush_output, ] { return false } if cursor.io_epoch != resource.io_epoch { return failure('custom duplex position changed while callback was suspended') } if cursor.duplex_phase in [.get_origin, .set_origin] { if cursor.duplex_phase == .get_origin { raw := custom_port_single_result(result)! logical := custom_binary_position_with_unread(raw, cursor.request_unread)! if !cursor.callback_done { resource.binary_position = raw } // This flag belongs to the shared request, before detaching private // progress. Old continuation shots must not acknowledge it twice. mut shared := cursor shared.callback_done = true mut current := m.detach_custom_port_state() mut next := current.binary_cursor_state() next.align_position = logical next.duplex_phase = .set_origin } else { if !cursor.callback_done { resource.binary_position = cursor.align_position clear_custom_binary_input(mut resource) } mut shared := cursor shared.callback_done = true mut current := m.detach_custom_port_state() mut next := current.binary_cursor_state() next.duplex_phase = .ready } return true } match state.operation { .get_position { raw := custom_port_single_result(result)! logical := custom_binary_position_with_unread(raw, cursor.request_unread)! if !cursor.callback_done { resource.binary_position = raw } mut shared := cursor shared.callback_done = true m.finish_custom_port_operation(logical) return true } .set_position { if !cursor.callback_done { resource.binary_position = state.position clear_custom_binary_input(mut resource) } mut shared := cursor shared.callback_done = true m.finish_custom_port_operation(unspecified()) return true } .write_u8, .put_u8, .write_bytes, .put_bytes { count := custom_port_single_result(result)!.as_integer()! if count < 0 || count > state.end - state.offset { return failure('custom writer returned an invalid count') } if !cursor.callback_done { if is_exact_integer(cursor.raw_position) { resource.binary_position = numeric_add(cursor.raw_position, integer(count))! } if count > 0 { clear_custom_binary_input(mut resource) } } mut shared := cursor shared.callback_done = true } .close_input, .close_output, .close_any, .flush_output {} else { count := custom_port_single_result(result)!.as_integer()! if count < 0 || count > state.request_count { return failure('custom reader returned an invalid count') } if !cursor.callback_done && is_exact_integer(cursor.raw_position) { resource.binary_position = numeric_add(cursor.raw_position, integer(count))! } mut shared := cursor shared.callback_done = true } } return false }