| 1 | module scheme |
| 2 | |
| 3 | // Encoded bytes must outlive Scheme callbacks and retained continuations. |
| 4 | // Each bounded packet owns its payload once; retries and captures share it. |
| 5 | @[heap] |
| 6 | struct CustomEncodedPacket { |
| 7 | bytes Value |
| 8 | mut: |
| 9 | offset int |
| 10 | } |
| 11 | |
| 12 | @[heap] |
| 13 | struct CustomTranscodedOutput { |
| 14 | resource &CustomPortResource |
| 15 | codec CodecKind |
| 16 | eol EolStyle |
| 17 | mode ErrorHandlingMode |
| 18 | mut: |
| 19 | raw OutputPort |
| 20 | pending &CustomEncodedPacket = unsafe { nil } |
| 21 | } |
| 22 | |
| 23 | fn (_ &CustomTranscodedOutput) write(_bytes []u8) !int { |
| 24 | return failure('Scheme port operation needs the resumable I/O driver') |
| 25 | } |
| 26 | |
| 27 | fn (mut vm VM) attach_custom_output(value Value, codec CodecKind, eol EolStyle, mode ErrorHandlingMode, code u8) !Value { |
| 28 | mut original := value.output_pointer() |
| 29 | mut resource := custom_output_resource(value) |
| 30 | adapter := &CustomTranscodedOutput{ resource: resource, raw: OutputPort{ ...original }, codec: codec, eol: eol, mode: mode } |
| 31 | result := vm.output_port(adapter)! |
| 32 | result.output_pointer().transcoder_code = code |
| 33 | result.output_pointer().buffer_mode = original.buffer_mode |
| 34 | resource.output_adapter = adapter |
| 35 | original.closed = true |
| 36 | original.owns_file = false |
| 37 | original.file = unsafe { nil } |
| 38 | original.stream = unsafe { nil } |
| 39 | original.sink = &closed_port |
| 40 | return result |
| 41 | } |
| 42 | |
| 43 | // This state is private to the suspended operation; pending output belongs |
| 44 | // to the shared sink. The identity/start check on acknowledgment prevents a |
| 45 | // replayed callback from rewinding a backlog already progressed elsewhere. |
| 46 | @[heap] |
| 47 | struct CustomEncodedState { |
| 48 | adapter &CustomTranscodedOutput |
| 49 | mut: |
| 50 | packet Value |
| 51 | offset int |
| 52 | characters int |
| 53 | active &CustomEncodedPacket = unsafe { nil } |
| 54 | request &CustomEncodedPacket = unsafe { nil } |
| 55 | request_own bool |
| 56 | closing_callback bool |
| 57 | revision u64 |
| 58 | have_revision bool |
| 59 | byte_offset int |
| 60 | byte_limit int |
| 61 | encoding_error IError = IError(unsafe { nil }) |
| 62 | failed bool |
| 63 | drain_only bool |
| 64 | } |
| 65 | |
| 66 | fn (mut state CustomPortOperation) encode_custom_packet() ! { |
| 67 | mut encoded := state.encoded |
| 68 | data := state.source.string_pointer() |
| 69 | if !encoded.have_revision || encoded.revision != data.revision { |
| 70 | encoded.byte_offset = text_offset(data, state.offset)! |
| 71 | encoded.byte_limit = text_offset(data, state.end)! |
| 72 | encoded.revision = data.revision |
| 73 | encoded.have_revision = true |
| 74 | } |
| 75 | adapter := encoded.adapter |
| 76 | first := encoded.byte_offset |
| 77 | mut cursor := TranscoderEncoder{ |
| 78 | text: text_view(data.value, 0, encoded.byte_limit) |
| 79 | offset: first |
| 80 | codec: adapter.codec |
| 81 | eol: adapter.eol |
| 82 | mode: adapter.mode |
| 83 | } |
| 84 | // Small writes need only a scalar-sized packet; large writes stay bounded. |
| 85 | // Four bytes cover any source scalar or translated line ending. The extra |
| 86 | // eight bytes let a CR/LF pair finish in the same packet without a copy. |
| 87 | remaining := state.end - state.offset |
| 88 | capacity := if remaining < 1022 { remaining * 4 + 8 } else { 4096 } |
| 89 | mut bytes := []u8{len: capacity} |
| 90 | mut used := 0 |
| 91 | for used <= bytes.len - 8 || cursor.has_pending { |
| 92 | step := cursor.next() or { |
| 93 | encoded.encoding_error = err |
| 94 | encoded.failed = true |
| 95 | break |
| 96 | } |
| 97 | if step.kind == .finished { break } |
| 98 | match adapter.codec { |
| 99 | .latin1 { |
| 100 | bytes[used] = u8(step.scalar) |
| 101 | used++ |
| 102 | } |
| 103 | .utf8 { used += unsafe { utf32_decode_to_buffer(u32(step.scalar), mut &bytes[used]) } } |
| 104 | .utf16 { used += bytevector_put_utf16(mut bytes, used, false, step.scalar) } |
| 105 | } |
| 106 | } |
| 107 | encoded.characters = unicode_length(text_view(data.value, first, cursor.offset)) |
| 108 | encoded.byte_offset = cursor.offset |
| 109 | encoded.offset = 0 |
| 110 | if used == 0 { |
| 111 | state.offset += encoded.characters |
| 112 | encoded.packet = unspecified() |
| 113 | return |
| 114 | } |
| 115 | unsafe { bytes.len = used } |
| 116 | packet := owned_bytevector(bytes) |
| 117 | encoded.packet = Value{ ...packet, read_only: true } |
| 118 | } |
| 119 | |
| 120 | fn (mut m Machine) step_custom_encoded_output(mut vm VM) ! { |
| 121 | mut state := custom_port_operation(m.env.slots[0]) |
| 122 | if state.lease.closing && (state.lease.close_attempted |
| 123 | || (!isnil(state.resource.duplex) && state.resource.close_attempted)) { |
| 124 | // An in-flight callback may resume its Scheme code after cleanup, |
| 125 | // but physical release is terminal for fresh wrapper I/O requests. |
| 126 | m.finish_custom_port_operation(unspecified()) |
| 127 | return |
| 128 | } |
| 129 | if isnil(state.encoded) { |
| 130 | state.encoded = &CustomEncodedState{ adapter: state.resource.output_adapter } |
| 131 | } |
| 132 | mut encoded := state.encoded |
| 133 | mut adapter := encoded.adapter |
| 134 | for { |
| 135 | if !isnil(adapter.pending) { |
| 136 | packet := adapter.pending |
| 137 | count := packet.bytes.bytevector_pointer().bytes.len - packet.offset |
| 138 | if count == 0 { |
| 139 | adapter.pending = unsafe { nil } |
| 140 | continue |
| 141 | } |
| 142 | encoded.request = packet |
| 143 | encoded.request_own = voidptr(packet) == voidptr(encoded.active) && packet.offset == encoded.offset |
| 144 | state.request_start = packet.offset |
| 145 | state.request_count = count |
| 146 | if !isnil(state.position_cursor) && m.prepare_custom_attached_write(mut vm)! { return } |
| 147 | m.push(state.resource.write) |
| 148 | m.push(packet.bytes) |
| 149 | m.push(integer(packet.offset)) |
| 150 | m.push(integer(count)) |
| 151 | m.apply(mut vm, 3, false)! |
| 152 | return |
| 153 | } |
| 154 | if encoded.packet.tag == .bytevector { |
| 155 | length := encoded.packet.bytevector_pointer().bytes.len |
| 156 | if encoded.offset < length { |
| 157 | if positioned_custom_attached(state.resource) && m.align_custom_attached_output(mut vm)! { |
| 158 | return |
| 159 | } |
| 160 | packet := &CustomEncodedPacket{ bytes: encoded.packet, offset: encoded.offset } |
| 161 | adapter.pending = packet |
| 162 | encoded.active = packet |
| 163 | continue |
| 164 | } |
| 165 | encoded.packet = unspecified() |
| 166 | encoded.active = unsafe { nil } |
| 167 | state.offset += encoded.characters |
| 168 | } |
| 169 | if encoded.drain_only { |
| 170 | state.encoded = unsafe { nil } |
| 171 | m.ip = 1 |
| 172 | return |
| 173 | } |
| 174 | if encoded.failed { return encoded.encoding_error } |
| 175 | if state.operation in [.close_any, .close_output, .close_input] { |
| 176 | if !state.lease.close_attempted && claim_custom_combined_close(state.lease) { |
| 177 | mut lease := state.lease |
| 178 | lease.close_attempted = true |
| 179 | encoded.closing_callback = true |
| 180 | m.push(state.resource.close) |
| 181 | m.apply(mut vm, 0, false)! |
| 182 | } else { |
| 183 | m.finish_custom_port_operation(unspecified()) |
| 184 | } |
| 185 | return |
| 186 | } |
| 187 | if state.operation == .flush_output || state.offset == state.end { |
| 188 | m.finish_custom_port_operation(unspecified()) |
| 189 | return |
| 190 | } |
| 191 | state.encode_custom_packet()! |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | fn (mut m Machine) collect_custom_encoded_output(result Value) ! { |
| 196 | state := custom_port_operation(m.env.slots[0]) |
| 197 | if state.encoded.closing_callback { |
| 198 | m.finish_custom_port_operation(unspecified()) |
| 199 | return |
| 200 | } |
| 201 | count := custom_port_single_result(result)!.as_integer()! |
| 202 | if count < 0 || count > state.request_count { |
| 203 | return failure('custom writer returned an invalid count') |
| 204 | } |
| 205 | if !isnil(state.position_cursor) { acknowledge_custom_attached_io(state, int(count), true)! } |
| 206 | mut current := m.detach_custom_port_state() |
| 207 | if !isnil(current.position_cursor) { finish_custom_attached_write(current) } |
| 208 | mut encoded := current.encoded |
| 209 | mut adapter := encoded.adapter |
| 210 | if !isnil(adapter.pending) && voidptr(adapter.pending) == voidptr(encoded.request) |
| 211 | && adapter.pending.offset == current.request_start { |
| 212 | adapter.pending.offset += int(count) |
| 213 | if adapter.pending.offset == adapter.pending.bytes.bytevector_pointer().bytes.len { |
| 214 | adapter.pending = unsafe { nil } |
| 215 | } |
| 216 | } |
| 217 | if encoded.request_own { encoded.offset += int(count) } |
| 218 | encoded.request = unsafe { nil } |
| 219 | } |
| 220 | |