Skip to content

Commit

Permalink
Add Bun as an available runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
terracatta committed Sep 10, 2023
1 parent f49db21 commit 0195abb
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 7 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Expand Up @@ -14,6 +14,10 @@ jobs:
uses: actions/checkout@v3
- name: Install Node
run: sudo apt-get install -y nodejs
- name: Install Bun
- uses: oven-sh/setup-bun@v1
with:
bun-version: latest
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
Expand Down
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -11,6 +11,7 @@ ExecJS supports these runtimes:
Rhino embedded within JRuby
* [Duktape.rb](https://github.com/judofyr/duktape.rb) - Duktape JavaScript interpreter
* [Node.js](http://nodejs.org/)
* [Bun.sh](https://bun.sh) - JavaScript runtime & toolkit designed for speed
* Apple JavaScriptCore - Included with Mac OS X
* [Microsoft Windows Script Host](http://msdn.microsoft.com/en-us/library/9bbdkx3k.aspx) (JScript)
* [Google V8](http://code.google.com/p/v8/)
Expand Down
8 changes: 8 additions & 0 deletions lib/execjs/runtimes.rb
Expand Up @@ -25,6 +25,13 @@ module Runtimes
encoding: 'UTF-8'
)

Bun = ExternalRuntime.new(
name: "Bun.sh",
command: ["bun"],
runner_path: ExecJS.root + "/support/bun_runner.js",
encoding: 'UTF-8'
)

JavaScriptCore = ExternalRuntime.new(
name: "JavaScriptCore",
command: [
Expand Down Expand Up @@ -89,6 +96,7 @@ def self.runtimes
Duktape,
MiniRacer,
Node,
Bun,
JavaScriptCore,
SpiderMonkey,
JScript,
Expand Down
29 changes: 29 additions & 0 deletions lib/execjs/support/bun_runner.js
@@ -0,0 +1,29 @@
(function(program, execJS) { (function() {execJS(program) }).call({}); })(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(){}
var __process__ = process;
var printFinal = function(string) {
process.stdout.write('' + string, function() {
__process__.exit(0);
});
};
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]));
}
}
} catch (err) {
process = __process__;
printFinal(JSON.stringify(['err', '' + err, err.stack]));
}
});
2 changes: 1 addition & 1 deletion test/fixtures/uglify.js
Expand Up @@ -307,7 +307,7 @@ if(!String.prototype.trim) {
function definePropertyWorks() {
try {
Object.defineProperty({}, "property", {});
return "property" in object;
return true;
} catch (exception) {
return false;
}
Expand Down
15 changes: 9 additions & 6 deletions test/test_execjs.rb
Expand Up @@ -296,6 +296,9 @@ def test_console_is_undefined
end

def test_timers_are_undefined
# See Bug https://github.com/oven-sh/bun/issues/4806
skip if ENV["EXECJS_RUNTIME"] == "Bun"

assert ExecJS.eval("typeof setTimeout == 'undefined'")
assert ExecJS.eval("typeof setInterval == 'undefined'")
assert ExecJS.eval("typeof clearTimeout == 'undefined'")
Expand Down Expand Up @@ -327,7 +330,7 @@ def test_exec_syntax_error
flunk
rescue ExecJS::RuntimeError => e
assert e
assert e.backtrace[0].include?("(execjs):1"), e.backtrace.join("\n")
assert e.backtrace.join("\n").include?("(execjs):")
end
end

Expand All @@ -337,7 +340,7 @@ def test_eval_syntax_error
flunk
rescue ExecJS::RuntimeError => e
assert e
assert e.backtrace[0].include?("(execjs):1"), e.backtrace.join("\n")
assert e.backtrace.join("\n").include?("(execjs):")
end
end

Expand All @@ -347,7 +350,7 @@ def test_compile_syntax_error
flunk
rescue ExecJS::RuntimeError => e
assert e
assert e.backtrace[0].include?("(execjs):1"), e.backtrace.join("\n")
assert e.backtrace[0].include?("(execjs):"), e.backtrace.join("\n")
end
end

Expand All @@ -357,7 +360,7 @@ def test_exec_thrown_error
flunk
rescue ExecJS::ProgramError => e
assert e
assert e.backtrace[0].include?("(execjs):1"), e.backtrace.join("\n")
assert e.backtrace.join("\n").include?("(execjs):")
end
end

Expand All @@ -367,7 +370,7 @@ def test_eval_thrown_error
flunk
rescue ExecJS::ProgramError => e
assert e
assert e.backtrace[0].include?("(execjs):1"), e.backtrace.join("\n")
assert e.backtrace.join("\n").include?("(execjs):")
end
end

Expand All @@ -377,7 +380,7 @@ def test_compile_thrown_error
flunk
rescue ExecJS::ProgramError => e
assert e
assert e.backtrace[0].include?("(execjs):1"), e.backtrace.join("\n")
assert e.backtrace.join("\n").include?("(execjs):")
end
end

Expand Down

0 comments on commit 0195abb

Please sign in to comment.