vile / scheme / port_custom_combined.v
80 lines · 69 sloc · 4.02 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3const custom_combined_binary_name = 'make-custom-binary-input/output-port'
4const custom_combined_text_name = 'make-custom-textual-input/output-port'
5
6fn (mut vm VM) install_custom_combined_constructors() {
7 vm.bind_global(custom_combined_binary_name, procedure_value(&Procedure{ mode: .native, name: custom_combined_binary_name, owner: vm.owner, native: custom_combined_binary }))
8 vm.bind_global(custom_combined_text_name, procedure_value(&Procedure{ mode: .native, name: custom_combined_text_name, owner: vm.owner, native: custom_combined_text }))
9}
10
11fn custom_combined_binary(mut vm VM, args []Value) !Value {
12 check_arity(custom_combined_binary_name, args.len, 6, 6)!
13 return vm.make_custom_binary_input_output_port(args[0].as_string()!, args[1], args[2], args[3], args[4], args[5])
14}
15
16fn custom_combined_text(mut vm VM, args []Value) !Value {
17 check_arity(custom_combined_text_name, args.len, 6, 6)!
18 return vm.make_custom_textual_input_output_port(args[0].as_string()!, args[1], args[2], args[3], args[4], args[5])
19}
20
21pub 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 {
22 return vm.make_custom_combined_port(id, read_callback, write_callback, get_position, set_position, close, true, options)
23}
24
25pub 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 {
26 return vm.make_custom_combined_port(id, read_callback, write_callback, get_position, set_position, close, false, options)
27}
28
29fn (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 {
30 vm.ready()!
31 vm.check_custom_port_callback(read_callback, false)!
32 vm.check_custom_port_callback(write_callback, false)!
33 vm.check_custom_port_callback(get_position, true)!
34 vm.check_custom_port_callback(set_position, true)!
35 vm.check_custom_port_callback(close, true)!
36 if options.read_size <= 0 { return failure('input read size must be positive') }
37 mut resource := &CustomPortResource{ id: id, read: read_callback, write: write_callback, get_position: get_position, set_position: set_position, close: close }
38 result := vm.input_output_port(&CustomPortInput{ resource: resource },
39 &CustomPortOutput{ resource: resource },
40 binary: binary
41 read_size: options.read_size
42 fold_case: options.fold_case
43 )!
44 resource.duplex = unsafe { &InputOutputPort(result.pointer()) }
45 return result
46}
47
48// These internal handles select one facet for the existing resumable drivers.
49// User-visible parameters and returned values retain the one combined identity.
50fn custom_combined_facet(operation PortIoOperation, value Value) Value {
51 if value.tag != .input_output_port { return value }
52 input := custom_input_only(operation) || (operation in [.get_position, .set_position] && !value.input_pointer().closed)
53 if input || (operation == .close_any && value.output_pointer().closed) {
54 return payload_value(.input_port, value.input_pointer())
55 }
56 return payload_value(.output_port, value.output_pointer())
57}
58
59fn (mut vm VM) prepare_custom_combined_operation(operation PortIoOperation, target Value, args []Value) !&CustomPortOperation {
60 vm.input_owner(target)!
61 check_combined_busy(target)!
62
63 state := vm.prepare_custom_port_operation(operation, custom_combined_facet(operation, target), args)!
64 if operation == .close_any {
65 mut lease := state.lease
66 lease.both = true
67 }
68 return state
69}
70
71// A combined resource survives a logical half-close. Its final closer is a
72// shared physical event, so an escaped/replayed close cannot invoke it twice.
73fn claim_custom_combined_close(lease &CustomPortLease) bool {
74 mut resource := lease.resource
75 if !resource.close.is_callable() { return false }
76 if isnil(resource.duplex) { return true }
77 if !resource.closed || resource.close_attempted { return false }
78 resource.close_attempted = true
79 return true
80}
81