vile / scheme / promise.v
139 lines · 131 sloc · 4.74 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3@[heap]
4struct Promise {
5 owner &Owner = unsafe { nil }
6mut:
7 ready bool
8 lazy bool
9 forwarded bool
10 rank u8
11 function &Function = unsafe { nil }
12 env &Environment = unsafe { nil }
13 result Value
14}
15
16fn promise_pointer(value Value) &Promise {
17 return unsafe { &Promise(value.pointer()) }
18}
19
20// Forwarding keeps every retained alias attached to the same memoized state.
21// Rank bounds even adversarial join trees; compression is iterative and needs
22// neither a temporary path array nor another allocation per promise.
23fn promise_root(value Value) &Promise {
24 mut root := promise_pointer(value)
25 for root.forwarded { root = promise_pointer(root.result) }
26 mut current := promise_pointer(value)
27 for current.forwarded {
28 next := promise_pointer(current.result)
29 current.result = payload_value(.promise, root)
30 current = next
31 }
32 return root
33}
34
35fn (mut promise Promise) discard_thunk() {
36 promise.function = unsafe { nil }
37 promise.env = unsafe { nil }
38}
39
40fn join_promises(mut left Promise, mut right Promise) {
41 if voidptr(&left) == voidptr(&right) { return }
42 if left.rank < right.rank {
43 left.forwarded = true
44 left.discard_thunk()
45 left.result = payload_value(.promise, &right)
46 return
47 }
48 // The target's state wins, irrespective of which root wins the union.
49 left.ready = right.ready
50 left.lazy = right.lazy
51 left.function = right.function
52 left.env = right.env
53 left.result = right.result
54 if left.rank == right.rank { left.rank++ }
55 right.forwarded = true
56 right.discard_thunk()
57 right.result = payload_value(.promise, &left)
58}
59
60fn (mut vm VM) install_promise_primitive() {
61 function := &Function{
62 code: unsafe { nil }
63 params: 1
64 slot_count: 1
65 instructions: [bridge_instruction(.force_enter, 0, 0), bridge_instruction(.force_finish, 0, 0),
66 bridge_instruction(.force_finish, 1, 0), bridge_instruction(.ret, 0, 0)]
67 }
68 vm.bind_global('force', procedure_value(&Procedure{ mode: .closure, name: 'force', owner: vm.owner, function: function }))
69 vm.bind_global('promise?', procedure_value(&Procedure{ mode: .primitive, name: 'promise?', owner: vm.owner, primitive: .promisep }))
70 ready := &Function{
71 code: unsafe { nil }
72 params: 1
73 slot_count: 1
74 instructions: [bridge_instruction(.make_promise, 0, 0), bridge_instruction(.ret, 0, 0)]
75 }
76 vm.bind_global('make-promise', procedure_value(&Procedure{ mode: .closure, name: 'make-promise', owner: vm.owner, function: ready }))
77}
78
79// Both entries run only on the private one-argument force bridge. Application
80// checks its arity before activation, so slot zero is a trusted VM index.
81@[direct_array_access]
82fn (mut m Machine) enter_force(mut vm VM) ! {
83 value := m.env.slots[0]
84 if value.tag != .promise { return failure('force needs a promise') }
85 if voidptr(promise_pointer(value).owner) != voidptr(vm.owner) {
86 return failure('promise belongs to another VM')
87 }
88 promise := promise_root(value)
89 if promise.ready {
90 m.finish_call(promise.result)
91 return
92 }
93 m.charge(vm.options.max_steps)!
94 if vm.options.max_frames > 0 && m.frames.len >= vm.options.max_frames {
95 return failure('continuation frame limit exceeded')
96 }
97 // A delay body is a compiler-owned zero-argument function. Enter it without
98 // constructing a temporary Scheme procedure, including for never-forced
99 // promises; no procedure object is needed at either stage.
100 function := promise.function
101 m.reserve_frame_slot(vm)!
102 scope := m.checked_activation_for(vm, function, promise.env, false)!
103 // The return instruction records this thunk's kind in immutable bytecode.
104 // A saved continuation can return after the shared state has advanced to a
105 // different thunk, so its finish action must not read that mutable kind.
106 m.frames << CallFrame{ function: m.function, env: m.env, ip: if promise.lazy { 2 } else { 1 }, base: m.base }
107 m.base = m.stack.len
108 m.function = function
109 m.env = scope
110 m.ip = 0
111}
112
113@[direct_array_access]
114fn (mut m Machine) finish_force(mut vm VM, lazy bool) ! {
115 result := m.pop_value()
116 mut promise := promise_root(m.env.slots[0])
117 // A recursive force may have memoized the first answer while this body was
118 // still running. Its answer wins, including after continuation resumption.
119 if !promise.ready {
120 if lazy {
121 if result.tag != .promise {
122 return failure('delay-force expression must return a promise')
123 }
124 if voidptr(promise_pointer(result).owner) != voidptr(vm.owner) {
125 return failure('promise belongs to another VM')
126 }
127 mut target := promise_root(result)
128 join_promises(mut promise, mut target)
129 // The thunk has returned to this bridge. Reuse the bridge and its
130 // original argument rather than retaining a pending force per link.
131 m.enter_force(mut vm)!
132 return
133 }
134 promise.result = result
135 promise.ready = true
136 promise.discard_thunk()
137 }
138 m.finish_call(promise.result)
139}
140