module scheme import reader // Captured port state belongs to the individual extractor. The instruction-only // bridge is shared, so first use in a borrowed arena publishes no cached owner. const memory_extract_bridge_code = [ Instruction{ op: .local, a: 1, b: 2, origin: reader.no_node } Instruction{ op: .local, a: 1, b: 0, origin: reader.no_node } Instruction{ op: .local, a: 1, b: 1, origin: reader.no_node } Instruction{ op: .tail_call, a: 2, origin: reader.no_node } ]! const memory_extract_bridge = Function{ code: unsafe { nil } capture_depth: 1 instructions: unsafe { static_bridge_instructions(&memory_extract_bridge_code[0], memory_extract_bridge_code.len) } } const memory_collect_bytes_bridge_code = [ Instruction{ op: .file_init, a: i32(FileMode.collect_bytes), origin: reader.no_node } Instruction{ op: .wind_init, origin: reader.no_node } Instruction{ op: .wind_enter, origin: reader.no_node } Instruction{ op: .wind_leave, origin: reader.no_node } Instruction{ op: .wind_finish, origin: reader.no_node } ]! const memory_collect_bytes_bridge = Function{ code: unsafe { nil } params: 1 rest: true slot_count: 5 instructions: unsafe { static_bridge_instructions(&memory_collect_bytes_bridge_code[0], memory_collect_bytes_bridge_code.len) } } const memory_collect_text_bridge_code = [ Instruction{ op: .file_init, a: i32(FileMode.collect_text), origin: reader.no_node } Instruction{ op: .wind_init, origin: reader.no_node } Instruction{ op: .wind_enter, origin: reader.no_node } Instruction{ op: .wind_leave, origin: reader.no_node } Instruction{ op: .wind_finish, origin: reader.no_node } ]! const memory_collect_text_bridge = Function{ code: unsafe { nil } params: 1 slot_count: 5 instructions: unsafe { static_bridge_instructions(&memory_collect_text_bridge_code[0], memory_collect_text_bridge_code.len) } } const memory_port_descriptors = [ NativeDescriptor{'open-bytevector-input-port', memory_open_bytes_input} NativeDescriptor{'open-string-input-port', memory_open_text_input} NativeDescriptor{'open-bytevector-output-port', memory_open_bytes_output} NativeDescriptor{'open-string-output-port', memory_open_text_output} NativeDescriptor{'port-eof?', memory_port_eof}, ]! const memory_port_names = 'open-bytevector-input-port open-string-input-port open-bytevector-output-port open-string-output-port port-eof? call-with-bytevector-output-port call-with-string-output-port' fn (mut vm VM) install_memory_port_extensions() { for i in 0 .. memory_port_descriptors.len { entry := &memory_port_descriptors[i] vm.bind_global(entry.name, vm.port_native_procedure(entry)) } for mode, name in ['call-with-bytevector-output-port', 'call-with-string-output-port'] { function := if mode == 0 { &memory_collect_bytes_bridge } else { &memory_collect_text_bridge } vm.bind_global(name, procedure_value(&Procedure{ mode: .closure, owner: vm.owner, name: name, function: function })) } } fn checked_optional_transcoder(value Value) ! { if value.tag == .boolean && !value.is_true() { return } value.as_transcoder()! } fn maybe_transcode(mut vm VM, binary Value, options Value) !Value { return if options.tag == .boolean { binary } else { vm.transcoded_port(binary, options)! } } fn memory_open_bytes_input(mut vm VM, args []Value) !Value { check_arity('open-bytevector-input-port', args.len, 1, 2)! options := if args.len == 2 { args[1] } else { boolean(false) } checked_optional_transcoder(options)! return maybe_transcode(mut vm, vm.open_input_bytevector(args[0])!, options) } fn memory_open_text_input(mut vm VM, args []Value) !Value { check_arity('open-string-input-port', args.len, 1, 1)! return vm.open_input_string(args[0].as_string()!) } fn memory_open_bytes_output(mut vm VM, args []Value) !Value { check_arity('open-bytevector-output-port', args.len, 0, 1)! options := if args.len == 1 { args[0] } else { boolean(false) } checked_optional_transcoder(options)! return vm.memory_output_pair(true, options) } fn memory_open_text_output(mut vm VM, args []Value) !Value { check_arity('open-string-output-port', args.len, 0, 0)! return vm.memory_output_pair(false, boolean(false)) } fn (mut vm VM) memory_output_pair(binary bool, options Value) !Value { raw := vm.memory_output_port(binary)! active := if binary { maybe_transcode(mut vm, raw, options)! } else { raw } extract := procedure_value(&Procedure{ mode: .closure owner: vm.owner name: 'output-port-extractor' function: &memory_extract_bridge env: &Environment{ escaped: true slots: [raw, active, procedure_value(&Procedure{ mode: .native, owner: vm.owner, native: memory_extract })] } }) mut result := value_storage(2) container_store_validated(mut result, 0, active) container_store_validated(mut result, 1, extract) return payload_value(.multiple, &Vector{ items: unsafe { result } }) } fn memory_extract(mut vm VM, args []Value) !Value { // Only the private bridge supplies these two captured arguments. return vm.memory_output_result(args[0], args[1], true) } fn (mut vm VM) memory_output_result(raw Value, active Value, clear bool) !Value { mut port := vm.output_owner(raw)! target := vm.output_owner(active)! if port.busy || target.busy { return failure('output port operation is already active') } if !target.closed { vm.flush_output_port(active)! } if !clear { return if port.binary { vm.get_output_bytevector(raw)! } else { vm.get_output_string(raw)! } } mut memory := port.memory_output // Extraction consumes the buffer. Move its storage into the published value // instead of copying bytes that the port is about to discard. A later write // starts with fresh storage and cannot mutate an earlier extraction. value := if port.binary { owned_bytevector(unsafe { memory.bytes }) } else if memory.bytes.len == 0 { owned_string('', 0) } else { length := memory.bytes.len // At exact capacity, array growth would double the allocation just for // the terminator. Copy to one exactly sized string only in that case. text := if length == memory.bytes.cap { memory.bytes.bytestr() } else { unsafe { (&u8(memory.bytes.data))[length] = 0 } unsafe { tos(memory.bytes.data, length) } } owned_string(text, unicode_length(text)) } memory.bytes = []u8{} unsafe { memory.bytes.flags.set(.noslices) } memory.position = -1 return value } fn memory_port_eof(mut vm VM, args []Value) !Value { check_arity('port-eof?', args.len, 1, 1)! port := vm.checked_input(args[0])! value := if port.binary { vm.peek_u8(args[0])! } else { vm.peek_char(args[0])! } return boolean(value.tag == .eof) }