module scheme const custom_text_input_name = 'make-custom-textual-input-port' const custom_text_output_name = 'make-custom-textual-output-port' fn (mut vm VM) install_custom_text_constructors() { vm.bind_global(custom_text_input_name, procedure_value(&Procedure{ mode: .native, name: custom_text_input_name, owner: vm.owner, native: custom_text_input })) vm.bind_global(custom_text_output_name, procedure_value(&Procedure{ mode: .native, name: custom_text_output_name, owner: vm.owner, native: custom_text_output })) } fn custom_text_input(mut vm VM, args []Value) !Value { check_arity(custom_text_input_name, args.len, 5, 5)! return vm.make_custom_textual_input_port(args[0].as_string()!, args[1], args[2], args[3], args[4]) } fn custom_text_output(mut vm VM, args []Value) !Value { check_arity(custom_text_output_name, args.len, 5, 5)! return vm.make_custom_textual_output_port(args[0].as_string()!, args[1], args[2], args[3], args[4]) } 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 { vm.ready()! vm.check_custom_port_callback(read_callback, false)! vm.check_custom_port_callback(get_position, true)! vm.check_custom_port_callback(set_position, true)! vm.check_custom_port_callback(close, true)! if options.read_size <= 0 { return failure('input read size must be positive') } resource := &CustomPortResource{ id: id, read: read_callback, get_position: get_position, set_position: set_position, close: close } // For custom text sources read_size counts characters in the callback buffer. return vm.input_port(&CustomPortInput{ resource: resource }, read_size: options.read_size fold_case: options.fold_case ) } pub fn (mut vm VM) make_custom_textual_output_port(id string, write_callback Value, get_position Value, set_position Value, close Value) !Value { vm.ready()! vm.check_custom_port_callback(write_callback, false)! vm.check_custom_port_callback(get_position, true)! vm.check_custom_port_callback(set_position, true)! vm.check_custom_port_callback(close, true)! resource := &CustomPortResource{ id: id, write: write_callback, get_position: get_position, set_position: set_position, close: close } return vm.output_port(&CustomPortOutput{ resource: resource }) } fn custom_input_only(operation PortIoOperation) bool { return operation in [.read_u8, .peek_u8, .get_u8, .lookahead_u8, .read_bytes, .get_bytes, .read_into, .get_into, .read_some, .read_all, .input_eof, .close_input, .read_char, .peek_char, .get_char, .lookahead_char, .read_text, .get_text, .text_all, .read_line, .get_line, .text_into, .read_datum, .get_datum] } fn custom_output_only(operation PortIoOperation) bool { return operation in [.write_u8, .put_u8, .write_bytes, .put_bytes, .close_output, .write_char, .put_char, .write_text, .put_text, .display_text, .write_datum, .write_shared, .write_simple, .put_datum, .newline, .flush_output] } // Text positions are opaque, port-specific checkpoints. The source's arbitrary // single Scheme value is retained unchanged and sent back only to its setter. struct CustomTextPosition { port Value raw Value unread []u8 point InputLocation no_fold bool ended bool coordinates &CustomTextSnapshot = unsafe { nil } } fn checked_custom_text_position(port Value, value Value) !&CustomTextPosition { if value.tag != .custom_port_position { return failure('expected a custom textual port position') } token := unsafe { &CustomTextPosition(value.pointer()) } identity := custom_text_identity(port) if token.port.tag != identity.tag || token.port.pointer() != identity.pointer() { return failure('position belongs to another port') } return token } fn custom_text_position(port Value, raw Value) !Value { if port.tag == .output_port { return payload_value(.custom_port_position, &CustomTextPosition{ port: custom_text_identity(port), raw: raw }) } refresh_custom_text_view(port)! input := port.input_pointer() return payload_value(.custom_port_position, &CustomTextPosition{ port: custom_text_identity(port) raw: raw unread: custom_text_unread(port)! point: input.input_location(input.position) no_fold: input.no_fold ended: input.ended coordinates: snapshot_custom_text_coordinates(port)! }) } fn (mut m Machine) custom_text_set_argument() !Value { state := custom_port_operation(m.env.slots[0]) return checked_custom_text_position(state.target, state.position)!.raw } fn (mut m Machine) restore_custom_text_position() ! { state := custom_port_operation(m.env.slots[0]) token := checked_custom_text_position(state.target, state.position)! mut port := state.target.input_pointer() // A checkpoint is reusable. The live reader gets its own mutable array. mut resource := state.resource resource.text_view = unspecified() resource.replay_active = false resource.replay_tail = []u8{} port.buffer = token.unread.clone() port.position = 0 port.mark = -1 port.ended = token.ended port.no_fold = token.no_fold if isnil(port.location) { port.location = &InputLocation{} } unsafe { *port.location = token.point } restore_custom_text_coordinates(resource, token) } // Internal immutable string views may borrow a host string or printer output. // Mark them read-only before exposing them; mutable Scheme strings keep their // original Value identity instead. No payload copy is needed for these views. fn custom_immutable_text(text string) Value { value := owned_string(text, unicode_length(text)) return Value{ ...value, read_only: true } } fn (mut vm VM) prepare_custom_text_output(operation PortIoOperation, target Value, args []Value) !&CustomPortOperation { vm.checked_text_output(target)! resource := custom_output_resource(target) if !isnil(resource.output_adapter) && operation in [.get_position, .set_position] { return failure('transcoded output port does not support positions') } mut source := unspecified() mut start := 0 mut end := 0 mut position := unspecified() match operation { .write_char, .put_char { ch := args[if operation == .write_char { 0 } else { 1 }].as_character()! source = custom_immutable_text(ch.str()) end = 1 } .write_text, .put_text { source = args[if operation == .write_text { 0 } else { 1 }] _ := string_data(source)! start, end = if operation == .write_text { sequence_range(args, 2, source.string_pointer().length)! } else { port_get_put_range(args, source.string_pointer().length)! } } .display_text { source = if args[0].tag == .string { args[0] } else { custom_immutable_text(display(args[0])!) } end = source.string_pointer().length } .write_datum, .write_shared, .write_simple, .put_datum { value := args[if operation == .put_datum { 1 } else { 0 }] source = custom_immutable_text(match operation { .write_shared { write_shared(value)! } .write_simple { write_simple(value)! } else { write(value)! } }) end = source.string_pointer().length } .newline { source = custom_immutable_text('\n') end = 1 } .get_position { if !resource.get_position.is_callable() { return failure('output port does not support positions') } } .set_position { position = args[1] checked_custom_text_position(target, position)! if !resource.set_position.is_callable() { return failure('output port does not support setting positions') } } .close_any, .close_output, .flush_output {} else { return failure('expected a binary output port') } } if source.tag == .string && start == end && isnil(resource.output_adapter) { return unsafe { nil } } return &CustomPortOperation{ operation: operation target: target resource: resource source: source offset: start end: end position: position lease: &CustomPortLease{ port: target resource: resource input_cursor: (operation !in [.get_position, .flush_output, .close_any, .close_output] && (source.tag != .string || start < end)) || custom_attached_pending(resource) closing: operation in [ .close_any, .close_output, ] } } } fn (mut m Machine) step_custom_text_output(mut vm VM) ! { state := custom_port_operation(m.env.slots[0]) if !isnil(state.encoded) || !isnil(state.resource.output_adapter) { m.step_custom_encoded_output(mut vm)! return } match state.operation { .get_position { m.push(state.resource.get_position) m.apply(mut vm, 0, false)! } .set_position { m.push(state.resource.set_position) m.push(m.custom_text_set_argument()!) m.apply(mut vm, 1, false)! } .close_output, .close_any { if !claim_custom_combined_close(state.lease) { m.finish_custom_port_operation(unspecified()) return } m.push(state.resource.close) m.apply(mut vm, 0, false)! } .flush_output { m.finish_custom_port_operation(unspecified()) } else { if state.offset == state.end { m.finish_custom_port_operation(unspecified()) return } begin_positioned_text_write(state) m.push(state.resource.write) m.push(state.source) m.push(integer(state.offset)) m.push(integer(state.end - state.offset)) m.apply(mut vm, 3, false)! } } } fn (mut m Machine) collect_custom_text_output(result Value) ! { mut state := custom_port_operation(m.env.slots[0]) if !isnil(state.encoded) { m.collect_custom_encoded_output(result)! return } match state.operation { .get_position { m.finish_custom_port_operation(custom_text_position(state.target, custom_port_single_result(result)!)!) } .set_position, .close_output, .close_any { m.finish_custom_port_operation(unspecified()) } else { count := custom_port_single_result(result)!.as_integer()! if count < 0 || count > state.end - state.offset { return failure('custom textual writer returned an invalid count') } if m.env.escaped { mut scope := m.detach_bridge() state = &CustomPortOperation{ ...state } scope.slots[0] = payload_value(.port_operation, state) } state.offset += int(count) } } }