Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/runtime/nodejs-compat.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ We update this page regularly. It reflects the latest version of Bun's compatibi

### [`node:console`](https://nodejs.org/api/console.html)

🟢 Fully implemented. Bun writes console output directly to the stdout/stderr file descriptors and formats it with its own inspector. As a result, replacing `process.stdout.write` does not capture the output, and object layout differs from `util.inspect`. `console.trace()` writes to stdout and `console.time*()` to stderr.
🟢 Fully implemented. Bun formats console output with its own inspector and writes it directly to the stdout/stderr file descriptors, unless `process.stdout.write` or `process.stderr.write` has been replaced: then each message is passed to the replacement as one string, as in Node.js. Object layout differs from `util.inspect`. `console.trace()` writes to stdout and `console.time*()` to stderr.

### [`node:dgram`](https://nodejs.org/api/dgram.html)

Expand Down
5 changes: 4 additions & 1 deletion src/aot/builtins/table/tests/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ fn split_binding_text(text: &str) -> Option<(BuiltinName, BuiltinName)> {
}

// Native code makes these from host functions of its own and hands them to the library or to the program, so no text names them.
const HANDED_OVER_BY_NATIVE_CODE: [BuiltinId; 18] = [
const HANDED_OVER_BY_NATIVE_CODE: [BuiltinId; 21] = [
BuiltinId::NativeNewCppFunctionBunProcessCppBun__Process__disconnect,
BuiltinId::NativeNewCppFunctionBunProcessCppJsFunctionDrainMicrotaskQueue,
BuiltinId::NativeNewCppFunctionBunProcessCppProcess_functionConsoleNoop,
BuiltinId::NativeNewCppFunctionBunProcessCppProcess_functionConsoleStderrWriteFailed,
BuiltinId::NativeNewCppFunctionBunProcessCppProcess_functionConsoleStdoutWriteFailed,
BuiltinId::NativeNewCppFunctionBunProcessCppJsFunction_emitWarning,
BuiltinId::NativeNewCppFunctionBunProcessCppJsFunction_throwValue,
BuiltinId::NativeNewCppFunctionBunProcessCppJsSend,
Expand Down
3 changes: 3 additions & 0 deletions src/codegen/builtins/host/native-cells.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ export const needsWholeInputStreamCell = "Process.needsWholeInputStream";
export const needsWholeOutputStreamCell = "Process.needsWholeOutputStream";
export const readsWindowSizeCell = "Process.readsWindowSize";
export const writesToStandardSinkCell = "Process.writesToStandardSink";
export const consoleNoopCell = "Process.consoleNoop";
export const consoleStdoutWriteFailedCell = "Process.consoleStdoutWriteFailed";
export const consoleStderrWriteFailedCell = "Process.consoleStderrWriteFailed";
export const argumentsCell = "Process.argv";
export const runtimeArgumentsCell = "Process.execArgv";
export const closedPromiseOfStreamCell = "WebStream.closedPromise";
Expand Down
55 changes: 54 additions & 1 deletion src/codegen/builtins/host/runtime-dependencies.builtins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import {
asyncContextCell,
closesChannelCell,
coercesEnvironmentValueCell,
consoleNoopCell,
consoleStderrWriteFailedCell,
consoleStdoutWriteFailedCell,
drainMicrotasksCell,
editsWindowsEnvironmentCell,
emitsProcessEventCell,
Expand Down Expand Up @@ -149,6 +152,28 @@ const writtenByWarnings = b.memberOfValue(
b.calledByHost({ times: "Repeatedly", this: b.nativeCell(standardErrorCell), args: [b.string], result: b.dropped }),
);

// The native console writes through a replaced write() as node's kWriteToConsole does (BunProcess.cpp Bun__Process__writeThroughStdioStream).
const writtenByConsole = (cell: string, failed: string) => {
const listener = { this: b.nativeCell(cell), args: [b.string, b.nativeCell(consoleNoopCell)], result: b.dropped };
return [
b.memberOfValue(
"listenerCount",
b.calledByHost({ times: "Repeatedly", this: b.nativeCell(cell), args: [b.string], result: [b.inspectedByHost] }),
),
b.memberOfValue("once", b.calledByHost({ times: "Repeatedly", ...listener })),
b.memberOfValue("removeListener", b.calledByHost({ times: "Repeatedly", ...listener })),
b.memberOfValue(
"write",
b.calledByHost({
times: "Repeatedly",
this: b.nativeCell(cell),
args: [b.string, b.nativeCell(failed)],
result: b.dropped,
}),
),
];
};

// Whatever makes a standard stream, the property of `process` holds it from then on, and so does the one of `console`.
const usesOfStandardStream: readonly (readonly b.HostUseSpec[])[] = [
[b.keptIn(standardInputCell)],
Expand Down Expand Up @@ -185,7 +210,17 @@ const wholeOutputStream = (descriptor: 1 | 2) =>
times: "Once",
this: globalObject,
args: [processObject, b.int(descriptor, descriptor), b.bool, kindOfDescriptor],
result: [b.memberOfValue(0, ...usesOfStandardStream[descriptor]), b.memberOfValue(1, b.inspectedByHost)],
result: [
b.memberOfValue(
0,
...usesOfStandardStream[descriptor],
...writtenByConsole(
descriptor === 1 ? standardOutputCell : standardErrorCell,
descriptor === 1 ? consoleStdoutWriteFailedCell : consoleStderrWriteFailedCell,
),
),
b.memberOfValue(1, b.inspectedByHost),
],
}),
);

Expand Down Expand Up @@ -802,6 +837,24 @@ export default b.declarations(
reads: ["ExternalState"],
implementation: b.hostCall,
}),
b.hostFunction(consoleNoopCell, "BunProcess.cpp", "Process_functionConsoleNoop", {
returns: b.undefined,
capability: "none",
implementation: b.hostCall,
}),
[
[consoleStdoutWriteFailedCell, "Process_functionConsoleStdoutWriteFailed"],
[consoleStderrWriteFailedCell, "Process_functionConsoleStderrWriteFailed"],
].flatMap(([cell, symbol]) =>
b.hostFunction(cell, "BunProcess.cpp", symbol, {
args: { error: b.any },
returns: b.undefined,
capability: "js",
throws: true,
uses: { error: b.inspected },
implementation: b.hostCall,
}),
),
// write() of the FileSink of the descriptor: what the row of %FileSink%.prototype.write says.
b.hostFunction(writesToStandardSinkCell, "BunProcess.cpp", "Process_functionWriteToStandardSink", {
args: { descriptor: b.any, data: b.any },
Expand Down
Loading
Loading