refactor(javascript): ask the effect log owner whether opaque and reference effects changed a read (#11252)

* refactor(javascript): ask one owner whether opaque code changed an intrinsic global read

Six intrinsic global readers combined semanticOpaqueGlobalMayHaveChanged and semanticOpaqueNameMayHaveChanged themselves, each choosing which effect kinds applied and where to time them.

isIntrinsicGlobalReference and opaqueCodeMayChangeGlobalRead in javascriptSemanticGlobalReference.ts now own both proofs: a sloppy direct eval visible from the reference may introduce its root name, and global code that may run before the observation site may replace a writable global. The owner also knows that undefined, NaN and Infinity are non-writable, so global code cannot replace them. Map lookups and XMLHttpRequest open calls pass the call as the observation site, keeping their call-time timing. An architecture guard forbids the two helpers outside their owners.

Slice 1 of docs/adr/javascript/pushed-effect-invalidation.md (#11188).

* refactor(javascript): ask one owner whether opaque code rebound a binding (#11254)

Ten binding readers combined semanticBindingCanBeRebound, semanticBindingMayHaveOpaqueEffect and semanticOpaqueBindingReadMayHaveChanged themselves; three repeated the same rebindability-and-effect pairing.

opaqueCodeMayRebindBinding(binding, state, read?) and opaqueCodeMayReachBindingReference(binding, state, capturePoint) in javascriptSemanticOpaqueEffects.ts now own the rebinding, read-timing, capture and introduced-name proofs. semanticBindingMayHaveOpaqueEffect is private to the owner and semanticOpaqueBindingReadMayHaveChanged is removed, so a reader cannot pick effect kinds again.

Slice 2 of docs/adr/javascript/pushed-effect-invalidation.md (#11188).

* refactor(javascript): ask one owner whether an intrinsic instance method changed (#11266)

* refactor(javascript): ask one owner whether an intrinsic instance method changed

Map lookups and XMLHttpRequest.open each walked receiver aliases and combined
member-effect checks for the method, the instance prototype link and the
intrinsic prototypes. isIntrinsicInstanceMethodCall now owns that walk; Map
lookups also gain the prototype-link check XHR already made. The member-effect
view asks semanticOpaqueReferenceEffects for opaque sites instead of combining
the rebinding and reach helpers, which become private.

Refs docs/adr/javascript/pushed-effect-invalidation.md (slice 3).

* refactor(javascript): keep global opaque-effect checks private to their owner

opaqueCodeMayChangeGlobalRead moves into javascriptSemanticOpaqueEffects.ts, so
semanticOpaqueGlobalMayHaveChanged and semanticOpaqueNameMayHaveChanged are no
longer exported and the slice 1 text guard is replaced by the compiler.

* refactor(javascript): make the effect log the one reader of recorded opaque sites (#11282)

* refactor(javascript): make the effect log the one reader of recorded opaque sites

Rename javascriptSemanticOpaqueEffects.ts to javascriptSemanticEffectLog.ts
(ADR decision 1) and move the reference-effects view
(visitSemanticReferenceEffectsAtSite, the effect report, the member-path
query and its completeness gate) out of javascriptSemanticAnalysis.ts into it.

Recorded opaque sites move behind javascriptSemanticEffectRecord.ts accessors
(direct-eval site, recorded direct eval, global-code sites), which the timing
producer in javascriptSemanticMutationInitializers.ts and the call frontier in
javascriptSemanticCalls.ts now ask instead of reading state.opaqueEffects.
The member-mutation completeness WeakSet duplicated
state.memberMutationFactsComplete and is removed.

An architecture guard keeps `.opaqueEffects` reads inside the log and its
record module.

* refactor(javascript): keep the opaque reference-effect generator private to the effect log

* refactor(javascript): drop analysis-hub imports that only the moved effect queries used

* refactor(javascript): ask the effect log for a binding reference's recorded effects (#11295)

The value engine applied member writes, escapes and direct-eval reach to a
binding's reference itself, with its own capture proofs. That application
moves into javascriptSemanticEffectLog.ts as
applySemanticBindingReferenceEffects, and opaqueCodeMayReachBindingReference
becomes private to the log.

A counted guard (javascript-effect-index-read) records every remaining read
of the per-kind effect indexes outside the log; it can only shrink, so new
reader-side checks fail the guard.

* refactor(javascript): ask the effect log whether an ambient path may have changed (#11308)

* refactor(javascript): ask the effect log mayObserve for member-path reads (#11313)

* refactor(javascript): name the member-path effect query mayObserve

* refactor(javascript): ask the effect log whether an ambient path effect may precede a read

* refactor(javascript): ask the effect log about require and process rebinding (#11318)

refactor(javascript): ask the effect log about require and process rebinding and effect generations

* refactor(javascript): build the ambient-write module interop in one owner (#11323)

* refactor(javascript): build the ambient-write module interop in one owner and ask the effect log in CommonJS receivers

* refactor(javascript): ask the effect log whether binding assignments precede a read (#11329)

refactor(javascript): ask the effect log whether binding assignments or prototype writes precede a read

* fix(javascript): count prototype escapes before resolving a URL object request target (#11368)

fix(javascript): count URL and Object prototype escapes before resolving a URL object target
This commit is contained in:
morluto authored and GitHub committed 2026-11-16 03:10:46 +08:00
1 parent 77e106c0f3
commit 9efaaffd75
38 files changed
+1428 -1019

No files matched your search

@@ -13,47 +13,25 @@ import {
} from "./javascriptSemanticState.js";
import { isUnmodifiedGlobalReference } from "./javascriptSemanticGlobalReference.js";
import { traverseJavaScriptValueAstSteps } from "./javascriptSemanticTraversal.js";
import { semanticPropertyPathKeyMatches } from "./javascriptSemanticPropertyPaths.js";
import { createSemanticModuleInterop } from "./javascriptModuleInterop.js";
import { semanticModuleInteropWithAmbientWrites } from "./javascriptModuleInterop.js";
import {
ambientPathWriteSites,
bindingMemberEffectMayPrecede,
} from "./javascriptSemanticEffectLog.js";
import { evaluateSemanticExpression } from "./javascriptSemanticValues.js";
import type { JavaScriptSemanticPropertyPath } from "./javascriptSemanticPropertyPaths.js";
import { semanticProjectedSourcesAtRead } from "./javascriptSemanticProjectedSources.js";
import {
isUnshadowedGlobal,
semanticBindingHasPriorValueWrite,
semanticEffectTiming,
} from "./javascriptSemanticMutationInitializers.js";
import { javaScriptRuntimeContext } from "./javascriptSourceParser.js";
const exportInvocations = new WeakMap<
JavaScriptSemanticAnalysisState,
ReturnType<typeof createSemanticModuleInterop>
>();
/** Share the intrinsic owner's positional and capture proofs across CJS consumers. */
const commonJsExportInvocation = (
node: t.CallExpression,
state: JavaScriptSemanticAnalysisState,
) => {
let interop = exportInvocations.get(state);
if (interop === undefined) {
interop = createSemanticModuleInterop(state);
for (const { node: write, path } of state.ambientWriteEffects)
if (write !== undefined && path[0] === "global")
interop.retainWrite(write, [
"intrinsic",
...path
.slice(1)
.map((key) =>
typeof key === "string" || typeof key === "number"
? String(key)
: null,
),
]);
exportInvocations.set(state, interop);
}
return interop.intrinsicInvocation(node);
};
) => semanticModuleInteropWithAmbientWrites(state).intrinsicInvocation(node);
type ReceiverStatus = "connected" | "detached" | "uncertain";
export interface CommonJsExportReceiverEffect {
@@ -99,22 +77,7 @@ const copiedOwnFields = (
if (t.isIdentifier(node)) {
const binding = resolveSemanticBindingState(state, node, node.name);
if (binding === undefined) return unknown;
if (
binding.memberWrites.some(
(effect) =>
semanticEffectTiming(source, effect.node, state) !== "after",
) ||
binding.escapedPaths.some(
(effect) =>
effect.node !== undefined &&
!(
(effect.node.start ?? -1) <= (source.start ?? -1) &&
(effect.node.end ?? -1) >= (source.end ?? -1)
) &&
semanticEffectTiming(source, effect.node, state) !== "after",
)
)
return unknown;
if (bindingMemberEffectMayPrecede(state, binding, source)) return unknown;
const selected = semanticProjectedSourcesAtRead(
binding,
node,
@@ -445,16 +408,12 @@ export function* indexCommonJsExportReceiversSteps(
const publicCallWrites = new WeakSet<t.Node>();
// Consume the shared write owner's resolved aliases, intrinsic invocation
// and keyed mutator facts instead of reinterpreting call names here.
for (const { node, path } of state.ambientWriteEffects) {
if (
(t.isCallExpression(node) || t.isOptionalCallExpression(node)) &&
path[0] === "commonjs-module" &&
path.length <= 2 &&
(path.length === 1 ||
semanticPropertyPathKeyMatches(path[1] ?? null, "exports"))
)
for (const node of ambientPathWriteSites(state, [
"commonjs-module",
"exports",
]))
if (t.isCallExpression(node) || t.isOptionalCallExpression(node))
publicCallWrites.add(node);
}
const valueOf = (node: t.Node): Identity => {
const value = unwrapJavaScriptExpression(node).node;
// An opaque result may alias another object, but reading the current