vile / scheme / port_custom_text_input.v
357 lines · 343 sloc · 12.3 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3import encoding.utf8
4
5fn (mut vm VM) prepare_custom_text_input(operation PortIoOperation, target Value, args []Value) !&CustomPortOperation {
6 port := if custom_input_closing(operation) {
7 vm.input_owner(target)!
8 } else {
9 vm.checked_text_input(target)!
10 }
11 if port.busy { return failure('input port operation is already active') }
12 mut source := unspecified()
13 mut start := 0
14 mut end := 0
15 mut position := unspecified()
16 match operation {
17 .read_char, .peek_char, .get_char, .lookahead_char, .input_eof, .get_position,
18 .close_input, .close_any, .read_datum, .get_datum {
19 }
20 .read_text, .get_text {
21 count := args[if operation == .read_text { 0 } else { 1 }].as_integer()!
22 if count < 0 || count > max_int {
23 return failure('string read count out of supported range')
24 }
25 end = int(count)
26 }
27 .text_all, .read_line, .get_line { end = max_int }
28 .text_into {
29 source = args[1]
30 source.require_mutable(.string)!
31 data := source.string_pointer()
32 first, last := port_get_put_range(args, data.length)!
33 start = first
34 end = last - first
35 }
36 .set_position { position = args[1] }
37 else { return failure('expected a binary input port') }
38 }
39 return new_custom_text_input_operation(operation, target, source, start, end, position)
40}
41
42fn new_custom_text_input_operation(operation PortIoOperation, target Value, source Value, start int, count int, position Value) !&CustomPortOperation {
43 mut state := new_custom_input_operation(operation, target, source, 0, count, position)!
44 state.start = start
45
46 if operation in [.read_datum, .get_datum] { state.frame = &CustomDatumFrame{} }
47 if operation == .text_into {
48 data := source.string_pointer()
49 state.snapshot = data.value
50 state.revision = data.revision
51 state.first_byte = text_offset(data, start)!
52 }
53 return state
54}
55
56fn (mut vm VM) custom_text_input_host_operation(operation PortIoOperation, target Value, source Value, start int, count int, position Value) !Value {
57 state := new_custom_text_input_operation(operation, custom_combined_facet(operation, target), source, start, count, position)!
58 return vm.call(procedure_value(&Procedure{ mode: .closure, name: 'custom text input', owner: vm.owner, function: &custom_port_io_bridges[int(operation)] }), [payload_value(.port_operation, state)])
59}
60
61fn (state &CustomPortOperation) copy_text_bytes(mut destination []u8, start int) {
62 if state.bytes.len > 0 {
63 unsafe { vmemcpy(&u8(destination.data) + start + state.offset - state.bytes.len, state.bytes.data, state.bytes.len) }
64 }
65 mut cursor := state.chunks
66 for !isnil(cursor) {
67 unsafe { vmemcpy(&u8(destination.data) + start + cursor.offset, cursor.bytes.data, cursor.bytes.len) }
68 cursor = cursor.previous
69 }
70}
71
72fn (state &CustomPortOperation) input_text_result() Value {
73 if state.characters == 0 && !state.line_done && state.end != 0 { return eof_value() }
74 if state.offset == 0 { return owned_string('', 0) }
75 if isnil(state.chunks) && state.bytes.cap > state.bytes.len {
76 unsafe { (&u8(state.bytes.data))[state.bytes.len] = 0 }
77 return owned_string(unsafe { tos(state.bytes.data, state.bytes.len) }, state.characters)
78 }
79 mut bytes := []u8{len: state.offset + 1}
80 state.copy_text_bytes(mut bytes, 0)
81 return owned_string(unsafe { tos(bytes.data, state.offset) }, state.characters)
82}
83
84fn (state &CustomPortOperation) finish_text_into() !Value {
85 if state.characters == 0 { return if state.end == 0 { integer(0) } else { eof_value() } }
86 mut data := state.source.string_pointer()
87 if data.revision != state.revision { return failure('string target changed during input') }
88 mut suffix := state.first_byte
89 for _ in 0 .. state.characters {
90 _, width := utf8.decode_rune_at(state.snapshot, suffix)
91 suffix += width
92 }
93 remaining := state.snapshot.len - suffix
94 if state.offset > max_int - 1 - state.first_byte - remaining {
95 return failure('string length exceeds supported range')
96 }
97 length := state.first_byte + state.offset + remaining
98 mut bytes := []u8{len: length + 1}
99 unsafe {
100 if state.first_byte > 0 { vmemcpy(bytes.data, state.snapshot.str, state.first_byte) }
101 if remaining > 0 {
102 vmemcpy(&u8(bytes.data) + state.first_byte + state.offset, state.snapshot.str + suffix, remaining)
103 }
104 }
105 state.copy_text_bytes(mut bytes, state.first_byte)
106 data.value = unsafe { tos(bytes.data, length) }
107 data.revision++
108 return integer(state.characters)
109}
110
111fn (mut m Machine) request_custom_text_input(mut vm VM) ! {
112 mut state := custom_port_operation(m.env.slots[0])
113 // A bounded speculative prefix can precede the current live callback view.
114 // Moving to its tail needs no Scheme call and must skip the collect opcode.
115 refresh_custom_text_view(state.target)!
116 mut port := state.target.input_pointer()
117 if port.position < port.buffer.len {
118 m.ip = 1
119 return
120 }
121 // Checkpoint consumed bytes before the callback can overwrite its buffer.
122 commit_custom_text_cursor(state.target)
123 port.buffer = []u8{}
124 port.position = 0
125 mut resource := state.resource
126 resource.text_view = unspecified()
127 if !isnil(resource.input_adapter) {
128 m.request_custom_decoded_input(mut vm)!
129 return
130 }
131 if positioned_custom_text(resource) {
132 m.request_positioned_text_input(mut vm)!
133 return
134 }
135 if resource.read_buffer.tag != .string || (state.operation == .text_into && resource.read_buffer.pointer() == state.source.pointer()) {
136 resource.read_buffer = owned_string('\x00'.repeat(port.read_size), port.read_size)
137 }
138 state.pending = resource.read_buffer
139 state.request_start = 0
140 state.request_count = state.pending.string_pointer().length
141 m.push(state.resource.read)
142 m.push(state.pending)
143 m.push(integer(0))
144 m.push(integer(state.request_count))
145 m.apply(mut vm, 3, false)!
146}
147
148fn (mut m Machine) finish_custom_text_read() ! {
149 state := custom_port_operation(m.env.slots[0])
150 result := if state.operation == .text_into {
151 state.finish_text_into()!
152 } else {
153 state.input_text_result()
154 }
155 m.finish_custom_port_operation(result)
156}
157
158fn (mut m Machine) step_custom_text_input(mut vm VM) ! {
159 mut state := custom_port_operation(m.env.slots[0])
160 refresh_custom_text_view(state.target)!
161 if state.operation in [.read_datum, .get_datum] {
162 m.step_custom_datum_input(mut vm) or { return custom_datum_operation_error(state.operation, err) }
163 return
164 }
165 mut port := state.target.input_pointer()
166 match state.operation {
167 .get_position {
168 m.push(state.resource.get_position)
169 m.apply(mut vm, 0, false)!
170 return
171 }
172 .set_position {
173 m.push(state.resource.set_position)
174 m.push(m.custom_text_set_argument()!)
175 m.apply(mut vm, 1, false)!
176 return
177 }
178 .close_input, .close_any {
179 if !claim_custom_combined_close(state.lease) {
180 m.finish_custom_port_operation(unspecified())
181 return
182 }
183 m.push(state.resource.close)
184 m.apply(mut vm, 0, false)!
185 return
186 }
187 else {}
188 }
189 if state.operation in [.read_char, .get_char, .peek_char, .lookahead_char, .input_eof] {
190 if port.position < port.buffer.len {
191 ch, width := port.peek_character()!
192 if state.operation in [.read_char, .get_char] { port.position += width }
193 m.finish_custom_port_operation(if state.operation == .input_eof {
194 boolean(false)
195 } else {
196 character(ch)!
197 })
198 } else if port.ended {
199 observe_custom_attached_eof(state.target)
200 m.finish_custom_port_operation(if state.operation == .input_eof {
201 boolean(true)
202 } else {
203 eof_value()
204 })
205 } else {
206 m.request_custom_text_input(mut vm)!
207 }
208 return
209 }
210 if state.line_cr {
211 if port.position == port.buffer.len && !port.ended {
212 m.request_custom_text_input(mut vm)!
213 return
214 }
215 if port.position < port.buffer.len {
216 ch, width := port.peek_character()!
217 if ch in [`\n`, rune(0x85)] { port.position += width }
218 }
219 if port.ended && port.position == port.buffer.len {
220 observe_custom_attached_eof(state.target)
221 }
222 state.line_cr = false
223 m.finish_custom_text_read()!
224 return
225 }
226 start := port.position
227 mut last := start
228 for port.position < port.buffer.len && state.characters < state.end {
229 ch, width := port.peek_character()!
230 if state.operation in [.read_line, .get_line] && (ch == `\n` || (state.operation == .read_line && ch in [
231 `\r`,
232 rune(0x85),
233 rune(0x2028),
234 ])) {
235 state.line_done = true
236 state.line_cr = ch == `\r`
237 port.position += width
238 break
239 }
240 port.position += width
241 last = port.position
242 state.characters++
243 }
244 if last > start {
245 length := last - start
246 if length > max_int - 1 - state.offset {
247 return failure('string length exceeds supported range')
248 }
249 state.bytes << unsafe { port.buffer[start..last] }
250 state.offset += length
251 }
252 if state.line_cr {
253 if port.position == port.buffer.len && !port.ended {
254 m.request_custom_text_input(mut vm)!
255 return
256 }
257 if port.position < port.buffer.len {
258 ch, width := port.peek_character()!
259 if ch in [`\n`, rune(0x85)] { port.position += width }
260 }
261 if port.ended && port.position == port.buffer.len {
262 observe_custom_attached_eof(state.target)
263 }
264 state.line_cr = false
265 }
266 if state.line_done || state.characters == state.end || port.ended {
267 if port.ended && port.position == port.buffer.len && state.characters < state.end && !state.line_done {
268 observe_custom_attached_eof(state.target)
269 }
270 m.finish_custom_text_read()!
271 } else {
272 m.request_custom_text_input(mut vm)!
273 }
274}
275
276fn (mut m Machine) collect_custom_text_input(result Value) ! {
277 state := custom_port_operation(m.env.slots[0])
278 match state.operation {
279 .get_position {
280 m.finish_custom_port_operation(custom_text_position(state.target, custom_port_single_result(result)!)!)
281 }
282 .set_position {
283 m.restore_custom_text_position()!
284 m.finish_custom_port_operation(unspecified())
285 }
286 .close_input, .close_any { m.finish_custom_port_operation(unspecified()) }
287 else {
288 if !isnil(state.resource.input_adapter) {
289 m.collect_custom_decoded_input(result)!
290 return
291 }
292 count := custom_port_single_result(result)!.as_integer()!
293 if count < 0 || count > state.request_count {
294 return failure('custom textual reader returned an invalid count')
295 }
296 mut current := m.detach_custom_port_state()
297 data := current.pending.string_pointer()
298 end := text_offset(data, count)!
299 mut port := current.target.input_pointer()
300 mut resource := current.resource
301 // A resumed callback publishes a new physical read-ahead view;
302 // replay metadata from an intervening operation belongs to the old one.
303 resource.replay_active = false
304 resource.replay_tail = []u8{}
305 resource.text_view = current.pending
306 resource.view_start = 0
307 resource.view_count = int(count)
308 resource.view_revision = data.revision
309 port.buffer = unsafe { data.value.str.vbytes(end) }
310 port.position = 0
311 port.ended = count == 0
312 publish_custom_text_origin(current)
313 current.pending = unspecified()
314 }
315 }
316}
317
318// Consume by adjusting the byte view, without moving or copying callback data.
319// Freeze diagnostic history before caller code may mutate an already-read prefix.
320fn commit_custom_text_cursor(value Value) {
321 mut port := value.input_pointer()
322 if port.position == 0 { return }
323 mut resource := custom_input_resource(value)
324 if !resource.replay_active && resource.text_view.tag == .string {
325 resource.view_start += unicode_length(unsafe { tos(port.buffer.data, port.position) })
326 }
327 trim_custom_attached_coordinates(resource, port.position, resource.replay_active)
328 trim_custom_text_coordinates(resource, unsafe { port.buffer[..port.position] }, resource.replay_active)
329 port.checkpoint_input()
330 port.buffer = unsafe { port.buffer[port.position..] }
331 port.position = 0
332}
333
334// A retained Scheme string may replace its UTF8 allocation. Keep the live input
335// cursor in characters and reacquire the new header only when revision changes.
336// Explicit position checkpoints use detached unread bytes instead of this view.
337fn refresh_custom_text_view(value Value) ! {
338 mut resource := custom_input_resource(value)
339 mut port := value.input_pointer()
340 mut restore := false
341 if resource.replay_active {
342 if port.position < port.buffer.len { return }
343 commit_custom_text_cursor(value)
344 resource.replay_active = false
345 port.buffer = unsafe { resource.replay_tail }
346 resource.replay_tail = []u8{}
347 restore = true
348 }
349 if resource.text_view.tag != .string { return }
350 data := resource.text_view.string_pointer()
351 if !restore && data.revision == resource.view_revision { return }
352 commit_custom_text_cursor(value)
353 first := text_offset(data, resource.view_start)!
354 last := text_offset(data, resource.view_count)!
355 port.buffer = unsafe { (data.value.str + first).vbytes(last - first) }
356 resource.view_revision = data.revision
357}
358