mirror of
https://github.com/morluto/rea.git
synced 2026-11-20 13:45:30 +00:00
fix(javascript): retain iterator prototype effects through array spreads (#12563)
* fix(javascript): retain ambient references through array spread * fix(javascript): retain spread aliases to inherited iterator close hooks * fix(javascript): share dense inline array spread layout (#12564) * fix(javascript): share dense inline array spread layout * fix(javascript): report exhausted dense spread layout allowance * fix(javascript): preserve getter receivers through array spread * fix(javascript): guard native array iteration from opaque global code * test(javascript): group spread receiver controls * test(javascript): preserve getter fixture tuple types * fix(javascript): preserve spread slot projections for iterator receivers
This commit is contained in:
1 parent
1a070dee80
commit
7cb3f1090c
20 files changed
+907
-98
No files matched your search
@@ -149,6 +149,46 @@ it("projects bounded alias selection as a resource-limit unknown", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it.each([
|
||||
["a bounded inline spread", "[...[1]]", 1, true],
|
||||
["a backed inline spread", "[...[1]]", 2, false],
|
||||
[
|
||||
"an unsupported spread after a backed spread",
|
||||
"[...[1], ...opaque]",
|
||||
2,
|
||||
false,
|
||||
],
|
||||
["a second inline spread", "[...[1], ...[2]]", 2, true],
|
||||
] as const)(
|
||||
"reports the alias selection limit only when it blocks %s",
|
||||
(_name, array, aliasSelections, bounded) => {
|
||||
const source = `export const answer = ${array};`;
|
||||
const ir = analyzeJavaScriptSemantics(source, {
|
||||
alias_selections: aliasSelections,
|
||||
});
|
||||
const graph = graphFor(source, ir);
|
||||
const unknown = graph.unknowns.find(
|
||||
({ reason, detail }) =>
|
||||
reason === "resource-limit" &&
|
||||
/Alias selection expansion/u.test(detail),
|
||||
);
|
||||
if (bounded) {
|
||||
expect(ir.coverage).toMatchObject({
|
||||
status: "partial",
|
||||
resourceLimits: ["alias-selections"],
|
||||
});
|
||||
expect(unknown?.detail).toContain(
|
||||
`semantic_precision.alias_selections=${aliasSelections}`,
|
||||
);
|
||||
} else {
|
||||
expect(ir.coverage.resourceLimits ?? []).not.toContain(
|
||||
"alias-selections",
|
||||
);
|
||||
expect(unknown).toBeUndefined();
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
it.each([
|
||||
["plain export", "const answer = 42; export { answer };", 0],
|
||||
["aliased export", "const answer = 42; export { answer as result };", 0],
|
||||
|
||||
@@ -1,6 +1,239 @@
|
||||
import { expect, it } from "vitest";
|
||||
import { graphForJavaScript } from "../../../tests/fixtures/javascriptSemanticGraph.js";
|
||||
|
||||
it.each([
|
||||
[
|
||||
"reflected prototype",
|
||||
"const protos=[...[Object.getPrototypeOf([])]];protos[0][Symbol.iterator]=function*(){};",
|
||||
"candidate",
|
||||
],
|
||||
[
|
||||
"reflected iterator next",
|
||||
"const protos=[...[Reflect.getPrototypeOf([].values())]];Reflect.set(protos[0], 'next', ()=>({done:true}));",
|
||||
"candidate",
|
||||
],
|
||||
[
|
||||
"reflective receiver in an earlier call",
|
||||
"configure();function configure(){const protos=[...[Reflect.getPrototypeOf([])]];protos[0][Symbol.iterator]=function*(){}}",
|
||||
"candidate",
|
||||
],
|
||||
[
|
||||
"a stored prototype passed to an earlier call",
|
||||
"const protos=[...[Object.getPrototypeOf([])]];disable(protos[0]);function disable(p){p[Symbol.iterator]=function*(){}}",
|
||||
"candidate",
|
||||
],
|
||||
[
|
||||
"an unknown projected index",
|
||||
"const protos=[...[Object.getPrototypeOf([])]];protos[index][Symbol.iterator]=function*(){};",
|
||||
"candidate",
|
||||
],
|
||||
[
|
||||
"an array operand borrowed through an alias",
|
||||
"const source=[Object.getPrototypeOf([])];const protos=[...source];protos[0][Symbol.iterator]=function*(){};",
|
||||
"candidate",
|
||||
],
|
||||
[
|
||||
"a prototype selected by destructuring",
|
||||
"const protos=[...[Object.getPrototypeOf([])]];const [p]=protos;p[Symbol.iterator]=function*(){};",
|
||||
"candidate",
|
||||
],
|
||||
[
|
||||
"a prototype selected by nested destructuring",
|
||||
"const [[p]]=[...[[Object.getPrototypeOf([])]]];p[Symbol.iterator]=function*(){};",
|
||||
"candidate",
|
||||
],
|
||||
[
|
||||
"a quoted index",
|
||||
'const protos=[...[Object.getPrototypeOf([])]];protos["0"][Symbol.iterator]=function*(){};',
|
||||
"candidate",
|
||||
],
|
||||
[
|
||||
"a folded index",
|
||||
"const protos=[...[Object.getPrototypeOf([])]];protos[0+0][Symbol.iterator]=function*(){};",
|
||||
"candidate",
|
||||
],
|
||||
[
|
||||
"an earlier disjoint slot",
|
||||
"const protos=[{},...[Object.getPrototypeOf([])]];protos[0][Symbol.iterator]=function*(){};",
|
||||
"resolved",
|
||||
],
|
||||
[
|
||||
"a shadowed prototype reader",
|
||||
"const Object={getPrototypeOf(){return {}}};const protos=[...[Object.getPrototypeOf([])]];protos[0][Symbol.iterator]=function*(){};",
|
||||
"resolved",
|
||||
],
|
||||
] as const)(
|
||||
"keeps iterator evidence sound through %s stored in an array with spread",
|
||||
(_name, edit, resolution) => {
|
||||
const graph = graphForJavaScript(
|
||||
`${edit} const pairs=[["before"]];const [[selected]]=pairs;pairs[0][0]="later";`,
|
||||
);
|
||||
const target = graph.nodes.find(
|
||||
(node) => node.kind === "binding" && node.label === "selected",
|
||||
);
|
||||
const relation = graph.relations.find(
|
||||
(edge) =>
|
||||
edge.relation === "destructures" &&
|
||||
edge.target_node_id === target?.node_id,
|
||||
);
|
||||
expect(relation).toMatchObject({ resolution });
|
||||
},
|
||||
);
|
||||
|
||||
it.each([
|
||||
[
|
||||
"a copied slot",
|
||||
'const items=[...["old"]];const [selected]=items;items[0]="late";',
|
||||
["0"],
|
||||
"resolved",
|
||||
],
|
||||
[
|
||||
"indices after a dense spread",
|
||||
'const items=[...["prefix"],"old"];const [,selected]=items;items[1]="late";',
|
||||
["1"],
|
||||
"resolved",
|
||||
],
|
||||
[
|
||||
"nested inline spread",
|
||||
'const items=[...[...["prefix","old"]]];const [,selected]=items;items[1]="late";',
|
||||
["1"],
|
||||
"resolved",
|
||||
],
|
||||
[
|
||||
"an empty spread",
|
||||
'const items=[...[],"old"];const [selected]=items;items[0]="late";',
|
||||
["0"],
|
||||
"resolved",
|
||||
],
|
||||
[
|
||||
"an object reached after a spread",
|
||||
'const items=[...[{mode:"a"}],{mode:"b"}];const [,{mode:selected}]=items;items[1].mode="late";',
|
||||
["1", "mode"],
|
||||
"resolved",
|
||||
],
|
||||
[
|
||||
"an opaque operand",
|
||||
'const items=[...other,"old"];const [selected]=items;items[0]="late";',
|
||||
["0"],
|
||||
"candidate",
|
||||
],
|
||||
[
|
||||
"a sparse operand",
|
||||
'const items=[...[,],"old"];const [,selected]=items;items[1]="late";',
|
||||
["1"],
|
||||
"candidate",
|
||||
],
|
||||
[
|
||||
"iterator replaced during operand evaluation",
|
||||
'const items=[...[(Object.getPrototypeOf([].values()).next=()=>({done:true}),"x")],"old"];const [selected]=items;items[0]="late";',
|
||||
["0"],
|
||||
"candidate",
|
||||
],
|
||||
[
|
||||
"iterator replaced between spread and pattern",
|
||||
'const items=[...["x"],"old"];Object.getPrototypeOf([].values()).next=()=>({done:true});const [,selected]=items;',
|
||||
["1"],
|
||||
"candidate",
|
||||
],
|
||||
[
|
||||
"iterator replaced after both acquisitions",
|
||||
'const items=[...["x"],"old"];const [,selected]=items;Object.getPrototypeOf([].values()).next=()=>({done:true});items[1]="late";',
|
||||
["1"],
|
||||
"resolved",
|
||||
],
|
||||
] as const)(
|
||||
"tracks %s in an array literal",
|
||||
(_name, source, path, resolution) => {
|
||||
const graph = graphForJavaScript(source);
|
||||
const target = graph.nodes.find(
|
||||
(node) => node.kind === "binding" && node.label === "selected",
|
||||
);
|
||||
const relation = graph.relations.find(
|
||||
(edge) =>
|
||||
edge.relation === "destructures" &&
|
||||
edge.target_node_id === target?.node_id,
|
||||
);
|
||||
expect(relation).toMatchObject({ resolution });
|
||||
expect(
|
||||
graph.nodes.find((node) => node.node_id === relation?.source_node_id)
|
||||
?.properties.property_path,
|
||||
).toEqual(path);
|
||||
},
|
||||
);
|
||||
|
||||
it.each([
|
||||
[
|
||||
"an earlier call",
|
||||
"configure();",
|
||||
"function configure() { Array.prototype[Symbol.iterator] = function* () {}; }",
|
||||
],
|
||||
[
|
||||
"a reflected prototype alias",
|
||||
"const proto = Object.getPrototypeOf([]); proto[Symbol.iterator] = function* () {};",
|
||||
"",
|
||||
],
|
||||
[
|
||||
"an earlier call through a reflected alias",
|
||||
"configure();",
|
||||
"function configure() { const proto = Reflect.getPrototypeOf([]); proto[Symbol.iterator] = function* () {}; }",
|
||||
],
|
||||
[
|
||||
"a reflected prototype stored by spread",
|
||||
"const protos = [...[Object.getPrototypeOf([])]]; protos[0][Symbol.iterator] = function* () {};",
|
||||
"",
|
||||
],
|
||||
[
|
||||
"a reflected iterator prototype stored by spread",
|
||||
"const protos = [...[Reflect.getPrototypeOf([].values())]]; Reflect.set(protos[0], 'next', () => ({ done: true }));",
|
||||
"",
|
||||
],
|
||||
])(
|
||||
"keeps the selected reference uncertain when %s replaces the spread iterator",
|
||||
(_name, before, after) => {
|
||||
const graph = graphForJavaScript(`
|
||||
const source = {mode: "source"}, tail = {mode: "tail"};
|
||||
${before}
|
||||
const items = [...[source], tail];
|
||||
items[0].mode = "changed";
|
||||
const {mode: selected} = tail;
|
||||
${after}
|
||||
`);
|
||||
const target = graph.nodes.find(
|
||||
(node) => node.kind === "binding" && node.label === "selected",
|
||||
);
|
||||
expect(
|
||||
graph.relations.find(
|
||||
(edge) =>
|
||||
edge.relation === "destructures" &&
|
||||
edge.target_node_id === target?.node_id,
|
||||
),
|
||||
).toMatchObject({ resolution: "candidate" });
|
||||
},
|
||||
);
|
||||
|
||||
it.each([
|
||||
[0, "a", "candidate"],
|
||||
[0, "b", "resolved"],
|
||||
[1, "a", "resolved"],
|
||||
[1, "b", "candidate"],
|
||||
] as const)(
|
||||
"retains only actual copies after an array literal write to slot %s, read of %s",
|
||||
(index, receiver, resolution) => {
|
||||
const graph = graphForJavaScript(
|
||||
`const a={mode:"a"},b={mode:"b"};const items=[...[a],b];items[${index}].mode="changed";const {mode:selected}=${receiver};`,
|
||||
);
|
||||
const target = graph.nodes.find(
|
||||
(node) => node.kind === "binding" && node.label === "selected",
|
||||
);
|
||||
const relation = graph.relations.find(
|
||||
(edge) =>
|
||||
edge.relation === "destructures" &&
|
||||
edge.target_node_id === target?.node_id,
|
||||
);
|
||||
expect(relation).toMatchObject({ resolution });
|
||||
},
|
||||
);
|
||||
|
||||
it.each([
|
||||
"const { mode: selected, fallback = 0 } = config;",
|
||||
'const { mode: selected, ["fallback"]: later } = config;',
|
||||
@@ -189,6 +422,41 @@ it.each([
|
||||
"const iterator = Object.create({ next: original }); Object.getPrototypeOf(iterator).next = replacement;",
|
||||
"resolved",
|
||||
],
|
||||
[
|
||||
"custom iterator from an inline spread slot",
|
||||
"const items = [...[{ values() { return {}; } }]]; Object.getPrototypeOf(items[0].values()).next = replacement;",
|
||||
"resolved",
|
||||
],
|
||||
[
|
||||
"native array from an inline spread slot",
|
||||
"const items = [0, ...[[]]]; Object.getPrototypeOf(items[1].values()).next = replacement;",
|
||||
"candidate",
|
||||
],
|
||||
[
|
||||
"typed array from a borrowed spread slot",
|
||||
"const source = [new Uint8Array()]; const items = [...source]; Object.getPrototypeOf(items[0].values()).next = replacement;",
|
||||
"candidate",
|
||||
],
|
||||
[
|
||||
"a Map copied by a fresh spread before a later member store",
|
||||
"const source = { value: new Map() }; const items = [0, ...[source.value]]; source.value = []; Object.getPrototypeOf(items[1].values()).next = replacement;",
|
||||
"resolved",
|
||||
],
|
||||
[
|
||||
"native array written to a source slot before the spread",
|
||||
"const source = [new Map()]; source[0] = []; const items = [...source]; Object.getPrototypeOf(items[0].values()).next = replacement;",
|
||||
"candidate",
|
||||
],
|
||||
[
|
||||
"source array yielded by its custom iterator",
|
||||
"const source = [{ values() { return {}; } }]; source[Symbol.iterator] = function* () { yield source; }; const items = [...source]; Object.getPrototypeOf(items[0].values()).next = replacement;",
|
||||
"candidate",
|
||||
],
|
||||
[
|
||||
"borrowed source whose iterator was installed by a descriptor",
|
||||
"const source = [{}]; Object.defineProperty(source, Symbol.iterator, { value: function* () { yield source; } }); const items = [...source]; Object.getPrototypeOf(items[0].values()).next = replacement;",
|
||||
"candidate",
|
||||
],
|
||||
[
|
||||
"direct prototype assignment",
|
||||
"Object.getPrototypeOf([][Symbol.iterator]()).next = replacement;",
|
||||
@@ -1146,6 +1414,21 @@ it.each([
|
||||
"Iterator.prototype.return = " + closeHandler + ";",
|
||||
"candidate",
|
||||
],
|
||||
[
|
||||
"global Iterator prototype stored in a spread",
|
||||
`const protos=[...[Iterator.prototype]];protos[0].return=${closeHandler};`,
|
||||
"candidate",
|
||||
],
|
||||
[
|
||||
"escaped Iterator prototype stored in a spread",
|
||||
"const protos=[...[Iterator.prototype]];configure(protos[0]);",
|
||||
"candidate",
|
||||
],
|
||||
[
|
||||
"reflected Iterator prototype stored in a spread",
|
||||
`const protos=[...[Object.getPrototypeOf(Object.getPrototypeOf([].values()))]];Reflect.set(protos[0],"return",${closeHandler});`,
|
||||
"candidate",
|
||||
],
|
||||
[
|
||||
"inherited Object prototype",
|
||||
"Object.prototype.return = " + closeHandler + ";",
|
||||
@@ -1153,7 +1436,7 @@ it.each([
|
||||
],
|
||||
[
|
||||
"unrelated Array prototype",
|
||||
"Array.prototype.return = " + closeHandler + ";",
|
||||
`const protos=[...[Array.prototype]];protos[0].return=${closeHandler};`,
|
||||
"resolved",
|
||||
],
|
||||
] as const)(
|
||||
@@ -1190,7 +1473,7 @@ it.each([
|
||||
[
|
||||
"hook after read",
|
||||
"",
|
||||
`Object.getPrototypeOf([].values()).return = ${closeHandler};`,
|
||||
`const protos=[...[Iterator.prototype]];protos[0].return=${closeHandler};`,
|
||||
"resolved",
|
||||
],
|
||||
] as const)(
|
||||
|
||||
@@ -312,7 +312,7 @@ export const createSemanticElectronAppPathChangeQuery = (
|
||||
for (const choice of semanticAliasSelectionChoices(key!)) {
|
||||
const sources = t.isObjectExpression(node)
|
||||
? semanticObjectPropertyReferences(node, [choice], state)
|
||||
: semanticArrayPropertyReferences(node, [choice]);
|
||||
: semanticArrayPropertyReferences(node, [choice], state);
|
||||
for (const source of sources)
|
||||
if (
|
||||
!source.inherited ||
|
||||
|
||||
@@ -1286,7 +1286,7 @@ export class JavaScriptRouteScan {
|
||||
}
|
||||
for (const element of node.elements)
|
||||
if (element !== null && !this.#queue(element)) return [];
|
||||
return semanticArrayPropertyReferences(node, [key]);
|
||||
return semanticArrayPropertyReferences(node, [key], this.index.bindings);
|
||||
}
|
||||
|
||||
#retainSelection(
|
||||
|
||||
@@ -13,6 +13,44 @@ import {
|
||||
createGlobalReferenceReader,
|
||||
} from "./javascriptSemanticAmbientWrites.js";
|
||||
import { parseJavaScriptSource } from "./javascriptSourceParser.js";
|
||||
import { ambientPathMayObserveAt } from "./javascriptSemanticEffectLog.js";
|
||||
|
||||
it.each([
|
||||
["writes", "protos[0].return = () => ({});", true],
|
||||
["escapes", "configure(protos[0]);", true],
|
||||
["unrelated writes", "protos[0].unrelated = true;", false],
|
||||
] as const)(
|
||||
"indexes %s to spread-stored Iterator.prototype before member analysis",
|
||||
(_name, effect, observed) => {
|
||||
const parsed = parseJavaScriptSource(`
|
||||
const protos = [...[Iterator.prototype]];
|
||||
const box = [0, { mode: 'before' }];
|
||||
const [before] = box;
|
||||
${effect}
|
||||
const [after] = box;
|
||||
`);
|
||||
if (parsed === null) throw new Error("Expected a parsed iterator fixture");
|
||||
const index = completeSemanticSteps(
|
||||
indexJavaScriptBindingsWithReceiversSteps(parsed.program),
|
||||
);
|
||||
completeSemanticSteps(index.indexAmbientWritesSteps());
|
||||
const observations: boolean[] = [];
|
||||
traverseJavaScriptAst(parsed.program, {
|
||||
enter(node) {
|
||||
if (t.isArrayPattern(node))
|
||||
observations.push(
|
||||
ambientPathMayObserveAt(
|
||||
index.bindings,
|
||||
node,
|
||||
["global", "Iterator", "prototype", "return"],
|
||||
"contents",
|
||||
),
|
||||
);
|
||||
},
|
||||
});
|
||||
expect(observations).toEqual([false, observed]);
|
||||
},
|
||||
);
|
||||
|
||||
it.each([
|
||||
[
|
||||
|
||||
@@ -35,7 +35,11 @@ import {
|
||||
} from "./javascriptSemanticMutationInitializers.js";
|
||||
import { semanticIterationSources } from "./javascriptSemanticIterationSources.js";
|
||||
import type { SemanticIterationSource } from "./javascriptSemanticIterationFacts.js";
|
||||
import { joinSemanticPropertyPaths } from "./javascriptSemanticPropertyPaths.js";
|
||||
import {
|
||||
joinSemanticPropertyPaths,
|
||||
semanticArrayIndex,
|
||||
} from "./javascriptSemanticPropertyPaths.js";
|
||||
import { semanticArrayPropertyReferences } from "./javascriptSemanticObjectReferences.js";
|
||||
import { createSemanticReturnedReferences } from "./javascriptSemanticReturnedReferences.js";
|
||||
import {
|
||||
GLOBAL_OBJECT_NAMES,
|
||||
@@ -44,13 +48,15 @@ import {
|
||||
isIntrinsicGlobalReference,
|
||||
} from "./javascriptSemanticGlobalReference.js";
|
||||
import { readSemanticRequireLoader } from "./javascriptSemanticRequireOriginSlot.js";
|
||||
import { semanticArrayIteratorPrototypePath } from "./javascriptSemanticArrayLayout.js";
|
||||
import {
|
||||
semanticArrayIteratorPrototypePath,
|
||||
semanticArrayIterationPrototypeEffectPath,
|
||||
semanticReferenceIterationUnknown,
|
||||
semanticReferenceSourcesInactive,
|
||||
} from "./javascriptSemanticReferenceSources.js";
|
||||
import { semanticCopiedReferencePath } from "./javascriptSemanticCopiedReferencePaths.js";
|
||||
import {
|
||||
semanticCapturedSelectionsAfterPropertyRead,
|
||||
semanticNormalizeCapturedSelections,
|
||||
semanticShiftCapturedSelections,
|
||||
type SemanticCapturedSelection,
|
||||
@@ -248,6 +254,8 @@ type AmbientIdentityUnknownReason =
|
||||
|
||||
interface AmbientResolution {
|
||||
readonly paths: readonly AmbientPath[];
|
||||
/** Tentative identities usable only to guard native iteration. */
|
||||
readonly spreadEffectPaths?: readonly AmbientPath[];
|
||||
readonly callerPaths: readonly AmbientPath[];
|
||||
readonly ownership: "local" | "ambient" | "unknown";
|
||||
readonly unknownReasons: readonly AmbientIdentityUnknownReason[];
|
||||
@@ -293,13 +301,14 @@ export const createAmbientPathResolver = (
|
||||
escapedContents = false,
|
||||
projection: AmbientProjection = [],
|
||||
receiver?: (node: t.Node, path: AmbientProjection) => void,
|
||||
observes?: "array-receiver",
|
||||
observes?: "array-receiver" | "ambient-effects" | "spread-effects",
|
||||
): AmbientResolution => {
|
||||
const prior = possibleWrites ? undefined : cached.get(node);
|
||||
if (prior !== undefined) return prior;
|
||||
const result = new Map<string, AmbientPath>();
|
||||
const callerPaths = new Map<string, AmbientPath>();
|
||||
let hasLocalIdentity = false;
|
||||
let projectedSpread = false;
|
||||
const unknownReasons = new Set<AmbientIdentityUnknownReason>();
|
||||
// The caller's write site selects initializers for the written target.
|
||||
// Reads reached through an initializer occur at their own position.
|
||||
@@ -706,7 +715,9 @@ export const createAmbientPathResolver = (
|
||||
if (callResult?.kind === "prototype") {
|
||||
continueWith(
|
||||
callResult.target,
|
||||
suffix === undefined ? undefined : ["__proto__", ...suffix],
|
||||
suffix === undefined && observes !== "spread-effects"
|
||||
? undefined
|
||||
: ["__proto__", ...(suffix ?? [])],
|
||||
);
|
||||
continue;
|
||||
}
|
||||
@@ -848,29 +859,70 @@ export const createAmbientPathResolver = (
|
||||
continueWith(property.value, suffix.slice(1));
|
||||
}
|
||||
} else if (t.isArrayExpression(root)) {
|
||||
const hasSpread = root.elements.some(t.isSpreadElement);
|
||||
if (possibleWrites && suffix?.[0] === "__proto__") {
|
||||
retain(["global", "Array", "prototype", ...suffix.slice(1)]);
|
||||
resolvedIdentity = true;
|
||||
} else if (suffix === undefined) {
|
||||
if (hasSpread && observes === "ambient-effects")
|
||||
projectedSpread = true;
|
||||
for (const element of root.elements)
|
||||
if (t.isSpreadElement(element)) continueWith(element.argument);
|
||||
else if (t.isExpression(element)) continueWith(element);
|
||||
} else if (!root.elements.some(t.isSpreadElement)) {
|
||||
const index = Number(key);
|
||||
if (key === null && possibleWrites) {
|
||||
for (const element of root.elements)
|
||||
if (t.isExpression(element))
|
||||
continueWith(element, suffix.slice(1));
|
||||
} else if (
|
||||
key !== null &&
|
||||
Number.isInteger(index) &&
|
||||
index >= 0 &&
|
||||
String(index) === key
|
||||
) {
|
||||
const element = root.elements[index];
|
||||
if (t.isExpression(element))
|
||||
continueWith(element, suffix.slice(1));
|
||||
} else if (
|
||||
hasSpread
|
||||
? possibleWrites &&
|
||||
(observes === "spread-effects" || observes === "array-receiver")
|
||||
: possibleWrites || key !== null
|
||||
) {
|
||||
const remaining = suffix.slice(1);
|
||||
for (const source of semanticArrayPropertyReferences(root, [
|
||||
key ?? null,
|
||||
])) {
|
||||
if (
|
||||
observes === "array-receiver" &&
|
||||
source.iterableFallbackPath !== undefined &&
|
||||
t.isArrayExpression(
|
||||
unwrapJavaScriptExpression(source.node).node,
|
||||
)
|
||||
) {
|
||||
// Follow the selected source slot of a fresh array. A
|
||||
// borrowed iterator can still expose the source itself.
|
||||
const path = [
|
||||
...source.path.map((part) =>
|
||||
part === null || typeof part === "object"
|
||||
? null
|
||||
: String(part),
|
||||
),
|
||||
...remaining,
|
||||
];
|
||||
queue(
|
||||
source.node,
|
||||
path,
|
||||
current.readsAtSelf,
|
||||
semanticNormalizeCapturedSelections(
|
||||
semanticCapturedSelectionsAfterPropertyRead(
|
||||
capturedSelections,
|
||||
source,
|
||||
),
|
||||
path.length,
|
||||
),
|
||||
);
|
||||
continue;
|
||||
}
|
||||
continueWith(source.node, remaining);
|
||||
// Native iteration depends on the effects being collected here.
|
||||
// A spread may expose its receiver or any of its children.
|
||||
if (source.iterableFallbackPath !== undefined)
|
||||
continueWith(source.node, [null, ...remaining]);
|
||||
}
|
||||
} else if (
|
||||
hasSpread &&
|
||||
observes === "ambient-effects" &&
|
||||
(key === null ||
|
||||
(key !== undefined && semanticArrayIndex(key) !== null))
|
||||
) {
|
||||
projectedSpread = true;
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
@@ -879,10 +931,23 @@ export const createAmbientPathResolver = (
|
||||
}
|
||||
}
|
||||
const paths = [...result.values()];
|
||||
// A separate walk cannot let a tentative selection mask an ordinary one
|
||||
// in the identity cache, or publish unrelated tentative ambient paths.
|
||||
const spreadEffectPaths = projectedSpread
|
||||
? resolve(
|
||||
node,
|
||||
capture,
|
||||
escapedContents,
|
||||
projection,
|
||||
undefined,
|
||||
"spread-effects",
|
||||
).paths
|
||||
: undefined;
|
||||
if (paths.length > 0 && hasLocalIdentity)
|
||||
unknownReasons.add("mixed-identities");
|
||||
const resolution: AmbientResolution = {
|
||||
paths,
|
||||
...(spreadEffectPaths === undefined ? {} : { spreadEffectPaths }),
|
||||
callerPaths: [...callerPaths.values()],
|
||||
ownership:
|
||||
unknownReasons.size > 0
|
||||
@@ -1150,15 +1215,33 @@ export function* indexJavaScriptAmbientWritesSteps(
|
||||
for (const { node, path } of writes) {
|
||||
const target =
|
||||
path[0] === "pending-target" ? targets[Number(path[1])] : undefined;
|
||||
const paths =
|
||||
target === undefined
|
||||
? [path]
|
||||
: ambientWrites(target.node, target.capture).paths.map((identity) => [
|
||||
...identity,
|
||||
...path.slice(2),
|
||||
]);
|
||||
for (const resolved of paths) {
|
||||
retainSemanticAmbientEffect(state, "write", resolved, node);
|
||||
if (target === undefined) {
|
||||
retainSemanticAmbientEffect(state, "write", path, node);
|
||||
continue;
|
||||
}
|
||||
const resolved = ambientWrites(
|
||||
target.node,
|
||||
target.capture,
|
||||
false,
|
||||
[],
|
||||
undefined,
|
||||
"ambient-effects",
|
||||
);
|
||||
const suffix = path.slice(2);
|
||||
for (const identity of resolved.paths)
|
||||
retainSemanticAmbientEffect(
|
||||
state,
|
||||
"write",
|
||||
[...identity, ...suffix],
|
||||
node,
|
||||
);
|
||||
for (const identity of resolved.spreadEffectPaths ?? []) {
|
||||
const prototype = semanticArrayIterationPrototypeEffectPath(
|
||||
[...identity, ...suffix],
|
||||
"write",
|
||||
);
|
||||
if (prototype !== undefined)
|
||||
retainSemanticAmbientEffect(state, "write", prototype, node);
|
||||
}
|
||||
}
|
||||
// Settle other ambient effects first. A syntactic resolver call is safe only
|
||||
@@ -1277,7 +1360,14 @@ export function* indexJavaScriptAmbientWritesSteps(
|
||||
{ node, target, browserReceiver, projection }: (typeof escapes)[number],
|
||||
trackPrototypeInspectionEffects = false,
|
||||
): boolean {
|
||||
const resolved = ambientWrites(target, node, true, projection);
|
||||
const resolved = ambientWrites(
|
||||
target,
|
||||
node,
|
||||
true,
|
||||
projection,
|
||||
undefined,
|
||||
"ambient-effects",
|
||||
);
|
||||
if (
|
||||
browserReceiver &&
|
||||
resolved.ownership === "ambient" &&
|
||||
@@ -1286,7 +1376,20 @@ export function* indexJavaScriptAmbientWritesSteps(
|
||||
)
|
||||
state.retainedGlobalCallReceivers.add(target);
|
||||
let changed = false;
|
||||
for (const path of resolved.paths) {
|
||||
const paths =
|
||||
resolved.spreadEffectPaths === undefined
|
||||
? resolved.paths
|
||||
: [
|
||||
...resolved.paths,
|
||||
...resolved.spreadEffectPaths.flatMap((identity) => {
|
||||
const path = semanticArrayIterationPrototypeEffectPath(
|
||||
identity,
|
||||
"escape",
|
||||
);
|
||||
return path === undefined ? [] : [path];
|
||||
}),
|
||||
];
|
||||
for (const path of paths) {
|
||||
// Only proven browser APIs keep their global receiver from user code.
|
||||
if (browserReceiver && path.length === 1 && path[0] === "global")
|
||||
continue;
|
||||
|
||||
@@ -41,6 +41,18 @@ const getterReceivers = [
|
||||
"array element after a hole",
|
||||
"const arr = [, { x: 1, get self() { return this; } }]; arr[1].self.x = 2; return arr[1].x;",
|
||||
],
|
||||
[
|
||||
"array element copied by spread",
|
||||
"const arr = [...[{ x: 1, get self() { return this; } }]]; arr[0].self.x = 2; return arr[0].x;",
|
||||
],
|
||||
[
|
||||
"saved receiver from a nested spread",
|
||||
"const arr = [...[{ x: 1 }, ...[{ x: 1, get self() { return this; } }]]]; const saved = arr[1].self; saved.x = 2; return arr[1].x;",
|
||||
],
|
||||
[
|
||||
"receiver in a wrapped spread operand",
|
||||
"const arr = [...([{ x: 1, get self() { return this; } }] satisfies object[])]; arr[0].self.x = 2; return arr[0].x;",
|
||||
],
|
||||
[
|
||||
"mixed object and array owners",
|
||||
"const outer = { rows: [{ box: { x: 1, get self() { return this; } } }] }; outer.rows[0].box.self.x = 2; return outer.rows[0].box.x;",
|
||||
@@ -107,6 +119,17 @@ const getterReceivers = [
|
||||
],
|
||||
] as const;
|
||||
|
||||
const preservedSpreadReceivers = [
|
||||
[
|
||||
"unrelated array element copied before a getter receiver",
|
||||
"const arr = [...[{ x: 1 }], { x: 1, get self() { return this; } }]; arr[1].self.x = 2; return arr[0].x;",
|
||||
],
|
||||
[
|
||||
"unrelated field on a copied getter receiver",
|
||||
"const arr = [...[{ x: 1, y: 1, get self() { return this; } }]]; arr[0].self.y = 2; return arr[0].x;",
|
||||
],
|
||||
] as const;
|
||||
|
||||
describe("getter receiver mutation aliases (#1665, #2654)", () => {
|
||||
it.each(getterReceivers)(
|
||||
"does not report a stale literal through %s",
|
||||
@@ -151,6 +174,7 @@ describe("getter receiver mutation aliases (#1665, #2654)", () => {
|
||||
"unrelated array element",
|
||||
"const arr = [{ x: 1, get self() { return this; } }, { x: 1 }]; arr[0].self.x = 2; return arr[1].x;",
|
||||
],
|
||||
...preservedSpreadReceivers,
|
||||
[
|
||||
"array replacement after a receiver was captured",
|
||||
"const original = [{ x: 1, get self() { return this; } }]; const replacement = [{ x: 1 }]; let arr = original; const saved = arr[0].self; arr = replacement; saved.x = 2; return replacement[0].x;",
|
||||
@@ -183,7 +207,7 @@ describe("getter receiver mutation aliases (#1665, #2654)", () => {
|
||||
"ordinary method read",
|
||||
"const box = { x: 1, self() { return this; } }; const method = box.self; return box.x;",
|
||||
],
|
||||
])("preserves a known literal for %s", (_shape, body) => {
|
||||
] as const)("preserves a known literal for %s", (_shape, body) => {
|
||||
expect(resultValue(body)).toMatchObject({ status: "literal", value: 1 });
|
||||
});
|
||||
it.each([
|
||||
|
||||
@@ -0,0 +1,104 @@
|
||||
import * as t from "@babel/types";
|
||||
import { expect, it } from "vitest";
|
||||
import { indexJavaScriptBindingsSteps } from "../../../tests/fixtures/javascriptSemanticIndex.js";
|
||||
import { semanticKnownArrayIterationAt } from "./javascriptSemanticReferenceSources.js";
|
||||
import { completeSemanticSteps } from "./javascriptSemanticTraversal.js";
|
||||
import { evaluateSemanticExpression } from "./javascriptSemanticValues.js";
|
||||
import { parseJavaScriptSource } from "./javascriptSourceParser.js";
|
||||
|
||||
function indexedInitializer(source: string) {
|
||||
const parsed = parseJavaScriptSource(source, "array-iteration.js");
|
||||
if (parsed === null) throw new Error("Expected parsed source");
|
||||
const state = completeSemanticSteps(
|
||||
indexJavaScriptBindingsSteps(parsed.program),
|
||||
);
|
||||
const statement = parsed.program.body.find(
|
||||
(statement) =>
|
||||
t.isVariableDeclaration(statement) &&
|
||||
statement.declarations.some(({ id }) =>
|
||||
t.isIdentifier(id, { name: "items" }),
|
||||
),
|
||||
);
|
||||
const array = t.isVariableDeclaration(statement)
|
||||
? statement.declarations[0]?.init
|
||||
: undefined;
|
||||
if (!t.isArrayExpression(array))
|
||||
throw new Error("Expected array initializer");
|
||||
return { state, array };
|
||||
}
|
||||
|
||||
it.each([
|
||||
["indirect eval before acquisition", 'const run=eval;run("0");', "", false],
|
||||
[
|
||||
"Function before acquisition",
|
||||
'const prepare=Function;prepare("return 0");',
|
||||
"",
|
||||
false,
|
||||
],
|
||||
["indirect eval after acquisition", "", 'const run=eval;run("0");', true],
|
||||
["shadowed eval", 'const eval=()=>{};const run=eval;run("0");', "", true],
|
||||
] as const)(
|
||||
"admits native array iteration with %s only when intact",
|
||||
(_name, before, after, intact) => {
|
||||
const { array, state } = indexedInitializer(
|
||||
`${before}const items=[1];${after}`,
|
||||
);
|
||||
expect(semanticKnownArrayIterationAt(array, state)).toBe(intact);
|
||||
},
|
||||
);
|
||||
|
||||
it.each([
|
||||
[
|
||||
"indirect eval before the spread",
|
||||
'const run=eval;run("Array.prototype[Symbol.iterator]=function*(){yield 2}");const items=[...[1]];',
|
||||
false,
|
||||
0,
|
||||
],
|
||||
[
|
||||
"Function before the spread",
|
||||
'const prepare=Function;prepare("return 0");const items=[...[1]];',
|
||||
false,
|
||||
0,
|
||||
],
|
||||
[
|
||||
"indirect eval in the operand",
|
||||
'const run=eval;const items=[...[(run("0"),1)]];',
|
||||
false,
|
||||
0,
|
||||
],
|
||||
[
|
||||
"indirect eval after the spread",
|
||||
'const items=[...[1]];const run=eval;run("0");',
|
||||
true,
|
||||
1,
|
||||
],
|
||||
[
|
||||
"shadowed eval",
|
||||
'const eval=()=>{};const run=eval;run("0");const items=[...[1]];',
|
||||
true,
|
||||
1,
|
||||
],
|
||||
[
|
||||
"earlier sibling opaque call",
|
||||
'const run=eval;const items=[run("0"),...[1]];',
|
||||
false,
|
||||
1,
|
||||
],
|
||||
[
|
||||
"later sibling opaque call",
|
||||
'const run=eval;const items=[...[1],run("0")];',
|
||||
true,
|
||||
2,
|
||||
],
|
||||
] as const)(
|
||||
"retains spread positions with %s only when intact",
|
||||
(_name, source, intact, count) => {
|
||||
const { array, state } = indexedInitializer(source);
|
||||
const value = evaluateSemanticExpression(array, state, array);
|
||||
expect(value).toMatchObject({
|
||||
status: "array",
|
||||
unknownItems: !intact,
|
||||
});
|
||||
expect(value.status === "array" && value.items).toHaveLength(count);
|
||||
},
|
||||
);
|
||||
@@ -0,0 +1,172 @@
|
||||
import * as t from "@babel/types";
|
||||
import {
|
||||
javaScriptExpressionWrapperTarget,
|
||||
unwrapJavaScriptExpression,
|
||||
} from "./javascriptAstValues.js";
|
||||
import {
|
||||
ambientPathMayObserveAt,
|
||||
semanticOpaqueGlobalMayHaveChanged,
|
||||
} from "./javascriptSemanticEffectLog.js";
|
||||
import type { JavaScriptSemanticPropertySelection } from "./javascriptSemanticPropertyPaths.js";
|
||||
import type { JavaScriptSemanticAnalysisState } from "./javascriptSemanticState.js";
|
||||
|
||||
/** Shared intrinsic reached through a native array iterator's prototype. */
|
||||
export const semanticArrayIteratorPrototypePath = [
|
||||
"array-iterator-prototype",
|
||||
] as const;
|
||||
const iteratorNextPath = [
|
||||
...semanticArrayIteratorPrototypePath,
|
||||
"next",
|
||||
] as const;
|
||||
|
||||
export const semanticNativeArrayIteratorIntactAt = (
|
||||
state: JavaScriptSemanticAnalysisState,
|
||||
acquisition: t.Node,
|
||||
): boolean =>
|
||||
!state.arrayIterationUnknown &&
|
||||
!semanticOpaqueGlobalMayHaveChanged(acquisition, state) &&
|
||||
!ambientPathMayObserveAt(state, acquisition, iteratorNextPath, "contents");
|
||||
|
||||
type ArrayLiteralSlot = {
|
||||
readonly kind: "slot";
|
||||
readonly element: t.Expression | null;
|
||||
readonly depth: number;
|
||||
};
|
||||
|
||||
type ArrayLiteralPart =
|
||||
| ArrayLiteralSlot
|
||||
| {
|
||||
readonly kind: "opaque-spread";
|
||||
readonly argument: t.Expression;
|
||||
};
|
||||
|
||||
const denseInlineSlots = (
|
||||
spread: t.SpreadElement,
|
||||
state: JavaScriptSemanticAnalysisState,
|
||||
allowance: { remaining: number },
|
||||
): readonly ArrayLiteralSlot[] | undefined => {
|
||||
const takeSelection = (): boolean => {
|
||||
if (allowance.remaining === 0) {
|
||||
state.valueResourceLimits.add("alias-selections");
|
||||
return false;
|
||||
}
|
||||
allowance.remaining--;
|
||||
return true;
|
||||
};
|
||||
const spreadArray = (part: t.SpreadElement) => {
|
||||
const argument = unwrapJavaScriptExpression(part.argument).node;
|
||||
return t.isArrayExpression(argument) &&
|
||||
semanticNativeArrayIteratorIntactAt(state, part)
|
||||
? argument
|
||||
: undefined;
|
||||
};
|
||||
const root = spreadArray(spread);
|
||||
if (root === undefined || !takeSelection()) return undefined;
|
||||
const pending = [{ array: root, index: 0 }];
|
||||
const slots: ArrayLiteralSlot[] = [];
|
||||
while (pending.length > 0) {
|
||||
const frame = pending.at(-1)!;
|
||||
if (frame.index === frame.array.elements.length) {
|
||||
pending.pop();
|
||||
continue;
|
||||
}
|
||||
if (!takeSelection()) return undefined;
|
||||
const element = frame.array.elements[frame.index++]!;
|
||||
if (element === null) return undefined;
|
||||
if (t.isSpreadElement(element)) {
|
||||
const array = spreadArray(element);
|
||||
if (array === undefined) return undefined;
|
||||
pending.push({ array, index: 0 });
|
||||
} else slots.push({ kind: "slot", element, depth: pending.length + 1 });
|
||||
}
|
||||
return slots;
|
||||
};
|
||||
|
||||
/** Source slots in destination order, with unresolved spread positions explicit. */
|
||||
export function* semanticArrayLiteralLayout(
|
||||
root: t.ArrayExpression,
|
||||
state?: JavaScriptSemanticAnalysisState,
|
||||
): IterableIterator<ArrayLiteralPart> {
|
||||
const allowance = { remaining: state?.precision.alias_selections ?? 0 };
|
||||
let opaque = false;
|
||||
for (const element of root.elements) {
|
||||
if (!t.isSpreadElement(element)) {
|
||||
yield { kind: "slot", element, depth: 1 };
|
||||
continue;
|
||||
}
|
||||
const slots =
|
||||
!opaque && state !== undefined
|
||||
? denseInlineSlots(element, state, allowance)
|
||||
: undefined;
|
||||
if (slots !== undefined) yield* slots;
|
||||
else {
|
||||
opaque = true;
|
||||
yield { kind: "opaque-spread", argument: element.argument };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Find where a literal element's identity is stored, including inline copies. */
|
||||
export const semanticArrayLiteralElementOwner = (
|
||||
element: t.Node,
|
||||
array: t.ArrayExpression,
|
||||
state: JavaScriptSemanticAnalysisState,
|
||||
):
|
||||
| {
|
||||
readonly array: t.ArrayExpression;
|
||||
readonly key: number | JavaScriptSemanticPropertySelection;
|
||||
}
|
||||
| undefined => {
|
||||
const { parentsByNode } = state;
|
||||
while (true) {
|
||||
let operand: t.Node = array;
|
||||
let spread = parentsByNode.get(operand);
|
||||
while (
|
||||
spread !== undefined &&
|
||||
javaScriptExpressionWrapperTarget(spread) === operand
|
||||
) {
|
||||
operand = spread;
|
||||
spread = parentsByNode.get(operand);
|
||||
}
|
||||
if (!t.isSpreadElement(spread) || spread.argument !== operand) break;
|
||||
const destination = parentsByNode.get(spread);
|
||||
if (!t.isArrayExpression(destination)) break;
|
||||
array = destination;
|
||||
}
|
||||
let minimumIndex = 0;
|
||||
let uncertainIndex = false;
|
||||
for (const part of semanticArrayLiteralLayout(array, state)) {
|
||||
if (part.kind === "opaque-spread") {
|
||||
uncertainIndex = true;
|
||||
continue;
|
||||
}
|
||||
if (part.element === element)
|
||||
return {
|
||||
array,
|
||||
key: uncertainIndex
|
||||
? { excludedKeys: [], startIndex: minimumIndex }
|
||||
: minimumIndex,
|
||||
};
|
||||
minimumIndex++;
|
||||
}
|
||||
return undefined;
|
||||
};
|
||||
|
||||
/** A complete index for consumers that need repeated lookups in one query. */
|
||||
export const semanticArrayLiteralSlots = (
|
||||
root: t.ArrayExpression,
|
||||
state: JavaScriptSemanticAnalysisState,
|
||||
): readonly (t.Expression | null)[] | undefined => {
|
||||
if (
|
||||
root.elements.every(
|
||||
(element): element is t.Expression | null => !t.isSpreadElement(element),
|
||||
)
|
||||
)
|
||||
return root.elements;
|
||||
const slots: (t.Expression | null)[] = [];
|
||||
for (const part of semanticArrayLiteralLayout(root, state)) {
|
||||
if (part.kind === "opaque-spread") return undefined;
|
||||
slots.push(part.element);
|
||||
}
|
||||
return slots;
|
||||
};
|
||||
@@ -147,7 +147,7 @@ export const createSemanticOpaqueGlobalObserver = (
|
||||
};
|
||||
|
||||
/** Global code can affect globals, but cannot rebind its caller's locals. */
|
||||
const semanticOpaqueGlobalMayHaveChanged = (
|
||||
export const semanticOpaqueGlobalMayHaveChanged = (
|
||||
read: t.Node,
|
||||
state: JavaScriptSemanticAnalysisState,
|
||||
): boolean =>
|
||||
|
||||
@@ -43,7 +43,7 @@ export const semanticGetterReceiverReferences = (
|
||||
containerReferences: (node, path) =>
|
||||
t.isObjectExpression(node)
|
||||
? semanticObjectPropertyReferences(node, path, state)
|
||||
: semanticArrayPropertyReferences(node, path),
|
||||
: semanticArrayPropertyReferences(node, path, state),
|
||||
},
|
||||
[{ node: operand }],
|
||||
).run(),
|
||||
|
||||
@@ -264,7 +264,11 @@ const localPropertyMethods = (
|
||||
(capture) => capture.depth === 1 && capture.ownEnumerable,
|
||||
)
|
||||
) {
|
||||
for (const prototype of semanticNativePrototypeChain(node, selected!)) {
|
||||
for (const prototype of semanticNativePrototypeChain(
|
||||
node,
|
||||
selected!,
|
||||
state,
|
||||
)) {
|
||||
if (selected === null || typeof selected === "object")
|
||||
return undefined;
|
||||
const at = current.memberReadAt ?? root;
|
||||
@@ -295,7 +299,7 @@ const localPropertyMethods = (
|
||||
}
|
||||
}
|
||||
const references = t.isArrayExpression(node)
|
||||
? semanticArrayPropertyReferences(node, [selected!])
|
||||
? semanticArrayPropertyReferences(node, [selected!], state)
|
||||
: remaining.length === 0 && accessor !== "invoke"
|
||||
? semanticObjectAccessorReferences(
|
||||
node,
|
||||
@@ -2366,7 +2370,7 @@ const referencedValues = (
|
||||
if (effect === "write" && path.length < 2) return [];
|
||||
return t.isObjectExpression(node)
|
||||
? semanticObjectPropertyReferences(node, path, state)
|
||||
: semanticArrayPropertyReferences(node, path);
|
||||
: semanticArrayPropertyReferences(node, path, state);
|
||||
}
|
||||
if (t.isClass(node) || t.isNewExpression(node))
|
||||
return constructedReferenceValues(
|
||||
|
||||
@@ -6,6 +6,7 @@ import {
|
||||
} from "./javascriptSemanticState.js";
|
||||
import {
|
||||
isRuntimeJavaScriptClassProperty,
|
||||
javaScriptExpressionWrapperTarget,
|
||||
semanticStaticPropertyKey,
|
||||
unwrapJavaScriptExpression,
|
||||
} from "./javascriptAstValues.js";
|
||||
@@ -15,6 +16,10 @@ import {
|
||||
type JavaScriptSemanticPropertyPath as PropertyPath,
|
||||
} from "./javascriptSemanticPropertyPaths.js";
|
||||
import { compareUnicodeCodePoints } from "../unicodeCodePointOrder.js";
|
||||
import {
|
||||
semanticArrayLiteralElementOwner,
|
||||
semanticArrayLiteralLayout,
|
||||
} from "./javascriptSemanticArrayLayout.js";
|
||||
import {
|
||||
SemanticGetterQuery,
|
||||
type GetterQueryJob,
|
||||
@@ -372,6 +377,13 @@ const objectReceiverReference = (
|
||||
const projection: PropertyPath[number][] = [];
|
||||
while (true) {
|
||||
const parent = state.parentsByNode.get(current);
|
||||
if (
|
||||
parent !== undefined &&
|
||||
javaScriptExpressionWrapperTarget(parent) === current
|
||||
) {
|
||||
current = parent;
|
||||
continue;
|
||||
}
|
||||
if (
|
||||
t.isVariableDeclarator(parent) &&
|
||||
parent.init === current &&
|
||||
@@ -397,21 +409,10 @@ const objectReceiverReference = (
|
||||
},
|
||||
];
|
||||
if (t.isArrayExpression(parent)) {
|
||||
let minimumIndex = 0;
|
||||
let uncertainIndex = false;
|
||||
for (const element of parent.elements) {
|
||||
if (element === current) break;
|
||||
if (t.isSpreadElement(element)) uncertainIndex = true;
|
||||
else minimumIndex++;
|
||||
}
|
||||
// A spread contributes an unknown number of slots. Retain the known
|
||||
// prefix without treating the element's syntax position as its index.
|
||||
projection.unshift(
|
||||
uncertainIndex
|
||||
? { excludedKeys: [], startIndex: minimumIndex }
|
||||
: minimumIndex,
|
||||
);
|
||||
current = parent;
|
||||
const slot = semanticArrayLiteralElementOwner(current, parent, state);
|
||||
if (slot === undefined) break;
|
||||
projection.unshift(slot.key);
|
||||
current = slot.array;
|
||||
continue;
|
||||
}
|
||||
if (t.isObjectProperty(parent) && parent.value === current) {
|
||||
@@ -854,6 +855,7 @@ const objectExpressionDefinedKeys = (
|
||||
export const semanticArrayPropertyReferences = (
|
||||
node: t.ArrayExpression,
|
||||
path: PropertyPath,
|
||||
state?: JavaScriptSemanticAnalysisState,
|
||||
): readonly JavaScriptSemanticObjectReference[] => {
|
||||
const [key, ...remaining] = path;
|
||||
const selected = key ?? null;
|
||||
@@ -863,8 +865,8 @@ export const semanticArrayPropertyReferences = (
|
||||
const references: JavaScriptSemanticObjectReference[] = [];
|
||||
let minimumIndex = 0;
|
||||
let uncertainIndex = false;
|
||||
for (const element of node.elements) {
|
||||
if (t.isSpreadElement(element)) {
|
||||
for (const part of semanticArrayLiteralLayout(node, state)) {
|
||||
if (part.kind === "opaque-spread") {
|
||||
if (selectedIndex === null || selectedIndex >= minimumIndex) {
|
||||
let sourceKey: PropertyPath[number] = null;
|
||||
if (!uncertainIndex) {
|
||||
@@ -884,15 +886,16 @@ export const semanticArrayPropertyReferences = (
|
||||
};
|
||||
}
|
||||
references.push({
|
||||
node: element.argument,
|
||||
node: part.argument,
|
||||
path: [sourceKey, ...remaining],
|
||||
capturedSelectionAt: element.argument,
|
||||
capturedSelectionAt: part.argument,
|
||||
iterableFallbackPath: [],
|
||||
});
|
||||
}
|
||||
uncertainIndex = true;
|
||||
continue;
|
||||
}
|
||||
const { element } = part;
|
||||
if (
|
||||
element !== null &&
|
||||
(uncertainIndex
|
||||
@@ -909,13 +912,14 @@ export const semanticArrayPropertyReferences = (
|
||||
export const semanticNativePrototypeChain = (
|
||||
node: t.ObjectExpression | t.ArrayExpression,
|
||||
key: PropertyPath[number],
|
||||
state?: JavaScriptSemanticAnalysisState,
|
||||
): readonly ("Object" | "Array")[] => {
|
||||
const exact =
|
||||
key !== null && typeof key !== "object" ? String(key) : undefined;
|
||||
if (t.isArrayExpression(node)) {
|
||||
if (exact === "length") return [];
|
||||
if (exact !== undefined) {
|
||||
const sources = semanticArrayPropertyReferences(node, [exact]);
|
||||
const sources = semanticArrayPropertyReferences(node, [exact], state);
|
||||
if (
|
||||
sources.length > 0 &&
|
||||
sources.every((source) => source.capturedSelectionAt === undefined)
|
||||
|
||||
@@ -123,7 +123,7 @@ export const hasFreshProjectedReceiver = (
|
||||
const sources = t.isObjectExpression(node)
|
||||
? semanticObjectPropertyReferences(node, [selected!], state)
|
||||
: t.isArrayExpression(node)
|
||||
? semanticArrayPropertyReferences(node, [selected!])
|
||||
? semanticArrayPropertyReferences(node, [selected!], state)
|
||||
: [];
|
||||
if (sources.length === 0) return false;
|
||||
if (
|
||||
|
||||
@@ -12,6 +12,7 @@ import {
|
||||
import { semanticBindingPrecedesCapture } from "./javascriptSemanticMutationInitializers.js";
|
||||
import { semanticArrayIndex } from "./javascriptSemanticPropertyPaths.js";
|
||||
import { semanticKnownArrayIteration } from "./javascriptSemanticReferenceSources.js";
|
||||
import { semanticArrayLiteralSlots } from "./javascriptSemanticArrayLayout.js";
|
||||
import { semanticSlotAtPath } from "./javascriptSemanticSlots.js";
|
||||
import { evaluateSemanticExpression } from "./javascriptSemanticValues.js";
|
||||
import type { JavaScriptSemanticValue } from "./javascriptSemanticValueTypes.js";
|
||||
@@ -175,6 +176,8 @@ export const createSemanticReadPresenceQuery = (
|
||||
!semanticKnownArrayIteration(array, state, acquisition))
|
||||
)
|
||||
return undefined;
|
||||
const elements = semanticArrayLiteralSlots(literal, state);
|
||||
if (elements === undefined) return undefined;
|
||||
return (path, beforeIterator) => {
|
||||
if (
|
||||
beforeIterator !== undefined ||
|
||||
@@ -186,7 +189,7 @@ export const createSemanticReadPresenceQuery = (
|
||||
const index = semanticArrayIndex(key);
|
||||
if (index === null) return undefined;
|
||||
const slot = array.items[index];
|
||||
const element = literal.elements[index];
|
||||
const element = elements[index];
|
||||
if (
|
||||
slot?.name !== key ||
|
||||
slot.presence !== "present" ||
|
||||
|
||||
@@ -12,6 +12,10 @@ import {
|
||||
import { semanticSlotAtPath } from "./javascriptSemanticSlots.js";
|
||||
import type { JavaScriptSemanticValue } from "./javascriptSemanticValueTypes.js";
|
||||
import { ambientPathMayObserveAt } from "./javascriptSemanticEffectLog.js";
|
||||
import {
|
||||
semanticArrayIteratorPrototypePath,
|
||||
semanticNativeArrayIteratorIntactAt,
|
||||
} from "./javascriptSemanticArrayLayout.js";
|
||||
|
||||
/**
|
||||
* Node's wrapper module inherits require. A copy cannot read that delegate
|
||||
@@ -34,14 +38,27 @@ interface ReferenceSource {
|
||||
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;
|
||||
const arrayIteratorReturnGlobalOwners = new Set(["Iterator", "Object"]);
|
||||
|
||||
/** Select prototype effects from spread sources whose exact index is unknown. */
|
||||
export const semanticArrayIterationPrototypeEffectPath = (
|
||||
path: readonly (string | null)[],
|
||||
kind: "write" | "escape",
|
||||
): readonly (string | null)[] | undefined => {
|
||||
const [namespace, owner, property] = path;
|
||||
if (namespace === semanticArrayIteratorPrototypePath[0]) return path;
|
||||
if (
|
||||
namespace !== "global" ||
|
||||
typeof owner !== "string" ||
|
||||
(owner !== "Array" && !arrayIteratorReturnGlobalOwners.has(owner))
|
||||
)
|
||||
return undefined;
|
||||
return property === "prototype" ||
|
||||
property === null ||
|
||||
(kind === "escape" && path.length === 2)
|
||||
? [namespace, owner, "prototype", ...path.slice(3)]
|
||||
: undefined;
|
||||
};
|
||||
|
||||
/** Native array iteration preserves numeric slots only while its iterator is intact. */
|
||||
export const semanticKnownArrayIteration = (
|
||||
@@ -49,17 +66,20 @@ export const semanticKnownArrayIteration = (
|
||||
state: JavaScriptSemanticAnalysisState,
|
||||
read: t.Node,
|
||||
): value is Extract<JavaScriptSemanticValue, { status: "array" }> =>
|
||||
!state.arrayIterationUnknown &&
|
||||
value?.status === "array" &&
|
||||
!ambientPathMayObserveAt(state, read, arrayIteratorNextPath, "contents") &&
|
||||
semanticNativeArrayIteratorIntactAt(state, read) &&
|
||||
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"],
|
||||
...Array.from(arrayIteratorReturnGlobalOwners, (owner) => [
|
||||
"global",
|
||||
owner,
|
||||
"prototype",
|
||||
"return",
|
||||
]),
|
||||
] as const;
|
||||
|
||||
/** References a possible native close hook can still obtain by advancing its iterator. */
|
||||
|
||||
@@ -1138,7 +1138,7 @@ const loaderObjectPropertySources = (
|
||||
return semanticAliasSelectionChoices(key).flatMap((choice) =>
|
||||
(t.isObjectExpression(object)
|
||||
? semanticObjectPropertyReferences(object, [choice], state)
|
||||
: semanticArrayPropertyReferences(object, [choice])
|
||||
: semanticArrayPropertyReferences(object, [choice], state)
|
||||
)
|
||||
.filter(
|
||||
(source) =>
|
||||
|
||||
@@ -540,9 +540,11 @@ export const createSemanticReturnedReferences = (
|
||||
const [key, ...path] = current.path ?? [];
|
||||
if (current.path !== undefined && key !== undefined) {
|
||||
for (const choice of semanticAliasSelectionChoices(key))
|
||||
for (const source of semanticArrayPropertyReferences(node, [
|
||||
choice,
|
||||
])) {
|
||||
for (const source of semanticArrayPropertyReferences(
|
||||
node,
|
||||
[choice],
|
||||
state,
|
||||
)) {
|
||||
let projection = source.path;
|
||||
if (
|
||||
source.capturedSelectionAt !== undefined &&
|
||||
|
||||
@@ -37,6 +37,7 @@ import {
|
||||
} from "./javascriptSemanticState.js";
|
||||
import { readSemanticRequireOriginCandidate } from "./javascriptSemanticRequireOriginSlot.js";
|
||||
import { compareUnicodeCodePoints } from "../unicodeCodePointOrder.js";
|
||||
import { semanticArrayLiteralLayout } from "./javascriptSemanticArrayLayout.js";
|
||||
import {
|
||||
readExactJavaScriptLiteral,
|
||||
semanticStaticPropertyKey,
|
||||
@@ -963,13 +964,14 @@ const evaluateArray = (
|
||||
const items: JavaScriptSemanticProperty[] = [];
|
||||
let unknownItems = false;
|
||||
let omittedItems: number | null = 0;
|
||||
for (const element of node.elements) {
|
||||
if (t.isSpreadElement(element)) {
|
||||
for (const part of semanticArrayLiteralLayout(node, context.state)) {
|
||||
if (part.kind === "opaque-spread") {
|
||||
unknownItems = true;
|
||||
omittedItems = null;
|
||||
// Subsequent elements have no fixed index after an unknown-length spread.
|
||||
break;
|
||||
}
|
||||
const { element, depth } = part;
|
||||
if (element === null) {
|
||||
items.push({
|
||||
name: String(items.length),
|
||||
@@ -981,7 +983,7 @@ const evaluateArray = (
|
||||
items.push({
|
||||
name: String(items.length),
|
||||
presence: "present",
|
||||
value: evaluateCapturedExpression(element, nestedContext(context)),
|
||||
value: evaluateCapturedExpression(element, nestedContext(context, depth)),
|
||||
});
|
||||
}
|
||||
return unknownItems
|
||||
@@ -1450,13 +1452,16 @@ const mergeValues = (
|
||||
return merged;
|
||||
};
|
||||
|
||||
const nestedContext = (context: EvaluationContext): EvaluationContext => ({
|
||||
const nestedContext = (
|
||||
context: EvaluationContext,
|
||||
depth = 1,
|
||||
): EvaluationContext => ({
|
||||
state: context.state,
|
||||
primitiveBindingValues: context.primitiveBindingValues,
|
||||
...(context.capturePoint === undefined
|
||||
? {}
|
||||
: { capturePoint: context.capturePoint }),
|
||||
expressionDepth: context.expressionDepth + 1,
|
||||
expressionDepth: context.expressionDepth + depth,
|
||||
bindings: context.bindings,
|
||||
dependence: context.dependence,
|
||||
memo: context.memo,
|
||||
|
||||
@@ -50,6 +50,7 @@ const expectedDependencies = [
|
||||
["arrayObject", ["0", "mode"]],
|
||||
["nestedElement", ["items", "0"]],
|
||||
["innerElement", ["0", "0"]],
|
||||
["spreadElement", ["1"]],
|
||||
["aliasedPrototypeElement", ["0", "0"], "candidate"],
|
||||
["selected", ["0"], "candidate"],
|
||||
["beforeClose", ["label"]],
|
||||
@@ -62,6 +63,23 @@ const expectedDependencies = [
|
||||
["projectedAfterAcquisition", ["parent", "next", "mode"], "candidate"],
|
||||
] as const;
|
||||
|
||||
const tracedBindings = [
|
||||
"declared",
|
||||
"ordinary",
|
||||
"arrayAssigned",
|
||||
"elided",
|
||||
"projected",
|
||||
"arrayObject",
|
||||
"nestedElement",
|
||||
"innerElement",
|
||||
"spreadElement",
|
||||
"aliasedPrototypeElement",
|
||||
"selected",
|
||||
"beforeClose",
|
||||
"consumed",
|
||||
"afterClose",
|
||||
] as const;
|
||||
|
||||
cliTest(
|
||||
"traces proved destructuring dependencies and leaves custom iteration uncertain",
|
||||
async ({ cli }) => {
|
||||
@@ -109,6 +127,9 @@ cliTest(
|
||||
const nestedPairs = [["read-time"]];
|
||||
const [[innerElement]] = nestedPairs;
|
||||
nestedPairs[0][0] = "later";
|
||||
const spreadSource = [...["prefix"], "read-time"];
|
||||
const [,spreadElement] = spreadSource;
|
||||
spreadSource[1] = "later";
|
||||
const box = { items: [] };
|
||||
const iterator = box.items.values();
|
||||
const prototype = Reflect.getPrototypeOf(iterator);
|
||||
@@ -162,21 +183,7 @@ cliTest(
|
||||
),
|
||||
).toMatchObject({ resolution: "resolved" });
|
||||
const queryPath = join(root, "query.json");
|
||||
for (const name of [
|
||||
"declared",
|
||||
"ordinary",
|
||||
"arrayAssigned",
|
||||
"elided",
|
||||
"projected",
|
||||
"arrayObject",
|
||||
"nestedElement",
|
||||
"innerElement",
|
||||
"aliasedPrototypeElement",
|
||||
"selected",
|
||||
"beforeClose",
|
||||
"consumed",
|
||||
"afterClose",
|
||||
]) {
|
||||
for (const name of tracedBindings) {
|
||||
const binding = graph.nodes.find(
|
||||
({ kind, label }) => kind === "binding" && label === name,
|
||||
);
|
||||
|
||||
Reference in new issue
Block a user