Files
scriptc/tests/corpus/stdout-patch-console-arguments.js
T
Malte UblandClaude Code 7dbcf07fe0 Pass console lines and byte chunks to a patched stdio write
Node's console writes each formatted line with stream.write(line,
errorHandler), so a replaced process.stdout.write sees a string and a
function; a Buffer write arrives as a Buffer. The stdio write hook carried
only a `wide` bit, so every intercepted chunk became a JavaScript string and
console lines carried a single argument.

Replace that bit with a `flags` int (SCR_STDIO_HOOK_WIDE, _CONSOLE, _BYTES)
and keep the hook signature at four parameters, so the console write path and
the byte paths differ only in what they pass. scr_stdio_intercept now builds a
chunk instead of a string, boxes SCR_STDIO_HOOK_BYTES as a Buffer, and appends
a console error handler for SCR_STDIO_HOOK_CONSOLE. Text callers of the old
string entry point move to a new scr_stdio_write_text.

Corpus: stdout-patch-console-arguments.js.

Co-Authored-By: Claude Code <noreply@anthropic.com>
Co-Authored-By: Claude 5.5 <noreply@anthropic.com>
2026-10-28 20:08:13 -07:00

30 lines
1.2 KiB
JavaScript

// What a replaced process.stdout.write receives from console: Node's
// console writes each formatted line with stream.write(line, errorHandler),
// so the replacement sees a string and a function as its second argument
// for console.log, info, debug, dir, count, table and grouped lines alike;
// a direct write passes exactly its own arguments. The error handler accepts
// a null error.
const original = process.stdout.write;
const seen = [];
process.stdout.write = function (chunk, encodingOrCallback, callback) {
const kind = typeof chunk === "string" ? "string" : Buffer.isBuffer(chunk) ? "Buffer" : chunk.constructor.name;
seen.push(`${kind}:${JSON.stringify(String(chunk))}:${typeof encodingOrCallback}:${typeof callback}`);
if (typeof encodingOrCallback === "function") encodingOrCallback(null);
return true;
};
console.log("line", 1);
console.info("info");
console.debug("debug");
console.log("%s=%d", "x", 2);
console.dir({ a: 1 });
console.count();
console.group("group");
console.log("inside");
console.groupEnd();
console.table([{ a: 1 }]);
process.stdout.write("direct");
process.stdout.write("hex", "utf8");
process.stdout.write(Buffer.from("bytes"));
process.stdout.write = original;
console.log(seen.join("\n"));