Skip to content
Permalink
Browse files

src: refactor bootstrap to use bootstrap object

PR-URL: #20917
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Refael Ackermann <refack@gmail.com>
  • Loading branch information...
jasnell committed May 23, 2018
1 parent dcecfb7 commit cb3d049badb772fc1ea7051540d50c89f73e36dd
@@ -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() {
@@ -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 {
@@ -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();
@@ -79,7 +101,7 @@
}

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

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

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

function pushValueToArray() {
for (var i = 0; i < arguments.length; i++)
@@ -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);

@@ -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) {
@@ -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],
@@ -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) {
@@ -9,55 +9,36 @@ const {
validateUint32
} = require('internal/validators');

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(directory) {
process.chdir = function chdir(directory) {
if (typeof directory !== 'string') {
throw new ERR_INVALID_ARG_TYPE('directory', 'string', directory);
}
return _chdir(directory);
}
};

function umask(mask) {
process.umask = function umask(mask) {
if (mask === undefined) {
// Get the mask
return _umask(mask);
}
mask = validateAndMaskMode(mask, 'mask');
return _umask(mask);
}
};
}

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(user, extraGroup) {
process.initgroups = function initgroups(user, extraGroup) {
validateId(user, 'user');
validateId(extraGroup, 'extraGroup');
// Result is 0 on success, 1 if user is unknown, 2 if group is unknown.
@@ -67,25 +48,25 @@ function setupPosixMethods() {
} else if (result === 2) {
throw new ERR_UNKNOWN_CREDENTIAL('Group', extraGroup);
}
}
};

function setegid(id) {
process.setegid = function setegid(id) {
return execId(id, 'Group', _setegid);
}
};

function seteuid(id) {
process.seteuid = function seteuid(id) {
return execId(id, 'User', _seteuid);
}
};

function setgid(id) {
process.setgid = function setgid(id) {
return execId(id, 'Group', _setgid);
}
};

function setuid(id) {
process.setuid = function setuid(id) {
return execId(id, 'User', _setuid);
}
};

function setgroups(groups) {
process.setgroups = function setgroups(groups) {
if (!Array.isArray(groups)) {
throw new ERR_INVALID_ARG_TYPE('groups', 'Array', groups);
}
@@ -98,7 +79,7 @@ function setupPosixMethods() {
if (result > 0) {
throw new ERR_UNKNOWN_CREDENTIAL('Group', groups[result - 1]);
}
}
};

function execId(id, type, method) {
validateId(id, 'id');
@@ -2,7 +2,7 @@

exports.setup = setupNextTick;

function setupNextTick() {
function setupNextTick(_setupNextTick, _setupPromises) {
const {
getDefaultTriggerAsyncId,
newAsyncId,
@@ -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
@@ -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;
@@ -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, {
@@ -309,6 +309,7 @@

'sources': [
'src/async_wrap.cc',
'src/bootstrapper.cc',
'src/callback_scope.cc',
'src/cares_wrap.cc',
'src/connection_wrap.cc',
Oops, something went wrong.

0 comments on commit cb3d049

Please sign in to comment.
You can’t perform that action at this time.