mirror of
https://github.com/morluto/rea.git
synced 2026-12-07 06:33:04 +08:00
fix(javascript): distinguish Promise and FileHandle releases (#9584)
* fix(javascript): distinguish Promise and FileHandle releases * fix(javascript): preserve reassigned handle candidates * test(javascript): select unisolated route fixtures explicitly * test(javascript): distinguish package scope in route fixtures * test(apple): separate identity and refusal fixture groups * fix(javascript): retain uncertain releases after opaque receiver writes --------- Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
This commit is contained in:
@@ -14,6 +14,8 @@ import {
|
||||
unwrapJavaScriptExpression,
|
||||
} from "./javascriptAstValues.js";
|
||||
import { isUnmodifiedGlobalReference } from "./javascriptSemanticAmbientWrites.js";
|
||||
import { semanticOpaqueBindingReadMayHaveChanged } from "./javascriptSemanticOpaqueEffects.js";
|
||||
import { semanticBindingAtRead } from "./javascriptSemanticMutationInitializers.js";
|
||||
import {
|
||||
resolveSemanticBindingState,
|
||||
type JavaScriptSemanticAnalysisState,
|
||||
@@ -51,6 +53,8 @@ interface ResourceCandidate {
|
||||
readonly method: JavaScriptSemanticResourceOperation["method"];
|
||||
readonly ownerCallableId: string | null;
|
||||
readonly resultBindingId: string | null;
|
||||
/** The initial result is the opening Promise; later writes may replace it. */
|
||||
readonly resultIsPromise: boolean;
|
||||
}
|
||||
|
||||
/** Recover built-in filesystem/network acquisition and local release handles. */
|
||||
@@ -345,6 +349,15 @@ const collectAcquisitions = (
|
||||
: { builtinAuthority: reference.builtinAuthority }),
|
||||
ownerCallableId: context.callableStack.at(-1) ?? null,
|
||||
resultBindingId: outerDataEffectBinding(node, context),
|
||||
resultIsPromise:
|
||||
reference.builtinAuthority === undefined &&
|
||||
reference.specifier === "fs/promises" &&
|
||||
reference.method === "open" &&
|
||||
!context.ancestors.some(
|
||||
(parent) =>
|
||||
t.isAwaitExpression(parent) &&
|
||||
unwrapJavaScriptExpression(parent.argument).node === node,
|
||||
),
|
||||
});
|
||||
});
|
||||
return output;
|
||||
@@ -381,8 +394,30 @@ const collectReleases = (
|
||||
const candidates =
|
||||
binding === undefined ? [] : (byBinding.get(binding.bindingId) ?? []);
|
||||
if (binding === undefined || candidates.length === 0) return;
|
||||
const linked = candidates.filter((acquisition) =>
|
||||
releaseMethodsFor(acquisition).includes(method),
|
||||
const opaqueReceiver = semanticOpaqueBindingReadMayHaveChanged(
|
||||
binding,
|
||||
member.object,
|
||||
context.state,
|
||||
);
|
||||
const receiver = candidates.some(({ resultIsPromise }) => resultIsPromise)
|
||||
? semanticBindingAtRead(binding, member.object, context.state).binding
|
||||
: binding;
|
||||
const knownAcquisitions = new Set<t.Node>(
|
||||
candidates
|
||||
.filter(({ builtinAuthority }) => builtinAuthority === undefined)
|
||||
.map(({ node }) => node),
|
||||
);
|
||||
const linked = candidates.filter(
|
||||
(acquisition) =>
|
||||
releaseMethodsFor(acquisition).includes(method) &&
|
||||
(!acquisition.resultIsPromise ||
|
||||
opaqueReceiver ||
|
||||
mayReceiveFulfilledHandle(
|
||||
receiver,
|
||||
acquisition,
|
||||
knownAcquisitions,
|
||||
context.state,
|
||||
)),
|
||||
);
|
||||
// No linked acquisition exposes this release method, so the call is not a
|
||||
// release of it at all: Node throws at the call site instead.
|
||||
@@ -399,6 +434,7 @@ const collectReleases = (
|
||||
? { builtinAuthority: "candidate" as const }
|
||||
: {}),
|
||||
resolution:
|
||||
!opaqueReceiver &&
|
||||
linked.length === 1 &&
|
||||
holdsOnlyAcquisitions(binding, linked, context.state)
|
||||
? "complete"
|
||||
@@ -408,6 +444,34 @@ const collectReleases = (
|
||||
return output;
|
||||
};
|
||||
|
||||
/** Unwrap only direct awaits; a standalone await does not write its operand. */
|
||||
const resourceInitializer = (node: t.Node) => {
|
||||
let value = unwrapJavaScriptExpression(node).node;
|
||||
let awaited = false;
|
||||
while (t.isAwaitExpression(value)) {
|
||||
awaited = true;
|
||||
value = unwrapJavaScriptExpression(value.argument).node;
|
||||
}
|
||||
return { value, awaited };
|
||||
};
|
||||
|
||||
/** Unknown receiver writes retain a candidate; known other acquisitions do not alias it. */
|
||||
const mayReceiveFulfilledHandle = (
|
||||
binding: JavaScriptSemanticBindingState,
|
||||
acquisition: ResourceCandidate,
|
||||
knownAcquisitions: ReadonlySet<t.Node>,
|
||||
state: JavaScriptSemanticAnalysisState,
|
||||
): boolean =>
|
||||
[...binding.initializers, ...binding.referenceInitializers].some(
|
||||
({ node, projection }) => {
|
||||
if (projection.length > 0) return true;
|
||||
const { value, awaited } = resourceInitializer(node);
|
||||
if (value === acquisition.node) return awaited;
|
||||
if (isNullishPlaceholder(value, state)) return false;
|
||||
return !knownAcquisitions.has(value);
|
||||
},
|
||||
);
|
||||
|
||||
/**
|
||||
* A lexical handle identifies its acquisition only while every value written
|
||||
* to the binding is that acquisition itself. Any replacement, even a
|
||||
@@ -423,9 +487,7 @@ const holdsOnlyAcquisitions = (
|
||||
[...binding.initializers, ...binding.referenceInitializers].every(
|
||||
({ node, projection }) => {
|
||||
if (projection.length > 0) return false;
|
||||
let value = unwrapJavaScriptExpression(node).node;
|
||||
if (t.isAwaitExpression(value))
|
||||
value = unwrapJavaScriptExpression(value.argument).node;
|
||||
const { value } = resourceInitializer(node);
|
||||
return (
|
||||
isNullishPlaceholder(value, state) ||
|
||||
linked.some((acquisition) => acquisition.node === value)
|
||||
|
||||
Reference in New Issue
Block a user