| 1 | module scheme |
| 2 | |
| 3 | import encoding.utf8 |
| 4 | |
| 5 | // Optional host capabilities use underlying byte positions. A Reader's position |
| 6 | // includes bytes returned to Vile's read-ahead buffer; Vile subtracts unread bytes. |
| 7 | // Methods may reenter the VM but must not operate on the same busy port. |
| 8 | pub interface PortPositionGetter { |
| 9 | mut: |
| 10 | port_position() !i64 |
| 11 | } |
| 12 | |
| 13 | pub interface PortPositionSetter { |
| 14 | mut: |
| 15 | set_port_position(i64) ! |
| 16 | } |
| 17 | |
| 18 | // Textual positions retain the port whose coordinate they describe. Input |
| 19 | // positions also retain reader directives and absolute diagnostic coordinates. |
| 20 | struct PortPosition { |
| 21 | port Value |
| 22 | offset i64 |
| 23 | point InputLocation |
| 24 | no_fold bool |
| 25 | } |
| 26 | |
| 27 | const position_port_names = 'port-has-port-position? port-position port-has-set-port-position!? set-port-position!' |
| 28 | |
| 29 | const position_port_descriptors = [ |
| 30 | NativeDescriptor{'port-has-port-position?', position_has_getter} |
| 31 | NativeDescriptor{'port-position', position_get} |
| 32 | NativeDescriptor{'port-has-set-port-position!?', position_has_setter} |
| 33 | NativeDescriptor{'set-port-position!', position_set}, |
| 34 | ]! |
| 35 | |
| 36 | fn (mut vm VM) install_position_ports() { |
| 37 | for index in 0 .. position_port_descriptors.len { |
| 38 | entry := &position_port_descriptors[index] |
| 39 | vm.bind_global(entry.name, vm.port_native_procedure(entry)) |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | fn position_has_getter(mut vm VM, args []Value) !Value { |
| 44 | check_arity('port-has-port-position?', args.len, 1, 1)! |
| 45 | return boolean(vm.port_has_position(args[0])!) |
| 46 | } |
| 47 | |
| 48 | fn position_has_setter(mut vm VM, args []Value) !Value { |
| 49 | check_arity('port-has-set-port-position!?', args.len, 1, 1)! |
| 50 | return boolean(vm.port_has_set_position(args[0])!) |
| 51 | } |
| 52 | |
| 53 | fn position_get(mut vm VM, args []Value) !Value { |
| 54 | check_arity('port-position', args.len, 1, 1)! |
| 55 | return vm.port_position(args[0]) |
| 56 | } |
| 57 | |
| 58 | fn position_set(mut vm VM, args []Value) !Value { |
| 59 | check_arity('set-port-position!', args.len, 2, 2)! |
| 60 | vm.set_port_position(args[0], args[1])! |
| 61 | return unspecified() |
| 62 | } |
| 63 | |
| 64 | fn (mut vm VM) position_capability(value Value, setter bool) !bool { |
| 65 | vm.ready()! |
| 66 | if value.tag == .input_output_port { |
| 67 | vm.input_owner(value)! |
| 68 | check_combined_busy(value)! |
| 69 | resource := custom_port_resource(value) |
| 70 | if !isnil(resource) { |
| 71 | if resource.closed || !isnil(resource.input_adapter) || !isnil(resource.output_adapter) { |
| 72 | return false |
| 73 | } |
| 74 | return (if setter { resource.set_position } else { resource.get_position }).is_callable() |
| 75 | } |
| 76 | file := combined_file_resource(value) |
| 77 | return !isnil(file) && file.seekable && file.stream.file.is_opened |
| 78 | } |
| 79 | if value.tag == .input_port { |
| 80 | mut port := vm.input_owner(value)! |
| 81 | if port.busy { return failure('input port operation is already active') } |
| 82 | if port.closed { return false } |
| 83 | resource := custom_input_resource(value) |
| 84 | if !isnil(resource) { |
| 85 | if !isnil(resource.input_adapter) { return false } |
| 86 | return (if setter { resource.set_position } else { resource.get_position }).is_callable() |
| 87 | } |
| 88 | if port.memory_input { return true } |
| 89 | if !isnil(port.stream) { |
| 90 | if !port.stream.raw { return false } |
| 91 | return file_has_position(port.stream.file.fd) |
| 92 | } |
| 93 | return if setter { |
| 94 | port.source is PortPositionSetter |
| 95 | } else { |
| 96 | port.source is PortPositionGetter |
| 97 | } |
| 98 | } |
| 99 | mut port := vm.output_owner(value)! |
| 100 | if port.busy { return failure('output port operation is already active') } |
| 101 | if port.closed { return false } |
| 102 | resource := custom_output_resource(value) |
| 103 | if !isnil(resource) { |
| 104 | if !isnil(resource.output_adapter) { return false } |
| 105 | return (if setter { resource.set_position } else { resource.get_position }).is_callable() |
| 106 | } |
| 107 | if !isnil(port.memory_output) { return true } |
| 108 | if !isnil(port.stream) { |
| 109 | if !port.stream.raw { return false } |
| 110 | return file_has_position(port.stream.file.fd) |
| 111 | } |
| 112 | return if setter { port.sink is PortPositionSetter } else { port.sink is PortPositionGetter } |
| 113 | } |
| 114 | |
| 115 | pub fn (mut vm VM) port_has_position(value Value) !bool { |
| 116 | return vm.position_capability(value, false) |
| 117 | } |
| 118 | |
| 119 | pub fn (mut vm VM) port_has_set_position(value Value) !bool { |
| 120 | return vm.position_capability(value, true) |
| 121 | } |
| 122 | |
| 123 | fn (mut port InputPort) source_position() !i64 { |
| 124 | if !isnil(port.stream) && port.stream.raw { |
| 125 | return file_port_position(port.stream.file.fd, 0, true) |
| 126 | } |
| 127 | if mut port.source is PortPositionGetter { |
| 128 | mut source := port.source |
| 129 | return source.port_position() |
| 130 | } |
| 131 | return failure('input port does not support positions') |
| 132 | } |
| 133 | |
| 134 | fn (mut port InputPort) seek_source(position i64) ! { |
| 135 | if !isnil(port.stream) && port.stream.raw { |
| 136 | file_port_position(port.stream.file.fd, position, false)! |
| 137 | return |
| 138 | } |
| 139 | if mut port.source is PortPositionSetter { |
| 140 | mut source := port.source |
| 141 | source.set_port_position(position)! |
| 142 | return |
| 143 | } |
| 144 | return failure('input port does not support setting positions') |
| 145 | } |
| 146 | |
| 147 | fn (mut port OutputPort) sink_position() !i64 { |
| 148 | if !isnil(port.stream) && port.stream.raw { |
| 149 | position := file_port_position(port.stream.file.fd, 0, true)! |
| 150 | if position > max_i64 - port.stream.pending.len { |
| 151 | return failure('port position exceeds supported range') |
| 152 | } |
| 153 | return position + port.stream.pending.len |
| 154 | } |
| 155 | if mut port.sink is PortPositionGetter { |
| 156 | mut sink := port.sink |
| 157 | return sink.port_position() |
| 158 | } |
| 159 | return failure('output port does not support positions') |
| 160 | } |
| 161 | |
| 162 | fn (mut port OutputPort) seek_sink(position i64) ! { |
| 163 | if !isnil(port.stream) && port.stream.raw { |
| 164 | file_port_position(port.stream.file.fd, position, false)! |
| 165 | return |
| 166 | } |
| 167 | if mut port.sink is PortPositionSetter { |
| 168 | mut sink := port.sink |
| 169 | sink.set_port_position(position)! |
| 170 | return |
| 171 | } |
| 172 | return failure('output port does not support setting positions') |
| 173 | } |
| 174 | |
| 175 | pub fn (mut vm VM) port_position(value Value) !Value { |
| 176 | vm.ready()! |
| 177 | if value.tag == .input_output_port { |
| 178 | vm.input_owner(value)! |
| 179 | check_combined_busy(value)! |
| 180 | if !isnil(custom_port_resource(value)) { |
| 181 | return vm.custom_combined_position_host(.get_position, value, unspecified()) |
| 182 | } |
| 183 | file := combined_file_resource(value) |
| 184 | if isnil(file) { return failure('combined port does not support positions') } |
| 185 | return integer(file.position()!) |
| 186 | } |
| 187 | if value.tag == .input_port { |
| 188 | mut port := vm.checked_input(value)! |
| 189 | if !isnil(custom_input_resource(value)) { |
| 190 | return vm.custom_input_host_operation(.get_position, value, unspecified(), 0, 0, unspecified()) |
| 191 | } |
| 192 | port.busy = true |
| 193 | defer { port.busy = false } |
| 194 | position := if port.memory_input { |
| 195 | i64(port.position) |
| 196 | } else { |
| 197 | raw := port.source_position()! |
| 198 | unread := port.buffer.len - port.position |
| 199 | if raw < unread { return failure('source position precedes buffered input') } |
| 200 | raw - unread |
| 201 | } |
| 202 | if position < 0 { return failure('input port returned a negative position') } |
| 203 | if port.binary { return integer(position) } |
| 204 | point := if port.memory_input { |
| 205 | InputLocation{} |
| 206 | } else { |
| 207 | port.input_location(port.position) |
| 208 | } |
| 209 | return payload_value(.port_position, &PortPosition{ port: value, offset: position, point: point, no_fold: port.no_fold }) |
| 210 | } |
| 211 | mut port := vm.checked_output(value)! |
| 212 | if !isnil(custom_output_resource(value)) { |
| 213 | return vm.custom_output_host_operation(.get_position, value, unspecified(), 0, 0, unspecified()) |
| 214 | } |
| 215 | port.busy = true |
| 216 | defer { port.busy = false } |
| 217 | if !isnil(port.memory_output) { |
| 218 | sink := port.memory_output |
| 219 | cursor := if sink.position < 0 { sink.bytes.len } else { sink.position } |
| 220 | if port.binary { return integer(cursor) } |
| 221 | characters := if cursor == 0 { |
| 222 | 0 |
| 223 | } else { |
| 224 | unicode_length(unsafe { tos(&u8(sink.bytes.data), cursor) }) |
| 225 | } |
| 226 | return payload_value(.port_position, &PortPosition{ port: value, offset: characters }) |
| 227 | } |
| 228 | position := port.sink_position()! |
| 229 | if position < 0 { return failure('output port returned a negative position') } |
| 230 | if port.binary { return integer(position) } |
| 231 | return payload_value(.port_position, &PortPosition{ port: value, offset: position }) |
| 232 | } |
| 233 | |
| 234 | fn position_offset(port Value, position Value, binary bool) !(i64, &PortPosition) { |
| 235 | if binary { |
| 236 | offset := position.as_integer()! |
| 237 | if offset < 0 { return failure('port position must be nonnegative') } |
| 238 | return offset, unsafe { nil } |
| 239 | } |
| 240 | if position.tag != .port_position { return failure('expected a textual port position') } |
| 241 | token := unsafe { &PortPosition(position.pointer()) } |
| 242 | if token.port.tag != port.tag || token.port.pointer() != port.pointer() { |
| 243 | return failure('position belongs to another port') |
| 244 | } |
| 245 | return token.offset, token |
| 246 | } |
| 247 | |
| 248 | pub fn (mut vm VM) set_port_position(value Value, position Value) ! { |
| 249 | vm.ready()! |
| 250 | if value.tag == .input_output_port { |
| 251 | vm.input_owner(value)! |
| 252 | check_combined_busy(value)! |
| 253 | if !isnil(custom_port_resource(value)) { |
| 254 | vm.custom_combined_position_host(.set_position, value, position)! |
| 255 | return |
| 256 | } |
| 257 | mut file := combined_file_resource(value) |
| 258 | if isnil(file) { return failure('combined port does not support setting positions') } |
| 259 | offset, _ := position_offset(value, position, true)! |
| 260 | file.seek(offset)! |
| 261 | return |
| 262 | } |
| 263 | if value.tag == .input_port { |
| 264 | mut port := vm.checked_input(value)! |
| 265 | if !isnil(custom_input_resource(value)) { |
| 266 | vm.custom_input_host_operation(.set_position, value, unspecified(), 0, 0, position)! |
| 267 | return |
| 268 | } |
| 269 | offset, token := position_offset(value, position, port.binary)! |
| 270 | if port.memory_input && offset > port.buffer.len { |
| 271 | return failure('input position out of bounds') |
| 272 | } |
| 273 | port.busy = true |
| 274 | defer { port.busy = false } |
| 275 | if port.memory_input { |
| 276 | port.position = int(offset) |
| 277 | port.location = unsafe { nil } |
| 278 | } else { |
| 279 | port.seek_source(offset)! |
| 280 | unsafe { port.buffer.len = 0 } |
| 281 | port.position = 0 |
| 282 | port.mark = -1 |
| 283 | port.ended = false |
| 284 | if !port.binary { |
| 285 | if isnil(port.location) { port.location = &InputLocation{} } |
| 286 | unsafe { *port.location = token.point } |
| 287 | } |
| 288 | } |
| 289 | if !port.binary { port.no_fold = token.no_fold } |
| 290 | return |
| 291 | } |
| 292 | mut port := vm.checked_output(value)! |
| 293 | if !isnil(custom_output_resource(value)) { |
| 294 | vm.custom_output_host_operation(.set_position, value, unspecified(), 0, 0, position)! |
| 295 | return |
| 296 | } |
| 297 | offset, token := position_offset(value, position, port.binary)! |
| 298 | mut memory_offset := 0 |
| 299 | if !isnil(port.memory_output) { |
| 300 | if port.binary { |
| 301 | if offset > max_int - 1 { return failure('output position exceeds supported range') } |
| 302 | memory_offset = int(offset) |
| 303 | } else { |
| 304 | memory_offset = memory_character_offset(port.memory_output.bytes, offset)! |
| 305 | } |
| 306 | } |
| 307 | vm.flush_output_port(value)! |
| 308 | port.busy = true |
| 309 | defer { port.busy = false } |
| 310 | if !isnil(port.memory_output) { |
| 311 | port.memory_output.position = if memory_offset == port.memory_output.bytes.len { |
| 312 | -1 |
| 313 | } else { |
| 314 | memory_offset |
| 315 | } |
| 316 | } else { |
| 317 | port.seek_sink(offset)! |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | fn memory_character_offset(bytes []u8, count i64) !int { |
| 322 | if count < 0 || count > bytes.len { return failure('textual position out of bounds') } |
| 323 | if count == 0 { return 0 } |
| 324 | text := unsafe { tos(&u8(bytes.data), bytes.len) } |
| 325 | mut offset := 0 |
| 326 | for _ in 0 .. int(count) { |
| 327 | if offset == text.len { return failure('textual position out of bounds') } |
| 328 | _, width := utf8.decode_rune_at(text, offset) |
| 329 | offset += width |
| 330 | } |
| 331 | return offset |
| 332 | } |
| 333 | |
| 334 | // Snapshots returned by get-output-* never alias this private backing array. |
| 335 | // Only this less-common overwrite path scans characters or moves a suffix. |
| 336 | fn (mut sink MemoryPortOutput) write_positioned(bytes []u8) !int { |
| 337 | if bytes.len == 0 { return 0 } |
| 338 | if sink.binary { |
| 339 | if bytes.len > max_int - 1 - sink.position { |
| 340 | return failure('memory port exceeds supported length') |
| 341 | } |
| 342 | end := sink.position + bytes.len |
| 343 | if end > sink.bytes.len { |
| 344 | old_length := sink.bytes.len |
| 345 | sink.bytes.ensure_cap(end) |
| 346 | unsafe { |
| 347 | sink.bytes.len = end |
| 348 | if sink.position > old_length { |
| 349 | vmemset(&u8(sink.bytes.data) + old_length, 0, sink.position - old_length) |
| 350 | } |
| 351 | } |
| 352 | } |
| 353 | unsafe { vmemmove(&u8(sink.bytes.data) + sink.position, bytes.data, bytes.len) } |
| 354 | sink.position = if end == sink.bytes.len { -1 } else { end } |
| 355 | return bytes.len |
| 356 | } |
| 357 | added := unsafe { tos(&u8(bytes.data), bytes.len) } |
| 358 | current := if sink.bytes.len == 0 { |
| 359 | '' |
| 360 | } else { |
| 361 | unsafe { tos(&u8(sink.bytes.data), sink.bytes.len) } |
| 362 | } |
| 363 | mut suffix := sink.position |
| 364 | for _ in 0 .. unicode_length(added) { |
| 365 | if suffix == current.len { break } |
| 366 | _, width := utf8.decode_rune_at(current, suffix) |
| 367 | suffix += width |
| 368 | } |
| 369 | remaining := sink.bytes.len - suffix |
| 370 | if bytes.len > max_int - 1 - sink.position - remaining { |
| 371 | return failure('memory port exceeds supported length') |
| 372 | } |
| 373 | length := sink.position + bytes.len + remaining |
| 374 | sink.bytes.ensure_cap(length) |
| 375 | unsafe { |
| 376 | vmemmove(&u8(sink.bytes.data) + sink.position + bytes.len, &u8(sink.bytes.data) + suffix, remaining) |
| 377 | vmemmove(&u8(sink.bytes.data) + sink.position, bytes.data, bytes.len) |
| 378 | sink.bytes.len = length |
| 379 | } |
| 380 | sink.position += bytes.len |
| 381 | if sink.position == sink.bytes.len { sink.position = -1 } |
| 382 | return bytes.len |
| 383 | } |
| 384 | |