mirror of
https://github.com/morluto/rea.git
synced 2026-11-20 13:45:30 +00:00
* fix(javascript): retain proven property presence at reads The read-time IR and projection boundary builds on the destructuring work in #7716. This change adds an indexed own-slot proof and uses it for ordinary member reads as well. Co-authored-by: SII-YDD <38822679+SII-YDD@users.noreply.github.com> * fix(javascript): retain destructuring proof in read order --------- Co-authored-by: SII-YDD <38822679+SII-YDD@users.noreply.github.com>
316 lines
9.8 KiB
TypeScript
316 lines
9.8 KiB
TypeScript
import * as t from "@babel/types";
|
|
|
|
import type {
|
|
JavaScriptSemanticCallable,
|
|
JavaScriptSemanticObjectOperation,
|
|
} from "./javascriptSemanticIr.js";
|
|
import { createSemanticCallableIdStack } from "./javascriptSemanticCallableStack.js";
|
|
import {
|
|
semanticStaticPropertyKey,
|
|
semanticWriteSiteReadsFirst,
|
|
semanticWriteSiteTarget,
|
|
semanticWriteTargets,
|
|
unwrapJavaScriptExpression,
|
|
} from "./javascriptAstValues.js";
|
|
import {
|
|
resolveSemanticBindingState,
|
|
type JavaScriptSemanticAnalysisState,
|
|
} from "./javascriptSemanticState.js";
|
|
import { traverseJavaScriptValueAst } from "./javascriptSemanticTraversal.js";
|
|
import { range } from "./javascriptStaticAnalysisHelpers.js";
|
|
import { createSemanticReadPresenceQuery } from "./javascriptSemanticValues.js";
|
|
|
|
/** Recover bounded static property reads, writes, spreads, and destructuring. */
|
|
export const collectJavaScriptSemanticObjects = (
|
|
program: t.Program,
|
|
state: JavaScriptSemanticAnalysisState,
|
|
callables: readonly JavaScriptSemanticCallable[],
|
|
): JavaScriptSemanticObjectOperation[] => {
|
|
const output: JavaScriptSemanticObjectOperation[] = [];
|
|
const callableIds = new Set(callables.map(({ callableId }) => callableId));
|
|
const callableStack = createSemanticCallableIdStack(callableIds);
|
|
const context: ObjectCollectionContext = {
|
|
state,
|
|
output,
|
|
writeTargets: new Map(),
|
|
readPresence: createSemanticReadPresenceQuery(state),
|
|
};
|
|
traverseJavaScriptValueAst(program, state.erasedSyntaxNodes, {
|
|
enter: (node, parent) => {
|
|
callableStack.enter(node, parent);
|
|
const ownerCallableId = callableStack.callables.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 (
|
|
t.isSpreadElement(node) &&
|
|
parent !== null &&
|
|
t.isObjectExpression(parent)
|
|
) {
|
|
const identity = expressionObjectIdentity(node.argument, state);
|
|
addObjectOperation(
|
|
{
|
|
node,
|
|
kind: "spread",
|
|
ownerCallableId,
|
|
objectBindingId: identity.bindingId,
|
|
targetBindingId: null,
|
|
propertyPath: identity.path,
|
|
},
|
|
state,
|
|
output,
|
|
);
|
|
} else if (
|
|
t.isVariableDeclarator(node) &&
|
|
(t.isObjectPattern(node.id) || t.isArrayPattern(node.id))
|
|
)
|
|
collectDestructuring(node.id, node.init, ownerCallableId, context);
|
|
else if (
|
|
t.isAssignmentExpression(node, { operator: "=" }) &&
|
|
(t.isObjectPattern(node.left) || t.isArrayPattern(node.left))
|
|
)
|
|
collectDestructuring(node.left, node.right, ownerCallableId, context);
|
|
},
|
|
exit: (node) => callableStack.exit(node),
|
|
});
|
|
return output;
|
|
};
|
|
|
|
interface ObjectCollectionContext {
|
|
readonly state: JavaScriptSemanticAnalysisState;
|
|
readonly output: JavaScriptSemanticObjectOperation[];
|
|
readonly writeTargets: Map<t.Node, t.Node>;
|
|
readonly readPresence: ReturnType<typeof createSemanticReadPresenceQuery>;
|
|
}
|
|
|
|
const collectMember = (
|
|
node: t.MemberExpression | t.OptionalMemberExpression,
|
|
parent: t.Node | null,
|
|
ownerCallableId: string | null,
|
|
context: ObjectCollectionContext,
|
|
): void => {
|
|
const { state, output } = context;
|
|
// A chain is one property access; retaining every prefix duplicates work and
|
|
// would make full paths quadratic in deeply nested producer expressions.
|
|
if (
|
|
(t.isMemberExpression(parent) || t.isOptionalMemberExpression(parent)) &&
|
|
parent.object === node
|
|
)
|
|
return;
|
|
const identity = expressionObjectIdentity(node, state);
|
|
const writeSite = context.writeTargets.get(node);
|
|
context.writeTargets.delete(node);
|
|
const write = writeSite !== undefined;
|
|
const readsBeforeWrite =
|
|
writeSite !== undefined && semanticWriteSiteReadsFirst(writeSite);
|
|
if (readsBeforeWrite)
|
|
addObjectOperation(
|
|
{
|
|
node,
|
|
kind: "read",
|
|
ownerCallableId,
|
|
objectBindingId: identity.bindingId,
|
|
targetBindingId: null,
|
|
propertyPath: identity.path,
|
|
},
|
|
state,
|
|
output,
|
|
);
|
|
addObjectOperation(
|
|
{
|
|
node,
|
|
kind: write ? "write" : "read",
|
|
provenOwnSlot:
|
|
!write &&
|
|
identity.path !== null &&
|
|
context.readPresence(identity.bindingId, node)?.(identity.path) ===
|
|
true,
|
|
ownerCallableId,
|
|
objectBindingId: identity.bindingId,
|
|
targetBindingId: null,
|
|
propertyPath: identity.path,
|
|
},
|
|
state,
|
|
output,
|
|
);
|
|
};
|
|
|
|
const collectDestructuring = (
|
|
root: t.Node,
|
|
initializer: t.Node | null | undefined,
|
|
ownerCallableId: string | null,
|
|
{ state, output, readPresence }: ObjectCollectionContext,
|
|
): void => {
|
|
const source = expressionObjectIdentity(initializer, state);
|
|
let ownSlot =
|
|
initializer == null || source.path === null
|
|
? undefined
|
|
: readPresence(source.bindingId, initializer);
|
|
// An unsupported property still occupies its place in the pattern's
|
|
// evaluation order, even when it cannot produce a graph operation.
|
|
const pending: ({
|
|
readonly pattern: t.Node;
|
|
readonly path: readonly string[];
|
|
readonly lookup?: "own" | "unproven";
|
|
} | null)[] = [{ pattern: root, path: [] }];
|
|
while (pending.length > 0) {
|
|
const next = pending.pop();
|
|
if (next === undefined) break;
|
|
if (next === null) {
|
|
ownSlot = undefined;
|
|
continue;
|
|
}
|
|
const { path } = next;
|
|
const propertyPath =
|
|
source.path === null ? null : [...source.path, ...path];
|
|
if (
|
|
next.lookup === "unproven" ||
|
|
(next.lookup === "own" &&
|
|
ownSlot !== undefined &&
|
|
propertyPath !== null &&
|
|
!ownSlot(propertyPath))
|
|
)
|
|
ownSlot = undefined;
|
|
const occurrence = unwrapJavaScriptExpression(next.pattern).node;
|
|
if (t.isAssignmentPattern(occurrence)) ownSlot = undefined;
|
|
const pattern = t.isAssignmentPattern(occurrence)
|
|
? unwrapJavaScriptExpression(occurrence.left).node
|
|
: occurrence;
|
|
if (t.isObjectPattern(pattern)) {
|
|
for (const property of pattern.properties.toReversed()) {
|
|
if (!t.isObjectProperty(property)) {
|
|
pending.push(null);
|
|
continue;
|
|
}
|
|
const name = semanticStaticPropertyKey(property.key, property.computed);
|
|
if (name === null) {
|
|
pending.push(null);
|
|
continue;
|
|
}
|
|
pending.push({
|
|
pattern: property.value,
|
|
path: [...path, name],
|
|
lookup: property.computed ? "unproven" : "own",
|
|
});
|
|
}
|
|
continue;
|
|
}
|
|
if (t.isArrayPattern(pattern)) {
|
|
ownSlot = undefined;
|
|
for (let index = pattern.elements.length - 1; index >= 0; index -= 1) {
|
|
const element = pattern.elements[index];
|
|
if (
|
|
element !== null &&
|
|
element !== undefined &&
|
|
!t.isRestElement(element)
|
|
)
|
|
pending.push({ pattern: element, path: [...path, String(index)] });
|
|
}
|
|
continue;
|
|
}
|
|
if (!t.isIdentifier(pattern)) {
|
|
ownSlot = undefined;
|
|
continue;
|
|
}
|
|
const targetBinding = resolveSemanticBindingState(
|
|
state,
|
|
pattern,
|
|
pattern.name,
|
|
);
|
|
if (targetBinding === undefined) ownSlot = undefined;
|
|
addObjectOperation(
|
|
{
|
|
node: occurrence,
|
|
kind: "destructure",
|
|
ownerCallableId,
|
|
objectBindingId: source.bindingId,
|
|
targetBindingId: targetBinding?.bindingId ?? null,
|
|
propertyPath,
|
|
provenOwnSlot: ownSlot !== undefined,
|
|
},
|
|
state,
|
|
output,
|
|
);
|
|
}
|
|
};
|
|
|
|
interface AddObjectOperationInput {
|
|
readonly node: t.Node;
|
|
readonly kind: JavaScriptSemanticObjectOperation["kind"];
|
|
readonly ownerCallableId: string | null;
|
|
readonly objectBindingId: string | null;
|
|
readonly targetBindingId: string | null;
|
|
readonly propertyPath: readonly string[] | null;
|
|
readonly provenOwnSlot?: boolean;
|
|
}
|
|
|
|
const addObjectOperation = (
|
|
input: AddObjectOperationInput,
|
|
state: JavaScriptSemanticAnalysisState,
|
|
output: JavaScriptSemanticObjectOperation[],
|
|
): void => {
|
|
output.push({
|
|
// These high-volume IDs must not retain intermediate concatenations.
|
|
objectOperationId: [
|
|
"object",
|
|
input.kind,
|
|
String(input.node.start ?? -1),
|
|
String(input.node.end ?? -1),
|
|
].join(":"),
|
|
kind: input.kind,
|
|
...(input.provenOwnSlot === true
|
|
? { sourcePresence: "present" as const }
|
|
: {}),
|
|
location: range(input.node),
|
|
ownerCallableId: input.ownerCallableId,
|
|
objectBindingId: input.objectBindingId,
|
|
targetBindingId: input.targetBindingId,
|
|
propertyPath: input.propertyPath,
|
|
resolution:
|
|
input.objectBindingId === null ||
|
|
input.propertyPath === null ||
|
|
(input.kind === "destructure" && input.targetBindingId === null)
|
|
? "partial"
|
|
: "complete",
|
|
});
|
|
};
|
|
|
|
interface ObjectIdentity {
|
|
readonly bindingId: string | null;
|
|
readonly path: readonly string[] | null;
|
|
}
|
|
|
|
const expressionObjectIdentity = (
|
|
node: t.Node | null | undefined,
|
|
state: JavaScriptSemanticAnalysisState,
|
|
): ObjectIdentity => {
|
|
const path: string[] = [];
|
|
let current = node;
|
|
let staticPath = true;
|
|
while (current !== null && current !== undefined) {
|
|
current = unwrapJavaScriptExpression(current).node;
|
|
if (
|
|
t.isMemberExpression(current) ||
|
|
t.isOptionalMemberExpression(current)
|
|
) {
|
|
const name = semanticStaticPropertyKey(
|
|
current.property,
|
|
current.computed,
|
|
);
|
|
if (name === null) staticPath = false;
|
|
else path.push(name);
|
|
current = current.object;
|
|
} else break;
|
|
}
|
|
return {
|
|
bindingId: t.isIdentifier(current)
|
|
? (resolveSemanticBindingState(state, current, current.name)?.bindingId ??
|
|
null)
|
|
: null,
|
|
path: staticPath ? path.reverse() : null,
|
|
};
|
|
};
|