vile / scheme / port_memory_extensions.v
173 lines · 153 sloc · 6.53 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3import reader
4
5// Captured port state belongs to the individual extractor. The instruction-only
6// bridge is shared, so first use in a borrowed arena publishes no cached owner.
7const memory_extract_bridge_code = [
8 Instruction{ op: .local, a: 1, b: 2, origin: reader.no_node }
9 Instruction{ op: .local, a: 1, b: 0, origin: reader.no_node }
10 Instruction{ op: .local, a: 1, b: 1, origin: reader.no_node }
11 Instruction{ op: .tail_call, a: 2, origin: reader.no_node }
12]!
13
14const memory_extract_bridge = Function{
15 code: unsafe { nil }
16 capture_depth: 1
17 instructions: unsafe { static_bridge_instructions(&memory_extract_bridge_code[0], memory_extract_bridge_code.len) }
18}
19
20const memory_collect_bytes_bridge_code = [
21 Instruction{ op: .file_init, a: i32(FileMode.collect_bytes), origin: reader.no_node }
22 Instruction{ op: .wind_init, origin: reader.no_node }
23 Instruction{ op: .wind_enter, origin: reader.no_node }
24 Instruction{ op: .wind_leave, origin: reader.no_node }
25 Instruction{ op: .wind_finish, origin: reader.no_node }
26]!
27
28const memory_collect_bytes_bridge = Function{
29 code: unsafe { nil }
30 params: 1
31 rest: true
32 slot_count: 5
33 instructions: unsafe { static_bridge_instructions(&memory_collect_bytes_bridge_code[0], memory_collect_bytes_bridge_code.len) }
34}
35
36const memory_collect_text_bridge_code = [
37 Instruction{ op: .file_init, a: i32(FileMode.collect_text), origin: reader.no_node }
38 Instruction{ op: .wind_init, origin: reader.no_node }
39 Instruction{ op: .wind_enter, origin: reader.no_node }
40 Instruction{ op: .wind_leave, origin: reader.no_node }
41 Instruction{ op: .wind_finish, origin: reader.no_node }
42]!
43
44const memory_collect_text_bridge = Function{
45 code: unsafe { nil }
46 params: 1
47 slot_count: 5
48 instructions: unsafe { static_bridge_instructions(&memory_collect_text_bridge_code[0], memory_collect_text_bridge_code.len) }
49}
50
51const memory_port_descriptors = [
52 NativeDescriptor{'open-bytevector-input-port', memory_open_bytes_input}
53 NativeDescriptor{'open-string-input-port', memory_open_text_input}
54 NativeDescriptor{'open-bytevector-output-port', memory_open_bytes_output}
55 NativeDescriptor{'open-string-output-port', memory_open_text_output}
56 NativeDescriptor{'port-eof?', memory_port_eof},
57]!
58
59const memory_port_names = 'open-bytevector-input-port open-string-input-port open-bytevector-output-port open-string-output-port port-eof? call-with-bytevector-output-port call-with-string-output-port'
60
61fn (mut vm VM) install_memory_port_extensions() {
62 for i in 0 .. memory_port_descriptors.len {
63 entry := &memory_port_descriptors[i]
64 vm.bind_global(entry.name, vm.port_native_procedure(entry))
65 }
66 for mode, name in ['call-with-bytevector-output-port', 'call-with-string-output-port'] {
67 function := if mode == 0 {
68 &memory_collect_bytes_bridge
69 } else {
70 &memory_collect_text_bridge
71 }
72 vm.bind_global(name, procedure_value(&Procedure{ mode: .closure, owner: vm.owner, name: name, function: function }))
73 }
74}
75
76fn checked_optional_transcoder(value Value) ! {
77 if value.tag == .boolean && !value.is_true() { return }
78 value.as_transcoder()!
79}
80
81fn maybe_transcode(mut vm VM, binary Value, options Value) !Value {
82 return if options.tag == .boolean { binary } else { vm.transcoded_port(binary, options)! }
83}
84
85fn memory_open_bytes_input(mut vm VM, args []Value) !Value {
86 check_arity('open-bytevector-input-port', args.len, 1, 2)!
87 options := if args.len == 2 { args[1] } else { boolean(false) }
88 checked_optional_transcoder(options)!
89 return maybe_transcode(mut vm, vm.open_input_bytevector(args[0])!, options)
90}
91
92fn memory_open_text_input(mut vm VM, args []Value) !Value {
93 check_arity('open-string-input-port', args.len, 1, 1)!
94 return vm.open_input_string(args[0].as_string()!)
95}
96
97fn memory_open_bytes_output(mut vm VM, args []Value) !Value {
98 check_arity('open-bytevector-output-port', args.len, 0, 1)!
99 options := if args.len == 1 { args[0] } else { boolean(false) }
100 checked_optional_transcoder(options)!
101 return vm.memory_output_pair(true, options)
102}
103
104fn memory_open_text_output(mut vm VM, args []Value) !Value {
105 check_arity('open-string-output-port', args.len, 0, 0)!
106 return vm.memory_output_pair(false, boolean(false))
107}
108
109fn (mut vm VM) memory_output_pair(binary bool, options Value) !Value {
110 raw := vm.memory_output_port(binary)!
111 active := if binary { maybe_transcode(mut vm, raw, options)! } else { raw }
112 extract := procedure_value(&Procedure{
113 mode: .closure
114 owner: vm.owner
115 name: 'output-port-extractor'
116 function: &memory_extract_bridge
117 env: &Environment{
118 escaped: true
119 slots: [raw, active,
120 procedure_value(&Procedure{ mode: .native, owner: vm.owner, native: memory_extract })]
121 }
122 })
123 mut result := value_storage(2)
124 container_store_validated(mut result, 0, active)
125 container_store_validated(mut result, 1, extract)
126 return payload_value(.multiple, &Vector{ items: unsafe { result } })
127}
128
129fn memory_extract(mut vm VM, args []Value) !Value {
130 // Only the private bridge supplies these two captured arguments.
131 return vm.memory_output_result(args[0], args[1], true)
132}
133
134fn (mut vm VM) memory_output_result(raw Value, active Value, clear bool) !Value {
135 mut port := vm.output_owner(raw)!
136 target := vm.output_owner(active)!
137 if port.busy || target.busy { return failure('output port operation is already active') }
138 if !target.closed { vm.flush_output_port(active)! }
139 if !clear {
140 return if port.binary { vm.get_output_bytevector(raw)! } else { vm.get_output_string(raw)! }
141 }
142 mut memory := port.memory_output
143 // Extraction consumes the buffer. Move its storage into the published value
144 // instead of copying bytes that the port is about to discard. A later write
145 // starts with fresh storage and cannot mutate an earlier extraction.
146 value := if port.binary {
147 owned_bytevector(unsafe { memory.bytes })
148 } else if memory.bytes.len == 0 {
149 owned_string('', 0)
150 } else {
151 length := memory.bytes.len
152 // At exact capacity, array growth would double the allocation just for
153 // the terminator. Copy to one exactly sized string only in that case.
154 text := if length == memory.bytes.cap {
155 memory.bytes.bytestr()
156 } else {
157 unsafe { (&u8(memory.bytes.data))[length] = 0 }
158 unsafe { tos(memory.bytes.data, length) }
159 }
160 owned_string(text, unicode_length(text))
161 }
162 memory.bytes = []u8{}
163 unsafe { memory.bytes.flags.set(.noslices) }
164 memory.position = -1
165 return value
166}
167
168fn memory_port_eof(mut vm VM, args []Value) !Value {
169 check_arity('port-eof?', args.len, 1, 1)!
170 port := vm.checked_input(args[0])!
171 value := if port.binary { vm.peek_u8(args[0])! } else { vm.peek_char(args[0])! }
172 return boolean(value.tag == .eof)
173}
174