@@ -4,7 +4,6 @@ var os = require('os');
4
4
var sleep = require ( 'sleep' ) ;
5
5
6
6
var tty = require ( 'tty' ) ;
7
- var nqpIo = require ( 'nqp-js-io' ) ;
8
7
9
8
var Hash = require ( './hash.js' ) ;
10
9
@@ -17,35 +16,54 @@ var Null = require('./null.js');
17
16
18
17
var mkdirp = require ( 'mkdirp' ) ;
19
18
19
+ const NQPException = require ( './nqp-exception.js' ) ;
20
+
20
21
var nqp = require ( 'nqp-runtime' ) ;
21
22
22
- nqpIo . SyncPipe . prototype . $$decont = function ( ctx ) {
23
- return this ;
24
- } ;
23
+ class SyncPipe extends NQPObject {
24
+ $$eoffh ( ) {
25
+ if ( this . $$buffer ) {
26
+ return ( this . $$buffer . length ? 0 : 1 ) ;
27
+ } else {
28
+ throw new NQPException ( `Can't use eoffh, this syncpipe is not connected yet` ) ;
29
+ }
30
+ }
25
31
26
- nqpIo . SyncPipe . prototype . $$can = function ( method ) {
27
- return 0 ;
28
- } ;
32
+ /* TODO: optimize to use the lowlevel Buffer inside the highlevel one without copying */
29
33
30
- nqpIo . SyncPipe . prototype . $$readfh = function ( buf , size ) {
31
- let lowlevel = this . $$readBuffer ( size ) ;
34
+ $$readfh ( buf , size ) {
35
+ if ( ! this . $$buffer ) {
36
+ throw new NQPException ( `Can't use readfh, this syncpipe is not connected yet` ) ;
37
+ }
32
38
33
- let elementSize = core . byteSize ( buf ) ;
39
+ let lowlevel = this . $$buffer . slice ( 0 , size ) ;
40
+ this . $$buffer = this . $$buffer . slice ( size ) ;
34
41
35
- let isUnsigned = buf . _STable . REPR . type . _STable . REPR . isUnsigned ;
42
+ let elementSize = core . byteSize ( buf ) ;
43
+
44
+ let isUnsigned = buf . _STable . REPR . type . _STable . REPR . isUnsigned ;
36
45
37
- if ( lowlevel ) {
38
- let offset = 0 ;
39
- buf . array . length = lowlevel . length / elementSize ;
40
- for ( var i = 0 ; i < lowlevel . length / elementSize ; i ++ ) {
41
- buf . array [ i ] = isUnsigned ? lowlevel . readUIntLE ( offset , elementSize ) : lowlevel . readIntLE ( offset , elementSize ) ;
42
- offset += elementSize ;
46
+ if ( lowlevel ) {
47
+ let offset = 0 ;
48
+ buf . array . length = lowlevel . length / elementSize ;
49
+ for ( var i = 0 ; i < lowlevel . length / elementSize ; i ++ ) {
50
+ buf . array [ i ] = isUnsigned ? lowlevel . readUIntLE ( offset , elementSize ) : lowlevel . readIntLE ( offset , elementSize ) ;
51
+ offset += elementSize ;
52
+ }
53
+ } else {
54
+ buf . array . length = 0 ;
43
55
}
44
- } else {
45
- buf . array . length = 0 ;
56
+
57
+ return buf ;
46
58
}
47
59
48
- return buf ;
60
+ $$closefh_i ( ) {
61
+ return this . $$status ;
62
+ }
63
+
64
+ $$can ( ) {
65
+ return 0 ;
66
+ }
49
67
} ;
50
68
51
69
function boolish ( bool ) {
@@ -201,8 +219,9 @@ class FileHandle extends IOHandle {
201
219
this . fd = fd ;
202
220
}
203
221
204
- $$closefh ( ) {
222
+ $$closefh_i ( ) {
205
223
fs . closeSync ( this . fd ) ;
224
+ return 0 ;
206
225
}
207
226
208
227
$$isttyfh ( ) {
@@ -317,21 +336,11 @@ op.seekfh = function(ctx, fh, offset, whence) {
317
336
} ;
318
337
319
338
op . closefh = function ( fh ) {
320
- if ( fh instanceof nqpIo . SyncPipe ) {
321
- fh . close ( ) ;
322
- return fh ;
323
- }
324
- fh . $$closefh ( ) ;
325
- return fh ;
339
+ fh . $$closefh_i ( ) ;
326
340
} ;
327
341
328
342
op . closefh_i = function ( fh ) {
329
- if ( fh instanceof nqpIo . SyncPipe ) {
330
- return fh . close ( ) ;
331
- }
332
- op . closefh ( fh ) ;
333
- /* TODO proper return value */
334
- return 0 ;
343
+ return fh . $$closefh_i ( ) ;
335
344
} ;
336
345
337
346
@@ -415,22 +424,57 @@ function stringifyEnv(ctx, hash) {
415
424
return stringifed ;
416
425
}
417
426
418
- op . spawn = function ( ctx , command , dir , env , input , output , error , flags ) {
427
+ function stringifyArray ( ctx , array ) {
419
428
let stringified = [ ] ;
420
- for ( let c of command . array ) {
421
- stringified . push ( nqp . toStr ( c , ctx ) ) ;
429
+ for ( let element of array . array ) {
430
+ stringified . push ( nqp . toStr ( element , ctx ) ) ;
422
431
}
432
+ return stringified ;
433
+ }
423
434
424
- return nqpIo . spawn ( stringified , dir , stringifyEnv ( ctx , env ) , convertNull ( input ) , convertNull ( output ) , convertNull ( error ) , flags ) ;
435
+ op . syncpipe = function ( ) {
436
+ return new SyncPipe ( ) ;
425
437
} ;
426
438
439
+ function run ( isShell , ctx , command , dir , env , input , output , error , flags ) {
440
+ const options = {
441
+ shell : isShell ,
442
+ cwd : dir ,
443
+ env : stringifyEnv ( ctx , env ) ,
444
+ stdio : [ process . stdin , 'pipe' , 'pipe' ]
445
+ } ;
427
446
428
- op . syncpipe = function ( ) {
429
- return new nqpIo . SyncPipe ( ) ;
447
+ let result ;
448
+ if ( isShell ) {
449
+ result = child_process . spawnSync ( command , options ) ;
450
+ } else {
451
+ let stringified = stringifyArray ( ctx , command ) ;
452
+ result = child_process . spawnSync ( stringified . shift ( ) , stringified , options ) ;
453
+ }
454
+
455
+ if ( flags & PIPE_CAPTURE_IN ) {
456
+ throw new NQPException ( 'nqp::shell with PIPE_CAPTURE_IN NYI' ) ;
457
+ }
458
+
459
+ if ( flags & PIPE_CAPTURE_OUT ) {
460
+ output . $$buffer = result . output [ 1 ] ;
461
+ output . $$status = result . status ;
462
+ }
463
+
464
+ if ( flags & PIPE_CAPTURE_ERR ) {
465
+ error . $$buffer = result . output [ 2 ] ;
466
+ error . $$status = result . status ;
467
+ }
468
+
469
+ return result . status ;
430
470
} ;
431
471
432
472
op . shell = function ( ctx , command , dir , env , input , output , error , flags ) {
433
- return nqpIo . shell ( command , dir , stringifyEnv ( ctx , env ) , convertNull ( input ) , convertNull ( output ) , convertNull ( error ) , flags ) ;
473
+ return run ( true , ctx , command , dir , env , input , output , error , flags ) ;
474
+ } ;
475
+
476
+ op . spawn = function ( ctx , command , dir , env , input , output , error , flags ) {
477
+ return run ( false , ctx , command , dir , env , input , output , error , flags ) ;
434
478
} ;
435
479
436
480
op . cwd = function ( ) {
0 commit comments