module scheme import io import os // Input stays first: input_pointer uses the same payload address as ordinary // input ports. The output facet is cold and only its accessor needs a tag arm. // Both facets live in one scanned allocation and share external Value identity. @[heap] struct InputOutputPort { mut: input InputPort output OutputPort } // input_output_port borrows independent host source/sink capabilities. Closing // either logical side leaves the other usable; closing the port closes both // logical sides without closing the borrowed host objects. The callbacks must // finish consuming borrowed buffers before returning and must not reenter this // same port through either direction. Positions are not advertised: independent // source/sink offsets do not identify one shared stream position. pub fn (mut vm VM) input_output_port(source io.Reader, sink io.Writer, options InputOptions) !Value { vm.ready()! if options.read_size <= 0 { return failure('input read size must be positive') } mut port := &InputOutputPort{ 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 } output: OutputPort{ owner: vm.owner, sink: sink, file_backed: sink is os.File || sink is CombinedFileState || sink is PortFileStream, binary: options.binary } } unsafe { port.input.buffer.flags.set(.noslices) } return payload_value(.input_output_port, port) } @[inline] fn check_combined_busy(value Value) ! { if value.tag == .input_output_port && (value.input_pointer().busy || value.output_pointer().busy) { return failure('port operation is already active') } } // close_port closes every direction of a combined port. Check both sides before // changing either, then preserve the first close error while releasing the other. pub fn (mut vm VM) close_port(value Value) ! { vm.ready()! if value.tag == .input_output_port { vm.input_owner(value)! check_combined_busy(value)! if !isnil(custom_port_resource(value)) { vm.custom_combined_host_operation(.close_any, value, [value])! return } mut group := combined_transcoded_resource(value) if !isnil(group) { group.close_all()! return } file := combined_file_resource(value) if !isnil(file) { file.close_all()! return } vm.close_input_port(value) or { vm.close_output_port(value) or {} return err } vm.close_output_port(value)! } else if value.tag == .input_port { vm.close_input_port(value)! } else { vm.close_output_port(value)! } }