Skip to content

Commit a6abf13

Browse files
committed
Wait for STDOUT to be flushed before exiting the node runtime
1 parent 2cac62d commit a6abf13

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

lib/execjs/support/node_runner.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
(function(program, execJS) { execJS(program) })(function(global, process, module, exports, require, console, setTimeout, setInterval, clearTimeout, clearInterval, setImmediate, clearImmediate) { #{source}
22
}, function(program) {
3-
var output, print = function(string) {
4-
process.stdout.write('' + string);
3+
var __process__ = process;
4+
5+
var printFinal = function(string) {
6+
process.stdout.write('' + string, function() {
7+
__process__.exit(0);
8+
});
59
};
610
try {
7-
var __process__ = process;
811
delete this.process;
912
delete this.console;
1013
delete this.setTimeout;
@@ -16,17 +19,16 @@
1619
result = program();
1720
this.process = __process__;
1821
if (typeof result == 'undefined' && result !== null) {
19-
print('["ok"]');
22+
printFinal('["ok"]');
2023
} else {
2124
try {
22-
print(JSON.stringify(['ok', result]));
25+
printFinal(JSON.stringify(['ok', result]));
2326
} catch (err) {
24-
print(JSON.stringify(['err', '' + err, err.stack]));
27+
printFinal(JSON.stringify(['err', '' + err, err.stack]));
2528
}
2629
}
2730
} catch (err) {
2831
this.process = __process__;
29-
print(JSON.stringify(['err', '' + err, err.stack]));
32+
printFinal(JSON.stringify(['err', '' + err, err.stack]));
3033
}
31-
__process__.exit(0);
3234
});

test/test_execjs.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,11 @@ def test_compile_large_scripts
306306
assert ExecJS.exec("function foo() {\n#{body}\n};\nreturn true")
307307
end
308308

309+
def test_large_return_value
310+
string = ExecJS.eval('(new Array(100001)).join("abcdef")')
311+
assert_equal 600_000, string.size
312+
end
313+
309314
def test_exec_syntax_error
310315
begin
311316
ExecJS.exec(")")

0 commit comments

Comments
 (0)