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

src: refactor bootstrap to use bootstrap object #20917

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
src: refactor bootstrap to use bootstrap object
  • Loading branch information
jasnell committed May 25, 2018
commit 1c3486ad6a453b5c4eed9f79a8f482b72ad65f82
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
67 changes: 24 additions & 43 deletions lib/internal/process/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);
}
Expand All @@ -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');
Expand Down
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 @@ -309,6 +309,7 @@

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