From 85c6b87a475f8e08a0f642e15b66173e67fe7745 Mon Sep 17 00:00:00 2001 From: Reese Armstrong Date: Thu, 5 Oct 2023 16:39:19 -0500 Subject: [PATCH] Add async/await & promise support to Bun runner --- lib/execjs/support/bun_runner.js | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/lib/execjs/support/bun_runner.js b/lib/execjs/support/bun_runner.js index ddbdedf..f5bd11a 100644 --- a/lib/execjs/support/bun_runner.js +++ b/lib/execjs/support/bun_runner.js @@ -1,4 +1,4 @@ -(function(program, execJS) { (function() {execJS(program) }).call({}); })(function(self, global, process, module, exports, require, console, setTimeout, setInterval, clearTimeout, clearInterval, setImmediate, clearImmediate) { #{source} +(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) { // Force BunJS to use sloppy mode see https://github.com/oven-sh/bun/issues/4527#issuecomment-1709520894 exports.abc = function(){} @@ -11,17 +11,21 @@ try { delete this.process; delete this.console; - result = 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])); + (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])); + } } - } + }).catch((err) => { + process = __process__; + printFinal(JSON.stringify(['err', '' + err, err.stack])); + }) } catch (err) { process = __process__; printFinal(JSON.stringify(['err', '' + err, err.stack]));