From 3ab72a6224c1624d965116da63e55dc2dbbafb3f Mon Sep 17 00:00:00 2001 From: Reese Armstrong Date: Thu, 29 Feb 2024 18:29:54 +0000 Subject: [PATCH] refactor and add test case --- lib/execjs/support/bun_runner.js | 26 +++++++++++--------------- test/test_execjs.rb | 9 +++++++++ 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/lib/execjs/support/bun_runner.js b/lib/execjs/support/bun_runner.js index f5bd11a..ec381d2 100644 --- a/lib/execjs/support/bun_runner.js +++ b/lib/execjs/support/bun_runner.js @@ -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; @@ -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])); diff --git a/test/test_execjs.rb b/test/test_execjs.rb index 4b0976f..b0c4d07 100644 --- a/test/test_execjs.rb +++ b/test/test_execjs.rb @@ -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)