diff options
Diffstat (limited to 'lib/internal/process/per_thread.js')
-rw-r--r-- | lib/internal/process/per_thread.js | 141 |
1 files changed, 56 insertions, 85 deletions
diff --git a/lib/internal/process/per_thread.js b/lib/internal/process/per_thread.js index 57cc9c3814..f1629e3a97 100644 --- a/lib/internal/process/per_thread.js +++ b/lib/internal/process/per_thread.js @@ -18,25 +18,30 @@ const { } = require('internal/errors'); const util = require('util'); const constants = internalBinding('constants').os.signals; -const { deprecate } = require('internal/util'); - -function setupAssert() { - process.assert = deprecate( - function(x, msg) { - if (!x) throw new ERR_ASSERTION(msg || 'assertion error'); - }, - 'process.assert() is deprecated. Please use the `assert` module instead.', - 'DEP0100'); + +function assert(x, msg) { + if (!x) throw new ERR_ASSERTION(msg || 'assertion error'); } -// Set up the process.cpuUsage() function. -function setupCpuUsage(_cpuUsage) { +// The execution of this function itself should not cause any side effects. +function wrapProcessMethods(binding, exceptionHandlerState) { + const { + hrtime: _hrtime, + hrtimeBigInt: _hrtimeBigInt, + cpuUsage: _cpuUsage, + memoryUsage: _memoryUsage + } = binding; + + function _rawDebug(...args) { + binding._rawDebug(util.format.apply(null, args)); + } + // Create the argument array that will be passed to the native function. const cpuValues = new Float64Array(2); // Replace the native function with the JS version that calls the native // function. - process.cpuUsage = function cpuUsage(prevValue) { + function cpuUsage(prevValue) { // If a previous value was passed in, ensure it has the correct shape. if (prevValue) { if (!previousValueIsValid(prevValue.user)) { @@ -80,7 +85,7 @@ function setupCpuUsage(_cpuUsage) { user: cpuValues[0], system: cpuValues[1] }; - }; + } // Ensure that a previously passed in value is valid. Currently, the native // implementation always returns numbers <= Number.MAX_SAFE_INTEGER. @@ -89,15 +94,13 @@ function setupCpuUsage(_cpuUsage) { num <= Number.MAX_SAFE_INTEGER && num >= 0; } -} -// 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 setupHrtime(_hrtime, _hrtimeBigInt) { + // 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. const hrValues = new Uint32Array(3); - process.hrtime = function hrtime(time) { + function hrtime(time) { _hrtime(hrValues); if (time !== undefined) { @@ -118,21 +121,18 @@ function setupHrtime(_hrtime, _hrtimeBigInt) { hrValues[0] * 0x100000000 + hrValues[1], hrValues[2] ]; - }; + } // Use a BigUint64Array in the closure because V8 does not have an API for // creating a BigInt out of a uint64_t yet. const hrBigintValues = new BigUint64Array(1); - process.hrtime.bigint = function() { + function hrtimeBigInt() { _hrtimeBigInt(hrBigintValues); return hrBigintValues[0]; - }; -} + } -function setupMemoryUsage(_memoryUsage) { const memValues = new Float64Array(4); - - process.memoryUsage = function memoryUsage() { + function memoryUsage() { _memoryUsage(memValues); return { rss: memValues[0], @@ -140,18 +140,9 @@ function setupMemoryUsage(_memoryUsage) { heapUsed: memValues[2], external: memValues[3] }; - }; -} - -function setupConfig() { - // Serialized config.gypi - process.config = JSON.parse(internalBinding('native_module').config); -} - - -function setupKillAndExit() { + } - process.exit = function(code) { + function exit(code) { if (code || code === 0) process.exitCode = code; @@ -159,10 +150,10 @@ function setupKillAndExit() { process._exiting = true; process.emit('exit', process.exitCode || 0); } - process.reallyExit(process.exitCode || 0); - }; + binding.reallyExit(process.exitCode || 0); + } - process.kill = function(pid, sig) { + function kill(pid, sig) { var err; if (process.env.NODE_V8_COVERAGE) { const { writeCoverage } = require('internal/process/coverage'); @@ -176,6 +167,8 @@ function setupKillAndExit() { // preserve null signal if (sig === (sig | 0)) { + // XXX(joyeecheung): we have to use process._kill here because + // it's monkey-patched by tests. err = process._kill(pid, sig); } else { sig = sig || 'SIGTERM'; @@ -190,22 +183,13 @@ function setupKillAndExit() { throw errnoException(err, 'kill'); return true; - }; -} - -function setupRawDebug(_rawDebug) { - process._rawDebug = function() { - _rawDebug(util.format.apply(null, arguments)); - }; -} - + } -function setupUncaughtExceptionCapture(exceptionHandlerState, - shouldAbortOnUncaughtToggle) { // shouldAbortOnUncaughtToggle is a typed array for faster // communication with JS. + const { shouldAbortOnUncaughtToggle } = binding; - process.setUncaughtExceptionCaptureCallback = function(fn) { + function setUncaughtExceptionCaptureCallback(fn) { if (fn === null) { exceptionHandlerState.captureFn = fn; shouldAbortOnUncaughtToggle[0] = 1; @@ -219,10 +203,22 @@ function setupUncaughtExceptionCapture(exceptionHandlerState, } exceptionHandlerState.captureFn = fn; shouldAbortOnUncaughtToggle[0] = 0; - }; + } - process.hasUncaughtExceptionCaptureCallback = function() { + function hasUncaughtExceptionCaptureCallback() { return exceptionHandlerState.captureFn !== null; + } + + return { + _rawDebug, + hrtime, + hrtimeBigInt, + cpuUsage, + memoryUsage, + kill, + exit, + setUncaughtExceptionCaptureCallback, + hasUncaughtExceptionCaptureCallback }; } @@ -326,38 +322,13 @@ function buildAllowedFlags() { Object.freeze(NodeEnvironmentFlagsSet.prototype.constructor); Object.freeze(NodeEnvironmentFlagsSet.prototype); - return process.allowedNodeEnvironmentFlags = Object.freeze( - new NodeEnvironmentFlagsSet( - allowedNodeEnvironmentFlags - )); -} - -function setupAllowedFlags() { - Object.defineProperty(process, 'allowedNodeEnvironmentFlags', { - get: buildAllowedFlags, - set(value) { - // If the user tries to set this to another value, override - // this completely to that value. - Object.defineProperty(this, 'allowedNodeEnvironmentFlags', { - value, - configurable: true, - enumerable: true, - writable: true - }); - }, - enumerable: true, - configurable: true - }); + return Object.freeze(new NodeEnvironmentFlagsSet( + allowedNodeEnvironmentFlags + )); } module.exports = { - setupAllowedFlags, - setupAssert, - setupCpuUsage, - setupHrtime, - setupMemoryUsage, - setupConfig, - setupKillAndExit, - setupRawDebug, - setupUncaughtExceptionCapture + assert, + buildAllowedFlags, + wrapProcessMethods }; |