| 1 | module scheme |
| 2 | |
| 3 | import reader |
| 4 | |
| 5 | // Closing a transferred resource owns its final release even if flushing |
| 6 | // fails. Only the error path allocates this queue; ordinary dispatch is unchanged. |
| 7 | // No nested VM.call is needed to execute a Scheme closer. |
| 8 | @[heap] |
| 9 | struct CustomCloseFailure { |
| 10 | original IError |
| 11 | leases []&CustomPortLease |
| 12 | exit_recovery &Continuation = unsafe { nil } |
| 13 | mut: |
| 14 | index int |
| 15 | in_flight bool |
| 16 | } |
| 17 | |
| 18 | const custom_close_failure_bridge_code = [ |
| 19 | Instruction{ op: .port_error_close_step, origin: reader.no_node } |
| 20 | Instruction{ op: .port_error_close_collect, origin: reader.no_node } |
| 21 | Instruction{ op: .jump, a: 0, origin: reader.no_node } |
| 22 | ]! |
| 23 | |
| 24 | const custom_close_failure_bridge = Function{ |
| 25 | code: unsafe { nil } |
| 26 | params: 1 |
| 27 | slot_count: 1 |
| 28 | instructions: unsafe { static_bridge_instructions(&custom_close_failure_bridge_code[0], custom_close_failure_bridge_code.len) } |
| 29 | } |
| 30 | |
| 31 | fn (mut m Machine) begin_custom_close_failure(owner &Owner, err IError) bool { |
| 32 | mut leases := []&CustomPortLease{} |
| 33 | mut cursor := WindRef{m.wind} |
| 34 | for !isnil(cursor.value) { |
| 35 | frame := cursor.value |
| 36 | if frame.after.tag == .port_operation { |
| 37 | lease := custom_port_lease(frame.after) |
| 38 | resource := lease.resource |
| 39 | if lease.closing && !lease.close_attempted && !isnil(resource.output_adapter) |
| 40 | && (isnil(resource.duplex) || resource.closed) |
| 41 | && resource.close.is_callable() && voidptr(resource.active) == voidptr(lease) |
| 42 | && resource.active_context == m.context { |
| 43 | leases << lease |
| 44 | } |
| 45 | } |
| 46 | cursor = WindRef{frame.parent} |
| 47 | } |
| 48 | if leases.len == 0 { return false } |
| 49 | original := m.located(err) |
| 50 | // The failure bridge abandons call frames. Preserve the committed exit's |
| 51 | // collector before doing so, including when its optional closer captures. |
| 52 | state := &CustomCloseFailure{ original: original, leases: leases, exit_recovery: m.process_exit_cleanup_recovery(owner) } |
| 53 | // Failed code cannot run again by falling through from cleanup. Captured |
| 54 | // activations already own their snapshots and remain independently usable. |
| 55 | m.truncate(0) |
| 56 | for i in 0 .. m.frames.len { container_store_validated(mut m.frames, i, CallFrame{}) } |
| 57 | unsafe { m.frames.len = 0 } |
| 58 | m.release_bridge_sites(-1) |
| 59 | m.env = &Environment{ slots: [payload_value(.port_operation, state)] } |
| 60 | m.function = &custom_close_failure_bridge |
| 61 | m.ip = 0 |
| 62 | m.base = 0 |
| 63 | m.origin = reader.no_node |
| 64 | m.finished = false |
| 65 | m.parameters = m.parameter_base |
| 66 | m.close_failure = state |
| 67 | return true |
| 68 | } |
| 69 | |
| 70 | fn (mut m Machine) step_custom_close_failure(mut vm VM) ! { |
| 71 | mut state := unsafe { &CustomCloseFailure(m.env.slots[0].pointer()) } |
| 72 | m.close_failure = state |
| 73 | for state.index < state.leases.len { |
| 74 | mut lease := state.leases[state.index] |
| 75 | if lease.close_attempted { |
| 76 | state.index++ |
| 77 | continue |
| 78 | } |
| 79 | lease.close_attempted = true |
| 80 | if !claim_custom_combined_close(lease) { |
| 81 | state.index++ |
| 82 | continue |
| 83 | } |
| 84 | state.in_flight = true |
| 85 | m.push(lease.resource.close) |
| 86 | m.apply(mut vm, 0, false)! |
| 87 | return |
| 88 | } |
| 89 | return state.original |
| 90 | } |
| 91 | |
| 92 | fn (mut m Machine) collect_custom_close_failure() { |
| 93 | m.pop_value() |
| 94 | mut state := unsafe { &CustomCloseFailure(m.env.slots[0].pointer()) } |
| 95 | if m.env.escaped { |
| 96 | mut scope := m.detach_bridge() |
| 97 | state = &CustomCloseFailure{ ...state, leases: unsafe { state.leases } } |
| 98 | scope.slots[0] = payload_value(.port_operation, state) |
| 99 | } |
| 100 | state.in_flight = false |
| 101 | state.index++ |
| 102 | m.close_failure = state |
| 103 | } |
| 104 | |