mirror of
https://github.com/morluto/rea.git
synced 2026-11-21 06:04:03 +00:00
* fix(javascript): retain array-pattern iterator close effects * fix(javascript): bound array-pattern close exposure * fix(javascript): follow stored array elements at iterator close * fix(javascript): retain iterator closes before array rest
216 lines
7.6 KiB
TypeScript
216 lines
7.6 KiB
TypeScript
import * as t from "@babel/types";
|
|
import type { JavaScriptSemanticAnalysisState } from "./javascriptSemanticState.js";
|
|
import {
|
|
semanticArrayIndex,
|
|
type JavaScriptSemanticPropertySelection,
|
|
type JavaScriptSemanticPropertyPath as PropertyPath,
|
|
} from "./javascriptSemanticPropertyPaths.js";
|
|
import {
|
|
evaluateSemanticExpression,
|
|
semanticValueDefinitelyDefined,
|
|
} from "./javascriptSemanticValues.js";
|
|
import { semanticSlotAtPath } from "./javascriptSemanticSlots.js";
|
|
import type { JavaScriptSemanticValue } from "./javascriptSemanticValueTypes.js";
|
|
import { ambientPathMayObserveAt } from "./javascriptSemanticEffectLog.js";
|
|
|
|
/**
|
|
* Node's wrapper module inherits require. A copy cannot read that delegate
|
|
* unless an earlier own-slot write or opaque module escape may have exposed it.
|
|
*/
|
|
export const semanticCommonJsRequireOwnEnumerableAt = (
|
|
capture: t.Node,
|
|
state: JavaScriptSemanticAnalysisState,
|
|
): boolean =>
|
|
ambientPathMayObserveAt(
|
|
state,
|
|
capture,
|
|
["commonjs-module", "require"],
|
|
"contents",
|
|
);
|
|
|
|
interface ReferenceSource {
|
|
readonly node: t.Node;
|
|
readonly projection: PropertyPath;
|
|
readonly iterationSources?: readonly ReferenceSource[];
|
|
}
|
|
|
|
/** Shared intrinsic reached through a native array iterator's prototype. */
|
|
export const semanticArrayIteratorPrototypePath = [
|
|
"array-iterator-prototype",
|
|
] as const;
|
|
const arrayIteratorNextPath = [
|
|
...semanticArrayIteratorPrototypePath,
|
|
"next",
|
|
] as const;
|
|
|
|
/** Native array iteration preserves numeric slots only while its iterator is intact. */
|
|
export const semanticKnownArrayIteration = (
|
|
value: JavaScriptSemanticValue | undefined,
|
|
state: JavaScriptSemanticAnalysisState,
|
|
read: t.Node,
|
|
): value is Extract<JavaScriptSemanticValue, { status: "array" }> =>
|
|
!state.arrayIterationUnknown &&
|
|
value?.status === "array" &&
|
|
!ambientPathMayObserveAt(state, read, arrayIteratorNextPath, "contents") &&
|
|
value.items.every((item) => semanticArrayIndex(item.name) !== null);
|
|
|
|
const arrayIteratorReturnPaths = [
|
|
[...semanticArrayIteratorPrototypePath, "return"],
|
|
[...semanticArrayIteratorPrototypePath, "__proto__", "return"],
|
|
[...semanticArrayIteratorPrototypePath, "__proto__", "__proto__", "return"],
|
|
["global", "Iterator", "prototype", "return"],
|
|
["global", "Object", "prototype", "return"],
|
|
] as const;
|
|
|
|
/** References a possible native close hook can still obtain by advancing its iterator. */
|
|
export const semanticArrayPatternCloseSelection = (
|
|
pattern: t.ArrayPattern,
|
|
owner: t.Node | undefined,
|
|
array: Extract<JavaScriptSemanticValue, { status: "array" }>,
|
|
state: JavaScriptSemanticAnalysisState,
|
|
): JavaScriptSemanticPropertySelection | undefined => {
|
|
if (
|
|
!arrayIteratorReturnPaths.some((path) =>
|
|
ambientPathMayObserveAt(state, pattern, path, "contents"),
|
|
)
|
|
)
|
|
return undefined;
|
|
const declaration =
|
|
owner === undefined ? undefined : state.parentsByNode.get(owner);
|
|
const last = pattern.elements.at(-1);
|
|
const hasRest = t.isRestElement(last);
|
|
let consumed = 0;
|
|
if (
|
|
t.isVariableDeclarator(owner) &&
|
|
t.isVariableDeclaration(declaration) &&
|
|
(declaration.kind === "const" || declaration.kind === "let")
|
|
) {
|
|
for (const [index, element] of pattern.elements.entries()) {
|
|
// A lexical rest drains the iterator only after earlier pattern work
|
|
// reaches it. A preceding default or nested binding can throw first.
|
|
if (hasRest && element === last) return undefined;
|
|
const slot = array.items[index];
|
|
const ownSlot =
|
|
consumed === index &&
|
|
slot?.name === String(index) &&
|
|
slot.presence === "present";
|
|
if (
|
|
element !== null &&
|
|
!t.isIdentifier(element) &&
|
|
!(
|
|
hasRest &&
|
|
owner.id === pattern &&
|
|
ownSlot &&
|
|
t.isAssignmentPattern(element) &&
|
|
t.isIdentifier(element.left) &&
|
|
semanticValueDefinitelyDefined(slot.value)
|
|
)
|
|
)
|
|
break;
|
|
if (ownSlot) consumed += 1;
|
|
else if (!hasRest) break;
|
|
}
|
|
}
|
|
// The literal suffix can be empty while the iterator still has references
|
|
// from stored writes. Resolve those separately at the close site.
|
|
return { excludedKeys: [], startIndex: consumed };
|
|
};
|
|
|
|
/** Invocation spreads use the same native-iteration proof at their source read. */
|
|
export const semanticKnownArrayIterationAt = (
|
|
node: t.Node,
|
|
state: JavaScriptSemanticAnalysisState,
|
|
): boolean =>
|
|
semanticKnownArrayIteration(
|
|
evaluateSemanticExpression(node, state, node),
|
|
state,
|
|
node,
|
|
);
|
|
|
|
/** Numeric destructuring projections do not select slots of custom iterators. */
|
|
export const semanticReferenceIterationUnknown = (
|
|
source: { readonly iterationSources?: readonly ReferenceSource[] },
|
|
state: JavaScriptSemanticAnalysisState,
|
|
): boolean =>
|
|
source.iterationSources?.some(
|
|
(iterable) => !selectedReferenceIterationKnown(iterable, state),
|
|
) === true;
|
|
|
|
const selectedReferenceIterationKnown = (
|
|
source: ReferenceSource,
|
|
state: JavaScriptSemanticAnalysisState,
|
|
): boolean => {
|
|
let values = [evaluateSemanticExpression(source.node, state, source.node)];
|
|
for (const key of source.projection) {
|
|
if (typeof key === "object" && key !== null) return false;
|
|
const selected = new Set<JavaScriptSemanticValue>();
|
|
for (const value of values) {
|
|
if (key === null) {
|
|
// for-of projects any position of a native array. Its finite slot
|
|
// inventory can prove every possible nested iterable without treating
|
|
// a computed object property or custom iterator as an own-index read.
|
|
if (
|
|
!semanticKnownArrayIteration(value, state, source.node) ||
|
|
value.unknownItems
|
|
)
|
|
return false;
|
|
for (const slot of value.items) {
|
|
if (slot.presence !== "present") return false;
|
|
selected.add(slot.value);
|
|
}
|
|
} else {
|
|
const slot = semanticSlotAtPath(value, [String(key)]);
|
|
if (slot.presence !== "present") return false;
|
|
selected.add(slot.value);
|
|
}
|
|
}
|
|
values = [...selected];
|
|
}
|
|
return values.every((value) =>
|
|
semanticKnownArrayIteration(value, state, source.node),
|
|
);
|
|
};
|
|
|
|
/** Exclude references whose source slot or destructuring fallback is absent. */
|
|
export const semanticReferenceSourcesInactive = (
|
|
reference: {
|
|
readonly iterationSources?: readonly ReferenceSource[];
|
|
readonly requiredSources?: readonly ReferenceSource[];
|
|
readonly fallbackSources?: readonly ReferenceSource[];
|
|
},
|
|
state: JavaScriptSemanticAnalysisState,
|
|
): boolean =>
|
|
reference.requiredSources?.some(
|
|
(source) =>
|
|
!semanticReferenceIterationUnknown(source, state) &&
|
|
semanticSelectedReferenceSlot(source, state, source.node)?.presence ===
|
|
"absent",
|
|
) === true ||
|
|
reference.fallbackSources?.some((source) => {
|
|
// Selection precedes effects through the chosen reference. A later escape
|
|
// cannot make a fallback that was excluded at destructuring reachable.
|
|
const slot = semanticSelectedReferenceSlot(source, state, source.node);
|
|
return (
|
|
!semanticReferenceIterationUnknown(source, state) &&
|
|
slot?.presence === "present" &&
|
|
semanticValueDefinitelyDefined(slot.value)
|
|
);
|
|
}) === true;
|
|
|
|
/** Select a retained source slot at its capture, before later effects. */
|
|
export const semanticSelectedReferenceSlot = (
|
|
source: ReferenceSource,
|
|
state: JavaScriptSemanticAnalysisState,
|
|
capturePoint?: t.Node,
|
|
) => {
|
|
const path: string[] = [];
|
|
for (const key of source.projection) {
|
|
if (key === null || typeof key === "object") return undefined;
|
|
path.push(String(key));
|
|
}
|
|
return semanticSlotAtPath(
|
|
evaluateSemanticExpression(source.node, state, capturePoint),
|
|
path,
|
|
);
|
|
};
|