module scheme const custom_combined_binary_name = 'make-custom-binary-input/output-port' const custom_combined_text_name = 'make-custom-textual-input/output-port' fn (mut vm VM) install_custom_combined_constructors() { vm.bind_global(custom_combined_binary_name, procedure_value(&Procedure{ mode: .native, name: custom_combined_binary_name, owner: vm.owner, native: custom_combined_binary })) vm.bind_global(custom_combined_text_name, procedure_value(&Procedure{ mode: .native, name: custom_combined_text_name, owner: vm.owner, native: custom_combined_text })) } fn custom_combined_binary(mut vm VM, args []Value) !Value { check_arity(custom_combined_binary_name, args.len, 6, 6)! return vm.make_custom_binary_input_output_port(args[0].as_string()!, args[1], args[2], args[3], args[4], args[5]) } fn custom_combined_text(mut vm VM, args []Value) !Value { check_arity(custom_combined_text_name, args.len, 6, 6)! return vm.make_custom_textual_input_output_port(args[0].as_string()!, args[1], args[2], args[3], args[4], args[5]) } pub fn (mut vm VM) make_custom_binary_input_output_port(id string, read_callback Value, write_callback Value, get_position Value, set_position Value, close Value, options InputOptions) !Value { return vm.make_custom_combined_port(id, read_callback, write_callback, get_position, set_position, close, true, options) } pub fn (mut vm VM) make_custom_textual_input_output_port(id string, read_callback Value, write_callback Value, get_position Value, set_position Value, close Value, options InputOptions) !Value { return vm.make_custom_combined_port(id, read_callback, write_callback, get_position, set_position, close, false, options) } fn (mut vm VM) make_custom_combined_port(id string, read_callback Value, write_callback Value, get_position Value, set_position Value, close Value, binary bool, options InputOptions) !Value { vm.ready()! vm.check_custom_port_callback(read_callback, false)! 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)! if options.read_size <= 0 { return failure('input read size must be positive') } mut resource := &CustomPortResource{ id: id, read: read_callback, write: write_callback, get_position: get_position, set_position: set_position, close: close } result := vm.input_output_port(&CustomPortInput{ resource: resource }, &CustomPortOutput{ resource: resource }, binary: binary read_size: options.read_size fold_case: options.fold_case )! resource.duplex = unsafe { &InputOutputPort(result.pointer()) } return result } // These internal handles select one facet for the existing resumable drivers. // User-visible parameters and returned values retain the one combined identity. fn custom_combined_facet(operation PortIoOperation, value Value) Value { if value.tag != .input_output_port { return value } input := custom_input_only(operation) || (operation in [.get_position, .set_position] && !value.input_pointer().closed) if input || (operation == .close_any && value.output_pointer().closed) { return payload_value(.input_port, value.input_pointer()) } return payload_value(.output_port, value.output_pointer()) } fn (mut vm VM) prepare_custom_combined_operation(operation PortIoOperation, target Value, args []Value) !&CustomPortOperation { vm.input_owner(target)! check_combined_busy(target)! state := vm.prepare_custom_port_operation(operation, custom_combined_facet(operation, target), args)! if operation == .close_any { mut lease := state.lease lease.both = true } return state } // A combined resource survives a logical half-close. Its final closer is a // shared physical event, so an escaped/replayed close cannot invoke it twice. fn claim_custom_combined_close(lease &CustomPortLease) bool { mut resource := lease.resource if !resource.close.is_callable() { return false } if isnil(resource.duplex) { return true } if !resource.closed || resource.close_attempted { return false } resource.close_attempted = true return true }