Skip to content

Commit

Permalink
src: refactor bootstrap to use bootstrap object
Browse files Browse the repository at this point in the history
PR-URL: nodejs#20917
Reviewed-By: Gus Caplan <[email protected]>
Reviewed-By: Michaël Zasso <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Ben Noordhuis <[email protected]>
Reviewed-By: Joyee Cheung <[email protected]>
Reviewed-By: Refael Ackermann <[email protected]>
  • Loading branch information
jasnell authored and targos committed Jun 6, 2018
1 parent f4287e6 commit c13cd35
Show file tree
Hide file tree
Showing 9 changed files with 244 additions and 197 deletions.
42 changes: 32 additions & 10 deletions lib/internal/bootstrap/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,19 @@
// process.binding(), process._linkedBinding(), internalBinding() and
// NativeModule. And then { internalBinding, NativeModule } will be passed
// into this bootstrapper to bootstrap Node.js core.

'use strict';

(function bootstrapNodeJSCore(process, { internalBinding, NativeModule }) {
(function bootstrapNodeJSCore(process,
// bootstrapper properties... destructured to
// avoid retaining a reference to the bootstrap
// object.
{ _setupProcessObject, _setupNextTick,
_setupPromises, _chdir, _cpuUsage,
_hrtime, _memoryUsage, _rawDebug,
_umask, _initgroups, _setegid, _seteuid,
_setgid, _setuid, _setgroups,
_shouldAbortOnUncaughtToggle },
{ internalBinding, NativeModule }) {
const exceptionHandlerState = { captureFn: null };

function startup() {
Expand All @@ -36,11 +45,24 @@
const _process = NativeModule.require('internal/process');
_process.setupConfig(NativeModule._source);
_process.setupSignalHandlers();
_process.setupUncaughtExceptionCapture(exceptionHandlerState);
_process.setupUncaughtExceptionCapture(exceptionHandlerState,
_shouldAbortOnUncaughtToggle);
NativeModule.require('internal/process/warning').setup();
NativeModule.require('internal/process/next_tick').setup();
NativeModule.require('internal/process/next_tick').setup(_setupNextTick,
_setupPromises);
NativeModule.require('internal/process/stdio').setup();
NativeModule.require('internal/process/methods').setup();
NativeModule.require('internal/process/methods').setup(_chdir,
_cpuUsage,
_hrtime,
_memoryUsage,
_rawDebug,
_umask,
_initgroups,
_setegid,
_seteuid,
_setgid,
_setuid,
_setgroups);

const perf = process.binding('performance');
const {
Expand All @@ -55,9 +77,9 @@
NODE_PERFORMANCE_MILESTONE_PRELOAD_MODULE_LOAD_END
} = perf.constants;

_process.setup_hrtime();
_process.setup_cpuUsage();
_process.setupMemoryUsage();
_process.setup_hrtime(_hrtime);
_process.setup_cpuUsage(_cpuUsage);
_process.setupMemoryUsage(_memoryUsage);
_process.setupKillAndExit();
if (global.__coverage__)
NativeModule.require('internal/process/write-coverage').setup();
Expand All @@ -79,7 +101,7 @@
}

_process.setupChannel();
_process.setupRawDebug();
_process.setupRawDebug(_rawDebug);

const browserGlobals = !process._noBrowserGlobals;
if (browserGlobals) {
Expand Down Expand Up @@ -294,7 +316,7 @@
}

function setupProcessObject() {
process._setupProcessObject(pushValueToArray);
_setupProcessObject(pushValueToArray);

function pushValueToArray() {
for (var i = 0; i < arguments.length; i++)
Expand Down
26 changes: 10 additions & 16 deletions lib/internal/process.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ process.assert = deprecate(
'DEP0100');

// Set up the process.cpuUsage() function.
function setup_cpuUsage() {
// Get the native function, which will be replaced with a JS version.
const _cpuUsage = process.cpuUsage;

function setup_cpuUsage(_cpuUsage) {
// Create the argument array that will be passed to the native function.
const cpuValues = new Float64Array(2);

Expand Down Expand Up @@ -92,8 +89,7 @@ function setup_cpuUsage() {
// The 3 entries filled in by the original process.hrtime contains
// the upper/lower 32 bits of the second part of the value,
// and the remaining nanoseconds of the value.
function setup_hrtime() {
const _hrtime = process.hrtime;
function setup_hrtime(_hrtime) {
const hrValues = new Uint32Array(3);

process.hrtime = function hrtime(time) {
Expand All @@ -120,12 +116,11 @@ function setup_hrtime() {
};
}

function setupMemoryUsage() {
const memoryUsage_ = process.memoryUsage;
function setupMemoryUsage(_memoryUsage) {
const memValues = new Float64Array(4);

process.memoryUsage = function memoryUsage() {
memoryUsage_(memValues);
_memoryUsage(memValues);
return {
rss: memValues[0],
heapTotal: memValues[1],
Expand Down Expand Up @@ -245,18 +240,17 @@ function setupChannel() {
}


function setupRawDebug() {
const rawDebug = process._rawDebug;
function setupRawDebug(_rawDebug) {
process._rawDebug = function() {
rawDebug(util.format.apply(null, arguments));
_rawDebug(util.format.apply(null, arguments));
};
}


function setupUncaughtExceptionCapture(exceptionHandlerState) {
// This is a typed array for faster communication with JS.
const shouldAbortOnUncaughtToggle = process._shouldAbortOnUncaughtToggle;
delete process._shouldAbortOnUncaughtToggle;
function setupUncaughtExceptionCapture(exceptionHandlerState,
shouldAbortOnUncaughtToggle) {
// shouldAbortOnUncaughtToggle is a typed array for faster
// communication with JS.

process.setUncaughtExceptionCaptureCallback = function(fn) {
if (fn === null) {
Expand Down
64 changes: 24 additions & 40 deletions lib/internal/process/methods.js
Original file line number Diff line number Diff line change
@@ -1,65 +1,49 @@
'use strict';

function setupProcessMethods() {
function setupProcessMethods(_chdir, _cpuUsage, _hrtime, _memoryUsage,
_rawDebug, _umask, _initgroups, _setegid,
_seteuid, _setgid, _setuid, _setgroups) {
// Non-POSIX platforms like Windows don't have certain methods.
if (process.setgid !== undefined) {
setupPosixMethods();
if (_setgid !== undefined) {
setupPosixMethods(_initgroups, _setegid, _seteuid,
_setgid, _setuid, _setgroups);
}

const { chdir: _chdir, umask: _umask } = process;

process.chdir = chdir;
process.umask = umask;

function chdir(...args) {
process.chdir = function chdir(...args) {
return _chdir(...args);
}
};

function umask(...args) {
process.umask = function umask(...args) {
return _umask(...args);
}
};
}

function setupPosixMethods() {
const {
initgroups: _initgroups,
setegid: _setegid,
seteuid: _seteuid,
setgid: _setgid,
setuid: _setuid,
setgroups: _setgroups
} = process;

process.initgroups = initgroups;
process.setegid = setegid;
process.seteuid = seteuid;
process.setgid = setgid;
process.setuid = setuid;
process.setgroups = setgroups;
function setupPosixMethods(_initgroups, _setegid, _seteuid,
_setgid, _setuid, _setgroups) {

function initgroups(...args) {
process.initgroups = function initgroups(...args) {
return _initgroups(...args);
}
};

function setegid(...args) {
process.setegid = function setegid(...args) {
return _setegid(...args);
}
};

function seteuid(...args) {
process.seteuid = function seteuid(...args) {
return _seteuid(...args);
}
};

function setgid(...args) {
process.setgid = function setgid(...args) {
return _setgid(...args);
}
};

function setuid(...args) {
process.setuid = function setuid(...args) {
return _setuid(...args);
}
};

function setgroups(...args) {
process.setgroups = function setgroups(...args) {
return _setgroups(...args);
}
};
}

exports.setup = setupProcessMethods;
8 changes: 4 additions & 4 deletions lib/internal/process/next_tick.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

exports.setup = setupNextTick;

function setupNextTick() {
function setupNextTick(_setupNextTick, _setupPromises) {
const {
getDefaultTriggerAsyncId,
newAsyncId,
Expand All @@ -14,10 +14,10 @@ function setupNextTick() {
emitDestroy,
symbols: { async_id_symbol, trigger_async_id_symbol }
} = require('internal/async_hooks');
const promises = require('internal/process/promises');
const emitPromiseRejectionWarnings =
require('internal/process/promises').setup(_setupPromises);
const { ERR_INVALID_CALLBACK } = require('internal/errors').codes;
const FixedQueue = require('internal/fixed_queue');
const { emitPromiseRejectionWarnings } = promises;

// tickInfo is used so that the C++ code in src/node.cc can
// have easy access to our nextTick state, and avoid unnecessary
Expand All @@ -26,7 +26,7 @@ function setupNextTick() {
const [
tickInfo,
runMicrotasks
] = process._setupNextTick(_tickCallback);
] = _setupNextTick(_tickCallback);

// *Must* match Environment::TickInfo::Fields in src/env.h.
const kHasScheduled = 0;
Expand Down
9 changes: 5 additions & 4 deletions lib/internal/process/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ const pendingUnhandledRejections = [];
const asyncHandledRejections = [];
let lastPromiseId = 0;

module.exports = {
emitPromiseRejectionWarnings
};
exports.setup = setupPromises;

process._setupPromises(unhandledRejection, handledRejection);
function setupPromises(_setupPromises) {
_setupPromises(unhandledRejection, handledRejection);
return emitPromiseRejectionWarnings;
}

function unhandledRejection(promise, reason) {
maybeUnhandledPromises.set(promise, {
Expand Down
1 change: 1 addition & 0 deletions node.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@

'sources': [
'src/async_wrap.cc',
'src/bootstrapper.cc',
'src/callback_scope.cc',
'src/cares_wrap.cc',
'src/connection_wrap.cc',
Expand Down
Loading

0 comments on commit c13cd35

Please sign in to comment.