| 1 | module scheme |
| 2 | |
| 3 | import io |
| 4 | import os |
| 5 | |
| 6 | // Input stays first: input_pointer uses the same payload address as ordinary |
| 7 | // input ports. The output facet is cold and only its accessor needs a tag arm. |
| 8 | // Both facets live in one scanned allocation and share external Value identity. |
| 9 | @[heap] |
| 10 | struct InputOutputPort { |
| 11 | mut: |
| 12 | input InputPort |
| 13 | output OutputPort |
| 14 | } |
| 15 | |
| 16 | // input_output_port borrows independent host source/sink capabilities. Closing |
| 17 | // either logical side leaves the other usable; closing the port closes both |
| 18 | // logical sides without closing the borrowed host objects. The callbacks must |
| 19 | // finish consuming borrowed buffers before returning and must not reenter this |
| 20 | // same port through either direction. Positions are not advertised: independent |
| 21 | // source/sink offsets do not identify one shared stream position. |
| 22 | pub fn (mut vm VM) input_output_port(source io.Reader, sink io.Writer, options InputOptions) !Value { |
| 23 | vm.ready()! |
| 24 | if options.read_size <= 0 { return failure('input read size must be positive') } |
| 25 | mut port := &InputOutputPort{ |
| 26 | input: InputPort{ owner: vm.owner, source: source, file_backed: source is os.File || source is CombinedFileState || source is PortFileStream, read_size: options.read_size, binary: options.binary, no_fold: !options.fold_case } |
| 27 | output: OutputPort{ owner: vm.owner, sink: sink, file_backed: sink is os.File || sink is CombinedFileState || sink is PortFileStream, binary: options.binary } |
| 28 | } |
| 29 | unsafe { port.input.buffer.flags.set(.noslices) } |
| 30 | return payload_value(.input_output_port, port) |
| 31 | } |
| 32 | |
| 33 | @[inline] |
| 34 | fn check_combined_busy(value Value) ! { |
| 35 | if value.tag == .input_output_port && (value.input_pointer().busy || value.output_pointer().busy) { |
| 36 | return failure('port operation is already active') |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | // close_port closes every direction of a combined port. Check both sides before |
| 41 | // changing either, then preserve the first close error while releasing the other. |
| 42 | pub fn (mut vm VM) close_port(value Value) ! { |
| 43 | vm.ready()! |
| 44 | if value.tag == .input_output_port { |
| 45 | vm.input_owner(value)! |
| 46 | check_combined_busy(value)! |
| 47 | if !isnil(custom_port_resource(value)) { |
| 48 | vm.custom_combined_host_operation(.close_any, value, [value])! |
| 49 | return |
| 50 | } |
| 51 | mut group := combined_transcoded_resource(value) |
| 52 | if !isnil(group) { |
| 53 | group.close_all()! |
| 54 | return |
| 55 | } |
| 56 | file := combined_file_resource(value) |
| 57 | if !isnil(file) { |
| 58 | file.close_all()! |
| 59 | return |
| 60 | } |
| 61 | vm.close_input_port(value) or { |
| 62 | vm.close_output_port(value) or {} |
| 63 | return err |
| 64 | } |
| 65 | vm.close_output_port(value)! |
| 66 | } else if value.tag == .input_port { |
| 67 | vm.close_input_port(value)! |
| 68 | } else { |
| 69 | vm.close_output_port(value)! |
| 70 | } |
| 71 | } |
| 72 | |