mirror of
https://github.com/morluto/rea.git
synced 2026-11-21 14:05:49 +00:00
fix(javascript): preserve read-time dependencies through const aliases (#12513)
fix(javascript): preserve read-time dependencies through const source aliases
This commit is contained in:
1 parent
c09f7172cf
commit
a5e9f3a979
2 files changed
+303
-5
No files matched your search
@@ -528,3 +528,217 @@ it.each([
|
||||
).toMatchObject({ resolution });
|
||||
},
|
||||
);
|
||||
|
||||
const objectAliasRead = "const { mode: selected } = alias;";
|
||||
const arrayAliasRead = "let selected; [selected] = alias;";
|
||||
|
||||
it.each([
|
||||
[
|
||||
"object write through the original after the read",
|
||||
false,
|
||||
objectAliasRead + "base.mode = 2;",
|
||||
"resolved",
|
||||
],
|
||||
[
|
||||
"object write through the alias after the read",
|
||||
false,
|
||||
objectAliasRead + "alias.mode = 2;",
|
||||
"resolved",
|
||||
],
|
||||
[
|
||||
"object write through the original before the read",
|
||||
false,
|
||||
"base.mode = 2;" + objectAliasRead,
|
||||
"candidate",
|
||||
],
|
||||
[
|
||||
"object write through the alias before the read",
|
||||
false,
|
||||
"alias.mode = 2;" + objectAliasRead,
|
||||
"candidate",
|
||||
],
|
||||
[
|
||||
"object delete through the original before the read",
|
||||
false,
|
||||
"delete base.mode;" + objectAliasRead,
|
||||
"candidate",
|
||||
],
|
||||
[
|
||||
"object delete through the alias before the read",
|
||||
false,
|
||||
"delete alias.mode;" + objectAliasRead,
|
||||
"candidate",
|
||||
],
|
||||
[
|
||||
"object write to an unrelated slot",
|
||||
false,
|
||||
"alias.other = 2;" + objectAliasRead + "base.mode = 2;",
|
||||
"resolved",
|
||||
],
|
||||
[
|
||||
"object multi-hop aliases",
|
||||
false,
|
||||
"const lifted = alias; const final = lifted; const {mode: selected} = final; base.mode = 2;",
|
||||
"resolved",
|
||||
],
|
||||
[
|
||||
"object alias passed to opaque code",
|
||||
false,
|
||||
"mutate(alias);" + objectAliasRead + "base.mode = 2;",
|
||||
"candidate",
|
||||
],
|
||||
[
|
||||
"array write through the original after the read",
|
||||
true,
|
||||
arrayAliasRead + "base[0] = 2;",
|
||||
"resolved",
|
||||
],
|
||||
[
|
||||
"array write through the alias after the read",
|
||||
true,
|
||||
arrayAliasRead + "alias[0] = 2;",
|
||||
"resolved",
|
||||
],
|
||||
[
|
||||
"array write through the original before the read",
|
||||
true,
|
||||
"base[0] = 2;" + arrayAliasRead,
|
||||
"candidate",
|
||||
],
|
||||
[
|
||||
"array write through the alias before the read",
|
||||
true,
|
||||
"alias[0] = 2;" + arrayAliasRead,
|
||||
"candidate",
|
||||
],
|
||||
[
|
||||
"array shortened through the original before the read",
|
||||
true,
|
||||
"base.length = 0;" + arrayAliasRead + "alias[0] = 2;",
|
||||
"candidate",
|
||||
],
|
||||
[
|
||||
"array shortened through the alias before the read",
|
||||
true,
|
||||
"alias.length = 0;" + arrayAliasRead + "base[0] = 2;",
|
||||
"candidate",
|
||||
],
|
||||
[
|
||||
"array multi-hop aliases",
|
||||
true,
|
||||
"const lifted = alias; let selected; [selected] = lifted; alias[0] = 2;",
|
||||
"resolved",
|
||||
],
|
||||
[
|
||||
"array instance iterator replaced through the original",
|
||||
true,
|
||||
"base[Symbol.iterator] = replacement;" + arrayAliasRead + "alias[0] = 2;",
|
||||
"candidate",
|
||||
],
|
||||
[
|
||||
"array instance iterator replaced through the alias",
|
||||
true,
|
||||
"alias[Symbol.iterator] = replacement;" + arrayAliasRead + "base[0] = 2;",
|
||||
"candidate",
|
||||
],
|
||||
[
|
||||
"array prototype iterator replaced before the read",
|
||||
true,
|
||||
"Array.prototype[Symbol.iterator] = replacement;" +
|
||||
arrayAliasRead +
|
||||
"base[0] = 2;",
|
||||
"candidate",
|
||||
],
|
||||
[
|
||||
"shared iterator next replaced before the read",
|
||||
true,
|
||||
"Object.getPrototypeOf([].values()).next = replacement;" +
|
||||
arrayAliasRead +
|
||||
"base[0] = 2;",
|
||||
"candidate",
|
||||
],
|
||||
[
|
||||
"shared iterator next replaced after the read",
|
||||
true,
|
||||
arrayAliasRead +
|
||||
"Object.getPrototypeOf([].values()).next = replacement; base[0] = 2;",
|
||||
"resolved",
|
||||
],
|
||||
] as const)(
|
||||
"keeps read-time dependencies for %s",
|
||||
(_name, array, reads, resolution) => {
|
||||
const graph = graphForJavaScript(`
|
||||
const replacement = () => ({ value: 3, done: false });
|
||||
const base = ${array ? "[1, 0]" : "{ mode: 1, other: 0 }"};
|
||||
const alias = base;
|
||||
${reads}
|
||||
`);
|
||||
const selected = graph.nodes.find(
|
||||
({ kind, label }) => kind === "binding" && label === "selected",
|
||||
);
|
||||
const relation = graph.relations.find(
|
||||
(edge) =>
|
||||
edge.relation === "destructures" &&
|
||||
edge.target_node_id === selected?.node_id,
|
||||
);
|
||||
expect(relation).toMatchObject({ resolution });
|
||||
},
|
||||
);
|
||||
|
||||
it.each([
|
||||
[
|
||||
"getter",
|
||||
"const base = { get mode() { return 1; } }; const alias = base; " +
|
||||
objectAliasRead +
|
||||
"alias.mode = 2;",
|
||||
],
|
||||
[
|
||||
"rebound source",
|
||||
"let base = { mode: 1 }; const alias = base; base = { mode: 2 }; " +
|
||||
objectAliasRead +
|
||||
"alias.mode = 3;",
|
||||
],
|
||||
[
|
||||
"rebound alias",
|
||||
"const base = { mode: 1 }; let alias = base; alias = { mode: 2 }; " +
|
||||
objectAliasRead +
|
||||
"alias.mode = 3;",
|
||||
],
|
||||
[
|
||||
"read before source initializer",
|
||||
"const alias = base; const base = { mode: 1 }; " +
|
||||
objectAliasRead +
|
||||
"base.mode = 2;",
|
||||
],
|
||||
] as const)("leaves %s uncertain in the const alias proof", (_name, source) => {
|
||||
const graph = graphForJavaScript(source);
|
||||
const selected = graph.nodes.find(
|
||||
({ kind, label }) => kind === "binding" && label === "selected",
|
||||
);
|
||||
expect(
|
||||
graph.relations.find(
|
||||
(edge) =>
|
||||
edge.relation === "destructures" &&
|
||||
edge.target_node_id === selected?.node_id,
|
||||
),
|
||||
).toMatchObject({ resolution: "candidate" });
|
||||
});
|
||||
|
||||
it.each([
|
||||
["", "base.mode = 2;", "resolved"],
|
||||
["base.mode = 2;", "", "candidate"],
|
||||
] as const)(
|
||||
"uses the allocation's effect ordering for a direct alias property read (%s)",
|
||||
(before, after, resolution) => {
|
||||
const graph = graphForJavaScript(`
|
||||
const base = { mode: 1 };
|
||||
const alias = base;
|
||||
${before}
|
||||
const selected = alias.mode;
|
||||
${after}
|
||||
`);
|
||||
expect(
|
||||
graph.relations.find(({ relation }) => relation === "reads-property"),
|
||||
).toMatchObject({ resolution });
|
||||
},
|
||||
);
|
||||
@@ -12,7 +12,17 @@ import { semanticBindingPrecedesCapture } from "./javascriptSemanticMutationInit
|
||||
import { semanticArrayIndex } from "./javascriptSemanticPropertyPaths.js";
|
||||
import { semanticKnownArrayIteration } from "./javascriptSemanticReferenceSources.js";
|
||||
import { evaluateSemanticExpression } from "./javascriptSemanticValues.js";
|
||||
import type { JavaScriptSemanticAnalysisState } from "./javascriptSemanticState.js";
|
||||
import {
|
||||
resolveSemanticBindingState,
|
||||
semanticStableInitializer,
|
||||
type JavaScriptSemanticAnalysisState,
|
||||
type JavaScriptSemanticBindingState,
|
||||
} from "./javascriptSemanticState.js";
|
||||
|
||||
interface ReadPresenceOrigin {
|
||||
readonly binding: JavaScriptSemanticBindingState;
|
||||
readonly value: t.Node;
|
||||
}
|
||||
|
||||
/** Prove a direct data slot in its source at the time of the read. */
|
||||
export const createSemanticReadPresenceQuery = (
|
||||
@@ -22,6 +32,56 @@ export const createSemanticReadPresenceQuery = (
|
||||
t.ObjectExpression,
|
||||
ReturnType<typeof staticObjectPropertyReader>
|
||||
>();
|
||||
const constAliasOrigins = new WeakMap<
|
||||
JavaScriptSemanticBindingState,
|
||||
ReadPresenceOrigin | null
|
||||
>();
|
||||
const constAliasOrigin = (
|
||||
initial: JavaScriptSemanticBindingState,
|
||||
): ReadPresenceOrigin | undefined => {
|
||||
const pending: JavaScriptSemanticBindingState[] = [];
|
||||
let binding = initial;
|
||||
let origin = constAliasOrigins.get(binding);
|
||||
while (origin === undefined) {
|
||||
pending.push(binding);
|
||||
const initializer = semanticStableInitializer(binding, state);
|
||||
if (
|
||||
binding.mutable ||
|
||||
!binding.declarationKinds.has("const") ||
|
||||
initializer === undefined ||
|
||||
initializer.projection.length !== 0 ||
|
||||
binding.referenceInitializers.length !== 0
|
||||
) {
|
||||
origin = null;
|
||||
break;
|
||||
}
|
||||
const value = unwrapJavaScriptExpression(initializer.node).node;
|
||||
if (!t.isIdentifier(value)) {
|
||||
origin = { binding, value };
|
||||
break;
|
||||
}
|
||||
const target = resolveSemanticBindingState(state, value, value.name);
|
||||
// Each accepted alias captures an already initialized const in this
|
||||
// activation, so the walk strictly precedes the last initializer read.
|
||||
if (
|
||||
target === undefined ||
|
||||
!semanticBindingPrecedesCapture(
|
||||
target,
|
||||
initializer.node,
|
||||
state.parentsByNode,
|
||||
) ||
|
||||
opaqueCodeMayRebindBinding(target, state, initializer.node)
|
||||
) {
|
||||
origin = null;
|
||||
break;
|
||||
}
|
||||
binding = target;
|
||||
origin = constAliasOrigins.get(binding);
|
||||
}
|
||||
for (const visited of pending) constAliasOrigins.set(visited, origin);
|
||||
return origin ?? undefined;
|
||||
};
|
||||
|
||||
return (
|
||||
bindingId: string | null,
|
||||
capture: t.Node,
|
||||
@@ -36,13 +96,27 @@ export const createSemanticReadPresenceQuery = (
|
||||
initializer === undefined ||
|
||||
initializer.projection.length !== 0 ||
|
||||
binding.referenceInitializers.length !== 0 ||
|
||||
!hasRecordedReferenceEffects(binding, state) ||
|
||||
state.conditionallySkippedExpressions.has(initializer.node) ||
|
||||
!semanticBindingPrecedesCapture(binding, capture, state.parentsByNode) ||
|
||||
opaqueCodeMayRebindBinding(binding, state, capture)
|
||||
)
|
||||
return undefined;
|
||||
const value = unwrapJavaScriptExpression(initializer.node).node;
|
||||
const initialValue = unwrapJavaScriptExpression(initializer.node).node;
|
||||
const origin = t.isIdentifier(initialValue)
|
||||
? constAliasOrigin(binding)
|
||||
: { binding, value: initialValue };
|
||||
if (
|
||||
origin === undefined ||
|
||||
!hasRecordedReferenceEffects(origin.binding, state) ||
|
||||
(origin.binding !== binding &&
|
||||
!semanticBindingPrecedesCapture(
|
||||
origin.binding,
|
||||
capture,
|
||||
state.parentsByNode,
|
||||
))
|
||||
)
|
||||
return undefined;
|
||||
const { value } = origin;
|
||||
if (source === "array-iteration") {
|
||||
if (!t.isArrayExpression(value)) return undefined;
|
||||
const array = evaluateSemanticExpression(
|
||||
@@ -64,7 +138,12 @@ export const createSemanticReadPresenceQuery = (
|
||||
return (
|
||||
slot?.name === key &&
|
||||
slot.presence === "present" &&
|
||||
!capturedBindingPathMayHaveChanged(binding, path, capture, state)
|
||||
!capturedBindingPathMayHaveChanged(
|
||||
origin.binding,
|
||||
path,
|
||||
capture,
|
||||
state,
|
||||
)
|
||||
);
|
||||
};
|
||||
}
|
||||
@@ -83,7 +162,12 @@ export const createSemanticReadPresenceQuery = (
|
||||
if (property.status !== "explicit") return false;
|
||||
receiver = unwrapJavaScriptExpression(property.value).node;
|
||||
}
|
||||
return !capturedBindingPathMayHaveChanged(binding, path, capture, state);
|
||||
return !capturedBindingPathMayHaveChanged(
|
||||
origin.binding,
|
||||
path,
|
||||
capture,
|
||||
state,
|
||||
);
|
||||
};
|
||||
};
|
||||
};
|
||||
Reference in new issue
Block a user