fix(javascript): preserve builtin identity and share alias traversal

Track module writes and escapes at their owner, preserve capture and reassignment ordering, and share CommonJS/default namespace identity without erasing parent-slot changes. Carry write sites into ambient alias resolution and use parsed write targets across consumers. Deduplicate returned-reference work with cycle context; retain unchanged temporal selection identity so provenance memoization stays effective.

Reviewed fixes include #2041, #2465, #2830, #2881, and #2948. Remove superseded mutation representations and the ineffective per-markValue dedup layer.

Validation: 380 focused JavaScript tests passed under a 384 MiB heap cap, including the previously aborting 30-level/128-call alias regression. Six public CLI/MCP builtin tests passed. Exact pdf.js completed-workflow performance remains unmeasured.
This commit is contained in:
morluto
2026-10-14 08:44:52 +08:00
parent 0f4c484be5
commit 04672f2b91
34 changed files with 3778 additions and 321 deletions
@@ -4,12 +4,12 @@ import type {
JavaScriptSemanticCallable,
JavaScriptSemanticObjectOperation,
} from "./javascriptSemanticIr.js";
import {
semanticCallableIdForNode,
semanticReadsBeforeWrite,
} from "./javascriptSemanticProjection.js";
import { semanticCallableIdForNode } from "./javascriptSemanticProjection.js";
import {
semanticStaticPropertyKey,
semanticWriteSiteReadsFirst,
semanticWriteSiteTarget,
semanticWriteTargets,
unwrapJavaScriptExpression,
} from "./javascriptAstValues.js";
import {
@@ -28,13 +28,21 @@ export const collectJavaScriptSemanticObjects = (
const output: JavaScriptSemanticObjectOperation[] = [];
const callableIds = new Set(callables.map(({ callableId }) => callableId));
const callableStack: string[] = [];
const context: ObjectCollectionContext = { state, output };
const context: ObjectCollectionContext = {
state,
output,
writeTargets: new Map(),
};
traverseJavaScriptAst(program, {
enter: (node, parent) => {
const callableId = semanticCallableIdForNode(node);
if (callableId !== null && callableIds.has(callableId))
callableStack.push(callableId);
const ownerCallableId = callableStack.at(-1) ?? null;
const writeTarget = semanticWriteSiteTarget(node);
if (writeTarget !== null)
for (const leaf of semanticWriteTargets(writeTarget))
if (!t.isIdentifier(leaf)) context.writeTargets.set(leaf, node);
if (t.isMemberExpression(node) || t.isOptionalMemberExpression(node))
collectMember(node, parent, ownerCallableId, context);
else if (
@@ -73,6 +81,7 @@ export const collectJavaScriptSemanticObjects = (
interface ObjectCollectionContext {
readonly state: JavaScriptSemanticAnalysisState;
readonly output: JavaScriptSemanticObjectOperation[];
readonly writeTargets: Map<t.Node, t.Node>;
}
const collectMember = (
@@ -90,10 +99,11 @@ const collectMember = (
)
return;
const identity = expressionObjectIdentity(node, state);
const write =
(t.isAssignmentExpression(parent) && parent.left === node) ||
(t.isUpdateExpression(parent) && parent.argument === node);
const readsBeforeWrite = write && semanticReadsBeforeWrite(node, parent);
const writeSite = context.writeTargets.get(node);
context.writeTargets.delete(node);
const write = writeSite !== undefined;
const readsBeforeWrite =
writeSite !== undefined && semanticWriteSiteReadsFirst(writeSite);
if (readsBeforeWrite)
addObjectOperation(
{