@@ -157,8 +157,6 @@ const handleConversion = {
157
157
function ChildProcess ( ) {
158
158
EventEmitter . call ( this ) ;
159
159
160
- var self = this ;
161
-
162
160
this . _closesNeeded = 1 ;
163
161
this . _closesGot = 0 ;
164
162
this . connected = false ;
@@ -171,41 +169,31 @@ function ChildProcess() {
171
169
this . _handle = new Process ( ) ;
172
170
this . _handle . owner = this ;
173
171
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 ) => {
188
173
if ( signalCode ) {
189
- self . signalCode = signalCode ;
174
+ this . signalCode = signalCode ;
190
175
} else {
191
- self . exitCode = exitCode ;
176
+ this . exitCode = exitCode ;
192
177
}
193
178
194
- if ( self . stdin ) {
195
- self . stdin . destroy ( ) ;
179
+ if ( this . stdin ) {
180
+ this . stdin . destroy ( ) ;
196
181
}
197
182
198
- self . _handle . close ( ) ;
199
- self . _handle = null ;
183
+ this . _handle . close ( ) ;
184
+ this . _handle = null ;
200
185
201
186
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 ;
204
192
205
- err . spawnargs = self . spawnargs . slice ( 1 ) ;
206
- self . emit ( 'error' , err ) ;
193
+ err . spawnargs = this . spawnargs . slice ( 1 ) ;
194
+ this . emit ( 'error' , err ) ;
207
195
} else {
208
- self . emit ( 'exit' , self . exitCode , self . signalCode ) ;
196
+ this . emit ( 'exit' , this . exitCode , this . signalCode ) ;
209
197
}
210
198
211
199
// if any of the stdio streams have not been touched,
@@ -214,9 +202,9 @@ function ChildProcess() {
214
202
// Do it on nextTick so that the user has one last chance
215
203
// to consume the output, if for example they only want to
216
204
// start reading the data once the process exits.
217
- process . nextTick ( flushStdio , self ) ;
205
+ process . nextTick ( flushStdio , this ) ;
218
206
219
- maybeClose ( self ) ;
207
+ maybeClose ( this ) ;
220
208
} ;
221
209
}
222
210
util . inherits ( ChildProcess , EventEmitter ) ;
@@ -262,10 +250,10 @@ function getHandleWrapType(stream) {
262
250
263
251
264
252
ChildProcess . prototype . spawn = function ( options ) {
265
- const self = this ;
266
253
var ipc ;
267
254
var ipcFd ;
268
255
var i ;
256
+
269
257
// If no `stdio` option was given - use default
270
258
var stdio = options . stdio || 'pipe' ;
271
259
@@ -291,7 +279,7 @@ ChildProcess.prototype.spawn = function(options) {
291
279
err === uv . UV_EMFILE ||
292
280
err === uv . UV_ENFILE ||
293
281
err === uv . UV_ENOENT ) {
294
- process . nextTick ( onErrorNT , self , err ) ;
282
+ process . nextTick ( onErrorNT , this , err ) ;
295
283
// There is no point in continuing when we've hit EMFILE or ENFILE
296
284
// because we won't be able to set up the stdio file descriptors.
297
285
// It's kind of silly that the de facto spec for ENOENT (the test suite)
@@ -319,20 +307,20 @@ ChildProcess.prototype.spawn = function(options) {
319
307
if ( stream . type === 'ignore' ) continue ;
320
308
321
309
if ( stream . ipc ) {
322
- self . _closesNeeded ++ ;
310
+ this . _closesNeeded ++ ;
323
311
continue ;
324
312
}
325
313
326
314
if ( stream . handle ) {
327
315
// when i === 0 - we're dealing with stdin
328
316
// (which is the only one writable pipe)
329
- stream . socket = createSocket ( self . pid !== 0 ?
317
+ stream . socket = createSocket ( this . pid !== 0 ?
330
318
stream . handle : null , i > 0 ) ;
331
319
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 ) ;
336
324
} ) ;
337
325
}
338
326
}
@@ -345,9 +333,10 @@ ChildProcess.prototype.spawn = function(options) {
345
333
this . stderr = stdio . length >= 3 && stdio [ 2 ] . socket !== undefined ?
346
334
stdio [ 2 ] . socket : null ;
347
335
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 ) ;
351
340
352
341
// Add .send() method and start listening for IPC data
353
342
if ( ipc !== undefined ) setupChannel ( this , ipc ) ;
@@ -778,12 +767,10 @@ function _validateStdio(stdio, sync) {
778
767
// (i.e. PipeWraps or fds)
779
768
stdio = stdio . reduce ( function ( acc , stdio , i ) {
780
769
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
+ }
787
774
}
788
775
789
776
// Defaults
@@ -860,7 +847,7 @@ function _validateStdio(stdio, sync) {
860
847
return acc ;
861
848
} , [ ] ) ;
862
849
863
- return { stdio : stdio , ipc : ipc , ipcFd : ipcFd } ;
850
+ return { stdio, ipc, ipcFd } ;
864
851
}
865
852
866
853
0 commit comments