Skip to content

Commit

Permalink
refactor and add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
Reese Armstrong committed Feb 29, 2024
1 parent 85c6b87 commit 3ab72a6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
26 changes: 11 additions & 15 deletions lib/execjs/support/bun_runner.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(function(program, execJS) { (function() {execJS(program) }).call({}); })(async function(self, global, process, module, exports, require, console, setTimeout, setInterval, clearTimeout, clearInterval, setImmediate, clearImmediate) { #{source}
}, function(program) {
}, async function(program) {
// Force BunJS to use sloppy mode see https://github.com/oven-sh/bun/issues/4527#issuecomment-1709520894
exports.abc = function(){}
var __process__ = process;
Expand All @@ -11,21 +11,17 @@
try {
delete this.process;
delete this.console;
(program()).then((result) => {
process = __process__;
if (typeof result == 'undefined' && result !== null) {
printFinal('["ok"]');
} else {
try {
printFinal(JSON.stringify(['ok', result]));
} catch (err) {
printFinal(JSON.stringify(['err', '' + err, err.stack]));
}
result = await program();
process = __process__;
if (typeof result == 'undefined' && result !== null) {
printFinal('["ok"]');
} else {
try {
printFinal(JSON.stringify(['ok', result]));
} catch (err) {
printFinal(JSON.stringify(['err', '' + err, err.stack]));
}
}).catch((err) => {
process = __process__;
printFinal(JSON.stringify(['err', '' + err, err.stack]));
})
}
} catch (err) {
process = __process__;
printFinal(JSON.stringify(['err', '' + err, err.stack]));
Expand Down
9 changes: 9 additions & 0 deletions test/test_execjs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,15 @@ def test_uglify
context.call("uglify", "function foo(bar) {\n return bar;\n}")
end

def test_async_bun
skip unless ENV["EXECJS_RUNTIME"] == "Bun"
source = <<-JS
async function testAsync() { return (await new Promise((resolve) => { resolve("it works!") } )) }
JS
context = ExecJS.compile(source)
assert_equal "it works!", context.call("testAsync")
end

private

def assert_output(expected, actual)
Expand Down

0 comments on commit 3ab72a6

Please sign in to comment.