| 1 | module scheme |
| 2 | |
| 3 | // Default wrappers and their descriptors share the stable identity owner's |
| 4 | // lifetime. First access can occur in a shorter caller arena without publishing |
| 5 | // that arena's storage. They do not retain the VM or any Scheme callback. |
| 6 | struct StandardPorts { |
| 7 | mut: |
| 8 | streams [3]PortFileStream |
| 9 | input InputPort = InputPort{ owner: unsafe { nil } } |
| 10 | outputs [2]OutputPort |
| 11 | } |
| 12 | |
| 13 | // Canonical parameter objects are retained independently of their public names. |
| 14 | // Default stdio wrappers are created on first use, keeping VM construction free |
| 15 | // of file/port state. The private marker cannot be constructed by Scheme code. |
| 16 | fn (mut vm VM) install_port_parameters() { |
| 17 | for index, name in ['current-input-port', 'current-output-port', 'current-error-port'] { |
| 18 | converter := procedure_value(&Procedure{ |
| 19 | mode: .native |
| 20 | name: name |
| 21 | owner: vm.owner |
| 22 | native: if index == 0 { convert_input_port } else { convert_output_port } |
| 23 | }) |
| 24 | parameter := parameter_value(Value{ tag: .port_default, bits: ValueBits{ whole: index } }, converter, vm.owner) |
| 25 | vm.port_parameters[index] = parameter |
| 26 | vm.bind_global(name, parameter) |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | fn convert_input_port(mut vm VM, args []Value) !Value { |
| 31 | check_arity('current-input-port', args.len, 1, 1)! |
| 32 | vm.input_owner(args[0])! |
| 33 | return args[0] |
| 34 | } |
| 35 | |
| 36 | fn convert_output_port(mut vm VM, args []Value) !Value { |
| 37 | check_arity('current output/error port', args.len, 1, 1)! |
| 38 | vm.output_owner(args[0])! |
| 39 | return args[0] |
| 40 | } |
| 41 | |
| 42 | @[direct_array_access] |
| 43 | fn (mut vm VM) default_port(index int) !Value { |
| 44 | mut storage := parameter_storage(vm.port_parameters[index]) |
| 45 | if storage.slots[0].tag != .port_default { return storage.slots[0] } |
| 46 | vm.port_state() |
| 47 | mut state := &vm.owner.stdio |
| 48 | stream := vm.standard_stream(index) |
| 49 | if index == 0 { |
| 50 | state.input = InputPort{ owner: vm.owner, source: stream, read_size: 1, file: &stream.file, file_backed: true } |
| 51 | unsafe { state.input.buffer.flags.set(.noslices) } |
| 52 | value := payload_value(.input_port, &state.input) |
| 53 | storage.slots[0] = value |
| 54 | } else { |
| 55 | state.outputs[index - 1] = OutputPort{ owner: vm.owner, sink: stream, file: &stream.file, file_backed: true } |
| 56 | value := payload_value(.output_port, &state.outputs[index - 1]) |
| 57 | storage.slots[0] = value |
| 58 | } |
| 59 | return storage.slots[0] |
| 60 | } |
| 61 | |
| 62 | @[direct_array_access] |
| 63 | fn (mut vm VM) current_port_value(index int) !Value { |
| 64 | parameter := vm.port_parameters[index].procedure_pointer() |
| 65 | binding := find_parameter_binding(vm.parameter_context(), parameter) |
| 66 | if !isnil(binding) { return binding.value } |
| 67 | value := parameter.env.slots[0] |
| 68 | return if value.tag == .port_default { vm.default_port(index)! } else { value } |
| 69 | } |
| 70 | |
| 71 | // Host setters update the visible dynamic location, just like calling its |
| 72 | // parameter procedure. Outside evaluation they update the default selection. |
| 73 | @[direct_array_access] |
| 74 | fn (mut vm VM) set_port_value(index int, value Value) { |
| 75 | vm.port_state() |
| 76 | parameter := vm.port_parameters[index] |
| 77 | mut binding := find_parameter_binding(vm.parameter_context(), parameter.procedure_pointer()) |
| 78 | if isnil(binding) { |
| 79 | mut storage := parameter_storage(parameter) |
| 80 | storage.slots[0] = value |
| 81 | } else { |
| 82 | binding.value = value |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | pub fn (mut vm VM) current_error_port() !Value { |
| 87 | vm.ready()! |
| 88 | return vm.current_port_value(2) |
| 89 | } |
| 90 | |
| 91 | pub fn (mut vm VM) set_error_port(value Value) ! { |
| 92 | vm.ready()! |
| 93 | vm.output_owner(value)! |
| 94 | vm.set_port_value(2, value) |
| 95 | } |
| 96 | |