fix(javascript): qualify releases after resource method mutation (#10166)

* fix(javascript): qualify releases after resource method mutation

* fix(javascript): qualify releases after opaque receiver mutation

* fix(javascript): preserve release ordering and mutation facts

* fix(javascript): order mutations of fresh resource receivers

* fix(javascript): match release mutations to captured receivers

* fix(javascript): check delegated release method identity

* fix(javascript): track paths back to native resource prototypes

* fix(javascript): respect native release member ownership

* fix(javascript): retain shared prototype effects across calls

* fix(javascript): order release effects after definite acquisition

* fix(javascript): preserve prototype effects across receiver replacement

* fix(javascript): preserve native release identity across proven effects

* fix(javascript): preserve shared prototype uncertainty after opaque calls

---------

Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
This commit is contained in:
RRFZandmorluto authored and GitHub committed 2026-11-18 17:02:04 -05:00
1 parent 2191896f3b
commit caab34d0de
8 files changed
+812 -56

No files matched your search

@@ -40,6 +40,7 @@ import {
semanticBindingLookupAt,
type JavaScriptSemanticAnalysisState,
type JavaScriptSemanticBindingState,
type JavaScriptSemanticMemberEffect,
type JavaScriptSemanticReferenceEffectsAtSite,
} from "./javascriptSemanticState.js";
@@ -587,11 +588,131 @@ export const prototypeMutationMayPrecede = (
semanticEffectTiming(read, effect.node, state) !== "after",
);
/**
* Whether the `method` a read selects from `binding`'s object may have been
* replaced before the read: by direct eval that can reach the object (a const
* binding still exposes its properties), or by a write to the object, to
* `method` or to its prototype, or by an escape of the object or its
* prototype. Passing the method itself cannot replace its native [[Call]], and
* the call that receives the object has already selected the method.
* With `fresh`, each activation initializes a new instance. Writes to a
* shared prototype can still reach later activations or replacement receivers.
* `targets` filters only effects confined to the selected instance.
*/
export const bindingMethodMayHaveChanged = (
binding: JavaScriptSemanticBindingState,
method: string,
read: t.Node,
state: JavaScriptSemanticAnalysisState,
{
fresh,
inherited,
definingPrototypeDepth,
isolatedNativeCloses,
targets,
}: {
fresh: boolean;
inherited: boolean;
definingPrototypeDepth: number | undefined;
isolatedNativeCloses: boolean;
targets: (effect: JavaScriptSemanticMemberEffect) => boolean;
},
): boolean => {
// A prototype hop stays on the receiver chain. A constructor hop can
// return to that chain through .prototype. A wildcard may do either.
const reachesReceiver = (
path: JavaScriptSemanticPropertyPath,
length: number,
escape = false,
) => {
if (!inherited) return length === 0;
let receiver = true;
let constructor = false;
for (let i = 0; i < length && (receiver || constructor); i++) {
const key = path[i]!;
const nextConstructor: boolean =
receiver && semanticPropertyPathKeyMatches(key, "constructor");
receiver =
(receiver && semanticPropertyPathKeyMatches(key, "__proto__")) ||
(constructor && semanticPropertyPathKeyMatches(key, "prototype"));
constructor = nextConstructor;
}
// The native constructor itself also exposes its mutable prototype.
return receiver || (escape && constructor);
};
const mayPrecede = (
effect: t.Node | undefined,
onInstance: boolean,
): boolean =>
semanticEffectTiming(read, effect, state) !== "after" &&
!(
fresh &&
onInstance &&
effect !== undefined &&
semanticReadPrecedesEffectWithinActivation(
read,
effect,
state.parentsByNode,
)
);
for (const effect of semanticOpaqueBindingEffects(
binding,
state,
"reference",
))
if (mayPrecede(effect, !inherited)) return true;
const shadowedWrite = (path: JavaScriptSemanticPropertyPath): boolean =>
definingPrototypeDepth !== undefined &&
path.length - 1 > definingPrototypeDepth &&
path.at(-1) === method &&
path.slice(0, -1).every((key) => key === "__proto__");
return (
binding.memberWrites.some(
(effect) =>
(effect.path.length > 1 || targets(effect)) &&
!shadowedWrite(effect.path) &&
// A write changes a slot on its parent. Rebinding C.prototype
// leaves the prototype of an already acquired receiver intact.
(effect.path.length === 0 ||
(reachesReceiver(effect.path, effect.path.length - 1) &&
(semanticPropertyPathKeyMatches(effect.path.at(-1)!, method) ||
(inherited &&
semanticPropertyPathKeyMatches(
effect.path.at(-1)!,
"__proto__",
))))) &&
mayPrecede(effect.node, effect.path.length <= 1),
) ||
binding.escapedPaths.some((effect) => {
// An opaque caller handed an instance can still reach its shared prototype.
const instanceOnly = effect.path.length === 0 && !inherited;
return (
!(isolatedNativeCloses && effect.path.length === 0) &&
(!instanceOnly || targets(effect)) &&
reachesReceiver(effect.path, effect.path.length, true) &&
effect.node !== state.parentsByNode.get(read) &&
mayPrecede(effect.node, instanceOnly)
);
})
);
};
/** Whether a member write to `binding`'s object is recorded. */
export const bindingHasMemberWrites = (
binding: JavaScriptSemanticBindingState,
): boolean => binding.memberWrites.length > 0;
/** An opaque release can see the receiver only through its recorded effects. */
export const bindingEffectsConfinedToReceiverCalls = (
binding: JavaScriptSemanticBindingState,
matchesCall: (node: t.Node) => boolean,
): boolean =>
!bindingHasMemberWrites(binding) &&
binding.escapedPaths.every(
({ node, path }) =>
path.length === 0 && node !== undefined && matchesCall(node),
);
/**
* The nodes recorded member writes to `binding`'s object involve, in record
* order: each write's site (undefined when it was recorded without one), then