| 1 | module scheme |
| 2 | |
| 3 | const custom_text_input_name = 'make-custom-textual-input-port' |
| 4 | const custom_text_output_name = 'make-custom-textual-output-port' |
| 5 | |
| 6 | fn (mut vm VM) install_custom_text_constructors() { |
| 7 | vm.bind_global(custom_text_input_name, procedure_value(&Procedure{ mode: .native, name: custom_text_input_name, owner: vm.owner, native: custom_text_input })) |
| 8 | vm.bind_global(custom_text_output_name, procedure_value(&Procedure{ mode: .native, name: custom_text_output_name, owner: vm.owner, native: custom_text_output })) |
| 9 | } |
| 10 | |
| 11 | fn custom_text_input(mut vm VM, args []Value) !Value { |
| 12 | check_arity(custom_text_input_name, args.len, 5, 5)! |
| 13 | return vm.make_custom_textual_input_port(args[0].as_string()!, args[1], args[2], args[3], args[4]) |
| 14 | } |
| 15 | |
| 16 | fn custom_text_output(mut vm VM, args []Value) !Value { |
| 17 | check_arity(custom_text_output_name, args.len, 5, 5)! |
| 18 | return vm.make_custom_textual_output_port(args[0].as_string()!, args[1], args[2], args[3], args[4]) |
| 19 | } |
| 20 | |
| 21 | pub fn (mut vm VM) make_custom_textual_input_port(id string, read_callback Value, get_position Value, set_position Value, close Value, options InputOptions) !Value { |
| 22 | vm.ready()! |
| 23 | vm.check_custom_port_callback(read_callback, false)! |
| 24 | vm.check_custom_port_callback(get_position, true)! |
| 25 | vm.check_custom_port_callback(set_position, true)! |
| 26 | vm.check_custom_port_callback(close, true)! |
| 27 | if options.read_size <= 0 { return failure('input read size must be positive') } |
| 28 | resource := &CustomPortResource{ id: id, read: read_callback, get_position: get_position, set_position: set_position, close: close } |
| 29 | // For custom text sources read_size counts characters in the callback buffer. |
| 30 | return vm.input_port(&CustomPortInput{ resource: resource }, |
| 31 | read_size: options.read_size |
| 32 | fold_case: options.fold_case |
| 33 | ) |
| 34 | } |
| 35 | |
| 36 | pub fn (mut vm VM) make_custom_textual_output_port(id string, write_callback Value, get_position Value, set_position Value, close Value) !Value { |
| 37 | vm.ready()! |
| 38 | vm.check_custom_port_callback(write_callback, false)! |
| 39 | vm.check_custom_port_callback(get_position, true)! |
| 40 | vm.check_custom_port_callback(set_position, true)! |
| 41 | vm.check_custom_port_callback(close, true)! |
| 42 | resource := &CustomPortResource{ id: id, write: write_callback, get_position: get_position, set_position: set_position, close: close } |
| 43 | return vm.output_port(&CustomPortOutput{ resource: resource }) |
| 44 | } |
| 45 | |
| 46 | fn custom_input_only(operation PortIoOperation) bool { |
| 47 | return operation in [.read_u8, .peek_u8, .get_u8, .lookahead_u8, .read_bytes, .get_bytes, |
| 48 | .read_into, .get_into, .read_some, .read_all, .input_eof, .close_input, .read_char, .peek_char, |
| 49 | .get_char, .lookahead_char, .read_text, .get_text, .text_all, .read_line, .get_line, |
| 50 | .text_into, .read_datum, .get_datum] |
| 51 | } |
| 52 | |
| 53 | fn custom_output_only(operation PortIoOperation) bool { |
| 54 | return operation in [.write_u8, .put_u8, .write_bytes, .put_bytes, .close_output, .write_char, |
| 55 | .put_char, .write_text, .put_text, .display_text, .write_datum, .write_shared, .write_simple, |
| 56 | .put_datum, .newline, .flush_output] |
| 57 | } |
| 58 | |
| 59 | // Text positions are opaque, port-specific checkpoints. The source's arbitrary |
| 60 | // single Scheme value is retained unchanged and sent back only to its setter. |
| 61 | struct CustomTextPosition { |
| 62 | port Value |
| 63 | raw Value |
| 64 | unread []u8 |
| 65 | point InputLocation |
| 66 | no_fold bool |
| 67 | ended bool |
| 68 | coordinates &CustomTextSnapshot = unsafe { nil } |
| 69 | } |
| 70 | |
| 71 | fn checked_custom_text_position(port Value, value Value) !&CustomTextPosition { |
| 72 | if value.tag != .custom_port_position { |
| 73 | return failure('expected a custom textual port position') |
| 74 | } |
| 75 | token := unsafe { &CustomTextPosition(value.pointer()) } |
| 76 | identity := custom_text_identity(port) |
| 77 | if token.port.tag != identity.tag || token.port.pointer() != identity.pointer() { |
| 78 | return failure('position belongs to another port') |
| 79 | } |
| 80 | return token |
| 81 | } |
| 82 | |
| 83 | fn custom_text_position(port Value, raw Value) !Value { |
| 84 | if port.tag == .output_port { |
| 85 | return payload_value(.custom_port_position, &CustomTextPosition{ port: custom_text_identity(port), raw: raw }) |
| 86 | } |
| 87 | refresh_custom_text_view(port)! |
| 88 | input := port.input_pointer() |
| 89 | return payload_value(.custom_port_position, &CustomTextPosition{ |
| 90 | port: custom_text_identity(port) |
| 91 | raw: raw |
| 92 | unread: custom_text_unread(port)! |
| 93 | point: input.input_location(input.position) |
| 94 | no_fold: input.no_fold |
| 95 | ended: input.ended |
| 96 | coordinates: snapshot_custom_text_coordinates(port)! |
| 97 | }) |
| 98 | } |
| 99 | |
| 100 | fn (mut m Machine) custom_text_set_argument() !Value { |
| 101 | state := custom_port_operation(m.env.slots[0]) |
| 102 | return checked_custom_text_position(state.target, state.position)!.raw |
| 103 | } |
| 104 | |
| 105 | fn (mut m Machine) restore_custom_text_position() ! { |
| 106 | state := custom_port_operation(m.env.slots[0]) |
| 107 | token := checked_custom_text_position(state.target, state.position)! |
| 108 | mut port := state.target.input_pointer() |
| 109 | // A checkpoint is reusable. The live reader gets its own mutable array. |
| 110 | mut resource := state.resource |
| 111 | resource.text_view = unspecified() |
| 112 | resource.replay_active = false |
| 113 | resource.replay_tail = []u8{} |
| 114 | port.buffer = token.unread.clone() |
| 115 | port.position = 0 |
| 116 | port.mark = -1 |
| 117 | port.ended = token.ended |
| 118 | port.no_fold = token.no_fold |
| 119 | if isnil(port.location) { port.location = &InputLocation{} } |
| 120 | unsafe { *port.location = token.point } |
| 121 | restore_custom_text_coordinates(resource, token) |
| 122 | } |
| 123 | |
| 124 | // Internal immutable string views may borrow a host string or printer output. |
| 125 | // Mark them read-only before exposing them; mutable Scheme strings keep their |
| 126 | // original Value identity instead. No payload copy is needed for these views. |
| 127 | fn custom_immutable_text(text string) Value { |
| 128 | value := owned_string(text, unicode_length(text)) |
| 129 | return Value{ ...value, read_only: true } |
| 130 | } |
| 131 | |
| 132 | fn (mut vm VM) prepare_custom_text_output(operation PortIoOperation, target Value, args []Value) !&CustomPortOperation { |
| 133 | vm.checked_text_output(target)! |
| 134 | resource := custom_output_resource(target) |
| 135 | if !isnil(resource.output_adapter) && operation in [.get_position, .set_position] { |
| 136 | return failure('transcoded output port does not support positions') |
| 137 | } |
| 138 | mut source := unspecified() |
| 139 | mut start := 0 |
| 140 | mut end := 0 |
| 141 | mut position := unspecified() |
| 142 | match operation { |
| 143 | .write_char, .put_char { |
| 144 | ch := args[if operation == .write_char { 0 } else { 1 }].as_character()! |
| 145 | source = custom_immutable_text(ch.str()) |
| 146 | end = 1 |
| 147 | } |
| 148 | .write_text, .put_text { |
| 149 | source = args[if operation == .write_text { 0 } else { 1 }] |
| 150 | _ := string_data(source)! |
| 151 | start, end = if operation == .write_text { |
| 152 | sequence_range(args, 2, source.string_pointer().length)! |
| 153 | } else { |
| 154 | port_get_put_range(args, source.string_pointer().length)! |
| 155 | } |
| 156 | } |
| 157 | .display_text { |
| 158 | source = if args[0].tag == .string { |
| 159 | args[0] |
| 160 | } else { |
| 161 | custom_immutable_text(display(args[0])!) |
| 162 | } |
| 163 | end = source.string_pointer().length |
| 164 | } |
| 165 | .write_datum, .write_shared, .write_simple, .put_datum { |
| 166 | value := args[if operation == .put_datum { 1 } else { 0 }] |
| 167 | source = custom_immutable_text(match operation { |
| 168 | .write_shared { write_shared(value)! } |
| 169 | .write_simple { write_simple(value)! } |
| 170 | else { write(value)! } |
| 171 | }) |
| 172 | end = source.string_pointer().length |
| 173 | } |
| 174 | .newline { |
| 175 | source = custom_immutable_text('\n') |
| 176 | end = 1 |
| 177 | } |
| 178 | .get_position { |
| 179 | if !resource.get_position.is_callable() { |
| 180 | return failure('output port does not support positions') |
| 181 | } |
| 182 | } |
| 183 | .set_position { |
| 184 | position = args[1] |
| 185 | checked_custom_text_position(target, position)! |
| 186 | if !resource.set_position.is_callable() { |
| 187 | return failure('output port does not support setting positions') |
| 188 | } |
| 189 | } |
| 190 | .close_any, .close_output, .flush_output {} |
| 191 | else { return failure('expected a binary output port') } |
| 192 | } |
| 193 | if source.tag == .string && start == end && isnil(resource.output_adapter) { |
| 194 | return unsafe { nil } |
| 195 | } |
| 196 | return &CustomPortOperation{ |
| 197 | operation: operation |
| 198 | target: target |
| 199 | resource: resource |
| 200 | source: source |
| 201 | offset: start |
| 202 | end: end |
| 203 | position: position |
| 204 | lease: &CustomPortLease{ |
| 205 | port: target |
| 206 | resource: resource |
| 207 | input_cursor: (operation !in [.get_position, .flush_output, .close_any, .close_output] && (source.tag != .string || start < end)) || custom_attached_pending(resource) |
| 208 | closing: operation in [ |
| 209 | .close_any, |
| 210 | .close_output, |
| 211 | ] |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | fn (mut m Machine) step_custom_text_output(mut vm VM) ! { |
| 217 | state := custom_port_operation(m.env.slots[0]) |
| 218 | if !isnil(state.encoded) || !isnil(state.resource.output_adapter) { |
| 219 | m.step_custom_encoded_output(mut vm)! |
| 220 | return |
| 221 | } |
| 222 | match state.operation { |
| 223 | .get_position { |
| 224 | m.push(state.resource.get_position) |
| 225 | m.apply(mut vm, 0, false)! |
| 226 | } |
| 227 | .set_position { |
| 228 | m.push(state.resource.set_position) |
| 229 | m.push(m.custom_text_set_argument()!) |
| 230 | m.apply(mut vm, 1, false)! |
| 231 | } |
| 232 | .close_output, .close_any { |
| 233 | if !claim_custom_combined_close(state.lease) { |
| 234 | m.finish_custom_port_operation(unspecified()) |
| 235 | return |
| 236 | } |
| 237 | m.push(state.resource.close) |
| 238 | m.apply(mut vm, 0, false)! |
| 239 | } |
| 240 | .flush_output { m.finish_custom_port_operation(unspecified()) } |
| 241 | else { |
| 242 | if state.offset == state.end { |
| 243 | m.finish_custom_port_operation(unspecified()) |
| 244 | return |
| 245 | } |
| 246 | begin_positioned_text_write(state) |
| 247 | m.push(state.resource.write) |
| 248 | m.push(state.source) |
| 249 | m.push(integer(state.offset)) |
| 250 | m.push(integer(state.end - state.offset)) |
| 251 | m.apply(mut vm, 3, false)! |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | fn (mut m Machine) collect_custom_text_output(result Value) ! { |
| 257 | mut state := custom_port_operation(m.env.slots[0]) |
| 258 | if !isnil(state.encoded) { |
| 259 | m.collect_custom_encoded_output(result)! |
| 260 | return |
| 261 | } |
| 262 | match state.operation { |
| 263 | .get_position { |
| 264 | m.finish_custom_port_operation(custom_text_position(state.target, custom_port_single_result(result)!)!) |
| 265 | } |
| 266 | .set_position, .close_output, .close_any { m.finish_custom_port_operation(unspecified()) } |
| 267 | else { |
| 268 | count := custom_port_single_result(result)!.as_integer()! |
| 269 | if count < 0 || count > state.end - state.offset { |
| 270 | return failure('custom textual writer returned an invalid count') |
| 271 | } |
| 272 | if m.env.escaped { |
| 273 | mut scope := m.detach_bridge() |
| 274 | state = &CustomPortOperation{ ...state } |
| 275 | scope.slots[0] = payload_value(.port_operation, state) |
| 276 | } |
| 277 | state.offset += int(count) |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | |