Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

console: split console.js into constructor.js, global.js and inspector.js for clarity #24709

Closed
wants to merge 3 commits into from
Closed
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
549 changes: 1 addition & 548 deletions lib/console.js

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions lib/inspector.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ const {
const { validateString } = require('internal/validators');
const util = require('util');
const { Connection, open, url } = process.binding('inspector');
const { originalConsole } = require('internal/process/per_thread');

if (!Connection)
throw new ERR_INSPECTOR_NOT_AVAILABLE();
Expand Down Expand Up @@ -103,6 +102,8 @@ module.exports = {
open: (port, host, wait) => open(port, host, !!wait),
close: process._debugEnd,
url: url,
console: originalConsole,
// This is dynamically added during bootstrap,
// where the console from the VM is still available
console: require('internal/console/inspector').consoleFromVM,
Session
};
68 changes: 15 additions & 53 deletions lib/internal/bootstrap/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,6 @@

const browserGlobals = !process._noBrowserGlobals;
if (browserGlobals) {
// we are setting this here to forward it to the inspector later
perThreadSetup.originalConsole = global.console;
setupGlobalTimeouts();
setupGlobalConsole();
setupGlobalURL();
Expand Down Expand Up @@ -438,16 +436,25 @@
}

function setupGlobalConsole() {
const originalConsole = global.console;
// Setup Node.js global.console.
const wrappedConsole = NativeModule.require('console');
const consoleFromVM = global.console;
const consoleFromNode =
NativeModule.require('internal/console/global');
// Override global console from the one provided by the VM
// to the one implemented by Node.js
Object.defineProperty(global, 'console', {
configurable: true,
enumerable: false,
value: wrappedConsole,
value: consoleFromNode,
writable: true
});
setupInspector(originalConsole, wrappedConsole);
// TODO(joyeecheung): can we skip this if inspector is not active?
if (process.config.variables.v8_enable_inspector) {
const inspector =
NativeModule.require('internal/console/inspector');
inspector.addInspectorApis(consoleFromNode, consoleFromVM);
// This will be exposed by `require('inspector').console` later.
inspector.consoleFromVM = consoleFromVM;
}
}

function setupGlobalURL() {
Expand Down Expand Up @@ -520,41 +527,6 @@
NativeModule.require('internal/domexception');
}

function setupInspector(originalConsole, wrappedConsole) {
if (!process.config.variables.v8_enable_inspector) {
return;
}
const CJSModule = NativeModule.require('internal/modules/cjs/loader');
const { addCommandLineAPI, consoleCall } = process.binding('inspector');
// Setup inspector command line API.
const { makeRequireFunction } =
NativeModule.require('internal/modules/cjs/helpers');
const path = NativeModule.require('path');
const cwd = tryGetCwd(path);

const consoleAPIModule = new CJSModule('<inspector console>');
consoleAPIModule.paths =
CJSModule._nodeModulePaths(cwd).concat(CJSModule.globalPaths);
addCommandLineAPI('require', makeRequireFunction(consoleAPIModule));
const config = {};
for (const key of Object.keys(wrappedConsole)) {
if (!originalConsole.hasOwnProperty(key))
continue;
// If global console has the same method as inspector console,
// then wrap these two methods into one. Native wrapper will preserve
// the original stack.
wrappedConsole[key] = consoleCall.bind(wrappedConsole,
originalConsole[key],
wrappedConsole[key],
config);
}
for (const key of Object.keys(originalConsole)) {
if (wrappedConsole.hasOwnProperty(key))
continue;
wrappedConsole[key] = originalConsole[key];
}
}

function noop() {}

function setupProcessFatal() {
Expand Down Expand Up @@ -633,17 +605,6 @@
}
}

function tryGetCwd(path) {
try {
return process.cwd();
} catch {
// getcwd(3) can fail if the current working directory has been deleted.
// Fall back to the directory name of the (absolute) executable path.
// It's not really correct but what are the alternatives?
return path.dirname(process.execPath);
}
}

function wrapForBreakOnFirstLine(source) {
if (!process._breakFirstLine)
return source;
Expand All @@ -654,6 +615,7 @@
function evalScript(name, body = wrapForBreakOnFirstLine(process._eval)) {
const CJSModule = NativeModule.require('internal/modules/cjs/loader');
const path = NativeModule.require('path');
const { tryGetCwd } = NativeModule.require('internal/util');
joyeecheung marked this conversation as resolved.
Show resolved Hide resolved
const cwd = tryGetCwd(path);

const module = new CJSModule(name);
Expand Down
Loading