diff --git a/packages/codemode/interpreter-support.md b/packages/codemode/interpreter-support.md index b7a6bab6d6a..edf12a80e97 100644 --- a/packages/codemode/interpreter-support.md +++ b/packages/codemode/interpreter-support.md @@ -100,6 +100,10 @@ ultimate source of truth. Upstream test262 files run verbatim from `test/test262 - [x] Function declarations, function expressions, and arrow functions. - [x] Synchronous and `async` functions. - [x] Closures, recursion, default parameters, rest parameters, and destructured parameters. +- [x] A call depth limit of 10000: deeper nesting throws a catchable `RangeError: Maximum call stack size exceeded` + at the overflowing call instead of running until the timeout. Callbacks invoked by built-ins count below the + call that invoked the built-in, and a resumed `await` starts from depth 0 as in JS, so long async chains such + as recursive pagination are unaffected. - [x] Expression and block function bodies. - [x] User callbacks for the supported Array, Map, Set, URLSearchParams, sort, string-replacement, and `Array.from` mapper APIs, with one shared acceptance rule everywhere including promise reactions. diff --git a/packages/codemode/src/interpreter/model.ts b/packages/codemode/src/interpreter/model.ts index 973d9451409..8c29b942200 100644 --- a/packages/codemode/src/interpreter/model.ts +++ b/packages/codemode/src/interpreter/model.ts @@ -7,7 +7,10 @@ import type { ProgramError } from "./objects.js" /** Any parsed node; the interpreter narrows on `type` and reads `loc` for diagnostics. */ export type AstNode = Node -export const CallSite = Context.Reference("codemode/CallSite", { defaultValue: () => undefined }) +/** The program call a built-in is running under: where to locate failures born inside it, and how deep the stack is there. */ +export const CallSite = Context.Reference<{ readonly node?: AstNode; readonly depth: number }>("codemode/CallSite", { + defaultValue: () => ({ depth: 0 }), +}) export type Binding = { mutable: boolean diff --git a/packages/codemode/src/interpreter/promises.ts b/packages/codemode/src/interpreter/promises.ts index b109ba6b0f4..d909eab5943 100644 --- a/packages/codemode/src/interpreter/promises.ts +++ b/packages/codemode/src/interpreter/promises.ts @@ -52,7 +52,7 @@ export class PromiseRuntime { return Effect.flatMap(CallSite, (site) => { // Allocate before forking so reruns get distinct IDs and diagnostics retain creation order. const id = this.nextID++ - const body = Effect.catchDefect(effect, (defect) => Effect.die(locate(defect, site))) + const body = Effect.catchDefect(effect, (defect) => Effect.die(locate(defect, site.node))) return Effect.map(Effect.forkIn(body, this.scope, { startImmediately: true }), (fiber) => { const promise = new ProgramPromise(this.proto, fiber) this.active.add(promise) diff --git a/packages/codemode/src/interpreter/runtime.ts b/packages/codemode/src/interpreter/runtime.ts index 2fda632b2a3..e9ccd9c4fda 100644 --- a/packages/codemode/src/interpreter/runtime.ts +++ b/packages/codemode/src/interpreter/runtime.ts @@ -280,6 +280,8 @@ export class Runtime { } } +const MAX_CALL_DEPTH = 10_000 + /** One activation: the top-level program or a single function call, evaluating against its own scope chain. */ class Frame { private generatorState?: GeneratorState @@ -288,6 +290,8 @@ class Frame { constructor( private readonly runtime: Runtime, private scopes: ScopeStack, + /** Nested call depth; resets when an await resumes, since the continuation runs from the job queue. */ + private depth = 0, ) {} run(program: Program): Effect.Effect { @@ -691,7 +695,10 @@ class Frame { private awaitValue(value: unknown): Effect.Effect { return Effect.flatMap(resolvePromise(this.runtime.runner, this.runtime.promises, value), (promise) => - this.settlePromise(promise), + Effect.ensuring( + this.settlePromise(promise), + Effect.sync(() => (this.depth = 0)), + ), ) } @@ -1569,7 +1576,7 @@ class Frame { } return yield* self.createToolCallPromise(callable.path, args) } - if (callable instanceof ProgramFunction) return yield* self.invokeFunction(callable, args) + if (callable instanceof ProgramFunction) return yield* self.invokeFunction(callable, args, node) if (callable instanceof NativeFunction) { return yield* self.native(() => (callable as NativeFunction).call(thisValue, args), node) } @@ -1582,7 +1589,7 @@ class Frame { return Effect.provideService( Effect.catchDefect(Effect.suspend(body), (defect) => Effect.die(locate(defect, node))), CallSite, - node, + { node, depth: this.depth }, ) } @@ -1610,45 +1617,50 @@ class Frame { }) } - invokeFunction(fn: ProgramFunction, args: Array): Effect.Effect { + // A callback invoked by a built-in runs below the call that invoked the built-in, so the deeper of the two counts. + invokeFunction(fn: ProgramFunction, args: Array, node?: AstNode): Effect.Effect { const self = this - const invocation = new Frame(this.runtime, new ScopeStack([...fn.capturedScopes, new Map()])) - const run = Effect.gen(function* () { - // Seed all parameters first so defaults cannot fall through to same-named outer bindings. - const paramScope = invocation.scopes.current() - for (const parameter of fn.parameters) { - for (const name of collectPatternNames(parameter)) { - paramScope.set(name, { mutable: true, value: undefined, initialized: false }) + return Effect.flatMap(CallSite, (site) => { + const depth = Math.max(self.depth, site.depth) + 1 + if (depth > MAX_CALL_DEPTH) throw rangeError("Maximum call stack size exceeded", node) + const invocation = new Frame(this.runtime, new ScopeStack([...fn.capturedScopes, new Map()]), depth) + const run = Effect.gen(function* () { + // Seed all parameters first so defaults cannot fall through to same-named outer bindings. + const paramScope = invocation.scopes.current() + for (const parameter of fn.parameters) { + for (const name of collectPatternNames(parameter)) { + paramScope.set(name, { mutable: true, value: undefined, initialized: false }) + } } - } - for (const [index, parameter] of fn.parameters.entries()) { - if (parameter.type === "RestElement") { - yield* invocation.declarePattern( - parameter.argument, - new ProgramArray(self.runtime.prototypes.Array, args.slice(index)), - true, - parameter, - true, - ) - break + for (const [index, parameter] of fn.parameters.entries()) { + if (parameter.type === "RestElement") { + yield* invocation.declarePattern( + parameter.argument, + new ProgramArray(self.runtime.prototypes.Array, args.slice(index)), + true, + parameter, + true, + ) + break + } + yield* invocation.declarePattern(parameter, args[index], true, parameter, true) } - yield* invocation.declarePattern(parameter, args[index], true, parameter, true) - } - if (fn.body.type === "BlockStatement") { - invocation.scopes.push() - invocation.hoistVars(fn.body.body, paramScope) - const result = yield* invocation.evaluateStatement(fn.body) - return result.kind === "return" ? result.value : undefined - } + if (fn.body.type === "BlockStatement") { + invocation.scopes.push() + invocation.hoistVars(fn.body.body, paramScope) + const result = yield* invocation.evaluateStatement(fn.body) + return result.kind === "return" ? result.value : undefined + } - return yield* invocation.evaluateExpression(fn.body) + return yield* invocation.evaluateExpression(fn.body) + }) + if (fn.generator) return Effect.succeed(this.createGenerator(invocation, run, fn.async)) + if (!fn.async) return run + return this.runtime.promises.createWithSelf((self) => + Effect.flatMap(run, (value) => resolvePromiseValue(invocation.runtime.runner, value, self)), + ) }) - if (fn.generator) return Effect.succeed(this.createGenerator(invocation, run, fn.async)) - if (!fn.async) return run - return this.runtime.promises.createWithSelf((self) => - Effect.flatMap(run, (value) => resolvePromiseValue(invocation.runtime.runner, value, self)), - ) } private createGenerator( diff --git a/packages/codemode/test/errors.test.ts b/packages/codemode/test/errors.test.ts index 4af2b31a59f..0998a1bb24a 100644 --- a/packages/codemode/test/errors.test.ts +++ b/packages/codemode/test/errors.test.ts @@ -131,3 +131,48 @@ describe("host errors escaping built-ins", () => { expect(failure.message).toBe("TypeError: Iterator next must be a function. (line 1, col 8)") }) }) + +describe("call depth", () => { + test("runaway recursion fails fast with a catchable RangeError", async () => { + const started = Date.now() + expect( + await value(` + const f = (n) => f(n + 1) + try { f(0) } catch (e) { return [e.name, e instanceof RangeError, e.message] } + `), + ).toEqual(["RangeError", true, "Maximum call stack size exceeded"]) + expect(Date.now() - started).toBeLessThan(2000) + }) + + test("uncaught overflow reports the call that overflowed", async () => { + const failure = await error(`const f = (n) => f(n + 1); return f(0)`) + expect(failure.kind).toBe("ExecutionFailure") + expect(failure.message).toBe("RangeError: Maximum call stack size exceeded (line 1, col 18)") + }) + + test("the limit is 10000 nested calls", async () => { + expect(await value(`let depth = 0; const f = () => { depth++; f() }; try { f() } catch { return depth }`)).toBe( + 10000, + ) + expect(await value(`const f = (n) => (n === 0 ? 0 : 1 + f(n - 1)); return f(9000)`)).toBe(9000) + }) + + test("recursion through a built-in callback counts", async () => { + const failure = await error(`const f = (n) => [n].map((x) => f(x + 1)); return f(0)`) + expect(failure.message).toStartWith("RangeError: Maximum call stack size exceeded") + }) + + test("an await resets the depth, so long async chains are fine", async () => { + expect( + await value(` + const page = async (n) => { await null; return n === 0 ? "done" : page(n - 1) } + return await page(3000) + `), + ).toBe("done") + }) + + test("an async function that recurses before its first await overflows like JS", async () => { + const failure = await error(`const f = async (n) => f(n + 1); return await f(0)`) + expect(failure.message).toStartWith("RangeError: Maximum call stack size exceeded") + }) +})