Browse Source

child_process: minor cleanup of internals

This commit removes self = this style assignments and replaces
some array iteration functions with a simpler for loop.

PR-URL: https://github.com/nodejs/node/pull/12348
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
v6
cjihrig 8 years ago
parent
commit
a9111f9738
  1. 81
      lib/internal/child_process.js

81
lib/internal/child_process.js

@ -157,8 +157,6 @@ const handleConversion = {
function ChildProcess() { function ChildProcess() {
EventEmitter.call(this); EventEmitter.call(this);
var self = this;
this._closesNeeded = 1; this._closesNeeded = 1;
this._closesGot = 0; this._closesGot = 0;
this.connected = false; this.connected = false;
@ -171,41 +169,31 @@ function ChildProcess() {
this._handle = new Process(); this._handle = new Process();
this._handle.owner = this; this._handle.owner = this;
this._handle.onexit = function(exitCode, signalCode) { this._handle.onexit = (exitCode, signalCode) => {
//
// follow 0.4.x behaviour:
//
// - normally terminated processes don't touch this.signalCode
// - signaled processes don't touch this.exitCode
//
// new in 0.9.x:
//
// - spawn failures are reported with exitCode < 0
//
var syscall = self.spawnfile ? 'spawn ' + self.spawnfile : 'spawn';
var err = (exitCode < 0) ? errnoException(exitCode, syscall) : null;
if (signalCode) { if (signalCode) {
self.signalCode = signalCode; this.signalCode = signalCode;
} else { } else {
self.exitCode = exitCode; this.exitCode = exitCode;
} }
if (self.stdin) { if (this.stdin) {
self.stdin.destroy(); this.stdin.destroy();
} }
self._handle.close(); this._handle.close();
self._handle = null; this._handle = null;
if (exitCode < 0) { if (exitCode < 0) {
if (self.spawnfile) var syscall = this.spawnfile ? 'spawn ' + this.spawnfile : 'spawn';
err.path = self.spawnfile; const err = errnoException(exitCode, syscall);
if (this.spawnfile)
err.path = this.spawnfile;
err.spawnargs = self.spawnargs.slice(1); err.spawnargs = this.spawnargs.slice(1);
self.emit('error', err); this.emit('error', err);
} else { } else {
self.emit('exit', self.exitCode, self.signalCode); this.emit('exit', this.exitCode, this.signalCode);
} }
// if any of the stdio streams have not been touched, // if any of the stdio streams have not been touched,
@ -214,9 +202,9 @@ function ChildProcess() {
// Do it on nextTick so that the user has one last chance // Do it on nextTick so that the user has one last chance
// to consume the output, if for example they only want to // to consume the output, if for example they only want to
// start reading the data once the process exits. // start reading the data once the process exits.
process.nextTick(flushStdio, self); process.nextTick(flushStdio, this);
maybeClose(self); maybeClose(this);
}; };
} }
util.inherits(ChildProcess, EventEmitter); util.inherits(ChildProcess, EventEmitter);
@ -262,10 +250,10 @@ function getHandleWrapType(stream) {
ChildProcess.prototype.spawn = function(options) { ChildProcess.prototype.spawn = function(options) {
const self = this;
var ipc; var ipc;
var ipcFd; var ipcFd;
var i; var i;
// If no `stdio` option was given - use default // If no `stdio` option was given - use default
var stdio = options.stdio || 'pipe'; var stdio = options.stdio || 'pipe';
@ -291,7 +279,7 @@ ChildProcess.prototype.spawn = function(options) {
err === uv.UV_EMFILE || err === uv.UV_EMFILE ||
err === uv.UV_ENFILE || err === uv.UV_ENFILE ||
err === uv.UV_ENOENT) { err === uv.UV_ENOENT) {
process.nextTick(onErrorNT, self, err); process.nextTick(onErrorNT, this, err);
// There is no point in continuing when we've hit EMFILE or ENFILE // There is no point in continuing when we've hit EMFILE or ENFILE
// because we won't be able to set up the stdio file descriptors. // because we won't be able to set up the stdio file descriptors.
// It's kind of silly that the de facto spec for ENOENT (the test suite) // It's kind of silly that the de facto spec for ENOENT (the test suite)
@ -319,20 +307,20 @@ ChildProcess.prototype.spawn = function(options) {
if (stream.type === 'ignore') continue; if (stream.type === 'ignore') continue;
if (stream.ipc) { if (stream.ipc) {
self._closesNeeded++; this._closesNeeded++;
continue; continue;
} }
if (stream.handle) { if (stream.handle) {
// when i === 0 - we're dealing with stdin // when i === 0 - we're dealing with stdin
// (which is the only one writable pipe) // (which is the only one writable pipe)
stream.socket = createSocket(self.pid !== 0 ? stream.socket = createSocket(this.pid !== 0 ?
stream.handle : null, i > 0); stream.handle : null, i > 0);
if (i > 0 && self.pid !== 0) { if (i > 0 && this.pid !== 0) {
self._closesNeeded++; this._closesNeeded++;
stream.socket.on('close', function() { stream.socket.on('close', () => {
maybeClose(self); maybeClose(this);
}); });
} }
} }
@ -345,9 +333,10 @@ ChildProcess.prototype.spawn = function(options) {
this.stderr = stdio.length >= 3 && stdio[2].socket !== undefined ? this.stderr = stdio.length >= 3 && stdio[2].socket !== undefined ?
stdio[2].socket : null; stdio[2].socket : null;
this.stdio = stdio.map(function(stdio) { this.stdio = [];
return stdio.socket === undefined ? null : stdio.socket;
}); for (i = 0; i < stdio.length; i++)
this.stdio.push(stdio[i].socket === undefined ? null : stdio[i].socket);
// Add .send() method and start listening for IPC data // Add .send() method and start listening for IPC data
if (ipc !== undefined) setupChannel(this, ipc); if (ipc !== undefined) setupChannel(this, ipc);
@ -778,12 +767,10 @@ function _validateStdio(stdio, sync) {
// (i.e. PipeWraps or fds) // (i.e. PipeWraps or fds)
stdio = stdio.reduce(function(acc, stdio, i) { stdio = stdio.reduce(function(acc, stdio, i) {
function cleanup() { function cleanup() {
acc.filter(function(stdio) { for (var i = 0; i < acc.length; i++) {
return stdio.type === 'pipe' || stdio.type === 'ipc'; if ((acc[i].type === 'pipe' || acc[i].type === 'ipc') && acc[i].handle)
}).forEach(function(stdio) { acc[i].handle.close();
if (stdio.handle) }
stdio.handle.close();
});
} }
// Defaults // Defaults
@ -860,7 +847,7 @@ function _validateStdio(stdio, sync) {
return acc; return acc;
}, []); }, []);
return {stdio: stdio, ipc: ipc, ipcFd: ipcFd}; return { stdio, ipc, ipcFd };
} }

Loading…
Cancel
Save