refactor(codemode): use acorn's typed AST in the interpreter (#48060)

This commit is contained in:
Aiden Cline
2026-09-08 22:20:12 -05:00
committed by GitHub
Unverified
parent 08e28fb915
commit 0da0772bc0
5 changed files with 305 additions and 382 deletions
+5 -11
View File
@@ -1,4 +1,4 @@
import { parse } from "acorn"
import { parse, type Program } from "acorn"
import { Cause, Effect, Scope } from "effect"
// #transpile: conditional import — full typescript on node/bun, an identity
// pass-through on workerd (the compiler is ~11 MiB and can't init there).
@@ -7,7 +7,7 @@ import type { DataValue, Diagnostic, ResolvedExecutionLimits, Result } from "../
import { toData } from "../data.js"
import { ToolRuntime } from "../tool-runtime.js"
import { normalizeError } from "./errors.js"
import { InterpreterRuntimeError, isRecord, type ProgramNode } from "./model.js"
import { InterpreterRuntimeError } from "./model.js"
import { PromiseRuntime } from "./promises.js"
import { Interpreter } from "./runtime.js"
@@ -107,7 +107,7 @@ export const executeProgram = <R>(
})
}
const parseProgram = (code: string): ProgramNode => {
const parseProgram = (code: string): Program => {
const transpiled = transpile(`async function __codemode__() {\n${code}\n}`)
if (transpiled.error !== undefined) {
@@ -117,19 +117,13 @@ const parseProgram = (code: string): ProgramNode => {
const bodyStart = transpiled.outputText.indexOf("{") + 1
const bodyEnd = transpiled.outputText.lastIndexOf("}")
const executableCode = transpiled.outputText.slice(bodyStart, bodyEnd)
const parsed = parse(executableCode, {
return parse(executableCode, {
ecmaVersion: "latest",
sourceType: "script",
allowReturnOutsideFunction: true,
allowAwaitOutsideFunction: true,
locations: true,
}) as unknown
if (!isRecord(parsed) || parsed.type !== "Program" || !Array.isArray(parsed.body)) {
throw new InterpreterRuntimeError("Failed to parse script as a Program node.")
}
return parsed as ProgramNode
})
}
const utf8ByteLength = (value: string): number => new TextEncoder().encode(value).byteLength
+5 -55
View File
@@ -1,28 +1,11 @@
import type { BlockStatement, Expression, Node, Pattern } from "acorn"
import type { Effect } from "effect"
import type { DiagnosticKind } from "../codemode.js"
import type { SafeObject } from "../data.js"
import type { Values } from "../values.js"
export type SourcePosition = {
line: number
column: number
}
export type SourceLocation = {
start: SourcePosition
end: SourcePosition
}
export type AstNode = {
type: string
loc?: SourceLocation
[key: string]: unknown
}
export type ProgramNode = AstNode & {
type: "Program"
body: Array<AstNode>
}
/** Any parsed node; the interpreter narrows on `type` and reads `loc` for diagnostics. */
export type AstNode = Node
export type Binding = {
mutable: boolean
@@ -43,8 +26,8 @@ export type MemberReference = {
export class CodeModeFunction {
constructor(
readonly parameters: ReadonlyArray<AstNode>,
readonly body: AstNode,
readonly parameters: ReadonlyArray<Pattern>,
readonly body: BlockStatement | Expression,
readonly capturedScopes: ReadonlyArray<Map<string, Binding>>,
readonly async: boolean,
readonly generator: boolean,
@@ -196,39 +179,6 @@ export const unsupportedSyntax = (kind: string, node: AstNode): InterpreterRunti
export const isRecord = (value: unknown): value is Record<string, unknown> =>
typeof value === "object" && value !== null
export const asNode = (value: unknown, context: string): AstNode => {
if (!isRecord(value) || typeof value.type !== "string") {
throw new InterpreterRuntimeError(`Invalid AST node while reading ${context}.`)
}
return value as AstNode
}
export const getArray = (node: AstNode, key: string): Array<unknown> => {
const value = node[key]
if (!Array.isArray(value)) throw new InterpreterRuntimeError(`Expected '${key}' to be an array.`, node)
return value
}
export const getString = (node: AstNode, key: string): string => {
const value = node[key]
if (typeof value !== "string") throw new InterpreterRuntimeError(`Expected '${key}' to be a string.`, node)
return value
}
export const getBoolean = (node: AstNode, key: string): boolean => {
const value = node[key]
if (typeof value !== "boolean") throw new InterpreterRuntimeError(`Expected '${key}' to be a boolean.`, node)
return value
}
export const getOptionalNode = (node: AstNode, key: string): AstNode | undefined => {
const value = node[key]
if (value === undefined || value === null) return undefined
return asNode(value, key)
}
export const getNode = (node: AstNode, key: string): AstNode => asNode(node[key], key)
export const sourceLocation = (node: AstNode): { readonly line: number; readonly column: number } => ({
line: Math.max(1, (node.loc?.start.line ?? 2) - 1),
column: Math.max(1, (node.loc?.start.column ?? 4) - 3),
File diff suppressed because it is too large Load Diff
@@ -236,7 +236,7 @@ describe("CodeMode JSON callback boundaries", () => {
},
"stringify",
[Object.fromEntries([["constructor", 1]])],
{ type: "CallExpression" },
{ type: "CallExpression", start: 0, end: 0 },
),
).toThrow("blocked property 'constructor'")
})
+18 -12
View File
@@ -835,7 +835,7 @@ describe("stdlib integration", () => {
return target
},
})
expect(invokeObjectMethod("assign", [target, source], { type: "CallExpression" })).toBe(target)
expect(invokeObjectMethod("assign", [target, source], { type: "CallExpression", start: 0, end: 0 })).toBe(target)
expect(reads).toEqual([])
expect(Object.hasOwn(target, IteratorSymbol)).toBe(false)
})
@@ -849,7 +849,9 @@ describe("stdlib integration", () => {
return target
},
})
expect(invokeObjectMethod("assign", [target, { nested }], { type: "CallExpression" })).toBe(target)
expect(invokeObjectMethod("assign", [target, { nested }], { type: "CallExpression", start: 0, end: 0 })).toBe(
target,
)
expect(reads).toEqual([])
expect(target).toEqual({ nested })
})
@@ -857,9 +859,9 @@ describe("stdlib integration", () => {
test("Object.assign rejects cycles through supported symbols on nested arrays", () => {
const target = {}
const nested = Object.defineProperty([], IteratorSymbol, { enumerable: true, value: target })
expect(() => invokeObjectMethod("assign", [target, { nested }], { type: "CallExpression" })).toThrow(
"Object.assign result contains a circular value.",
)
expect(() =>
invokeObjectMethod("assign", [target, { nested }], { type: "CallExpression", start: 0, end: 0 }),
).toThrow("Object.assign result contains a circular value.")
expect(Object.hasOwn(target, "nested")).toBe(false)
})
@@ -876,9 +878,9 @@ describe("stdlib integration", () => {
},
},
})
expect(() => invokeObjectMethod("assign", [target, { nested }], { type: "CallExpression" })).toThrow(
"Object.assign result contains a circular value.",
)
expect(() =>
invokeObjectMethod("assign", [target, { nested }], { type: "CallExpression", start: 0, end: 0 }),
).toThrow("Object.assign result contains a circular value.")
expect(reads).toEqual([])
})
@@ -899,7 +901,7 @@ describe("stdlib integration", () => {
},
},
)
expect(() => invokeObjectMethod("assign", [target, source], { type: "CallExpression" })).toThrow(
expect(() => invokeObjectMethod("assign", [target, source], { type: "CallExpression", start: 0, end: 0 })).toThrow(
"Object.assign could not assign property",
)
expect(Reflect.get(target, IteratorSymbol)).toBe(previous)
@@ -987,9 +989,13 @@ describe("stdlib integration", () => {
},
})
const target = {}
expect(invokeObjectMethod("assign", [target, { left: shared, right: shared }], { type: "CallExpression" })).toBe(
target,
)
expect(
invokeObjectMethod("assign", [target, { left: shared, right: shared }], {
type: "CallExpression",
start: 0,
end: 0,
}),
).toBe(target)
expect(target).toEqual({ left: shared, right: shared })
expect(reads).toEqual([true])
})