| 1 | module scheme |
| 2 | |
| 3 | // Positioned binary duplex callbacks describe one shared backend cursor. |
| 4 | // Ports without either position callback keep independent RX/TX algorithms. |
| 5 | // Attached transcoders use raw byte boundaries and are handled separately. |
| 6 | enum CustomBinaryDuplexPhase { |
| 7 | none |
| 8 | get_origin |
| 9 | set_origin |
| 10 | ready |
| 11 | } |
| 12 | |
| 13 | // Cold state is allocated only for positioned binary duplex operations that |
| 14 | // need a callback or reconciliation. Ordinary custom ports retain one pointer, |
| 15 | // rather than two Value payloads and request metadata in every operation. |
| 16 | @[heap] |
| 17 | struct CustomBinaryCursor { |
| 18 | kind CustomCursorKind = .binary |
| 19 | mut: |
| 20 | duplex_phase CustomBinaryDuplexPhase |
| 21 | io_epoch u64 |
| 22 | raw_position Value |
| 23 | align_position Value |
| 24 | callback_done bool |
| 25 | request_unread int |
| 26 | } |
| 27 | |
| 28 | fn (state &CustomPortOperation) binary_cursor() &CustomBinaryCursor { |
| 29 | return unsafe { &CustomBinaryCursor(state.position_cursor) } |
| 30 | } |
| 31 | |
| 32 | fn (mut state CustomPortOperation) binary_cursor_state() &CustomBinaryCursor { |
| 33 | if isnil(state.position_cursor) { state.position_cursor = &CustomBinaryCursor{} } |
| 34 | return state.binary_cursor() |
| 35 | } |
| 36 | |
| 37 | fn positioned_custom_binary(resource &CustomPortResource) bool { |
| 38 | return !isnil(resource.duplex) && resource.duplex.input.binary |
| 39 | && isnil(resource.input_adapter) && isnil(resource.output_adapter) |
| 40 | && (resource.get_position.is_callable() || resource.set_position.is_callable()) |
| 41 | } |
| 42 | |
| 43 | fn defer_custom_binary_close(lease &CustomPortLease) bool { |
| 44 | return lease.closing && !lease.both && lease.port.tag == .input_port |
| 45 | && positioned_custom_binary(lease.resource) && !lease.resource.duplex.input.closed |
| 46 | && !lease.resource.duplex.output.closed |
| 47 | } |
| 48 | |
| 49 | fn custom_binary_unread(resource &CustomPortResource) int { |
| 50 | port := &resource.duplex.input |
| 51 | return if port.closed { 0 } else { port.buffer.len - port.position } |
| 52 | } |
| 53 | |
| 54 | fn clear_custom_binary_input(mut resource CustomPortResource) { |
| 55 | mut port := &resource.duplex.input |
| 56 | if port.closed { return } |
| 57 | port.buffer = []u8{} |
| 58 | port.position = 0 |
| 59 | port.mark = -1 |
| 60 | port.ended = false |
| 61 | port.location = unsafe { nil } |
| 62 | } |
| 63 | |
| 64 | // Counts are bounded buffer lengths; absolute coordinates remain Scheme exact |
| 65 | // integers. In-flight callbacks invalidate the cached physical coordinate: an |
| 66 | // escape can move the backend without ever returning an acknowledgment. |
| 67 | fn begin_custom_binary_callback(state &CustomPortOperation, changes_position bool) { |
| 68 | if !positioned_custom_binary(state.resource) { return } |
| 69 | mut current := unsafe { &CustomPortOperation(state) } |
| 70 | mut cursor := current.binary_cursor_state() |
| 71 | mut resource := state.resource |
| 72 | cursor.callback_done = false |
| 73 | cursor.request_unread = custom_binary_unread(resource) |
| 74 | cursor.io_epoch = resource.io_epoch |
| 75 | cursor.raw_position = resource.binary_position |
| 76 | if changes_position { resource.binary_position = unspecified() } |
| 77 | } |
| 78 | |
| 79 | fn (mut vm VM) custom_combined_position_host(operation PortIoOperation, target Value, position Value) !Value { |
| 80 | args := if operation == .set_position { [target, position] } else { [target] } |
| 81 | return vm.custom_combined_host_operation(operation, target, args) |
| 82 | } |
| 83 | |
| 84 | fn (mut vm VM) custom_combined_host_operation(operation PortIoOperation, target Value, args []Value) !Value { |
| 85 | state := vm.prepare_custom_combined_operation(operation, target, args)! |
| 86 | 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)]) |
| 87 | } |
| 88 | |
| 89 | fn (mut m Machine) step_custom_binary_duplex(mut vm VM) !bool { |
| 90 | mut state := custom_port_operation(m.env.slots[0]) |
| 91 | if !positioned_custom_binary(state.resource) { return false } |
| 92 | writing := state.operation in [.write_u8, .put_u8, .write_bytes, .put_bytes] && state.offset < state.end |
| 93 | half_close := defer_custom_binary_close(state.lease) |
| 94 | if !writing && !half_close { return false } |
| 95 | mut cursor := state.binary_cursor_state() |
| 96 | mut resource := state.resource |
| 97 | match cursor.duplex_phase { |
| 98 | .none { |
| 99 | if custom_binary_unread(resource) == 0 { |
| 100 | cursor.duplex_phase = .ready |
| 101 | } else { |
| 102 | if !resource.set_position.is_callable() { |
| 103 | return failure('cannot rewind unread custom duplex input without a position setter') |
| 104 | } |
| 105 | cursor.duplex_phase = if resource.get_position.is_callable() { |
| 106 | .get_origin |
| 107 | } else { |
| 108 | .set_origin |
| 109 | } |
| 110 | if cursor.duplex_phase == .set_origin { |
| 111 | if !is_exact_integer(resource.binary_position) { |
| 112 | return failure('custom duplex position is unknown; set its position before switching to output') |
| 113 | } |
| 114 | cursor.align_position = custom_binary_logical_position(resource, resource.binary_position)! |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | else {} |
| 119 | } |
| 120 | match cursor.duplex_phase { |
| 121 | .get_origin { |
| 122 | begin_custom_binary_callback(state, false) |
| 123 | m.push(resource.get_position) |
| 124 | m.apply(mut vm, 0, false)! |
| 125 | return true |
| 126 | } |
| 127 | .set_origin { |
| 128 | begin_custom_binary_callback(state, true) |
| 129 | m.push(resource.set_position) |
| 130 | m.push(cursor.align_position) |
| 131 | m.apply(mut vm, 1, false)! |
| 132 | return true |
| 133 | } |
| 134 | .ready { |
| 135 | if half_close { state.lease.begin_close() } |
| 136 | } |
| 137 | .none {} |
| 138 | } |
| 139 | return false |
| 140 | } |
| 141 | |
| 142 | fn custom_binary_logical_position(resource &CustomPortResource, raw Value) !Value { |
| 143 | check_custom_binary_position(raw)! |
| 144 | return custom_binary_position_with_unread(raw, custom_binary_unread(resource)) |
| 145 | } |
| 146 | |
| 147 | fn custom_binary_position_with_unread(raw Value, unread int) !Value { |
| 148 | check_custom_binary_position(raw)! |
| 149 | position := numeric_subtract(raw, integer(unread))! |
| 150 | if exact_sign(position) < 0 { return failure('source position precedes buffered input') } |
| 151 | return position |
| 152 | } |
| 153 | |
| 154 | // Callback collection is the commit point. A continuation returning after a |
| 155 | // different operation cannot publish a stale physical coordinate or discard |
| 156 | // that operation's fresh read-ahead. Replaying without an intervening lease |
| 157 | // retains the existing private-operation replay policy. |
| 158 | fn (mut m Machine) collect_custom_binary_duplex(result Value) !bool { |
| 159 | state := custom_port_operation(m.env.slots[0]) |
| 160 | if !positioned_custom_binary(state.resource) { return false } |
| 161 | mut resource := state.resource |
| 162 | if isnil(state.position_cursor) { return false } |
| 163 | mut cursor := state.binary_cursor() |
| 164 | if cursor.duplex_phase !in [.get_origin, .set_origin] && state.operation in [ |
| 165 | .close_input, |
| 166 | .close_output, |
| 167 | .close_any, |
| 168 | .flush_output, |
| 169 | ] { |
| 170 | return false |
| 171 | } |
| 172 | if cursor.io_epoch != resource.io_epoch { |
| 173 | return failure('custom duplex position changed while callback was suspended') |
| 174 | } |
| 175 | if cursor.duplex_phase in [.get_origin, .set_origin] { |
| 176 | if cursor.duplex_phase == .get_origin { |
| 177 | raw := custom_port_single_result(result)! |
| 178 | logical := custom_binary_position_with_unread(raw, cursor.request_unread)! |
| 179 | if !cursor.callback_done { resource.binary_position = raw } |
| 180 | // This flag belongs to the shared request, before detaching private |
| 181 | // progress. Old continuation shots must not acknowledge it twice. |
| 182 | mut shared := cursor |
| 183 | shared.callback_done = true |
| 184 | mut current := m.detach_custom_port_state() |
| 185 | mut next := current.binary_cursor_state() |
| 186 | next.align_position = logical |
| 187 | next.duplex_phase = .set_origin |
| 188 | } else { |
| 189 | if !cursor.callback_done { |
| 190 | resource.binary_position = cursor.align_position |
| 191 | clear_custom_binary_input(mut resource) |
| 192 | } |
| 193 | mut shared := cursor |
| 194 | shared.callback_done = true |
| 195 | mut current := m.detach_custom_port_state() |
| 196 | mut next := current.binary_cursor_state() |
| 197 | next.duplex_phase = .ready |
| 198 | } |
| 199 | return true |
| 200 | } |
| 201 | match state.operation { |
| 202 | .get_position { |
| 203 | raw := custom_port_single_result(result)! |
| 204 | logical := custom_binary_position_with_unread(raw, cursor.request_unread)! |
| 205 | if !cursor.callback_done { resource.binary_position = raw } |
| 206 | mut shared := cursor |
| 207 | shared.callback_done = true |
| 208 | m.finish_custom_port_operation(logical) |
| 209 | return true |
| 210 | } |
| 211 | .set_position { |
| 212 | if !cursor.callback_done { |
| 213 | resource.binary_position = state.position |
| 214 | clear_custom_binary_input(mut resource) |
| 215 | } |
| 216 | mut shared := cursor |
| 217 | shared.callback_done = true |
| 218 | m.finish_custom_port_operation(unspecified()) |
| 219 | return true |
| 220 | } |
| 221 | .write_u8, .put_u8, .write_bytes, .put_bytes { |
| 222 | count := custom_port_single_result(result)!.as_integer()! |
| 223 | if count < 0 || count > state.end - state.offset { |
| 224 | return failure('custom writer returned an invalid count') |
| 225 | } |
| 226 | if !cursor.callback_done { |
| 227 | if is_exact_integer(cursor.raw_position) { |
| 228 | resource.binary_position = numeric_add(cursor.raw_position, integer(count))! |
| 229 | } |
| 230 | if count > 0 { clear_custom_binary_input(mut resource) } |
| 231 | } |
| 232 | mut shared := cursor |
| 233 | shared.callback_done = true |
| 234 | } |
| 235 | .close_input, .close_output, .close_any, .flush_output {} |
| 236 | else { |
| 237 | count := custom_port_single_result(result)!.as_integer()! |
| 238 | if count < 0 || count > state.request_count { |
| 239 | return failure('custom reader returned an invalid count') |
| 240 | } |
| 241 | if !cursor.callback_done && is_exact_integer(cursor.raw_position) { |
| 242 | resource.binary_position = numeric_add(cursor.raw_position, integer(count))! |
| 243 | } |
| 244 | mut shared := cursor |
| 245 | shared.callback_done = true |
| 246 | } |
| 247 | } |
| 248 | return false |
| 249 | } |
| 250 | |