mirror of
https://github.com/morluto/rea.git
synced 2026-12-07 00:34:10 +08:00
fix(javascript): close member-path cycles through self stores (#7131)
A write below a slot that stores a value read from the same slot, such as `s.c = s.c.p; s.c.i = 1` on a parameter, revisits the binding with one more segment after the stored key on every turn. The cycle closure only recognized segments added at the front of the path (`node = node[key]`), so the work-list never terminated and exhausted the owned worker heap (CodeMirror 5 `mode/stylus/stylus.js`). Recognize a contiguous segment inserted after a stable base, check that repeating it after that base can never be pruned, and escape through it once, as the self-alias rule already does. Front insertions keep their existing order and result. Fixes #7130 Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
70b96b65d9
commit
25f0f9934d
@@ -1,7 +1,14 @@
|
||||
import * as t from "@babel/types";
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { analyzeJavaScriptSemantics } from "./javascriptSemanticAnalysis.js";
|
||||
import {
|
||||
analyzeJavaScriptSemantics,
|
||||
analyzeParsedJavaScriptSemantics,
|
||||
} from "./javascriptSemanticAnalysis.js";
|
||||
import { onlyCallable } from "./javascriptSemanticAnalysis.fixture.js";
|
||||
import type { JavaScriptSemanticIr } from "./javascriptSemanticIr.js";
|
||||
import { traverseJavaScriptAst } from "./javascriptSemanticTraversal.js";
|
||||
import { parseJavaScriptSource } from "./javascriptSourceParser.js";
|
||||
|
||||
const returnedValue = (body: string, expression: string) =>
|
||||
onlyCallable(
|
||||
@@ -109,3 +116,88 @@ describe("member-path cycles and captured aliases", () => {
|
||||
).toBe("unknown");
|
||||
});
|
||||
});
|
||||
|
||||
describe("member-path cycles through self stores", () => {
|
||||
/**
|
||||
* Analyze a source while counting member-object reads. An unclosed cycle
|
||||
* grows its path on every revisit; failing at a fixed count keeps that
|
||||
* regression fast instead of exhausting the test worker's heap.
|
||||
*/
|
||||
const analyzeWithBoundedMemberReads = (
|
||||
source: string,
|
||||
limit: number,
|
||||
): { readonly ir: JavaScriptSemanticIr; readonly reads: number } => {
|
||||
const file = parseJavaScriptSource(source);
|
||||
if (file === null) throw new Error("fixture must parse");
|
||||
let reads = 0;
|
||||
traverseJavaScriptAst(file, {
|
||||
enter: (node) => {
|
||||
if (!t.isMemberExpression(node)) return;
|
||||
const object = node.object;
|
||||
Object.defineProperty(node, "object", {
|
||||
enumerable: true,
|
||||
configurable: true,
|
||||
get: () => {
|
||||
if (++reads > limit)
|
||||
throw new Error(`member-object reads exceeded ${String(limit)}`);
|
||||
return object;
|
||||
},
|
||||
});
|
||||
},
|
||||
});
|
||||
reads = 0;
|
||||
const ir = analyzeParsedJavaScriptSemantics(file);
|
||||
return { ir, reads };
|
||||
};
|
||||
|
||||
it.each([
|
||||
["one key", "s.c = s.c.p; s.c.i = 1;"],
|
||||
["a nested key", "s.a.b = s.a.b.next; s.a.b.x = 1;"],
|
||||
["a computed key", "s[k] = s[k].p; s[k].i = 1;"],
|
||||
["a two-step segment", "s.c = s.c.p.q; s.c.i = 1;"],
|
||||
[
|
||||
"two alternating slots",
|
||||
"s.c = s.d.p; s.d = s.c.q; s.c.i = 1; s.d.j = 2;",
|
||||
],
|
||||
[
|
||||
"a CodeMirror-style context stack",
|
||||
`s.context = new Context(s.context);
|
||||
s.context = s.context.prev;
|
||||
s.context.indent = 0;
|
||||
s.context.line.firstWord = "x";`,
|
||||
],
|
||||
])(
|
||||
"closes a parameter slot that stores a value read from itself through %s",
|
||||
(_shape, body) => {
|
||||
// Each call can see the previous call's store, so the written receiver
|
||||
// may be any number of segments below the slot's original value.
|
||||
const { ir, reads } = analyzeWithBoundedMemberReads(
|
||||
`function Context(prev) { this.prev = prev; this.line = {}; }
|
||||
function update(s, k) { ${body} }
|
||||
module.exports = update;`,
|
||||
5_000,
|
||||
);
|
||||
expect(ir.coverage).toEqual({ status: "complete", omittedCount: 0 });
|
||||
expect(reads).toBeGreaterThan(0);
|
||||
},
|
||||
);
|
||||
|
||||
it("keeps unrelated slots exact when a local stack pops in a loop", () => {
|
||||
const body = `
|
||||
const seed = {
|
||||
context: { prev: { prev: { value: 1 }, value: 1 }, value: 1 },
|
||||
keep: 3,
|
||||
};
|
||||
const state = seed;
|
||||
while (state.flag) state.context = state.context.prev;
|
||||
state.context.value = 2;
|
||||
`;
|
||||
expect(returnedValue(body, "seed.context.prev.prev.value")?.status).toBe(
|
||||
"unknown",
|
||||
);
|
||||
expect(returnedValue(body, "seed.keep")).toEqual({
|
||||
status: "literal",
|
||||
value: 3,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user