| 1 | module scheme |
| 2 | |
| 3 | import encoding.utf8 |
| 4 | import strings |
| 5 | |
| 6 | // The R6 names use port-first arguments and start/count ranges. They share the |
| 7 | // existing text/binary port implementation; none builds a second argument array. |
| 8 | const get_put_port_names = 'get-u8 lookahead-u8 get-bytevector-n get-bytevector-n! get-bytevector-some get-bytevector-all put-u8 put-bytevector get-char lookahead-char get-string-n get-string-n! get-string-all get-line get-datum put-char put-string put-datum' |
| 9 | |
| 10 | const get_put_port_descriptors = [ |
| 11 | NativeDescriptor{'get-u8', port_get_u8} |
| 12 | NativeDescriptor{'lookahead-u8', port_lookahead_u8} |
| 13 | NativeDescriptor{'get-bytevector-n', port_get_bytevector_n} |
| 14 | NativeDescriptor{'get-bytevector-n!', port_get_bytevector_into} |
| 15 | NativeDescriptor{'get-bytevector-some', port_get_bytevector_some} |
| 16 | NativeDescriptor{'get-bytevector-all', port_get_bytevector_all} |
| 17 | NativeDescriptor{'put-u8', port_put_u8} |
| 18 | NativeDescriptor{'put-bytevector', port_put_bytevector} |
| 19 | NativeDescriptor{'get-char', port_get_char} |
| 20 | NativeDescriptor{'lookahead-char', port_lookahead_char} |
| 21 | NativeDescriptor{'get-string-n', port_get_string_n} |
| 22 | NativeDescriptor{'get-string-n!', port_get_string_into} |
| 23 | NativeDescriptor{'get-string-all', port_get_string_all} |
| 24 | NativeDescriptor{'get-line', port_get_line} |
| 25 | NativeDescriptor{'get-datum', port_get_datum} |
| 26 | NativeDescriptor{'put-char', port_put_char} |
| 27 | NativeDescriptor{'put-string', port_put_string} |
| 28 | NativeDescriptor{'put-datum', port_put_datum}, |
| 29 | ]! |
| 30 | |
| 31 | fn (mut vm VM) install_get_put_ports() { |
| 32 | for index in 0 .. get_put_port_descriptors.len { |
| 33 | entry := &get_put_port_descriptors[index] |
| 34 | vm.bind_global(entry.name, vm.port_native_procedure(entry)) |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | fn port_get_put_range(args []Value, length int) !(int, int) { |
| 39 | start := if args.len > 2 { args[2].as_integer()! } else { i64(0) } |
| 40 | if start < 0 || start > length { return failure('port range start out of bounds') } |
| 41 | count := if args.len > 3 { args[3].as_integer()! } else { i64(length) - start } |
| 42 | // Subtraction also validates start+count without overflowing a signed int. |
| 43 | if count < 0 || count > i64(length) - start { return failure('port range count out of bounds') } |
| 44 | return int(start), int(start + count) |
| 45 | } |
| 46 | |
| 47 | fn port_get_u8(mut vm VM, args []Value) !Value { |
| 48 | check_arity('get-u8', args.len, 1, 1)! |
| 49 | return vm.read_u8(args[0]) |
| 50 | } |
| 51 | |
| 52 | fn port_lookahead_u8(mut vm VM, args []Value) !Value { |
| 53 | check_arity('lookahead-u8', args.len, 1, 1)! |
| 54 | return vm.peek_u8(args[0]) |
| 55 | } |
| 56 | |
| 57 | fn port_get_bytevector_n(mut vm VM, args []Value) !Value { |
| 58 | check_arity('get-bytevector-n', args.len, 2, 2)! |
| 59 | count := args[1].as_integer()! |
| 60 | if count < 0 || count > max_int { return failure('bytevector read count out of bounds') } |
| 61 | return vm.read_bytevector(args[0], int(count)) |
| 62 | } |
| 63 | |
| 64 | fn port_get_bytevector_into(mut vm VM, args []Value) !Value { |
| 65 | check_arity('get-bytevector-n!', args.len, 4, 4)! |
| 66 | data := bytevector_data(args[1])! |
| 67 | start, end := port_get_put_range(args, data.bytes.len)! |
| 68 | return vm.read_bytevector_into(args[0], args[1], start, end) |
| 69 | } |
| 70 | |
| 71 | pub fn (mut vm VM) read_bytevector_some(value Value) !Value { |
| 72 | vm.ready()! |
| 73 | mut port := vm.checked_binary_input(value)! |
| 74 | if !isnil(custom_input_resource(value)) { |
| 75 | return vm.custom_input_host_operation(.read_some, value, unspecified(), 0, max_int, unspecified()) |
| 76 | } |
| 77 | port.busy = true |
| 78 | defer { |
| 79 | port.busy = false |
| 80 | port.release_large_frame() |
| 81 | } |
| 82 | if !port.ensure_bytes(1)! { return eof_value() } |
| 83 | // Drain the available chunk. Asking for read_size bytes through read-n would |
| 84 | // wait for another callback even when some bytes have already arrived. |
| 85 | bytes := copy_byte_range(port.buffer, port.position, port.buffer.len) |
| 86 | port.position = port.buffer.len |
| 87 | return owned_bytevector(bytes) |
| 88 | } |
| 89 | |
| 90 | fn port_get_bytevector_some(mut vm VM, args []Value) !Value { |
| 91 | check_arity('get-bytevector-some', args.len, 1, 1)! |
| 92 | return vm.read_bytevector_some(args[0]) |
| 93 | } |
| 94 | |
| 95 | fn port_get_bytevector_all(mut vm VM, args []Value) !Value { |
| 96 | check_arity('get-bytevector-all', args.len, 1, 1)! |
| 97 | return vm.read_bytevector(args[0], max_int) |
| 98 | } |
| 99 | |
| 100 | fn port_put_u8(mut vm VM, args []Value) !Value { |
| 101 | check_arity('put-u8', args.len, 2, 2)! |
| 102 | byte := args[1].as_integer()! |
| 103 | if byte < 0 || byte > 255 { return failure('byte must be between 0 and 255') } |
| 104 | vm.write_u8(args[0], u8(byte))! |
| 105 | return unspecified() |
| 106 | } |
| 107 | |
| 108 | fn port_put_bytevector(mut vm VM, args []Value) !Value { |
| 109 | check_arity('put-bytevector', args.len, 2, 4)! |
| 110 | data := bytevector_data(args[1])! |
| 111 | start, end := port_get_put_range(args, data.bytes.len)! |
| 112 | vm.write_bytevector(args[0], args[1], start, end)! |
| 113 | return unspecified() |
| 114 | } |
| 115 | |
| 116 | fn port_get_char(mut vm VM, args []Value) !Value { |
| 117 | check_arity('get-char', args.len, 1, 1)! |
| 118 | return vm.read_char(args[0]) |
| 119 | } |
| 120 | |
| 121 | fn port_lookahead_char(mut vm VM, args []Value) !Value { |
| 122 | check_arity('lookahead-char', args.len, 1, 1)! |
| 123 | return vm.peek_char(args[0]) |
| 124 | } |
| 125 | |
| 126 | fn port_get_string_n(mut vm VM, args []Value) !Value { |
| 127 | check_arity('get-string-n', args.len, 2, 2)! |
| 128 | count := args[1].as_integer()! |
| 129 | if count < 0 || count > max_int { return failure('string read count out of bounds') } |
| 130 | return vm.read_string(args[0], int(count)) |
| 131 | } |
| 132 | |
| 133 | // Build the replacement straight from decoded input spans, keeping the target's |
| 134 | // unchanged prefix and suffix. No temporary Scheme string or rune array is made. |
| 135 | // The old header remains rooted across callbacks; publish the new string once. |
| 136 | pub fn (mut vm VM) read_string_into(value Value, target Value, start int, count int) !Value { |
| 137 | vm.ready()! |
| 138 | target.require_mutable(.string)! |
| 139 | mut data := target.string_pointer() |
| 140 | if start < 0 || start > data.length || count < 0 || count > data.length - start { |
| 141 | return failure('string read range out of bounds') |
| 142 | } |
| 143 | mut port := vm.checked_text_input(value)! |
| 144 | if !isnil(custom_input_resource(value)) { |
| 145 | return vm.custom_text_input_host_operation(.text_into, value, target, start, count, unspecified()) |
| 146 | } |
| 147 | if count == 0 { return integer(0) } |
| 148 | first := text_offset(data, start)! |
| 149 | snapshot := data.value |
| 150 | revision := data.revision |
| 151 | port.busy = true |
| 152 | defer { |
| 153 | port.busy = false |
| 154 | port.release_large_frame() |
| 155 | } |
| 156 | _, first_width := port.peek_character()! |
| 157 | if first_width == 0 { return eof_value() } |
| 158 | mut out := strings.new_buffer(snapshot.len) |
| 159 | defer { unsafe { out.free() } } |
| 160 | out.write_string(text_view(snapshot, 0, first)) |
| 161 | mut copied := 0 |
| 162 | for copied < count { |
| 163 | _, width := port.peek_character()! |
| 164 | if width == 0 { break } |
| 165 | if width > max_int - 1 - out.len() { |
| 166 | return failure('string length exceeds supported range') |
| 167 | } |
| 168 | unsafe { out.write_ptr(&u8(port.buffer.data) + port.position, width) } |
| 169 | port.position += width |
| 170 | copied++ |
| 171 | } |
| 172 | mut suffix := first |
| 173 | for _ in 0 .. copied { |
| 174 | _, width := utf8.decode_rune_at(snapshot, suffix) |
| 175 | suffix += width |
| 176 | } |
| 177 | if snapshot.len - suffix > max_int - 1 - out.len() { |
| 178 | return failure('string length exceeds supported range') |
| 179 | } |
| 180 | out.write_string(text_view(snapshot, suffix, snapshot.len)) |
| 181 | if data.revision != revision { return failure('string target changed during input') } |
| 182 | data.value = out.take_string() |
| 183 | data.revision++ |
| 184 | return integer(copied) |
| 185 | } |
| 186 | |
| 187 | fn port_get_string_into(mut vm VM, args []Value) !Value { |
| 188 | check_arity('get-string-n!', args.len, 4, 4)! |
| 189 | _ := string_data(args[1])! |
| 190 | data := args[1].string_pointer() |
| 191 | start, end := port_get_put_range(args, data.length)! |
| 192 | return vm.read_string_into(args[0], args[1], start, end - start) |
| 193 | } |
| 194 | |
| 195 | fn port_get_string_all(mut vm VM, args []Value) !Value { |
| 196 | check_arity('get-string-all', args.len, 1, 1)! |
| 197 | return vm.read_string(args[0], max_int) |
| 198 | } |
| 199 | |
| 200 | fn port_get_line(mut vm VM, args []Value) !Value { |
| 201 | check_arity('get-line', args.len, 1, 1)! |
| 202 | return vm.read_port_text(args[0], -2) |
| 203 | } |
| 204 | |
| 205 | fn port_get_datum(mut vm VM, args []Value) !Value { |
| 206 | check_arity('get-datum', args.len, 1, 1)! |
| 207 | return vm.read_input(args[0]) |
| 208 | } |
| 209 | |
| 210 | fn port_put_char(mut vm VM, args []Value) !Value { |
| 211 | check_arity('put-char', args.len, 2, 2)! |
| 212 | ch := args[1].as_character()! |
| 213 | mut port := vm.checked_text_output(args[0])! |
| 214 | port.write_character(u32(ch))! |
| 215 | return unspecified() |
| 216 | } |
| 217 | |
| 218 | fn port_put_string(mut vm VM, args []Value) !Value { |
| 219 | check_arity('put-string', args.len, 2, 4)! |
| 220 | _ := string_data(args[1])! |
| 221 | data := args[1].string_pointer() |
| 222 | start, end := port_get_put_range(args, data.length)! |
| 223 | first := text_offset(data, start)! |
| 224 | last := text_offset(data, end)! |
| 225 | // Keep a stable string header alive across short writes and host callbacks. |
| 226 | text := text_view(data.value, first, last) |
| 227 | mut port := vm.checked_text_output(args[0])! |
| 228 | port.write_text(text)! |
| 229 | return unspecified() |
| 230 | } |
| 231 | |
| 232 | fn port_put_datum(mut vm VM, args []Value) !Value { |
| 233 | check_arity('put-datum', args.len, 2, 2)! |
| 234 | mut port := vm.checked_text_output(args[0])! |
| 235 | port.write_text(write(args[1])!)! |
| 236 | return unspecified() |
| 237 | } |
| 238 | |