| 1 | module scheme |
| 2 | |
| 3 | // A lease is a dynamic resource identity. Continuations share its identity and |
| 4 | // the physical port, while private acknowledgment/progress state is detached. |
| 5 | @[heap] |
| 6 | struct CustomPortLease { |
| 7 | port Value |
| 8 | resource &CustomPortResource |
| 9 | closing bool |
| 10 | input_cursor bool |
| 11 | mut: |
| 12 | close_attempted bool |
| 13 | close_started bool |
| 14 | both bool |
| 15 | } |
| 16 | |
| 17 | fn custom_port_lease(value Value) &CustomPortLease { |
| 18 | return unsafe { &CustomPortLease(value.pointer()) } |
| 19 | } |
| 20 | |
| 21 | fn (lease &CustomPortLease) enter(context u64) ! { |
| 22 | mut resource := lease.resource |
| 23 | // A transferred binary alias cannot regain ownership through an escaped |
| 24 | // closing continuation. Its lease must not clear the new facade's buffers. |
| 25 | if !isnil(resource.duplex) { |
| 26 | current := if lease.port.tag == .input_port { |
| 27 | voidptr(&resource.duplex.input) |
| 28 | } else { |
| 29 | voidptr(&resource.duplex.output) |
| 30 | } |
| 31 | leased := if lease.port.tag == .input_port { |
| 32 | voidptr(lease.port.input_pointer()) |
| 33 | } else { |
| 34 | voidptr(lease.port.output_pointer()) |
| 35 | } |
| 36 | if leased != current { return failure('port resource belongs to a transferred facade') } |
| 37 | } |
| 38 | if !isnil(resource.active) { return failure('port operation is already active') } |
| 39 | if lease.port.tag == .input_port { |
| 40 | mut port := lease.port.input_pointer() |
| 41 | if port.busy { return failure('input port operation is already active') } |
| 42 | if !lease.closing && (port.closed || resource.closed) { |
| 43 | return failure('input port is closed') |
| 44 | } |
| 45 | port.busy = true |
| 46 | if lease.input_cursor && voidptr(resource.cursor_lease) != voidptr(lease) { |
| 47 | resource.cursor_lease = lease |
| 48 | resource.cursor_epoch++ |
| 49 | } |
| 50 | } else { |
| 51 | mut port := lease.port.output_pointer() |
| 52 | if port.busy { return failure('output port operation is already active') } |
| 53 | if !lease.closing && (port.closed || resource.closed) { |
| 54 | return failure('output port is closed') |
| 55 | } |
| 56 | port.busy = true |
| 57 | } |
| 58 | resource.active = lease |
| 59 | resource.active_context = context |
| 60 | if (positioned_custom_binary(resource) || (positioned_custom_text(resource) && (lease.input_cursor || lease.closing)) |
| 61 | || (positioned_custom_attached(resource) && (lease.closing || (lease.port.tag == .input_port && lease.input_cursor)))) |
| 62 | && voidptr(resource.io_lease) != voidptr(lease) { |
| 63 | resource.io_lease = lease |
| 64 | resource.io_epoch++ |
| 65 | } |
| 66 | if lease.closing && !defer_custom_binary_close(lease) && !defer_custom_text_close(lease) && !defer_custom_attached_close(lease) { |
| 67 | lease.begin_close() |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | // Reconciliation can fail before a half-close changes the public open state. |
| 72 | // All other closes still publish their closed facets on lease entry. |
| 73 | fn (lease &CustomPortLease) begin_close() { |
| 74 | mut current := unsafe { &CustomPortLease(lease) } |
| 75 | current.close_started = true |
| 76 | mut resource := lease.resource |
| 77 | if lease.port.tag == .input_port { |
| 78 | mut port := lease.port.input_pointer() |
| 79 | port.closed = true |
| 80 | } else { |
| 81 | mut port := lease.port.output_pointer() |
| 82 | port.closed = true |
| 83 | } |
| 84 | if isnil(resource.duplex) { |
| 85 | resource.closed = true |
| 86 | } else { |
| 87 | mut duplex := resource.duplex |
| 88 | if lease.both { |
| 89 | duplex.input.closed = true |
| 90 | duplex.output.closed = true |
| 91 | } |
| 92 | resource.closed = duplex.input.closed && duplex.output.closed |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | fn (lease &CustomPortLease) release(context u64) { |
| 97 | mut resource := lease.resource |
| 98 | if voidptr(resource.active) != voidptr(lease) || resource.active_context != context { return } |
| 99 | if lease.port.tag == .input_port && !lease.port.input_pointer().binary { |
| 100 | commit_custom_text_cursor(lease.port) |
| 101 | } |
| 102 | resource.active = unsafe { nil } |
| 103 | resource.active_context = 0 |
| 104 | if lease.port.tag == .input_port { |
| 105 | mut port := lease.port.input_pointer() |
| 106 | port.busy = false |
| 107 | if lease.close_started { |
| 108 | port.source = &closed_port |
| 109 | port.buffer = []u8{} |
| 110 | port.location = unsafe { nil } |
| 111 | resource.read_buffer = unspecified() |
| 112 | resource.text_view = unspecified() |
| 113 | resource.replay_active = false |
| 114 | resource.replay_tail = []u8{} |
| 115 | resource.cursor_lease = unsafe { nil } |
| 116 | clear_custom_text_coordinates(resource) |
| 117 | resource.release_transcoded_input_storage() |
| 118 | } |
| 119 | } else { |
| 120 | mut port := lease.port.output_pointer() |
| 121 | port.busy = false |
| 122 | if lease.close_started { port.sink = &closed_port } |
| 123 | } |
| 124 | if lease.close_started && lease.both { |
| 125 | mut duplex := resource.duplex |
| 126 | duplex.input.busy = false |
| 127 | duplex.input.source = &closed_port |
| 128 | duplex.input.buffer = []u8{} |
| 129 | duplex.input.location = unsafe { nil } |
| 130 | duplex.output.busy = false |
| 131 | duplex.output.sink = &closed_port |
| 132 | resource.read_buffer = unspecified() |
| 133 | resource.text_view = unspecified() |
| 134 | resource.replay_active = false |
| 135 | resource.replay_tail = []u8{} |
| 136 | resource.cursor_lease = unsafe { nil } |
| 137 | clear_custom_text_coordinates(resource) |
| 138 | resource.release_transcoded_input_storage() |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | fn (mut m Machine) cleanup_custom_port_leases() { |
| 143 | mut cursor := WindRef{m.wind} |
| 144 | for !isnil(cursor.value) { |
| 145 | frame := cursor.value |
| 146 | if frame.after.tag == .port_operation { custom_port_lease(frame.after).release(m.context) } |
| 147 | cursor = WindRef{frame.parent} |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | @[heap] |
| 152 | struct CustomPortOperation { |
| 153 | operation PortIoOperation |
| 154 | target Value |
| 155 | resource &CustomPortResource |
| 156 | source Value |
| 157 | end int |
| 158 | position Value |
| 159 | lease &CustomPortLease |
| 160 | mut: |
| 161 | frame &CustomDatumFrame = unsafe { nil } |
| 162 | encoded &CustomEncodedState = unsafe { nil } |
| 163 | characters int |
| 164 | line_done bool |
| 165 | line_cr bool |
| 166 | snapshot string |
| 167 | revision u64 |
| 168 | first_byte int |
| 169 | start int |
| 170 | offset int |
| 171 | bytes []u8 |
| 172 | chunks &CustomInputChunk = unsafe { nil } |
| 173 | pending Value |
| 174 | request_start int |
| 175 | request_count int |
| 176 | wind &WindFrame = unsafe { nil } |
| 177 | position_cursor voidptr |
| 178 | } |
| 179 | |
| 180 | fn custom_port_operation(value Value) &CustomPortOperation { |
| 181 | return unsafe { &CustomPortOperation(value.pointer()) } |
| 182 | } |
| 183 | |
| 184 | fn (mut m Machine) enter_custom_port_operation() ! { |
| 185 | mut state := custom_port_operation(m.env.slots[0]) |
| 186 | state.lease.enter(m.context)! |
| 187 | marker := payload_value(.port_operation, state.lease) |
| 188 | state.wind = &WindFrame{ |
| 189 | parent: m.wind |
| 190 | depth: if isnil(m.wind) { 1 } else { m.wind.depth + 1 } |
| 191 | before: marker |
| 192 | after: marker |
| 193 | } |
| 194 | m.wind = state.wind |
| 195 | } |
| 196 | |
| 197 | fn (mut m Machine) finish_custom_port_operation(value Value) { |
| 198 | state := custom_port_operation(m.env.slots[0]) |
| 199 | state.lease.release(m.context) |
| 200 | m.wind = state.wind.parent |
| 201 | m.finish_call(value) |
| 202 | } |
| 203 | |
| 204 | fn (mut m Machine) step_custom_port_operation(mut vm VM) ! { |
| 205 | if m.step_custom_binary_duplex(mut vm)! { return } |
| 206 | state := custom_port_operation(m.env.slots[0]) |
| 207 | if positioned_custom_attached(state.resource) { |
| 208 | if m.step_custom_attached_position(mut vm)! { return } |
| 209 | } |
| 210 | // Keep the resumable text-position driver cold for ordinary custom I/O. |
| 211 | // Clang leaves this larger helper out of line in the production binary. |
| 212 | if positioned_custom_text(state.resource) { |
| 213 | if m.step_custom_text_duplex(mut vm)! { return } |
| 214 | } |
| 215 | if state.target.tag == .input_port && state.operation in [.close_input, .close_any] |
| 216 | && state.resource.closed && !isnil(state.resource.output_adapter) { |
| 217 | // A failed TX half-close can leave an encoded suffix. Final RX close |
| 218 | // drains it on this same machine before claiming the shared closer. |
| 219 | m.step_custom_encoded_output(mut vm)! |
| 220 | return |
| 221 | } |
| 222 | if custom_port_operation(m.env.slots[0]).target.tag == .input_port { |
| 223 | if !custom_port_operation(m.env.slots[0]).target.input_pointer().binary { |
| 224 | m.step_custom_text_input(mut vm)! |
| 225 | return |
| 226 | } |
| 227 | m.step_custom_input_operation(mut vm)! |
| 228 | return |
| 229 | } |
| 230 | if !state.target.output_pointer().binary { |
| 231 | m.step_custom_text_output(mut vm)! |
| 232 | return |
| 233 | } |
| 234 | match state.operation { |
| 235 | .write_u8, .put_u8, .write_bytes, .put_bytes { |
| 236 | if state.offset == state.end { |
| 237 | m.finish_custom_port_operation(unspecified()) |
| 238 | return |
| 239 | } |
| 240 | begin_custom_binary_callback(state, true) |
| 241 | m.push(state.resource.write) |
| 242 | m.push(state.source) |
| 243 | m.push(integer(state.offset)) |
| 244 | m.push(integer(state.end - state.offset)) |
| 245 | m.apply(mut vm, 3, false)! |
| 246 | } |
| 247 | .close_output, .close_any, .close_input { |
| 248 | if !claim_custom_combined_close(state.lease) { |
| 249 | m.finish_custom_port_operation(unspecified()) |
| 250 | return |
| 251 | } |
| 252 | m.push(state.resource.close) |
| 253 | m.apply(mut vm, 0, false)! |
| 254 | } |
| 255 | .get_position { |
| 256 | begin_custom_binary_callback(state, false) |
| 257 | m.push(state.resource.get_position) |
| 258 | m.apply(mut vm, 0, false)! |
| 259 | } |
| 260 | .flush_output { m.finish_custom_port_operation(unspecified()) } |
| 261 | .set_position { |
| 262 | begin_custom_binary_callback(state, true) |
| 263 | m.push(state.resource.set_position) |
| 264 | m.push(state.position) |
| 265 | m.apply(mut vm, 1, false)! |
| 266 | } |
| 267 | else { return failure('invalid output port operation') } |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | fn (mut m Machine) collect_custom_port_operation() ! { |
| 272 | result := m.pop_value() |
| 273 | initial_state := custom_port_operation(m.env.slots[0]) |
| 274 | if positioned_custom_attached(initial_state.resource) { |
| 275 | if m.collect_custom_attached_position(result)! { return } |
| 276 | } |
| 277 | if m.collect_custom_binary_duplex(result)! { return } |
| 278 | if m.collect_custom_text_duplex(result)! { return } |
| 279 | input_state := custom_port_operation(m.env.slots[0]) |
| 280 | if input_state.target.tag == .input_port && !isnil(input_state.encoded) { |
| 281 | m.collect_custom_encoded_output(result)! |
| 282 | return |
| 283 | } |
| 284 | if custom_port_operation(m.env.slots[0]).target.tag == .input_port { |
| 285 | if !custom_port_operation(m.env.slots[0]).target.input_pointer().binary { |
| 286 | m.collect_custom_text_input(result)! |
| 287 | return |
| 288 | } |
| 289 | m.collect_custom_input_operation(result)! |
| 290 | return |
| 291 | } |
| 292 | mut state := custom_port_operation(m.env.slots[0]) |
| 293 | if !state.target.output_pointer().binary { |
| 294 | m.collect_custom_text_output(result)! |
| 295 | return |
| 296 | } |
| 297 | match state.operation { |
| 298 | .write_u8, .put_u8, .write_bytes, .put_bytes { |
| 299 | count := custom_port_single_result(result)!.as_integer()! |
| 300 | if count < 0 || count > state.end - state.offset { |
| 301 | return failure('custom writer returned an invalid count') |
| 302 | } |
| 303 | if m.env.escaped { state = m.detach_custom_port_state() } |
| 304 | // Zero is permitted; retry on the same machine, so instruction and |
| 305 | // cancellation budgets still bound a callback that makes no progress. |
| 306 | state.offset += int(count) |
| 307 | } |
| 308 | .get_position { |
| 309 | value := custom_port_single_result(result)! |
| 310 | check_custom_binary_position(value)! |
| 311 | m.finish_custom_port_operation(value) |
| 312 | } |
| 313 | else { m.finish_custom_port_operation(unspecified()) } |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | // Explicit V entry is a host boundary. Scheme primitives take the hybrid branch |
| 318 | // in Machine.apply, preserving their existing Scheme continuation instead. |
| 319 | fn (mut vm VM) custom_output_host_operation(operation PortIoOperation, target Value, source Value, start int, end int, position Value) !Value { |
| 320 | if target.tag == .input_output_port { |
| 321 | return vm.custom_output_host_operation(operation, custom_combined_facet(operation, target), source, start, end, position) |
| 322 | } |
| 323 | resource := custom_output_resource(target) |
| 324 | if !isnil(resource.output_adapter) && operation in [.get_position, .set_position] { |
| 325 | return failure('transcoded output port does not support positions') |
| 326 | } |
| 327 | if operation in [.write_u8, .put_u8, .write_bytes, .put_bytes] && start == end { |
| 328 | return unspecified() |
| 329 | } |
| 330 | if operation == .get_position && !resource.get_position.is_callable() { |
| 331 | return failure('output port does not support positions') |
| 332 | } |
| 333 | if operation == .set_position { |
| 334 | if target.output_pointer().binary { |
| 335 | check_custom_binary_position(position)! |
| 336 | } else { |
| 337 | checked_custom_text_position(target, position)! |
| 338 | } |
| 339 | if !resource.set_position.is_callable() { |
| 340 | return failure('output port does not support setting positions') |
| 341 | } |
| 342 | } |
| 343 | state := &CustomPortOperation{ |
| 344 | operation: operation |
| 345 | target: target |
| 346 | resource: resource |
| 347 | source: source |
| 348 | offset: start |
| 349 | end: end |
| 350 | position: position |
| 351 | lease: &CustomPortLease{ |
| 352 | port: target |
| 353 | resource: resource |
| 354 | input_cursor: operation !in [.get_position, .flush_output, .close_any, .close_output] |
| 355 | closing: operation in [ |
| 356 | .close_output, |
| 357 | .close_any, |
| 358 | ] |
| 359 | } |
| 360 | } |
| 361 | return vm.call(procedure_value(&Procedure{ mode: .closure, name: 'custom port operation', owner: vm.owner, function: &custom_port_io_bridges[int(operation)] }), [payload_value(.port_operation, state)]) |
| 362 | } |
| 363 | |
| 364 | fn (mut m Machine) tail_custom_port_close(mut vm VM, target Value) ! { |
| 365 | state := vm.prepare_custom_port_operation(.close_any, target, [target])! |
| 366 | m.push(procedure_value(&Procedure{ mode: .closure, name: 'close-port', owner: vm.owner, function: &custom_port_io_bridges[int(PortIoOperation.close_any)] })) |
| 367 | m.push(payload_value(.port_operation, state)) |
| 368 | m.apply(mut vm, 1, true)! |
| 369 | } |
| 370 | |
| 371 | fn custom_port_single_result(value Value) !Value { |
| 372 | if value.tag == .multiple { return failure('custom port callback must return one value') } |
| 373 | return value |
| 374 | } |
| 375 | |
| 376 | // Enter this private one-argument bridge directly. Its public procedure is |
| 377 | // already owned/checked by apply; allocating a replacement closure is needless. |
| 378 | // Ordinary Scheme closure application keeps its existing hot path. |
| 379 | fn (mut m Machine) call_custom_port_bridge(mut vm VM, function &Function, state &CustomPortOperation, call_base int, tail bool) ! { |
| 380 | if !tail && vm.options.max_frames > 0 && m.frames.len >= vm.options.max_frames { |
| 381 | return failure('continuation frame limit exceeded') |
| 382 | } |
| 383 | if !tail { m.reserve_frame_slot(vm)! } |
| 384 | m.enter_bridge(vm, tail)! |
| 385 | mut scope := m.checked_activation_for(vm, function, unsafe { nil }, tail)! |
| 386 | scope.slots[0] = payload_value(.port_operation, state) |
| 387 | if !tail { |
| 388 | m.frames << CallFrame{ function: m.function, env: m.env, ip: m.ip, base: m.base } |
| 389 | m.base = call_base |
| 390 | } |
| 391 | m.truncate(if tail { m.base } else { call_base }) |
| 392 | m.function = function |
| 393 | m.env = scope |
| 394 | m.ip = 0 |
| 395 | } |
| 396 | |