Files
scriptc/tests/corpus/8520-stream-object-encoding-arg.ts
T
Chris Tate 85fdf4ce74 Trim the per-chunk work of compiled object-mode stream callbacks
- A typed write or transform callback whose encoding parameter is a
  pointer union with an undefined arm receives the null pointer directly
  when an object-mode write passed no encoding, instead of converting the
  encoding value through a dyn box
  (scr_stream_chunk_encoding_undefined).
- The stream completion callbacks carry SCR_CLOSURE_KIND_RECEIVER_FREE:
  their thunks never read `this`, so compiled callers skip the receiver
  bracket.
- The completion thunks borrow the stream from the closure's box instead
  of retaining and releasing it on every call; the closure, alive for the
  call, holds the box.
- settle_next, entry_convert and clear_buffer_each test their early-out
  conditions in inline wrappers before calling the full functions.

A corpus program covers the encoding argument in object mode, byte mode
and transforms.
2026-10-29 00:40:53 -05:00

43 lines
1.3 KiB
TypeScript

// The encoding argument a typed write or transform callback receives:
// undefined for an object-mode write without one, the given string with
// one, and 'buffer' for byte-mode chunks.
import { Transform, Writable } from "node:stream";
const seen: string[] = [];
const objects = new Writable({
objectMode: true,
write(chunk: { n: number }, encoding, cb) {
seen.push(`object ${chunk.n} ${typeof encoding} ${String(encoding)}`);
cb();
},
});
objects.write({ n: 1 });
objects.write({ n: 2 }, "utf8");
objects.write({ n: 3 }, undefined);
objects.end(() => {
const bytes = new Writable({
write(chunk: Buffer, encoding, cb) {
seen.push(`bytes ${chunk.length} ${typeof encoding} ${String(encoding)}`);
cb();
},
});
bytes.write("abc");
bytes.write(Buffer.from("de"));
bytes.end(() => {
const upper = new Transform({
objectMode: true,
transform(row: { n: number }, encoding, cb) {
seen.push(`transform ${row.n} ${typeof encoding} ${String(encoding)}`);
cb(null, { n: row.n * 10 });
},
});
upper.on("data", (row: { n: number }) => seen.push(`data ${row.n}`));
upper.on("end", () => {
for (const line of seen) console.log(line);
});
upper.write({ n: 4 });
upper.write({ n: 5 }, "latin1");
upper.end();
});
});