Skip to content
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
38 changes: 24 additions & 14 deletions lib/internal/bootstrap/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,16 @@
// process.binding(), process._linkedBinding(), internalBinding() and
// NativeModule. And then { internalBinding, NativeModule } will be passed
// into this bootstrapper to bootstrap Node.js core.

//
// The bootstrapper argument is an object containing functions and properties
// that are only relevant during the bootstrap process. A permanent reference
// to this object must not be maintained, allowing the object to gc when it is
// no longer needed.
'use strict';

(function bootstrapNodeJSCore(process, { internalBinding, NativeModule }) {
(function bootstrapNodeJSCore(process,
bootstrapper,
{ internalBinding, NativeModule }) {
const exceptionHandlerState = { captureFn: null };

function startup() {
Expand All @@ -36,11 +42,11 @@
const _process = NativeModule.require('internal/process');
_process.setupConfig(NativeModule._source);
_process.setupSignalHandlers();
_process.setupUncaughtExceptionCapture(exceptionHandlerState);
_process.setupUncaughtExceptionCapture(exceptionHandlerState, bootstrapper);
NativeModule.require('internal/process/warning').setup();
NativeModule.require('internal/process/next_tick').setup();
NativeModule.require('internal/process/next_tick').setup(bootstrapper);
NativeModule.require('internal/process/stdio').setup();
NativeModule.require('internal/process/methods').setup();
NativeModule.require('internal/process/methods').setup(bootstrapper);

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

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

_process.setupChannel();
_process.setupRawDebug();
_process.setupRawDebug(bootstrapper);

const browserGlobals = !process._noBrowserGlobals;
if (browserGlobals) {
Expand Down Expand Up @@ -116,9 +122,14 @@
'DeprecationWarning', 'DEP0062', startup, true);
}

if (process.binding('config').experimentalModules ||
process.binding('config').experimentalVMModules) {
if (process.binding('config').experimentalModules) {
const {
experimentalModules,
experimentalVMModules,
pendingDeprecation
} = process.binding('config');

if (experimentalModules || experimentalVMModules) {
if (experimentalModules) {
process.emitWarning(
'The ESM module loader is experimental.',
'ExperimentalWarning', undefined);
Expand All @@ -129,7 +140,6 @@
{
// Install legacy getters on the `util` binding for typechecking.
// TODO(addaleax): Turn into a full runtime deprecation.
const { pendingDeprecation } = process.binding('config');
const { deprecate } = NativeModule.require('internal/util');
const utilBinding = process.binding('util');
const types = internalBinding('types');
Expand Down Expand Up @@ -294,7 +304,7 @@
}

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

function pushValueToArray() {
for (var i = 0; i < arguments.length; i++)
Expand Down
31 changes: 12 additions & 19 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,23 +240,21 @@ 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 }) {
// _shouldAbortOnUncaughtToggleis a typed array for faster
// communication with JS.
process.setUncaughtExceptionCaptureCallback = function(fn) {
if (fn === null) {
exceptionHandlerState.captureFn = fn;
shouldAbortOnUncaughtToggle[0] = 1;
_shouldAbortOnUncaughtToggle[0] = 1;
return;
}
if (typeof fn !== 'function') {
Expand All @@ -271,7 +264,7 @@ function setupUncaughtExceptionCapture(exceptionHandlerState) {
throw new ERR_UNCAUGHT_EXCEPTION_CAPTURE_ALREADY_SET();
}
exceptionHandlerState.captureFn = fn;
shouldAbortOnUncaughtToggle[0] = 0;
_shouldAbortOnUncaughtToggle[0] = 0;
};

process.hasUncaughtExceptionCaptureCallback = function() {
Expand Down
69 changes: 27 additions & 42 deletions lib/internal/process/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,55 +9,40 @@ const {
validateUint32
} = require('internal/validators');

function setupProcessMethods() {
function setupProcessMethods(bootstrapper) {
// Non-POSIX platforms like Windows don't have certain methods.
if (process.setgid !== undefined) {
setupPosixMethods();
if (bootstrapper._setgid !== undefined) {
setupPosixMethods(bootstrapper);
}

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

process.chdir = chdir;
process.umask = umask;
setupChdir(bootstrapper);
setupUmask(bootstrapper);
}

function chdir(directory) {
function setupChdir({ _chdir }) {
process.chdir = function chdir(directory) {
if (typeof directory !== 'string') {
throw new ERR_INVALID_ARG_TYPE('directory', 'string', directory);
}
return _chdir(directory);
}
};
}

function umask(mask) {
function setupUmask({ _umask }) {
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 +52,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 +83,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
3 changes: 3 additions & 0 deletions node.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@
'src/node_api.cc',
'src/node_api.h',
'src/node_api_types.h',
'src/node_bootstrap.cc',
'src/node_buffer.cc',
'src/node_config.cc',
'src/node_constants.cc',
Expand All @@ -336,6 +337,7 @@
'src/node_platform.cc',
'src/node_perf.cc',
'src/node_postmortem_metadata.cc',
'src/node_process.cc',
'src/node_serdes.cc',
'src/node_trace_events.cc',
'src/node_types.cc',
Expand Down Expand Up @@ -395,6 +397,7 @@
'src/node_perf_common.h',
'src/node_persistent.h',
'src/node_platform.h',
'src/node_process.h',
'src/node_root_certs.h',
'src/node_version.h',
'src/node_watchdog.h',
Expand Down
Loading