fix(javascript): preserve global escapes across callback boundaries

This commit is contained in:
RRFZ authored and Ysopwnable committed 2026-11-14 04:46:27 +01:00
1 parent 54677ec1d6
commit 4e1e9b62a7
2 files changed
+32 -11

No files matched your search

@@ -965,17 +965,19 @@ const createGlobalPathRead = (
// That escape cannot have replaced the callee before the call selects it.
const escapePrecedesRead = effects.escapes.some((effect) => {
const escape = effect.node;
if (
escape !== undefined &&
escape !== site &&
typeof escape.start === "number" &&
typeof escape.end === "number" &&
typeof site.start === "number" &&
typeof site.end === "number" &&
site.start >= escape.start &&
site.end <= escape.end
)
return false;
// Containment alone does not order deferred callback/class bodies.
// Only a read reached without crossing such a boundary precedes escape.
if (escape !== undefined && escape !== site) {
let parent = state.parentsByNode.get(site);
while (
parent !== undefined &&
!t.isFunction(parent) &&
!t.isClass(parent)
) {
if (parent === escape) return false;
parent = state.parentsByNode.get(parent);
}
}
return semanticEffectTiming(site, escape, state) !== "after";
});
if (
@@ -156,3 +156,22 @@ it("selects a constructor before an argument can escape the global object", () =
`),
).toEqual([]);
});
it.each(["Symbol", "BigInt"])(
"retains %s identity uncertainty inside an escaped callback",
(constructor) => {
const result = operations(`
import configure from "./configure.js";
import { setTimeout, clearTimeout } from "node:timers";
configure(globalThis, () => {
const handle = setTimeout(() => {}, ${constructor}("1"));
clearTimeout(handle);
});
`);
expect(result).toMatchObject([
{ kind: "schedule", resolution: "complete" },
{ kind: "cancel", resolution: "complete" },
]);
expect(result[1]?.linkedTimerId).toBe(result[0]?.timerId);
},
);