vile / scheme / port_control.v
256 lines · 233 sloc · 8.72 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3import reader
4
5enum FileMode as u8 {
6 call_input
7 call_output
8 with_input
9 with_output
10 call_port
11 collect_bytes
12 collect_text
13}
14
15// Instruction-only bridges are independent of the allocation scope in which a
16// file controller first runs. Each invocation still owns its extent and closure.
17const file_enter_bridge_code = [
18 Instruction{ op: .file_enter, origin: reader.no_node }
19]!
20
21const file_enter_bridge = Function{
22 code: unsafe { nil }
23 capture_depth: 1
24 instructions: unsafe { static_bridge_instructions(&file_enter_bridge_code[0], file_enter_bridge_code.len) }
25}
26
27const file_leave_bridge_code = [
28 Instruction{ op: .file_leave, origin: reader.no_node }
29]!
30
31const file_leave_bridge = Function{
32 code: unsafe { nil }
33 capture_depth: 1
34 instructions: unsafe { static_bridge_instructions(&file_leave_bridge_code[0], file_leave_bridge_code.len) }
35}
36
37const file_call_bridge_code = [
38 Instruction{ op: .local, a: 1, b: 1, origin: reader.no_node }
39 Instruction{ op: .file_argument, origin: reader.no_node }
40 Instruction{ op: .call, a: 1, origin: reader.no_node }
41 Instruction{ op: .file_return, origin: reader.no_node }
42]!
43
44const file_call_bridge = Function{
45 code: unsafe { nil }
46 capture_depth: 1
47 instructions: unsafe { static_bridge_instructions(&file_call_bridge_code[0], file_call_bridge_code.len) }
48}
49
50const file_input_bridge_code = [
51 Instruction{ op: .file_parameterize, origin: reader.no_node }
52 Instruction{ op: .file_return, a: 1, origin: reader.no_node }
53]!
54
55const file_input_bridge = Function{
56 code: unsafe { nil }
57 capture_depth: 1
58 slot_count: 4
59 instructions: unsafe { static_bridge_instructions(&file_input_bridge_code[0], file_input_bridge_code.len) }
60}
61
62const file_output_bridge_code = [
63 Instruction{ op: .file_parameterize, a: 1, origin: reader.no_node }
64 Instruction{ op: .file_return, a: 1, origin: reader.no_node }
65]!
66
67const file_output_bridge = Function{
68 code: unsafe { nil }
69 capture_depth: 1
70 slot_count: 4
71 instructions: unsafe { static_bridge_instructions(&file_output_bridge_code[0], file_output_bridge_code.len) }
72}
73
74@[heap]
75struct FileExtent {
76 path string
77 mode FileMode
78 transcoder Value
79mut:
80 file Value
81 memory Value
82 returned bool
83 binding &PortBinding = unsafe { nil }
84}
85
86// Host entries can nest through native callbacks. Each actual wind entry owns
87// its physical file context. Current-port selection uses ordinary parameter
88// bindings; it is never saved in mutable file-controller activations.
89@[heap]
90struct PortBinding {
91 previous &PortBinding = unsafe { nil }
92 previous_extent &PortBinding = unsafe { nil }
93 extent &FileExtent
94 context u64
95}
96
97fn (mut vm VM) install_file_controls() {
98 for mode, name in ['call-with-input-file', 'call-with-output-file', 'with-input-from-file',
99 'with-output-to-file', 'call-with-port'] {
100 function := &Function{
101 code: unsafe { nil }
102 params: 2
103 slot_count: 5
104 instructions: [bridge_instruction(.file_init, mode, 0),
105 bridge_instruction(.wind_init, 0, 0), bridge_instruction(.wind_enter, 0, 0),
106 bridge_instruction(.wind_leave, 0, 0), bridge_instruction(.wind_finish, 0, 0)]
107 }
108 vm.bind_global(name, procedure_value(&Procedure{ mode: .closure, name: name, owner: vm.owner, function: function }))
109 }
110}
111
112// File controllers are cold; keep their allocation/setup out of instruction dispatch.
113@[noinline]
114fn (mut m Machine) init_file_control(mut vm VM, mode FileMode) ! {
115 collect := mode in [.collect_bytes, .collect_text]
116 path := if mode == .call_port || collect { '' } else { m.env.slots[0].as_string()! }
117 if mode == .call_port {
118 if m.env.slots[0].tag == .input_port {
119 vm.input_owner(m.env.slots[0])!
120 } else {
121 vm.output_owner(m.env.slots[0])!
122 }
123 }
124 callback := m.env.slots[if collect { 0 } else { 1 }]
125 if !callback.is_callable() { return failure('file callback must be a procedure') }
126 vm.check_callable_owner(callback)!
127 if path.contains('\x00') { return failure('file name contains NUL') }
128 mut options := boolean(false)
129 if mode == .collect_bytes {
130 rest := m.env.slots[1]
131 count := rest.list_length()!
132 check_arity('call-with-bytevector-output-port', count + 1, 1, 2)!
133 if count == 1 { options = rest.pair_pointer().car }
134 checked_optional_transcoder(options)!
135 }
136 extent := &FileExtent{
137 path: path
138 mode: mode
139 transcoder: options
140 file: if mode == .call_port {
141 m.env.slots[0]
142 } else {
143 unspecified()
144 }
145 }
146 scope := &Environment{ slots: [payload_value(.file_extent, extent), callback], escaped: true }
147 body := if mode in [.call_input, .call_output, .call_port, .collect_bytes, .collect_text] {
148 &file_call_bridge
149 } else if mode == .with_input {
150 &file_input_bridge
151 } else {
152 &file_output_bridge
153 }
154 for index, function in [&file_enter_bridge, body, &file_leave_bridge] {
155 m.env.slots[index] = procedure_value(&Procedure{ mode: .closure, owner: vm.owner, function: function, env: scope })
156 }
157}
158
159fn (m &Machine) file_extent() &FileExtent {
160 return unsafe { &FileExtent(m.env.parent.slots[0].pointer()) }
161}
162
163// File controllers are cold; keep their allocation/setup out of instruction dispatch.
164@[noinline]
165fn (mut m Machine) enter_file_control(mut vm VM) ! {
166 mut extent := m.file_extent()
167 // Open only on the first actual entry. Reentry through a saved continuation
168 // refers to the same port. call-with-file keeps it open on escape; a
169 // prior real callback return or the optional with-file policy may close it.
170 if extent.file.tag == .unspecified {
171 if extent.mode in [.collect_bytes, .collect_text] {
172 extent.memory = vm.memory_output_port(extent.mode == .collect_bytes)!
173 extent.file = if extent.mode == .collect_bytes {
174 maybe_transcode(mut vm, extent.memory, extent.transcoder)!
175 } else {
176 extent.memory
177 }
178 } else {
179 extent.file = if extent.mode in [.call_input, .with_input] {
180 vm.open_input_file(extent.path)!
181 } else {
182 vm.open_output_file(extent.path)!
183 }
184 }
185 }
186 extent.returned = false
187 mut state := vm.port_state()
188 binding := &PortBinding{ previous: state.bindings, previous_extent: extent.binding, extent: extent, context: m.context }
189 extent.binding = binding
190 state.bindings = binding
191 m.finish_call(unspecified())
192}
193
194fn (mut vm VM) leave_file_binding(binding &PortBinding, close_file bool) ! {
195 mut state := vm.port_state()
196 mut extent := binding.extent
197 state.bindings = binding.previous
198 extent.binding = binding.previous_extent
199 if !close_file { return }
200 vm.close_port(extent.file)!
201}
202
203// File controllers are cold; keep their allocation/setup out of instruction dispatch.
204@[noinline]
205fn (mut m Machine) leave_file_control(mut vm VM) ! {
206 binding := m.file_extent().binding
207 // This is private bridge state, inaccessible to Scheme code. Every entered
208 // extent leaves in stack order, including wind transfers between entries.
209 if isnil(binding) || isnil(vm.ports) || voidptr(vm.ports.bindings) != voidptr(binding) || binding.context != m.context {
210 return failure('invalid file extent state')
211 }
212 close_file := binding.extent.returned || binding.extent.mode in [.with_input, .with_output]
213 if close_file && !isnil(custom_port_resource(binding.extent.file)) {
214 vm.leave_file_binding(binding, false)!
215 m.tail_custom_port_close(mut vm, binding.extent.file)!
216 return
217 }
218 vm.leave_file_binding(binding, close_file)!
219 m.finish_call(unspecified())
220}
221
222fn (mut vm VM) cleanup_file_context(context u64) {
223 if isnil(vm.ports) { return }
224 for !isnil(vm.ports.bindings) && vm.ports.bindings.context == context {
225 // Ordinary errors do not run Scheme after callbacks. File controllers
226 // still restore host state and close descriptors at the entry boundary.
227 // Preserve the original failure if cleanup encounters a second I/O error.
228 // An abandoned Scheme callback has no native stack to resume. Match
229 // nonreturning call-with-port: restore the binding without invoking close.
230 binding := vm.ports.bindings
231 vm.leave_file_binding(binding, isnil(custom_port_resource(binding.extent.file))) or {}
232 }
233}
234
235@[noinline]
236fn (mut m Machine) return_file_body(mut vm VM, parameterized bool) ! {
237 mut extent := m.file_extent()
238 extent.returned = true
239 if extent.mode in [.collect_bytes, .collect_text] {
240 m.pop_value()
241 m.finish_call(vm.memory_output_result(extent.memory, extent.file, false)!)
242 return
243 }
244 if parameterized { m.finish_parameter_bindings(1) } else { m.finish_call(m.pop_value()) }
245}
246
247// Put with-file selection in the ordinary parameter wind chain. Captured
248// continuations retain the same mutable location, and reentry restores it using
249// the existing parameter-context wind markers rather than VM-global saves.
250@[direct_array_access]
251fn (mut m Machine) parameterize_file_body(mut vm VM, index int) ! {
252 m.env.slots[0] = vm.port_parameters[index]
253 m.env.slots[1] = m.file_extent().file
254 m.env.slots[2] = m.env.parent.slots[1]
255 m.install_parameter_bindings(mut vm, 1)!
256}
257