Compare commits

...

2 Commits

Author SHA1 Message Date
autofix-ci[bot]
7e15f5bf97 [autofix.ci] apply automated fixes 2026-02-27 00:36:25 +00:00
Claude Bot
68e18a0a92 fix(repl): enable tail call optimization by evaluating in strict mode
JSC only performs Tail Call Optimization (TCO) in strict mode. The REPL
evaluated code as a sloppy mode Program, which prevented TCO and caused
`RangeError: Maximum call stack size exceeded` for tail-recursive functions.

Prepend `"use strict";` to the evaluated source code so JSC can apply
TCO. Top-level `var` declarations still become global properties in
strict mode, so REPL variable persistence is unaffected.

Closes #27488

Co-Authored-By: Claude <noreply@anthropic.com>
2026-02-27 00:34:30 +00:00
3 changed files with 33 additions and 11 deletions

View File

@@ -228,16 +228,16 @@ To build for macOS x64:
The order of the `--target` flag does not matter, as long as they're delimited by a `-`.
| --target | Operating System | Architecture | Modern | Baseline | Libc |
| --------------------- | ---------------- | ------------ | ------ | -------- | ----- |
| bun-linux-x64 | Linux | x64 | ✅ | ✅ | glibc |
| bun-linux-arm64 | Linux | arm64 | ✅ | N/A | glibc |
| bun-windows-x64 | Windows | x64 | ✅ | ✅ | - |
| bun-windows-arm64 | Windows | arm64 | ✅ | N/A | - |
| bun-darwin-x64 | macOS | x64 | ✅ | ✅ | - |
| bun-darwin-arm64 | macOS | arm64 | ✅ | N/A | - |
| bun-linux-x64-musl | Linux | x64 | ✅ | ✅ | musl |
| bun-linux-arm64-musl | Linux | arm64 | ✅ | N/A | musl |
| --target | Operating System | Architecture | Modern | Baseline | Libc |
| -------------------- | ---------------- | ------------ | ------ | -------- | ----- |
| bun-linux-x64 | Linux | x64 | ✅ | ✅ | glibc |
| bun-linux-arm64 | Linux | arm64 | ✅ | N/A | glibc |
| bun-windows-x64 | Windows | x64 | ✅ | ✅ | - |
| bun-windows-arm64 | Windows | arm64 | ✅ | N/A | - |
| bun-darwin-x64 | macOS | x64 | ✅ | ✅ | - |
| bun-darwin-arm64 | macOS | arm64 | ✅ | N/A | - |
| bun-linux-x64-musl | Linux | x64 | ✅ | ✅ | musl |
| bun-linux-arm64-musl | Linux | arm64 | ✅ | N/A | musl |
<Warning>
On x64 platforms, Bun uses SIMD optimizations which require a modern CPU supporting AVX2 instructions. The `-baseline`

View File

@@ -6165,7 +6165,7 @@ extern "C" JSC::EncodedJSValue Bun__REPL__evaluate(
auto& vm = JSC::getVM(globalObject);
auto scope = DECLARE_TOP_EXCEPTION_SCOPE(vm);
WTF::String source = WTF::String::fromUTF8(std::span { sourcePtr, sourceLen });
WTF::String source = makeString("\"use strict\";"_s, WTF::String::fromUTF8(std::span { sourcePtr, sourceLen }));
WTF::String filename = filenameLen > 0
? WTF::String::fromUTF8(std::span { filenamePtr, filenameLen })
: "[repl]"_s;

View File

@@ -0,0 +1,22 @@
import { expect, test } from "bun:test";
import { bunEnv, bunExe } from "harness";
// https://github.com/oven-sh/bun/issues/27488
// Tail calls in the REPL should work (requires strict mode for JSC TCO)
test("REPL supports tail call optimization", async () => {
await using proc = Bun.spawn({
cmd: [bunExe(), "repl", "-e", "const foo = n => n <= 0 ? n : foo(n-1); console.log(foo(100000));"],
env: bunEnv,
stdout: "pipe",
stderr: "pipe",
});
const [stdout, stderr, exitCode] = await Promise.all([
new Response(proc.stdout).text(),
new Response(proc.stderr).text(),
proc.exited,
]);
expect(stdout.trim()).toBe("0");
expect(exitCode).toBe(0);
});