module scheme @[heap] struct Promise { owner &Owner = unsafe { nil } mut: ready bool lazy bool forwarded bool rank u8 function &Function = unsafe { nil } env &Environment = unsafe { nil } result Value } fn promise_pointer(value Value) &Promise { return unsafe { &Promise(value.pointer()) } } // Forwarding keeps every retained alias attached to the same memoized state. // Rank bounds even adversarial join trees; compression is iterative and needs // neither a temporary path array nor another allocation per promise. fn promise_root(value Value) &Promise { mut root := promise_pointer(value) for root.forwarded { root = promise_pointer(root.result) } mut current := promise_pointer(value) for current.forwarded { next := promise_pointer(current.result) current.result = payload_value(.promise, root) current = next } return root } fn (mut promise Promise) discard_thunk() { promise.function = unsafe { nil } promise.env = unsafe { nil } } fn join_promises(mut left Promise, mut right Promise) { if voidptr(&left) == voidptr(&right) { return } if left.rank < right.rank { left.forwarded = true left.discard_thunk() left.result = payload_value(.promise, &right) return } // The target's state wins, irrespective of which root wins the union. left.ready = right.ready left.lazy = right.lazy left.function = right.function left.env = right.env left.result = right.result if left.rank == right.rank { left.rank++ } right.forwarded = true right.discard_thunk() right.result = payload_value(.promise, &left) } fn (mut vm VM) install_promise_primitive() { function := &Function{ code: unsafe { nil } params: 1 slot_count: 1 instructions: [bridge_instruction(.force_enter, 0, 0), bridge_instruction(.force_finish, 0, 0), bridge_instruction(.force_finish, 1, 0), bridge_instruction(.ret, 0, 0)] } vm.bind_global('force', procedure_value(&Procedure{ mode: .closure, name: 'force', owner: vm.owner, function: function })) vm.bind_global('promise?', procedure_value(&Procedure{ mode: .primitive, name: 'promise?', owner: vm.owner, primitive: .promisep })) ready := &Function{ code: unsafe { nil } params: 1 slot_count: 1 instructions: [bridge_instruction(.make_promise, 0, 0), bridge_instruction(.ret, 0, 0)] } vm.bind_global('make-promise', procedure_value(&Procedure{ mode: .closure, name: 'make-promise', owner: vm.owner, function: ready })) } // Both entries run only on the private one-argument force bridge. Application // checks its arity before activation, so slot zero is a trusted VM index. @[direct_array_access] fn (mut m Machine) enter_force(mut vm VM) ! { value := m.env.slots[0] if value.tag != .promise { return failure('force needs a promise') } if voidptr(promise_pointer(value).owner) != voidptr(vm.owner) { return failure('promise belongs to another VM') } promise := promise_root(value) if promise.ready { m.finish_call(promise.result) return } m.charge(vm.options.max_steps)! if vm.options.max_frames > 0 && m.frames.len >= vm.options.max_frames { return failure('continuation frame limit exceeded') } // A delay body is a compiler-owned zero-argument function. Enter it without // constructing a temporary Scheme procedure, including for never-forced // promises; no procedure object is needed at either stage. function := promise.function m.reserve_frame_slot(vm)! scope := m.checked_activation_for(vm, function, promise.env, false)! // The return instruction records this thunk's kind in immutable bytecode. // A saved continuation can return after the shared state has advanced to a // different thunk, so its finish action must not read that mutable kind. m.frames << CallFrame{ function: m.function, env: m.env, ip: if promise.lazy { 2 } else { 1 }, base: m.base } m.base = m.stack.len m.function = function m.env = scope m.ip = 0 } @[direct_array_access] fn (mut m Machine) finish_force(mut vm VM, lazy bool) ! { result := m.pop_value() mut promise := promise_root(m.env.slots[0]) // A recursive force may have memoized the first answer while this body was // still running. Its answer wins, including after continuation resumption. if !promise.ready { if lazy { if result.tag != .promise { return failure('delay-force expression must return a promise') } if voidptr(promise_pointer(result).owner) != voidptr(vm.owner) { return failure('promise belongs to another VM') } mut target := promise_root(result) join_promises(mut promise, mut target) // The thunk has returned to this bridge. Reuse the bridge and its // original argument rather than retaining a pending force per link. m.enter_force(mut vm)! return } promise.result = result promise.ready = true promise.discard_thunk() } m.finish_call(promise.result) }