|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +var _lazyConstants = null; |
| 4 | + |
| 5 | +function lazyConstants() { |
| 6 | + if (!_lazyConstants) { |
| 7 | + _lazyConstants = process.binding('constants'); |
| 8 | + } |
| 9 | + return _lazyConstants; |
| 10 | +} |
| 11 | + |
| 12 | +exports.setup_hrtime = setup_hrtime; |
| 13 | +exports.setupConfig = setupConfig; |
| 14 | +exports.setupKillAndExit = setupKillAndExit; |
| 15 | +exports.setupSignalHandlers = setupSignalHandlers; |
| 16 | +exports.setupChannel = setupChannel; |
| 17 | +exports.setupRawDebug = setupRawDebug; |
| 18 | + |
| 19 | + |
| 20 | +const assert = process.assert = function(x, msg) { |
| 21 | + if (!x) throw new Error(msg || 'assertion error'); |
| 22 | +}; |
| 23 | + |
| 24 | + |
| 25 | +function setup_hrtime() { |
| 26 | + const _hrtime = process.hrtime; |
| 27 | + const hrValues = new Uint32Array(3); |
| 28 | + |
| 29 | + process.hrtime = function hrtime(ar) { |
| 30 | + _hrtime(hrValues); |
| 31 | + |
| 32 | + if (typeof ar !== 'undefined') { |
| 33 | + if (Array.isArray(ar)) { |
| 34 | + const sec = (hrValues[0] * 0x100000000 + hrValues[1]) - ar[0]; |
| 35 | + const nsec = hrValues[2] - ar[1]; |
| 36 | + return [nsec < 0 ? sec - 1 : sec, nsec < 0 ? nsec + 1e9 : nsec]; |
| 37 | + } |
| 38 | + |
| 39 | + throw new TypeError('process.hrtime() only accepts an Array tuple'); |
| 40 | + } |
| 41 | + |
| 42 | + return [ |
| 43 | + hrValues[0] * 0x100000000 + hrValues[1], |
| 44 | + hrValues[2] |
| 45 | + ]; |
| 46 | + }; |
| 47 | +} |
| 48 | + |
| 49 | + |
| 50 | +function setupConfig(_source) { |
| 51 | + // NativeModule._source |
| 52 | + // used for `process.config`, but not a real module |
| 53 | + var config = _source.config; |
| 54 | + delete _source.config; |
| 55 | + |
| 56 | + // strip the gyp comment line at the beginning |
| 57 | + config = config.split('\n') |
| 58 | + .slice(1) |
| 59 | + .join('\n') |
| 60 | + .replace(/"/g, '\\"') |
| 61 | + .replace(/'/g, '"'); |
| 62 | + |
| 63 | + process.config = JSON.parse(config, function(key, value) { |
| 64 | + if (value === 'true') return true; |
| 65 | + if (value === 'false') return false; |
| 66 | + return value; |
| 67 | + }); |
| 68 | +} |
| 69 | + |
| 70 | + |
| 71 | +function setupKillAndExit() { |
| 72 | + |
| 73 | + process.exit = function(code) { |
| 74 | + if (code || code === 0) |
| 75 | + process.exitCode = code; |
| 76 | + |
| 77 | + if (!process._exiting) { |
| 78 | + process._exiting = true; |
| 79 | + process.emit('exit', process.exitCode || 0); |
| 80 | + } |
| 81 | + process.reallyExit(process.exitCode || 0); |
| 82 | + }; |
| 83 | + |
| 84 | + process.kill = function(pid, sig) { |
| 85 | + var err; |
| 86 | + |
| 87 | + if (pid != (pid | 0)) { |
| 88 | + throw new TypeError('invalid pid'); |
| 89 | + } |
| 90 | + |
| 91 | + // preserve null signal |
| 92 | + if (0 === sig) { |
| 93 | + err = process._kill(pid, 0); |
| 94 | + } else { |
| 95 | + sig = sig || 'SIGTERM'; |
| 96 | + if (lazyConstants()[sig] && |
| 97 | + sig.slice(0, 3) === 'SIG') { |
| 98 | + err = process._kill(pid, lazyConstants()[sig]); |
| 99 | + } else { |
| 100 | + throw new Error(`Unknown signal: ${sig}`); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + if (err) { |
| 105 | + var errnoException = require('util')._errnoException; |
| 106 | + throw errnoException(err, 'kill'); |
| 107 | + } |
| 108 | + |
| 109 | + return true; |
| 110 | + }; |
| 111 | +} |
| 112 | + |
| 113 | + |
| 114 | +function setupSignalHandlers() { |
| 115 | + // Load events module in order to access prototype elements on process like |
| 116 | + // process.addListener. |
| 117 | + var signalWraps = {}; |
| 118 | + |
| 119 | + function isSignal(event) { |
| 120 | + return typeof event === 'string' && |
| 121 | + event.slice(0, 3) === 'SIG' && |
| 122 | + lazyConstants().hasOwnProperty(event); |
| 123 | + } |
| 124 | + |
| 125 | + // Detect presence of a listener for the special signal types |
| 126 | + process.on('newListener', function(type, listener) { |
| 127 | + if (isSignal(type) && |
| 128 | + !signalWraps.hasOwnProperty(type)) { |
| 129 | + var Signal = process.binding('signal_wrap').Signal; |
| 130 | + var wrap = new Signal(); |
| 131 | + |
| 132 | + wrap.unref(); |
| 133 | + |
| 134 | + wrap.onsignal = function() { process.emit(type); }; |
| 135 | + |
| 136 | + var signum = lazyConstants()[type]; |
| 137 | + var err = wrap.start(signum); |
| 138 | + if (err) { |
| 139 | + wrap.close(); |
| 140 | + var errnoException = require('util')._errnoException; |
| 141 | + throw errnoException(err, 'uv_signal_start'); |
| 142 | + } |
| 143 | + |
| 144 | + signalWraps[type] = wrap; |
| 145 | + } |
| 146 | + }); |
| 147 | + |
| 148 | + process.on('removeListener', function(type, listener) { |
| 149 | + if (signalWraps.hasOwnProperty(type) && this.listenerCount(type) === 0) { |
| 150 | + signalWraps[type].close(); |
| 151 | + delete signalWraps[type]; |
| 152 | + } |
| 153 | + }); |
| 154 | +} |
| 155 | + |
| 156 | + |
| 157 | +function setupChannel() { |
| 158 | + // If we were spawned with env NODE_CHANNEL_FD then load that up and |
| 159 | + // start parsing data from that stream. |
| 160 | + if (process.env.NODE_CHANNEL_FD) { |
| 161 | + var fd = parseInt(process.env.NODE_CHANNEL_FD, 10); |
| 162 | + assert(fd >= 0); |
| 163 | + |
| 164 | + // Make sure it's not accidentally inherited by child processes. |
| 165 | + delete process.env.NODE_CHANNEL_FD; |
| 166 | + |
| 167 | + var cp = require('child_process'); |
| 168 | + |
| 169 | + // Load tcp_wrap to avoid situation where we might immediately receive |
| 170 | + // a message. |
| 171 | + // FIXME is this really necessary? |
| 172 | + process.binding('tcp_wrap'); |
| 173 | + |
| 174 | + cp._forkChild(fd); |
| 175 | + assert(process.send); |
| 176 | + } |
| 177 | +} |
| 178 | + |
| 179 | + |
| 180 | +function setupRawDebug() { |
| 181 | + var format = require('util').format; |
| 182 | + var rawDebug = process._rawDebug; |
| 183 | + process._rawDebug = function() { |
| 184 | + rawDebug(format.apply(null, arguments)); |
| 185 | + }; |
| 186 | +} |
0 commit comments