Skip to content

Commit c27c99f

Browse files
committed
[js] Remove debugging leftover
1 parent 3292466 commit c27c99f

File tree

1 file changed

+84
-40
lines changed
  • src/vm/js/nqp-runtime

1 file changed

+84
-40
lines changed

src/vm/js/nqp-runtime/io.js

Lines changed: 84 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ var os = require('os');
44
var sleep = require('sleep');
55

66
var tty = require('tty');
7-
var nqpIo = require('nqp-js-io');
87

98
var Hash = require('./hash.js');
109

@@ -17,35 +16,54 @@ var Null = require('./null.js');
1716

1817
var mkdirp = require('mkdirp');
1918

19+
const NQPException = require('./nqp-exception.js');
20+
2021
var nqp = require('nqp-runtime');
2122

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+
}
2531

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 */
2933

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+
}
3238

33-
let elementSize = core.byteSize(buf);
39+
let lowlevel = this.$$buffer.slice(0, size);
40+
this.$$buffer = this.$$buffer.slice(size);
3441

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;
3645

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;
4355
}
44-
} else {
45-
buf.array.length = 0;
56+
57+
return buf;
4658
}
4759

48-
return buf;
60+
$$closefh_i() {
61+
return this.$$status;
62+
}
63+
64+
$$can() {
65+
return 0;
66+
}
4967
};
5068

5169
function boolish(bool) {
@@ -201,8 +219,9 @@ class FileHandle extends IOHandle {
201219
this.fd = fd;
202220
}
203221

204-
$$closefh() {
222+
$$closefh_i() {
205223
fs.closeSync(this.fd);
224+
return 0;
206225
}
207226

208227
$$isttyfh() {
@@ -317,21 +336,11 @@ op.seekfh = function(ctx, fh, offset, whence) {
317336
};
318337

319338
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();
326340
};
327341

328342
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();
335344
};
336345

337346

@@ -415,22 +424,57 @@ function stringifyEnv(ctx, hash) {
415424
return stringifed;
416425
}
417426

418-
op.spawn = function(ctx, command, dir, env, input, output, error, flags) {
427+
function stringifyArray(ctx, array) {
419428
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));
422431
}
432+
return stringified;
433+
}
423434

424-
return nqpIo.spawn(stringified, dir, stringifyEnv(ctx, env), convertNull(input), convertNull(output), convertNull(error), flags);
435+
op.syncpipe = function() {
436+
return new SyncPipe();
425437
};
426438

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+
};
427446

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;
430470
};
431471

432472
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);
434478
};
435479

436480
op.cwd = function() {

0 commit comments

Comments
 (0)