Skip to content

Commit e298dd6

Browse files
committed
[js] Make nqp::stderr and nqp::stdout output to console.error and console.log
1 parent b42996b commit e298dd6

File tree

1 file changed

+58
-1
lines changed

1 file changed

+58
-1
lines changed

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

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,64 @@
11
const op = {};
2+
const Null = require('./null.js');
3+
const Hash = require('./hash.js');
4+
const core = require('./core.js');
5+
26
exports.op = op;
37

8+
class BufferedConsole {
9+
constructor(output) {
10+
this.buffered = [];
11+
this.output = output;
12+
}
13+
14+
$$writefh(buf) {
15+
const buffer = core.toRawBuffer(buf);
16+
this.$$write(buffer.toString());
17+
}
18+
19+
$$write(str) {
20+
const lines = str.split(/\n/);
21+
if (lines.length === 0) return;
22+
for (let i = 0; i < lines.length-1; i++) {
23+
if (i === 0) {
24+
this.output(this.buffered.join('') + lines[0]);
25+
this.buffered.length = 0;
26+
} else {
27+
this.output(lines[i]);
28+
}
29+
}
30+
31+
if (lines[lines.length - 1] !== '') {
32+
this.buffered.push(lines[lines.length - 1]);
33+
}
34+
}
35+
}
36+
37+
const STDOUT = new BufferedConsole(output => console.log(output));
38+
const STDERR = new BufferedConsole(output => console.error(output));
39+
440
op.say = function(arg) {
5-
console.log(arg);
41+
STDOUT.$$write(arg + '\n');
42+
return arg;
43+
};
44+
45+
op.print = function(arg) {
46+
STDOUT.$$write(arg);
647
return arg;
748
};
49+
50+
op.getstdin = function(arg) {
51+
return Null;
52+
};
53+
54+
op.getstdout = function(arg) {
55+
return STDOUT;
56+
};
57+
58+
op.getstderr = function(arg) {
59+
return STDERR;
60+
};
61+
62+
op.getenvhash = function() {
63+
return new Hash();
64+
};

0 commit comments

Comments
 (0)