Skip to content

Commit a9111f9

Browse files
committed
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: #12348 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
1 parent d06eb53 commit a9111f9

File tree

1 file changed

+34
-47
lines changed

1 file changed

+34
-47
lines changed

lib/internal/child_process.js

+34-47
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,6 @@ const handleConversion = {
157157
function ChildProcess() {
158158
EventEmitter.call(this);
159159

160-
var self = this;
161-
162160
this._closesNeeded = 1;
163161
this._closesGot = 0;
164162
this.connected = false;
@@ -171,41 +169,31 @@ function ChildProcess() {
171169
this._handle = new Process();
172170
this._handle.owner = this;
173171

174-
this._handle.onexit = function(exitCode, signalCode) {
175-
//
176-
// follow 0.4.x behaviour:
177-
//
178-
// - normally terminated processes don't touch this.signalCode
179-
// - signaled processes don't touch this.exitCode
180-
//
181-
// new in 0.9.x:
182-
//
183-
// - spawn failures are reported with exitCode < 0
184-
//
185-
var syscall = self.spawnfile ? 'spawn ' + self.spawnfile : 'spawn';
186-
var err = (exitCode < 0) ? errnoException(exitCode, syscall) : null;
187-
172+
this._handle.onexit = (exitCode, signalCode) => {
188173
if (signalCode) {
189-
self.signalCode = signalCode;
174+
this.signalCode = signalCode;
190175
} else {
191-
self.exitCode = exitCode;
176+
this.exitCode = exitCode;
192177
}
193178

194-
if (self.stdin) {
195-
self.stdin.destroy();
179+
if (this.stdin) {
180+
this.stdin.destroy();
196181
}
197182

198-
self._handle.close();
199-
self._handle = null;
183+
this._handle.close();
184+
this._handle = null;
200185

201186
if (exitCode < 0) {
202-
if (self.spawnfile)
203-
err.path = self.spawnfile;
187+
var syscall = this.spawnfile ? 'spawn ' + this.spawnfile : 'spawn';
188+
const err = errnoException(exitCode, syscall);
189+
190+
if (this.spawnfile)
191+
err.path = this.spawnfile;
204192

205-
err.spawnargs = self.spawnargs.slice(1);
206-
self.emit('error', err);
193+
err.spawnargs = this.spawnargs.slice(1);
194+
this.emit('error', err);
207195
} else {
208-
self.emit('exit', self.exitCode, self.signalCode);
196+
this.emit('exit', this.exitCode, this.signalCode);
209197
}
210198

211199
// if any of the stdio streams have not been touched,
@@ -214,9 +202,9 @@ function ChildProcess() {
214202
// Do it on nextTick so that the user has one last chance
215203
// to consume the output, if for example they only want to
216204
// start reading the data once the process exits.
217-
process.nextTick(flushStdio, self);
205+
process.nextTick(flushStdio, this);
218206

219-
maybeClose(self);
207+
maybeClose(this);
220208
};
221209
}
222210
util.inherits(ChildProcess, EventEmitter);
@@ -262,10 +250,10 @@ function getHandleWrapType(stream) {
262250

263251

264252
ChildProcess.prototype.spawn = function(options) {
265-
const self = this;
266253
var ipc;
267254
var ipcFd;
268255
var i;
256+
269257
// If no `stdio` option was given - use default
270258
var stdio = options.stdio || 'pipe';
271259

@@ -291,7 +279,7 @@ ChildProcess.prototype.spawn = function(options) {
291279
err === uv.UV_EMFILE ||
292280
err === uv.UV_ENFILE ||
293281
err === uv.UV_ENOENT) {
294-
process.nextTick(onErrorNT, self, err);
282+
process.nextTick(onErrorNT, this, err);
295283
// There is no point in continuing when we've hit EMFILE or ENFILE
296284
// because we won't be able to set up the stdio file descriptors.
297285
// It's kind of silly that the de facto spec for ENOENT (the test suite)
@@ -319,20 +307,20 @@ ChildProcess.prototype.spawn = function(options) {
319307
if (stream.type === 'ignore') continue;
320308

321309
if (stream.ipc) {
322-
self._closesNeeded++;
310+
this._closesNeeded++;
323311
continue;
324312
}
325313

326314
if (stream.handle) {
327315
// when i === 0 - we're dealing with stdin
328316
// (which is the only one writable pipe)
329-
stream.socket = createSocket(self.pid !== 0 ?
317+
stream.socket = createSocket(this.pid !== 0 ?
330318
stream.handle : null, i > 0);
331319

332-
if (i > 0 && self.pid !== 0) {
333-
self._closesNeeded++;
334-
stream.socket.on('close', function() {
335-
maybeClose(self);
320+
if (i > 0 && this.pid !== 0) {
321+
this._closesNeeded++;
322+
stream.socket.on('close', () => {
323+
maybeClose(this);
336324
});
337325
}
338326
}
@@ -345,9 +333,10 @@ ChildProcess.prototype.spawn = function(options) {
345333
this.stderr = stdio.length >= 3 && stdio[2].socket !== undefined ?
346334
stdio[2].socket : null;
347335

348-
this.stdio = stdio.map(function(stdio) {
349-
return stdio.socket === undefined ? null : stdio.socket;
350-
});
336+
this.stdio = [];
337+
338+
for (i = 0; i < stdio.length; i++)
339+
this.stdio.push(stdio[i].socket === undefined ? null : stdio[i].socket);
351340

352341
// Add .send() method and start listening for IPC data
353342
if (ipc !== undefined) setupChannel(this, ipc);
@@ -778,12 +767,10 @@ function _validateStdio(stdio, sync) {
778767
// (i.e. PipeWraps or fds)
779768
stdio = stdio.reduce(function(acc, stdio, i) {
780769
function cleanup() {
781-
acc.filter(function(stdio) {
782-
return stdio.type === 'pipe' || stdio.type === 'ipc';
783-
}).forEach(function(stdio) {
784-
if (stdio.handle)
785-
stdio.handle.close();
786-
});
770+
for (var i = 0; i < acc.length; i++) {
771+
if ((acc[i].type === 'pipe' || acc[i].type === 'ipc') && acc[i].handle)
772+
acc[i].handle.close();
773+
}
787774
}
788775

789776
// Defaults
@@ -860,7 +847,7 @@ function _validateStdio(stdio, sync) {
860847
return acc;
861848
}, []);
862849

863-
return {stdio: stdio, ipc: ipc, ipcFd: ipcFd};
850+
return { stdio, ipc, ipcFd };
864851
}
865852

866853

0 commit comments

Comments
 (0)