refactor(codemode): keep the machine's hot paths free of per-evaluation closures

This commit is contained in:
James Long
2026-11-04 20:50:07 +00:00
Unverified
parent 8a4479e1ef
commit 1dd74883d6
9 changed files with 87 additions and 67 deletions
@@ -274,11 +274,7 @@ export class Interpreter<R> {
// The call returns its promise at once. Arguments cross on the caller's stack, since `toJSON` is program code, and a
// promise argument is awaited first; the host call starts once they have crossed, so admission and hooks happen at
// the call site, and runs concurrently with the program.
*toolCall(
path: ReadonlyArray<string>,
args: Array<Value>,
site: AstNode | undefined,
): Eval<Value> {
*toolCall(path: ReadonlyArray<string>, args: Array<Value>, site: AstNode | undefined): Eval<Value> {
const ctx = this
const runner = ctx.runner
const promise = runner.newPromise()
@@ -364,7 +360,8 @@ class Frame<R> {
this.scopes.push()
try {
// Imports load before any code runs, as in a module.
for (const statement of program.body) if (statement.type === "ImportDeclaration") bindImport(this.ctx, this.scopes, statement)
for (const statement of program.body)
if (statement.type === "ImportDeclaration") bindImport(this.ctx, this.scopes, statement)
predeclareLexical(this.scopes, program.body)
hoistFunctions(this.ctx.builtins, this.scopes, program.body)
hoistVars(this.scopes, program.body)
@@ -1128,7 +1125,11 @@ class Frame<R> {
}
const key = yield* this.destructuringPropertyKey(property)
consumed.add(typeof key === "symbol" ? key : String(key))
yield* this.assignPattern(property.value, destructuredProperty(this.ctx.builtins, value, key, property), property)
yield* this.assignPattern(
property.value,
destructuredProperty(this.ctx.builtins, value, key, property),
property,
)
}
return
}
@@ -1249,11 +1250,7 @@ class Frame<R> {
const right = this.evaluate(node.right)
if (isEval(right) || current instanceof Obj || right instanceof Obj)
return this.compoundAfter(node, current, right)
return this.scopes.set(
left.name,
applyBinaryOperator(node.operator.slice(0, -1), current, right, node),
left,
)
return this.scopes.set(left.name, applyBinaryOperator(node.operator.slice(0, -1), current, right, node), left)
}
case "SequenceExpression":
return this.evaluateSequenceExpression(node)
@@ -1801,8 +1798,7 @@ class Frame<R> {
while (true) {
const method = kind === "next" ? iterator.next : get(iterator.iterator, kind)
if (method === undefined || method === null) {
if (kind === "return")
throw new GeneratorReturn(asynchronous ? yield* this.awaitValue(input, node) : input)
if (kind === "return") throw new GeneratorReturn(asynchronous ? yield* this.awaitValue(input, node) : input)
yield* this.closeIterator(iterator, node)
throw typeError("The delegated iterator does not provide a throw() method.", node)
}
@@ -244,8 +244,7 @@ export const operatorConversion = (
const equality = operator === "==" || operator === "!="
const other = lhs instanceof Obj ? rhs : lhs
const converts =
primitiveOperators.has(operator) ||
(equality && other !== null && other !== undefined && typeof other !== "object")
primitiveOperators.has(operator) || (equality && other !== null && other !== undefined && typeof other !== "object")
if (!converts) return undefined
return { kind: "primitive", hint: operator === "+" || equality ? "default" : "number" }
}
@@ -452,7 +451,9 @@ export const hoistFunctions = (
): void => {
for (const node of statements) {
if (node.type !== "FunctionDeclaration") continue
scopes.current().set(node.id.name, { mutable: true, value: createFunction(builtins, scopes, node), initialized: true })
scopes
.current()
.set(node.id.name, { mutable: true, value: createFunction(builtins, scopes, node), initialized: true })
}
}
@@ -637,7 +638,11 @@ export const writableReference = (
export const assignToReference = (target: Obj, key: PropertyKey, next: Value, node: AstNode): void => {
const written = (() => {
try {
rejectCircularInsertion(target, next, target instanceof Arr ? "Array assignment result" : "Object assignment result")
rejectCircularInsertion(
target,
next,
target instanceof Arr ? "Array assignment result" : "Object assignment result",
)
return set(target, key, next)
} catch (error) {
throw locate(error, node)
@@ -140,7 +140,6 @@ export const MAX_CALL_DEPTH = 10_000
export const isEval = <A>(value: A | Eval<A>): value is Eval<A> =>
typeof value === "object" && value !== null && typeof (value as { readonly next?: unknown }).next === "function"
/** The body a generator object resumes: a `return()` request ends it with the returned value. */
export function* generatorBody(body: Iterable<Signal, Value, unknown>): Eval<Value> {
try {
@@ -150,4 +149,3 @@ export function* generatorBody(body: Iterable<Signal, Value, unknown>): Eval<Val
throw error
}
}
+3 -1
View File
@@ -237,7 +237,9 @@ const generator = (task: MachineTask, fn: Fn, frame: Frame): GeneratorObj => {
const builtins = task.ctx.builtins
const scopes = frame.scope
const body = new MachineTask(task.machine, scopes)
body.stack.push(new Frame(CallKind, fn.body, RUN, scopes, [fn, undefined, undefined, undefined, false, frame.slots[5]]))
body.stack.push(
new Frame(CallKind, fn.body, RUN, scopes, [fn, undefined, undefined, undefined, false, frame.slots[5]]),
)
const proto = get(fn, "prototype")
return new GeneratorObj(
proto instanceof Obj ? proto : fn.async ? builtins.AsyncGenerator : builtins.Generator,
+24 -25
View File
@@ -67,7 +67,7 @@ import {
import { capability } from "../interpreter/promises.js"
import { isOpaque, typeofValue } from "../interpreter/references.js"
import { Await, type GeneratorRequest, isEval, Yield } from "../interpreter/signals.js"
import { type Frame, kind, type Out, PENDING, type Pending } from "./frame.js"
import { type Frame, kind, type Out, PENDING } from "./frame.js"
import { operate, toPrimitive, toPropertyKey } from "./calls.js"
import { spread } from "./iteration.js"
import { assigning, bind } from "./patterns.js"
@@ -481,7 +481,6 @@ function assignmentExpression(
const phase = frame?.phase ?? 0
const left = node.left
const operator = node.operator
const park = (next: number, slots?: Array<unknown>): Pending => task.park(frame, mark, Assignment, node, next, slots)
const logical = logicalAssignment(operator)
if (!logical && operator !== "=" && !compoundOperators.has(operator)) {
throw typeError(`Unsupported assignment operator '${operator}'.`, node)
@@ -495,7 +494,7 @@ function assignmentExpression(
if (!shouldAssign(operator, current)) return current
}
const right = named(task, node.right, name)
if (right === PENDING) return park(1)
if (right === PENDING) return task.park(frame, mark, Assignment, node, 1)
return task.scopes.set(name, right, left)
}
// The binary operator a compound assignment applies: `+=` is `+`.
@@ -507,7 +506,7 @@ function assignmentExpression(
if (phase === 0) {
current = task.scopes.get(name, left)
const evaluated = ev(task, node.right)
if (evaluated === PENDING) return park(1, [current])
if (evaluated === PENDING) return task.park(frame, mark, Assignment, node, 1, [current])
right = evaluated
} else {
current = frame!.slots[0] as Value
@@ -515,7 +514,7 @@ function assignmentExpression(
}
if (current instanceof Obj || right instanceof Obj) {
const result = operate(task, binary, current, right, node)
if (result === PENDING) return park(2)
if (result === PENDING) return task.park(frame, mark, Assignment, node, 2)
next = result
} else next = applyBinaryOperator(binary, current, right, node)
} else next = input as Value
@@ -525,10 +524,11 @@ function assignmentExpression(
let right: Value
if (phase === 0) {
const evaluated = ev(task, node.right)
if (evaluated === PENDING) return park(1)
if (evaluated === PENDING) return task.park(frame, mark, Assignment, node, 1)
right = evaluated
} else right = phase === 1 ? (input as Value) : (frame!.slots[0] as Value)
if (phase <= 1 && bind(task, left, right, node, assigning) === PENDING) return park(2, [right])
if (phase <= 1 && bind(task, left, right, node, assigning) === PENDING)
return task.park(frame, mark, Assignment, node, 2, [right])
return right
}
if (left.type !== "MemberExpression") {
@@ -542,7 +542,7 @@ function assignmentExpression(
let resolved = input as Reference
if (phase === 0) {
const evaluated = member(task, undefined, left, undefined, false) as Out<Reference>
if (evaluated === PENDING) return park(1)
if (evaluated === PENDING) return task.park(frame, mark, Assignment, node, 1)
resolved = evaluated
}
reference = writableReference(resolved, left)
@@ -557,13 +557,13 @@ function assignmentExpression(
let right = input as Value
if (phase <= 1) {
const evaluated = ev(task, node.right)
if (evaluated === PENDING) return park(2, [reference, current])
if (evaluated === PENDING) return task.park(frame, mark, Assignment, node, 2, [reference, current])
right = evaluated
}
if (operator === "=" || logical) next = right
else {
const result = operate(task, operator.slice(0, -1), current, right, node)
if (result === PENDING) return park(3, [reference, current])
if (result === PENDING) return task.park(frame, mark, Assignment, node, 3, [reference, current])
next = result
}
} else next = input as Value
@@ -582,13 +582,6 @@ function updateExpression(task: MachineTask, frame: Frame | undefined, node: Upd
if (increment === undefined) {
throw typeError(`Unsupported update operator '${operator}'.`, node)
}
// CodeMode numeric coercion, not host Number(), so opaque runtime references reject clearly.
const operand = (current: Value): number => {
if (isOpaque(current)) {
throw invalidData(`'${operator}' requires a data value.`, argument)
}
return coerceToNumber(current)
}
if (argument.type === "Identifier") {
let current = input as Value
if (phase === 0) {
@@ -597,7 +590,7 @@ function updateExpression(task: MachineTask, frame: Frame | undefined, node: Upd
if (converted === PENDING) return task.park(frame, mark, Update, node, 1)
current = converted
}
const before = operand(current)
const before = operand(operator, argument, current)
const next = before + increment
task.scopes.set(argument.name, next, argument)
return node.prefix ? next : before
@@ -619,12 +612,20 @@ function updateExpression(task: MachineTask, frame: Frame | undefined, node: Upd
if (converted === PENDING) return task.park(frame, mark, Update, node, 2, [reference])
current = converted
} else reference = frame!.slots[0] as MemberReference
const value = operand(current)
const value = operand(operator, argument, current)
const next = value + increment
assignToReference(reference.target, reference.key, next, argument)
return node.prefix ? next : value
}
// CodeMode numeric coercion, not host Number(), so opaque runtime references reject clearly.
const operand = (operator: string, argument: Expression, current: Value): number => {
if (isOpaque(current)) {
throw invalidData(`'${operator}' requires a data value.`, argument)
}
return coerceToNumber(current)
}
const Update = kind("UpdateExpression", updateExpression)
// Slots: the object and the index of the property being evaluated, and its key once known. Phases: 0 starts the
@@ -636,7 +637,6 @@ function objectExpression(task: MachineTask, frame: Frame | undefined, node: Obj
const object = frame === undefined ? new Obj(ctx.builtins.Object) : (frame.slots[0] as Obj)
let index = frame === undefined ? 0 : (frame.slots[1] as number)
let key = frame?.slots[2] as PropertyKey
const park = (next: number): Pending => task.park(frame, mark, ObjectKind, node, next, [object, index, key])
const properties = node.properties
for (; index < properties.length; index++, phase = 0) {
const property = properties[index]!
@@ -644,7 +644,7 @@ function objectExpression(task: MachineTask, frame: Frame | undefined, node: Obj
let spread = input as Value
if (phase === 0) {
const evaluated = ev(task, property.argument)
if (evaluated === PENDING) return park(1)
if (evaluated === PENDING) return task.park(frame, mark, ObjectKind, node, 1, [object, index, key])
spread = evaluated
}
if (spread === null || spread === undefined) continue
@@ -658,7 +658,7 @@ function objectExpression(task: MachineTask, frame: Frame | undefined, node: Obj
const keyNode = property.key
if (property.computed) {
const evaluated = ev(task, keyNode)
if (evaluated === PENDING) return park(2)
if (evaluated === PENDING) return task.park(frame, mark, ObjectKind, node, 2, [object, index, key])
input = evaluated
phase = 2
} else if (keyNode.type === "Identifier") key = keyNode.name
@@ -667,7 +667,7 @@ function objectExpression(task: MachineTask, frame: Frame | undefined, node: Obj
}
if (phase === 2) {
const converted = toPropertyKey(task, input as Value, property.key)
if (converted === PENDING) return park(3)
if (converted === PENDING) return task.park(frame, mark, ObjectKind, node, 3, [object, index, key])
input = converted
phase = 3
}
@@ -681,7 +681,7 @@ function objectExpression(task: MachineTask, frame: Frame | undefined, node: Obj
? "[Symbol.asyncIterator]"
: String(key)
const evaluated = named(task, property.value as Expression, name)
if (evaluated === PENDING) return park(4)
if (evaluated === PENDING) return task.park(frame, mark, ObjectKind, node, 4, [object, index, key])
value = evaluated
}
define(object, key, value)
@@ -889,4 +889,3 @@ function importExpression(task: MachineTask, frame: Frame | undefined, node: Imp
}
const Import = kind("ImportExpression", importExpression)
+15 -13
View File
@@ -11,7 +11,7 @@ import {
} from "../interpreter/operations.js"
import { describeValue } from "../interpreter/references.js"
import { Interruption } from "../interpreter/signals.js"
import { type Frame, kind, type Out, PENDING, type Pending } from "./frame.js"
import { type Frame, kind, type Out, PENDING } from "./frame.js"
import { toPropertyKey } from "./calls.js"
import { ev, member, named } from "./expressions.js"
import { type Iter, iterate, iteratorClose, iteratorStep } from "./iteration.js"
@@ -122,7 +122,6 @@ function objectPattern(
}
const consumed = slots[4] as Set<PropertyKey>
const properties = pattern.properties
const park = (next: number): Pending => task.park(frame, mark, ObjectPatternKind, pattern, next, slots)
while ((slots[3] as number) < properties.length) {
const index = slots[3] as number
const property = properties[index]!
@@ -130,7 +129,8 @@ function objectPattern(
slots[3] = index + 1
const rest = new Obj(task.ctx.builtins.Object)
assign(rest, enumerableSource(task.ctx, "Object destructuring", value, pattern), consumed)
if (bind(task, property.argument, rest, property, mode) === PENDING) return park(0)
if (bind(task, property.argument, rest, property, mode) === PENDING)
return task.park(frame, mark, ObjectPatternKind, pattern, 0, slots)
continue
}
let key: PropertyKey
@@ -141,7 +141,7 @@ function objectPattern(
const keyNode = property.key
if (property.computed) {
const computed = ev(task, keyNode)
if (computed === PENDING) return park(1)
if (computed === PENDING) return task.park(frame, mark, ObjectPatternKind, pattern, 1, slots)
input = computed
phase = 1
} else if (keyNode.type === "Identifier") key = keyNode.name
@@ -150,7 +150,7 @@ function objectPattern(
}
if (phase === 1) {
const converted = toPropertyKey(task, input as Value, property.key)
if (converted === PENDING) return park(2)
if (converted === PENDING) return task.park(frame, mark, ObjectPatternKind, pattern, 2, slots)
input = converted
phase = 2
}
@@ -159,7 +159,8 @@ function objectPattern(
consumed.add(typeof key! === "symbol" ? key : String(key!))
slots[3] = index + 1
const item = destructuredProperty(task.ctx.builtins, value, key!, property)
if (bind(task, property.value, item, property, mode) === PENDING) return park(0)
if (bind(task, property.value, item, property, mode) === PENDING)
return task.park(frame, mark, ObjectPatternKind, pattern, 0, slots)
}
return undefined
}
@@ -188,11 +189,10 @@ function arrayPattern(
const mark = task.stack.length
const mode = slots[2] as Mode
let phase = frame?.phase ?? 0
const park = (next: number): Pending => task.park(frame, mark, ArrayPatternKind, pattern, next, slots)
if (phase === 0) {
slots = [slots[0], slots[1], mode, undefined, 0, false, undefined, undefined]
const opened = iterate(task, slots[0] as Value, pattern)
if (opened === PENDING) return park(OPEN)
if (opened === PENDING) return task.park(frame, mark, ArrayPatternKind, pattern, OPEN, slots)
input = opened
phase = OPEN
}
@@ -201,7 +201,8 @@ function arrayPattern(
slots[3] = input
phase = ELEMENT
// An iterator with something to close needs a frame, so a failure while binding can close it.
if (frame === undefined && !(input instanceof Position)) return park(ELEMENT)
if (frame === undefined && !(input instanceof Position))
return task.park(frame, mark, ArrayPatternKind, pattern, ELEMENT, slots)
}
if (phase === FAILING) return fail(task, frame!, pattern)
if (phase === FAILED) throw slots[7]
@@ -222,11 +223,11 @@ function arrayPattern(
const target = rest ? element.argument : element
const result = bind(task, target, rest ? new Arr(task.ctx.builtins.Array) : undefined, element, mode)
if (rest) return task.tail(frame, result)
if (result === PENDING) return park(ELEMENT)
if (result === PENDING) return task.park(frame, mark, ArrayPatternKind, pattern, ELEMENT, slots)
continue
}
const step = iteratorStep(task, iterator, pattern)
if (step === PENDING) return park(STEPPED)
if (step === PENDING) return task.park(frame, mark, ArrayPatternKind, pattern, STEPPED, slots)
input = step
phase = STEPPED
}
@@ -250,7 +251,8 @@ function arrayPattern(
// close.
if (frame !== undefined) frame.phase = step.done ? ELEMENT : BINDING
const result = bind(task, element, step.done ? undefined : step.value, pattern, mode)
if (result === PENDING) return park(step.done ? ELEMENT : BINDING)
if (result === PENDING)
return task.park(frame, mark, ArrayPatternKind, pattern, step.done ? ELEMENT : BINDING, slots)
if (frame !== undefined) frame.phase = ELEMENT
phase = ELEMENT
continue
@@ -269,7 +271,7 @@ function arrayPattern(
}
while (!slots[5]) {
const step = iteratorStep(task, iterator, pattern)
if (step === PENDING) return park(REST)
if (step === PENDING) return task.park(frame, mark, ArrayPatternKind, pattern, REST, slots)
slots[5] = step.done
if (!step.done) rest.push(step.value)
}
+9 -5
View File
@@ -228,7 +228,12 @@ function ifStatement(task: MachineTask, frame: Frame | undefined, node: IfStatem
const If = kind("IfStatement", ifStatement)
function throwStatement(task: MachineTask, frame: Frame | undefined, node: ThrowStatement, input: unknown): never | typeof PENDING {
function throwStatement(
task: MachineTask,
frame: Frame | undefined,
node: ThrowStatement,
input: unknown,
): never | typeof PENDING {
let value = input as Value
if (frame === undefined) {
const mark = task.stack.length
@@ -434,9 +439,7 @@ function forStatement(task: MachineTask, frame: Frame, node: ForStatement, input
if (init?.type === "VariableDeclaration" && init.kind !== "var") predeclareLexical(task.scopes, [init])
if (init) {
const done =
init.type === "VariableDeclaration"
? variableDeclaration(task, undefined, init, undefined)
: ev(task, init)
init.type === "VariableDeclaration" ? variableDeclaration(task, undefined, init, undefined) : ev(task, init)
if (done === PENDING) {
frame.phase = 1
return PENDING
@@ -873,7 +876,8 @@ function programStep(task: MachineTask, frame: Frame, node: Program, input: unkn
// Keep top-level declarations separate so they can shadow builtins.
task.scopes.push()
// Imports load before any code runs, as in a module.
for (const statement of body) if (statement.type === "ImportDeclaration") bindImport(task.ctx, task.scopes, statement)
for (const statement of body)
if (statement.type === "ImportDeclaration") bindImport(task.ctx, task.scopes, statement)
predeclareLexical(task.scopes, body)
hoistFunctions(task.ctx.builtins, task.scopes, body)
hoistVars(task.scopes, body)
+8 -1
View File
@@ -62,7 +62,14 @@ export class MachineTask {
* yet), the frame is made now and slipped in at `mark`, the stack height when the function began, so it sits below
* the frames that work pushed.
*/
park(frame: Frame | undefined, mark: number, kind: Kind, node: AstNode, phase: number, slots: Array<unknown> = []): Pending {
park(
frame: Frame | undefined,
mark: number,
kind: Kind,
node: AstNode,
phase: number,
slots: Array<unknown> = [],
): Pending {
if (frame !== undefined) {
frame.phase = phase
frame.slots = slots
+9 -2
View File
@@ -116,7 +116,10 @@ describe("machine evaluator", () => {
for (let i = 0; i < 3; i++) fns.push(() => id(i))
return [out, fns.map((f) => f())]
`),
).toEqual([["zero", "one", "d", "d"], [0, 1, 2]])
).toEqual([
["zero", "one", "d", "d"],
[0, 1, 2],
])
})
test("conversions call program valueOf and toString", async () => {
@@ -141,7 +144,11 @@ describe("machine evaluator", () => {
const all = await Promise.all([1, 2, 3].map(twice))
return [log, [...g()], all]
`),
).toEqual([["done", "done"], [1, 2], [2, 4, 6]])
).toEqual([
["done", "done"],
[1, 2],
[2, 4, 6],
])
})
test("runaway recursion stops at the same depth with the same location", async () => {