Skip to content

Commit

Permalink
Wait for STDOUT to be flushed before exiting the node runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
byroot committed May 14, 2021
1 parent 2cac62d commit a6abf13
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
18 changes: 10 additions & 8 deletions lib/execjs/support/node_runner.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
(function(program, execJS) { execJS(program) })(function(global, process, module, exports, require, console, setTimeout, setInterval, clearTimeout, clearInterval, setImmediate, clearImmediate) { #{source}
}, function(program) {
var output, print = function(string) {
process.stdout.write('' + string);
var __process__ = process;

var printFinal = function(string) {
process.stdout.write('' + string, function() {
__process__.exit(0);
});
};
try {
var __process__ = process;
delete this.process;
delete this.console;
delete this.setTimeout;
Expand All @@ -16,17 +19,16 @@
result = program();
this.process = __process__;
if (typeof result == 'undefined' && result !== null) {
print('["ok"]');
printFinal('["ok"]');
} else {
try {
print(JSON.stringify(['ok', result]));
printFinal(JSON.stringify(['ok', result]));
} catch (err) {
print(JSON.stringify(['err', '' + err, err.stack]));
printFinal(JSON.stringify(['err', '' + err, err.stack]));
}
}
} catch (err) {
this.process = __process__;
print(JSON.stringify(['err', '' + err, err.stack]));
printFinal(JSON.stringify(['err', '' + err, err.stack]));
}
__process__.exit(0);
});
5 changes: 5 additions & 0 deletions test/test_execjs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,11 @@ def test_compile_large_scripts
assert ExecJS.exec("function foo() {\n#{body}\n};\nreturn true")
end

def test_large_return_value
string = ExecJS.eval('(new Array(100001)).join("abcdef")')
assert_equal 600_000, string.size
end

def test_exec_syntax_error
begin
ExecJS.exec(")")
Expand Down

0 comments on commit a6abf13

Please sign in to comment.