Skip to content

Commit 3ab72a6

Browse files
author
Reese Armstrong
committed
refactor and add test case
1 parent 85c6b87 commit 3ab72a6

File tree

2 files changed

+20
-15
lines changed

2 files changed

+20
-15
lines changed

lib/execjs/support/bun_runner.js

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
(function(program, execJS) { (function() {execJS(program) }).call({}); })(async function(self, global, process, module, exports, require, console, setTimeout, setInterval, clearTimeout, clearInterval, setImmediate, clearImmediate) { #{source}
2-
}, function(program) {
2+
}, async function(program) {
33
// Force BunJS to use sloppy mode see https://github.com/oven-sh/bun/issues/4527#issuecomment-1709520894
44
exports.abc = function(){}
55
var __process__ = process;
@@ -11,21 +11,17 @@
1111
try {
1212
delete this.process;
1313
delete this.console;
14-
(program()).then((result) => {
15-
process = __process__;
16-
if (typeof result == 'undefined' && result !== null) {
17-
printFinal('["ok"]');
18-
} else {
19-
try {
20-
printFinal(JSON.stringify(['ok', result]));
21-
} catch (err) {
22-
printFinal(JSON.stringify(['err', '' + err, err.stack]));
23-
}
14+
result = await program();
15+
process = __process__;
16+
if (typeof result == 'undefined' && result !== null) {
17+
printFinal('["ok"]');
18+
} else {
19+
try {
20+
printFinal(JSON.stringify(['ok', result]));
21+
} catch (err) {
22+
printFinal(JSON.stringify(['err', '' + err, err.stack]));
2423
}
25-
}).catch((err) => {
26-
process = __process__;
27-
printFinal(JSON.stringify(['err', '' + err, err.stack]));
28-
})
24+
}
2925
} catch (err) {
3026
process = __process__;
3127
printFinal(JSON.stringify(['err', '' + err, err.stack]));

test/test_execjs.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,15 @@ def test_uglify
450450
context.call("uglify", "function foo(bar) {\n return bar;\n}")
451451
end
452452

453+
def test_async_bun
454+
skip unless ENV["EXECJS_RUNTIME"] == "Bun"
455+
source = <<-JS
456+
async function testAsync() { return (await new Promise((resolve) => { resolve("it works!") } )) }
457+
JS
458+
context = ExecJS.compile(source)
459+
assert_equal "it works!", context.call("testAsync")
460+
end
461+
453462
private
454463

455464
def assert_output(expected, actual)

0 commit comments

Comments
 (0)