| 1 | module scheme |
| 2 | |
| 3 | import reader |
| 4 | |
| 5 | @[heap] |
| 6 | struct Continuation { |
| 7 | owner &Owner = unsafe { nil } |
| 8 | context u64 |
| 9 | wind &WindFrame = unsafe { nil } |
| 10 | function &Function = unsafe { nil } |
| 11 | env &Environment = unsafe { nil } |
| 12 | ip int |
| 13 | base int |
| 14 | origin reader.NodeId = reader.no_node |
| 15 | stack []Value |
| 16 | frames []CallFrame |
| 17 | bridge_sites []BridgeSite |
| 18 | parameters &ParameterBinding = unsafe { nil } |
| 19 | parameter_base &ParameterBinding = unsafe { nil } |
| 20 | close_failure &CustomCloseFailure = unsafe { nil } |
| 21 | } |
| 22 | |
| 23 | // This signal unwinds reentrant V callbacks to the active Scheme invocation |
| 24 | // that owns the continuation. Ordinary Scheme jumps restore directly instead. |
| 25 | struct ContinuationTransfer { |
| 26 | Error |
| 27 | continuation &Continuation |
| 28 | result Value |
| 29 | } |
| 30 | |
| 31 | fn (e &ContinuationTransfer) msg() string { |
| 32 | return 'Scheme continuation transfer' |
| 33 | } |
| 34 | |
| 35 | // is_control_transfer lets native callbacks preserve nonlocal Scheme/host control |
| 36 | // when handling errors themselves. Propagating eval/call with ! does this too. |
| 37 | pub fn is_control_transfer(err IError) bool { |
| 38 | return err is ContinuationTransfer || err is ExitRequest || err is CancellationRequest || err is ArenaUsageLimitRequest || err is ResourceLimitRequest |
| 39 | } |
| 40 | |
| 41 | fn (mut vm VM) next_context() u64 { |
| 42 | vm.serial++ |
| 43 | return vm.serial |
| 44 | } |
| 45 | |
| 46 | fn (mut vm VM) leave_context() { |
| 47 | index := vm.active.len - 1 |
| 48 | // Runtime failure abandons this entry's dynamic bindings without running |
| 49 | // Scheme winders. Host flush callbacks during descriptor cleanup must see |
| 50 | // the enclosing entry's parameter base, not the abandoned inner extent. |
| 51 | vm.active[index].parameters = vm.active[index].parameter_base |
| 52 | vm.active[index].cleanup_goops_migrations() |
| 53 | vm.active[index].cleanup_custom_port_leases() |
| 54 | vm.active[index].cleanup_library_context() |
| 55 | vm.cleanup_file_context(vm.active[index].context) |
| 56 | vm.active[index].cleanup_goops_method_cache() |
| 57 | vm.active[index].cleanup_goops_applicability_cache() |
| 58 | vm.pop_active_context() |
| 59 | } |
| 60 | |
| 61 | @[inline] |
| 62 | fn (mut vm VM) pop_active_context() { |
| 63 | index := vm.active.len - 1 |
| 64 | container_store_validated(mut vm.active, index, unsafe { &Machine(nil) }) |
| 65 | unsafe { vm.active.len = index } |
| 66 | } |
| 67 | |
| 68 | fn (vm &VM) continuation_target(context u64) u64 { |
| 69 | for i := vm.active.len - 1; i >= 0; i-- { |
| 70 | machine := vm.active[i] |
| 71 | if machine.context == context || machine.logical_context == context { |
| 72 | return machine.context |
| 73 | } |
| 74 | } |
| 75 | return 0 |
| 76 | } |
| 77 | |
| 78 | fn escape_environment(env &Environment) { |
| 79 | mut cursor := EnvironmentRef{env} |
| 80 | for !isnil(cursor.value) { |
| 81 | mut current := cursor.value |
| 82 | if current.escaped { break } |
| 83 | current.escaped = true |
| 84 | cursor = EnvironmentRef{current.parent} |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | fn (m &Machine) capture(owner &Owner, call_base int) &Continuation { |
| 89 | // Every live activation must stay out of the return cache and tail-reuse |
| 90 | // path, including callers that never created a lexical closure themselves. |
| 91 | escape_environment(m.env) |
| 92 | for frame in m.frames { escape_environment(frame.env) } |
| 93 | return &Continuation{ |
| 94 | owner: owner |
| 95 | context: m.logical_context |
| 96 | wind: m.wind |
| 97 | parameters: m.parameters |
| 98 | parameter_base: m.parameter_base |
| 99 | close_failure: m.close_failure |
| 100 | function: m.function |
| 101 | env: m.env |
| 102 | ip: m.ip |
| 103 | base: m.base |
| 104 | origin: m.origin |
| 105 | stack: copy_values(m.stack, 0, call_base) |
| 106 | frames: m.frames.clone() |
| 107 | bridge_sites: m.bridge_sites.clone() |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | fn (mut m Machine) transfer(mut vm VM, continuation &Continuation, result Value) !bool { |
| 112 | target := vm.continuation_target(continuation.context) |
| 113 | destination := if target != 0 && target != m.context { |
| 114 | unsafe { &WindFrame(nil) } |
| 115 | } else { |
| 116 | continuation.wind |
| 117 | } |
| 118 | if !same_wind(m.wind, destination) { |
| 119 | m.start_wind_transfer(mut vm, continuation, result, destination)! |
| 120 | return true |
| 121 | } |
| 122 | if target != 0 && target != m.context { return false } |
| 123 | m.reserve_restore_buffers(vm, continuation)! |
| 124 | m.restore(continuation, result) |
| 125 | return true |
| 126 | } |
| 127 | |
| 128 | fn (mut m Machine) accept_transfer(mut vm VM, err IError) !bool { |
| 129 | if err is ContinuationTransfer { |
| 130 | if voidptr(err.continuation.owner) != voidptr(vm.owner) { |
| 131 | return failure('continuation belongs to another VM') |
| 132 | } |
| 133 | return m.transfer(mut vm, err.continuation, err.result) |
| 134 | } |
| 135 | return false |
| 136 | } |
| 137 | |
| 138 | fn (mut m Machine) reserve_restore_buffers(vm &VM, continuation &Continuation) ! { |
| 139 | m.reserve_buffer(vm, .stack, continuation.stack.len + 1)! |
| 140 | m.reserve_buffer(vm, .frames, continuation.frames.len)! |
| 141 | m.reserve_buffer(vm, .bridge_sites, continuation.bridge_sites.len)! |
| 142 | } |
| 143 | |
| 144 | @[direct_array_access] |
| 145 | fn (mut m Machine) restore(continuation &Continuation, result Value) { |
| 146 | // Snapshots own separate immutable buffers. Restore into this invocation's |
| 147 | // private buffers so multi-shot jumps can reuse capacity without mutating |
| 148 | // the snapshot or allocating a fresh stack on every invocation. |
| 149 | m.truncate(0) |
| 150 | m.stack << continuation.stack |
| 151 | m.push(result) |
| 152 | for i in 0 .. m.frames.len { container_store_validated(mut m.frames, i, CallFrame{}) } |
| 153 | unsafe { m.frames.len = 0 } |
| 154 | m.frames << continuation.frames |
| 155 | m.release_bridge_sites(-1) |
| 156 | m.bridge_sites << continuation.bridge_sites |
| 157 | m.function = continuation.function |
| 158 | m.env = continuation.env |
| 159 | m.ip = continuation.ip |
| 160 | m.base = continuation.base |
| 161 | m.origin = continuation.origin |
| 162 | m.logical_context = continuation.context |
| 163 | m.wind = continuation.wind |
| 164 | m.parameters = continuation.parameters |
| 165 | m.parameter_base = continuation.parameter_base |
| 166 | m.close_failure = continuation.close_failure |
| 167 | m.finished = isnil(continuation.function) |
| 168 | // Preserve this invocation's step count; jumping cannot reset its budget. |
| 169 | } |
| 170 | |