From 0a7d288ed81ae63bd0270c67feb2e8fad6b77696 Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Mon, 16 Nov 2026 06:28:41 +0800 Subject: [PATCH] fix(javascript): move reader effect reads into the effect log and order require and process rebinding by activation (#11356) * refactor(javascript): move the last reader effect reads into the effect log * refactor(javascript): skip escapes when a reader asks only about member writes * fix(javascript): order require and process rebinding by activation timing (#11479) * refactor(javascript): ask one ordering whether require was rebound before a call * refactor(javascript): order process replacement by activation, like require * fix(javascript): keep instance field values out of literal-loop source ordering * fix(javascript): stop effect ordering at instance field values inside the effect semanticEffectFollowsReference proved that an assignment follows a read written inside its own right-hand side, even when the read sits in an instance field value, which runs on construction after the assignment. process = class { value = process.env.MODE; }; new process() therefore kept the original process identity, and require = class { fs = require('fs') } kept a Node module origin. The first walk now stops at instance field values like it stops at function bodies. * fix(javascript): attribute process effects that may reach the original object (#11521) * fix(javascript): attribute process effects that may reach the original object A write or escape through the global process object was attributed to node:process only when no replacement of process may precede it. Effects need the opposite question: they reach the original unless a replacement with a fresh object is proven to run before them. The effect log gains ambientReplacementPrecedes (the must counterpart of the may-observe queries), RequireOrigin owns the effect question, and member mutation asks it for direct, qualified and ambient process effects. Refs #11488 * fix(javascript): prove a process replacement with the global path reader ambientReplacementPrecedes accepted any earlier fresh assignment of the process path, even when a later write might restore the original or when the assignment went through a possible alias of the global object. The global path reader already owns definite replacement at a site (it checks the written namespace and every competing write), so the effect question asks it and keeps only the freshness check. ambientReplacementPrecedes is removed. --- scripts/verify/architecture-guards.mjs | 22 +- .../javascript-effect-index-read.json | 6 +- src/domain/javascript/javascriptRouteScan.ts | 8 +- .../javascriptSemanticAmbientWrites.ts | 19 +- ...tSemanticAnalysis.builtin_defaults.test.ts | 149 ++++++++++ .../javascriptSemanticDataEffectHelpers.ts | 84 +++--- .../javascript/javascriptSemanticEffectLog.ts | 254 +++++++++++++++++- .../javascriptSemanticMemberMutations.ts | 16 +- ...scriptSemanticMutationInitializers.test.ts | 55 +++- .../javascriptSemanticMutationInitializers.ts | 52 +++- .../javascriptSemanticProjectedSources.ts | 162 ++--------- .../javascriptSemanticProjection.ts | 3 +- .../javascriptSemanticRequireOrigin.ts | 73 ++++- .../javascriptSourceParserContext.test.ts | 128 +++++++++ 14 files changed, 770 insertions(+), 261 deletions(-) diff --git a/scripts/verify/architecture-guards.mjs b/scripts/verify/architecture-guards.mjs index 135b7db33a..fe6f607792 100644 --- a/scripts/verify/architecture-guards.mjs +++ b/scripts/verify/architecture-guards.mjs @@ -364,8 +364,13 @@ checkCounted( // Pushed effect invalidation (docs/adr/javascript/pushed-effect-invalidation.md). // Recorded effects belong to the effect log. A reader that reads an effect -// index itself decides on its own which effect kinds and proofs apply; new -// readers ask the log instead, and each migrated reader lowers this count. +// index itself decides on its own which effect kinds and proofs apply; readers +// ask the log instead. Every reader has moved; the remaining counts are the +// producers that build these indexes (javascriptSemanticAmbientWrites.ts, +// javascriptSemanticMemberMutations.ts, and javascriptSemanticAnalysis.ts, +// which creates and settles them) and the timing-proof owner +// (javascriptSemanticMutationInitializers.ts), whose position proofs over +// recorded sites the log delegates to. checkCounted( "read of a JavaScript effect index outside the effect log (ask javascriptSemanticEffectLog.ts)", "javascript-effect-index-read", @@ -374,6 +379,19 @@ checkCounted( ["src/domain/javascript/javascriptSemanticEffectLog.ts"], ); +// Effect timing has one owner (javascriptSemanticMutationInitializers.ts) and +// one consumer, the effect log. Everything else asks the log whether an effect +// may precede a read, naming the ordering it relies on. +check( + "semanticEffectTiming outside the effect log (ask semanticEffectMayPrecede or a query in javascriptSemanticEffectLog.ts)", + files, + /\bsemanticEffectTiming\b/u, + [ + "src/domain/javascript/javascriptSemanticEffectLog.ts", + "src/domain/javascript/javascriptSemanticMutationInitializers.ts", + ], +); + // Coverage status has one vocabulary, coverageStatusSchema in // src/domain/coverage.ts. A local enum that contains "complete" either copies // it or drops `unknown`, which forces an unknown producer to pick a side. diff --git a/scripts/verify/baselines/javascript-effect-index-read.json b/scripts/verify/baselines/javascript-effect-index-read.json index 1157c05043..2cbc84cc4b 100644 --- a/scripts/verify/baselines/javascript-effect-index-read.json +++ b/scripts/verify/baselines/javascript-effect-index-read.json @@ -1,10 +1,6 @@ { - "src/domain/javascript/javascriptRouteScan.ts": 1, "src/domain/javascript/javascriptSemanticAmbientWrites.ts": 25, "src/domain/javascript/javascriptSemanticAnalysis.ts": 4, - "src/domain/javascript/javascriptSemanticDataEffectHelpers.ts": 3, "src/domain/javascript/javascriptSemanticMemberMutations.ts": 5, - "src/domain/javascript/javascriptSemanticMutationInitializers.ts": 3, - "src/domain/javascript/javascriptSemanticProjectedSources.ts": 3, - "src/domain/javascript/javascriptSemanticProjection.ts": 1 + "src/domain/javascript/javascriptSemanticMutationInitializers.ts": 3 } diff --git a/src/domain/javascript/javascriptRouteScan.ts b/src/domain/javascript/javascriptRouteScan.ts index b9c24a3152..f930cc9e00 100644 --- a/src/domain/javascript/javascriptRouteScan.ts +++ b/src/domain/javascript/javascriptRouteScan.ts @@ -32,6 +32,7 @@ import { } from "./javascriptSemanticAliasSelections.js"; import { semanticProjectedSourcesAtRead } from "./javascriptSemanticProjectedSources.js"; import { semanticBindingAtRead } from "./javascriptSemanticMutationInitializers.js"; +import { semanticMemberWriteNodes } from "./javascriptSemanticEffectLog.js"; import { javaScriptRuntimeContext } from "./javascriptSourceParser.js"; import { semanticObjectPropertyReferences, @@ -1101,11 +1102,8 @@ export class JavaScriptRouteScan { // selected-source list. Repeated reads share cached object field parsing. for (const sources of [binding.initializers, binding.referenceInitializers]) for (const source of sources) if (!this.#queue(source.node)) return; - for (const write of binding.memberWrites) { - if (!this.#queue(write.node ?? node)) return; - for (const source of write.assignedSources ?? []) - if (!this.#queue(source.node)) return; - } + for (const site of semanticMemberWriteNodes(binding)) + if (!this.#queue(site ?? node)) return; // Value flow excludes proven replacements; lifetime identity eligibility // would keep them and incorrectly hide otherwise independent routes. const sources = semanticProjectedSourcesAtRead( diff --git a/src/domain/javascript/javascriptSemanticAmbientWrites.ts b/src/domain/javascript/javascriptSemanticAmbientWrites.ts index c5d530bdaf..b5412a972b 100644 --- a/src/domain/javascript/javascriptSemanticAmbientWrites.ts +++ b/src/domain/javascript/javascriptSemanticAmbientWrites.ts @@ -10,7 +10,11 @@ import { electronMemberPath, } from "./javascriptElectronMemberWrites.js"; import { createSemanticModuleInterop } from "./javascriptModuleInterop.js"; -import { ambientEffectCovers } from "./javascriptSemanticEffectLog.js"; +import { + ambientEffectCovers, + semanticEffectMayFollow, + semanticEffectMayPrecede, +} from "./javascriptSemanticEffectLog.js"; import { resolveSemanticBindingState, semanticStableInitializer, @@ -20,7 +24,6 @@ import { isUnshadowedGlobal, semanticBindingAtRead, semanticDefiniteEffectPrecedesReference, - semanticEffectTiming, } from "./javascriptSemanticMutationInitializers.js"; import { semanticIterationSources } from "./javascriptSemanticIterationSources.js"; import type { SemanticIterationSource } from "./javascriptSemanticIterationFacts.js"; @@ -821,8 +824,8 @@ export function* indexJavaScriptAmbientWritesSteps( ); return ( matches.length === 0 || - matches.some( - (entry) => semanticEffectTiming(node, entry.node, state) !== "after", + matches.some((entry) => + semanticEffectMayPrecede(state, node, entry.node), ) ); }; @@ -951,7 +954,7 @@ const createGlobalPathRead = ( node.callee === site ) return false; - return semanticEffectTiming(site, node, state) !== "after"; + return semanticEffectMayPrecede(state, site, node); }); if ( (escapesCovered && effects.escapes.length === 0) || @@ -959,8 +962,8 @@ const createGlobalPathRead = ( (writesCovered && effects.writes.length === 0) ) return { status: "unknown" }; - const active = effects.writes.filter( - (effect) => semanticEffectTiming(site, effect.node, state) !== "after", + const active = effects.writes.filter((effect) => + semanticEffectMayPrecede(state, site, effect.node), ); if (active.length === 0) return { status: "unmodified" }; if ( @@ -1013,7 +1016,7 @@ const globalPathReplacement = ( effects.some( (effect) => effect.node !== confirmed && - semanticEffectTiming(confirmed, effect.node, state) !== "before" && + semanticEffectMayFollow(state, confirmed, effect.node) && (effect.node === undefined || !semanticDefiniteEffectPrecedesReference( effect.node, diff --git a/src/domain/javascript/javascriptSemanticAnalysis.builtin_defaults.test.ts b/src/domain/javascript/javascriptSemanticAnalysis.builtin_defaults.test.ts index d18f80d26f..df258b32e6 100644 --- a/src/domain/javascript/javascriptSemanticAnalysis.builtin_defaults.test.ts +++ b/src/domain/javascript/javascriptSemanticAnalysis.builtin_defaults.test.ts @@ -58,6 +58,155 @@ it.each([ expect(ir.configurationOperations.map(({ key }) => key)).toEqual(["MODE"]); }); +// Whether `process` still names the original global process object uses +// activation timing, like the require loader. Each row states why the +// replacement is proven to run after the read or write, or why it may not. +it.each([ + { + source: `const value = process.env.kept; for (const x of [1]) process = other;`, + keys: ["kept"], + why: "the loop is a later top-level statement, so it starts only after the read", + }, + { + source: `const value = process.env.kept; for (const x of []) process = other;`, + keys: ["kept"], + why: "an empty array literal with unchanged array iteration never runs the body", + }, + { + source: `process = wrap(process.env.kept);`, + keys: ["kept"], + why: "the right-hand side is evaluated before the assignment stores its value", + }, + { + source: `process.env = {}; for (const x of [1]) process = other; const proc = require('process'); const mode = proc.env.wrong;`, + keys: [], + why: "the env write runs before the loop, so it reaches the original object", + }, + { + source: `globalThis.process.env = {}; for (const x of [1]) globalThis.process = other; const proc = require('process'); const mode = proc.env.wrong;`, + keys: [], + why: "a qualified write before the loop reaches the original object", + }, + { + source: `Array.prototype[Symbol.iterator] = gen; const value = process.env.maybe; for (const x of []) process = other;`, + keys: [], + why: "a replaced array iterator can yield from an empty literal", + }, + { + source: `for (const x of [1]) process = other; const value = process.env.wrong;`, + keys: [], + why: "the loop body runs before the read", + }, + { + source: `class Box { value = process.env.wrong; } for (const x of [1]) process = other; new Box();`, + keys: [], + why: "an instance field runs on construction, after the loop", + }, + { + source: `process = class { value = process.env.wrong; }; new process();`, + keys: [], + why: "a field of the replacement class runs on construction, after the replacement", + }, +])( + "orders process replacement by activation: $source ($why)", + ({ source, keys }) => { + expect( + analyzeCommonJsSemanticFixture(source).configurationOperations.map( + ({ key }) => key, + ), + ).toEqual(keys); + }, +); + +// A write through `process` reaches the original object unless a replacement +// is proven to run before it. A replacement that may run first is not enough. +it.each([ + { + source: `process.env = {}; while (ready) process = other;`, + keys: [], + why: "the loop body may never run, and runs only after the write", + }, + { + source: `if (ready) process = other; process.env = {};`, + keys: [], + why: "the replacement may be skipped, so the write may reach the original", + }, + { + source: `process.env = {}; if (ready) process = other;`, + keys: [], + why: "the write runs before the replacement", + }, + { + source: `if (ready) globalThis.process = other; globalThis.process.env = {};`, + keys: [], + why: "a skipped qualified replacement leaves the original in place", + }, + { + source: `function swap() { process = other; } swap(); process.env = {};`, + keys: [], + why: "a replacement inside a function body is not proven to run", + }, + { + source: `function write() { process.env = {}; } write(); process = other;`, + keys: [], + why: "the write runs in a call made before the replacement", + }, + { + source: `process = other; process.env = {};`, + keys: [], + why: "the replacement may be the original object itself", + }, + { + source: `if (ready) process = other; patch(process);`, + keys: [], + why: "an escape that may reach the original is kept like a write", + }, + { + source: `if (ready) globalThis.process = other; patch(globalThis.process);`, + keys: [], + why: "a qualified escape that may reach the original is kept", + }, + { + source: `if (ready) process = other; process.env.kept = "";`, + keys: [], + why: "a nested write that may reach the original is kept", + }, + { + source: `process = {}; patch(process);`, + keys: ["kept"], + why: "a fresh replacement always runs before the escape", + }, + { + source: `process = {}; process.env = {};`, + keys: ["kept"], + why: "a fresh object always replaces the original before the write", + }, + { + source: `globalThis.process = {}; process.env = {};`, + keys: ["kept"], + why: "a fresh qualified replacement always runs before the write", + }, + { + source: `const original = process; process = {}; if (ready) process = original; process.env = {};`, + keys: [], + why: "a later conditional write may restore the original before the write", + }, + { + source: `const host = ready ? globalThis : {}; host.process = {}; process.env = {};`, + keys: [], + why: "a write through a possible alias of the global object may not replace process", + }, +])( + "attributes process writes that may reach the original: $source ($why)", + ({ source, keys }) => { + expect( + analyzeCommonJsSemanticFixture( + `${source} const proc = require('process'); const mode = proc.env.kept;`, + ).configurationOperations.map(({ key }) => key), + ).toEqual(keys); + }, +); + it("preserves exports captured before a later loader overwrite", () => { const ir = analyzeCommonJsSemanticFixture(` const fs=require('node:fs'); fs.readFileSync('before'); diff --git a/src/domain/javascript/javascriptSemanticDataEffectHelpers.ts b/src/domain/javascript/javascriptSemanticDataEffectHelpers.ts index 88f0e0a8a8..871bd6bea5 100644 --- a/src/domain/javascript/javascriptSemanticDataEffectHelpers.ts +++ b/src/domain/javascript/javascriptSemanticDataEffectHelpers.ts @@ -28,12 +28,12 @@ import { semanticTopLevelEffectFollowsCapture, } from "./javascriptSemanticMutationInitializers.js"; import { createSemanticCallableIdStack } from "./javascriptSemanticCallableStack.js"; +import { bindingMemberEffectMayPrecedeReads } from "./javascriptSemanticEffectLog.js"; import { resolveSemanticBindingState, semanticStableInitializer, type JavaScriptSemanticAnalysisState, type JavaScriptSemanticBindingState, - type JavaScriptSemanticMemberEffect, type JavaScriptSemanticRequiredModuleEffect, } from "./javascriptSemanticState.js"; import { traverseJavaScriptValueAst } from "./javascriptSemanticTraversal.js"; @@ -477,37 +477,29 @@ const computeBuiltinBindingPath = ( // Provenance identifies the module, independently of whether a selected slot // still refers to its original built-in after member mutation or escape. if ( - binding.memberWrites.some( - ({ path, node }) => - affectsBuiltinPath(path, selectedPath) && - !( - semanticTopLevelEffectFollowsCapture( + bindingMemberEffectMayPrecedeReads( + state, + binding, + (kind, { path, node }) => { + if (!affectsBuiltinPath(path, selectedPath)) return undefined; + if (kind === "member-write") + return [ path.length === 0 && provenanceCapture !== undefined ? provenanceCapture : capture, - node, - state.parentsByNode, - ) || - (savedObjectCapture !== undefined && - path.length < selectedPath.length && - semanticTopLevelEffectFollowsCapture( - savedObjectCapture, - node, - state.parentsByNode, - )) - ), - ) || - builtinEscapes(binding, state, allowCandidate).some( - ({ path, node }) => - affectsBuiltinPath(path, selectedPath) && + ...(savedObjectCapture !== undefined && + path.length < selectedPath.length + ? [savedObjectCapture] + : []), + ]; // A function-value escape cannot replace its native [[Call]]. An // escaped containing object can still replace the selected method. - (!callableValue || path.length < selectedPath.length) && - !semanticTopLevelEffectFollowsCapture( - capture, - node, - state.parentsByNode, - ), + return (!callableValue || path.length < selectedPath.length) && + !originalBuiltinReceiver(node, state, allowCandidate) + ? [capture] + : undefined; + }, + "top-level", ) ) return null; @@ -766,10 +758,6 @@ interface BuiltinEffectIndex { string, readonly JavaScriptSemanticRequiredModuleEffect[] >; - readonly bindingEscapes: Map< - string, - readonly JavaScriptSemanticMemberEffect[] - >; readonly receivers: WeakMap; readonly candidateReceivers: WeakMap; } @@ -794,7 +782,6 @@ const builtinEffectIndex = ( ]), ), moduleEffects: new Map(), - bindingEscapes: new Map(), receivers: new WeakMap(), candidateReceivers: new WeakMap(), }; @@ -803,23 +790,6 @@ const builtinEffectIndex = ( return index; }; -const builtinEscapes = ( - binding: JavaScriptSemanticBindingState, - state: JavaScriptSemanticAnalysisState, - allowCandidate = false, -): readonly JavaScriptSemanticMemberEffect[] => { - const cache = builtinEffectIndex(state).bindingEscapes; - const key = `${binding.bindingId}:${allowCandidate ? "candidate" : "proven"}`; - let effects = cache.get(key); - if (effects === undefined) { - effects = binding.escapedPaths.filter( - ({ node }) => !originalBuiltinReceiver(node, state, allowCandidate), - ); - cache.set(key, effects); - } - return effects; -}; - const builtinModuleEffects = ( specifier: string, state: JavaScriptSemanticAnalysisState, @@ -999,11 +969,17 @@ const computeOriginalBuiltinReceiver = ( !BUILTIN_RECEIVER_METHODS[ posixReceiver ? "path/posix" : origin.specifier ]?.has(method) || - receiver?.memberWrites.some( - ({ path, node: write }) => - affectsBuiltinPath(path, [...receiverPath, method]) && - !semanticTopLevelEffectFollowsCapture(node, write, state.parentsByNode), - ) || + (receiver !== undefined && + bindingMemberEffectMayPrecedeReads( + state, + receiver, + (_, { path }) => + affectsBuiltinPath(path, [...receiverPath, method]) + ? [node] + : undefined, + "top-level", + "member-writes", + )) || sources.some((source) => { const shared = method === null diff --git a/src/domain/javascript/javascriptSemanticEffectLog.ts b/src/domain/javascript/javascriptSemanticEffectLog.ts index 6d3ea7d5ce..eee8b6268c 100644 --- a/src/domain/javascript/javascriptSemanticEffectLog.ts +++ b/src/domain/javascript/javascriptSemanticEffectLog.ts @@ -19,10 +19,17 @@ import { invalidateSemanticMutationPaths, } from "./javascriptSemanticMutationValues.js"; import type { JavaScriptSemanticValue } from "./javascriptSemanticValueTypes.js"; +import type { SemanticAliasSelectionPath } from "./javascriptSemanticAliasSelections.js"; +import { + semanticEffectPathCandidates, + semanticEffectPathMatches, + semanticSomeEffectPathCandidate, +} from "./javascriptSemanticEffectPathIndex.js"; import { isUnshadowedGlobal, semanticBindingEffectFollowsCapture, semanticEscapeFollowsCapture, + semanticDefiniteEffectPrecedesReference, semanticEffectTiming, semanticBindingEffectFollowsRead, semanticReadPrecedesEffectWithinActivation, @@ -321,19 +328,34 @@ export const ambientEffectCovers = ( */ export type SemanticEffectOrdering = "activation" | "top-level"; -const effectMayPrecedeRead = ( +/** + * Whether `effect` may run before `read` under `ordering`. Owners that index + * their own effects (the ambient-write producer, RequireOrigin's + * `module.require` writes) ask this; readers of recorded effects ask the + * queries below. + */ +export const semanticEffectMayPrecede = ( state: JavaScriptSemanticAnalysisState, read: t.Node, effect: t.Node | undefined, - ordering: SemanticEffectOrdering, + ordering: SemanticEffectOrdering = "activation", ): boolean => ordering === "activation" ? semanticEffectTiming(read, effect, state) !== "after" : !semanticTopLevelEffectFollowsCapture(read, effect, state.parentsByNode); +/** Whether `effect` may run after `read`: it is not proven to run before. */ +export const semanticEffectMayFollow = ( + state: JavaScriptSemanticAnalysisState, + read: t.Node, + effect: t.Node | undefined, +): boolean => semanticEffectTiming(read, effect, state) !== "before"; + type AmbientEffect = JavaScriptSemanticAnalysisState["ambientWriteEffects"][number]; +type MemberWrite = JavaScriptSemanticBindingState["memberWrites"][number]; + /** Matching effects per queried path; the effect lists only grow, so their * lengths tell when a retained selection is stale. */ const ambientPathSelections = new WeakMap< @@ -384,18 +406,19 @@ const ambientPathEffects = ( /** * `mayObserve` for an ambient member path: whether a recorded write to `path` * or to one of its prefixes may run before `read`. With `"contents"`, an - * escape of it to code whose effects are not evaluated counts too. + * escape of it to code whose effects are not evaluated counts too. Ambient + * questions use activation timing only, so every reader of one ambient path + * or name gets the same answer for the same read. */ export const ambientPathMayObserveAt = ( state: JavaScriptSemanticAnalysisState, read: t.Node, path: readonly string[], observes: "value" | "contents" = "value", - ordering: SemanticEffectOrdering = "activation", ): boolean => { const { writes, escapes } = ambientPathEffects(state, path); const mayPrecede = ({ node }: AmbientEffect): boolean => - effectMayPrecedeRead(state, read, node, ordering); + semanticEffectMayPrecede(state, read, node); return ( writes.some(mayPrecede) || (observes === "contents" && escapes.some(mayPrecede)) @@ -404,16 +427,17 @@ export const ambientPathMayObserveAt = ( /** * `mayObserve` for an unbound name: whether a recorded assignment to the - * ambient binding `name` may run before `read`. + * ambient binding `name` may run before `read`. The loader and the origin of + * one unbound `require` call, and the original global `process` object, ask + * this one question. */ export const ambientBindingMayObserveWriteAt = ( state: JavaScriptSemanticAnalysisState, read: t.Node, name: string, - ordering: SemanticEffectOrdering = "activation", ): boolean => (state.ambientBindingWrites.get(name) ?? []).some((write) => - effectMayPrecedeRead(state, read, write, ordering), + semanticEffectMayPrecede(state, read, write), ); /** @@ -489,6 +513,39 @@ export const bindingMemberEffectMayPrecede = ( semanticEffectTiming(read, effect.node, state) !== "after", ); +/** + * Whether a recorded member write to `binding`'s object, or (with + * `"member-effects"`) an escape of it, may run before every read `observedAt` + * names for it. `observedAt` returns undefined for an effect the reader does + * not ask about. A reader that asks only about writes skips the escapes, which + * grow with every call through the binding. + */ +export const bindingMemberEffectMayPrecedeReads = ( + state: JavaScriptSemanticAnalysisState, + binding: JavaScriptSemanticBindingState, + observedAt: ( + kind: "member-write" | "escape", + effect: MemberWrite, + ) => readonly t.Node[] | undefined, + ordering: SemanticEffectOrdering, + observes: "member-writes" | "member-effects" = "member-effects", +): boolean => { + for (const [kind, effects] of [ + ["member-write", binding.memberWrites], + ["escape", observes === "member-effects" ? binding.escapedPaths : []], + ] as const) + for (const effect of effects) { + const reads = observedAt(kind, effect); + if ( + reads?.every((read) => + semanticEffectMayPrecede(state, read, effect.node, ordering), + ) + ) + return true; + } + return false; +}; + /** * Whether an assignment to `binding` recorded as a reference initializer * (a pattern or other non-ordinary write) may run before `read`. @@ -525,6 +582,25 @@ export const prototypeMutationMayPrecede = ( semanticEffectTiming(read, effect.node, state) !== "after", ); +/** Whether a member write to `binding`'s object is recorded. */ +export const bindingHasMemberWrites = ( + binding: JavaScriptSemanticBindingState, +): boolean => binding.memberWrites.length > 0; + +/** + * 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 + * the sources it assigned. + */ +export function* semanticMemberWriteNodes( + binding: JavaScriptSemanticBindingState, +): Generator { + for (const effect of binding.memberWrites) { + yield effect.node; + for (const source of effect.assignedSources ?? []) yield source.node; + } +} + /** The sources recorded member writes assigned into `binding`'s object. */ export function* semanticMemberWriteSources( binding: JavaScriptSemanticBindingState, @@ -533,6 +609,168 @@ export function* semanticMemberWriteSources( yield* effect.assignedSources ?? []; } +const sameLiteralPrefix = ( + prefix: JavaScriptSemanticPropertyPath, + path: SemanticAliasSelectionPath, +): boolean => + prefix.every( + (key, index) => + key !== null && + typeof key !== "object" && + path[index] !== null && + typeof path[index] !== "object" && + String(key) === String(path[index]), + ); + +/** + * `mayObserve` for a projected member read of `binding`'s object at `path`: + * the recorded member writes to `path` or to one of its prefixes that may run + * before the read, in record order. `read.at(depth)` is where the selection + * at `depth` is read; `read.capturedAt(depth)` is the copy that captured it, + * when the read went through shallow copies, and `read.captured` are the + * initializers the read's binding was captured from. + * + * Two replacement proofs stay here. A definite `=` write to a captured prefix + * after the capture replaces the receiver, so writes through that prefix are + * not observed when `freshReceiver` shows the new receiver is a fresh + * allocation. The last definite `=` write to the selected path itself that + * precedes the read is the replacement; `superseded` tells which earlier + * writes it replaces. + */ +export const semanticBindingMemberWritesObservedAt = ( + state: JavaScriptSemanticAnalysisState, + binding: JavaScriptSemanticBindingState, + path: SemanticAliasSelectionPath, + read: { + readonly at: (depth: number) => t.Node; + readonly capturedAt: ((depth: number) => t.Node | undefined) | undefined; + readonly captured: readonly { + readonly node: t.Node; + readonly projection: unknown; + }[]; + }, + freshReceiver: ( + root: t.Node, + path: JavaScriptSemanticPropertyPath, + state: JavaScriptSemanticAnalysisState, + ) => boolean, +): { + readonly writes: readonly MemberWrite[]; + /** Whether a definite replacement of the selected path precedes the read. */ + readonly replaced: boolean; + readonly superseded: (effect: MemberWrite) => boolean; + /** Whether an escape of `path` or a prefix may precede the read. */ + readonly escapeMayPrecede: () => boolean; +} => { + const { at, capturedAt, captured } = read; + // Only a definite assignment can replace an already captured receiver. + // Select candidates once per query rather than rescanning every propagated + // may-write for each matching effect. + type PrefixReplacement = MemberWrite & { + readonly receiverIdentity: "definite"; + readonly node: t.AssignmentExpression; + }; + const prefixReplacements = + capturedAt === undefined + ? [] + : binding.memberWrites.filter( + (replacement): replacement is PrefixReplacement => + replacement.receiverIdentity === "definite" && + t.isAssignmentExpression(replacement.node, { operator: "=" }) && + replacement.path.length < path.length, + ); + const targetsReplacedPrefix = (effect: MemberWrite): boolean => + prefixReplacements.some((replacement) => { + const capture = capturedAt!(replacement.path.length); + const receiverAt = effect.receiverAt ?? effect.node; + return ( + capture !== undefined && + receiverAt !== undefined && + replacement.path.length < effect.path.length && + sameLiteralPrefix(replacement.path, effect.path) && + semanticTopLevelEffectFollowsCapture( + capture, + replacement.node, + state.parentsByNode, + ) && + semanticDefiniteEffectPrecedesReference( + replacement.node, + receiverAt, + state.parentsByNode, + ) && + freshReceiver( + replacement.node.right, + effect.path.slice(replacement.path.length, -1), + state, + ) + ); + }); + // Every condition is a pure query. Test the selected path first: it is the + // cheapest and rejects most records of a binding with many member paths. + const matches = (effect: MemberWrite) => + semanticEffectPathMatches(effect.path, path) && + (effect.receiverInitializers === undefined || + effect.receiverInitializers.length === 0 || + captured.length === 0 || + effect.receiverInitializers.some((receiver) => + captured.some( + (source) => + source.node === receiver.node && + JSON.stringify(source.projection) === + JSON.stringify(receiver.projection), + ), + )) && + semanticEffectTiming(at(effect.path.length), effect.node, state) !== + "after" && + !targetsReplacedPrefix(effect); + const writes = semanticEffectPathCandidates( + binding.memberWrites, + path, + ).filter(matches); + let replacement: MemberWrite | undefined; + for (const effect of writes) { + if ( + effect.receiverIdentity === "definite" && + t.isAssignmentExpression(effect.node, { operator: "=" }) && + sameLiteralPrefix(effect.path, path) && + semanticDefiniteEffectPrecedesReference( + effect.node, + at(effect.path.length), + state.parentsByNode, + ) && + (replacement === undefined || + semanticDefiniteEffectPrecedesReference( + replacement.node!, + effect.node, + state.parentsByNode, + ) || + (replacement.node === effect.node && + (replacement.assignedSources?.[0]?.target?.start ?? -1) < + (effect.assignedSources?.[0]?.target?.start ?? -1))) + ) + replacement = effect; + } + const superseded = (effect: MemberWrite) => + replacement !== undefined && + effect.node !== undefined && + effect !== replacement && + (semanticEffectTiming(replacement.node!, effect.node, state) === "before" || + (effect.node === replacement.node && + (effect.assignedSources?.[0]?.target?.start ?? -1) < + (replacement.assignedSources?.[0]?.target?.start ?? -1))); + return { + writes, + replaced: replacement !== undefined, + superseded, + escapeMayPrecede: () => + semanticSomeEffectPathCandidate( + binding.escapedPaths, + path, + (effect) => matches(effect) && !superseded(effect), + ), + }; +}; + /** Declaration assignability is independent of observed explicit writes. */ const semanticBindingCanBeRebound = ( binding: JavaScriptSemanticBindingState, diff --git a/src/domain/javascript/javascriptSemanticMemberMutations.ts b/src/domain/javascript/javascriptSemanticMemberMutations.ts index 973633deb5..db900f0100 100644 --- a/src/domain/javascript/javascriptSemanticMemberMutations.ts +++ b/src/domain/javascript/javascriptSemanticMemberMutations.ts @@ -15,7 +15,7 @@ import { import { semanticGlobalProcessOrigin, semanticGlobalProcessPathOrigin, - semanticOriginalGlobalProcessAt, + semanticEffectMayReachOriginalGlobalProcess, semanticMutableBuiltinOrigin, semanticRequireExportsIdentity, semanticRequireOriginCandidate, @@ -761,7 +761,7 @@ export function* collectSemanticMemberMutationsSteps( path[1] === "process" && path.length > 2 && node !== undefined && - semanticOriginalGlobalProcessAt(node, state) + semanticEffectMayReachOriginalGlobalProcess(node, state) ) recordRequiredEffect("node:process", path.slice(2), "write", node); const selections = new Map(); @@ -1068,7 +1068,11 @@ export function* collectSemanticMemberMutationsSteps( ); continue; } - const globalProcess = semanticGlobalProcessPathOrigin(expression, state); + const globalProcess = semanticGlobalProcessPathOrigin( + expression, + state, + "effect", + ); if (globalProcess !== undefined) { recordRequiredEffect( globalProcess.specifier, @@ -1097,7 +1101,11 @@ export function* collectSemanticMemberMutationsSteps( recordedEffectCount += 1; clearSemanticValueCaches(state); } - const processOrigin = semanticGlobalProcessOrigin(expression, state); + const processOrigin = semanticGlobalProcessOrigin( + expression, + state, + "effect", + ); if (processOrigin !== undefined) recordRequiredEffect( processOrigin.specifier, diff --git a/src/domain/javascript/javascriptSemanticMutationInitializers.test.ts b/src/domain/javascript/javascriptSemanticMutationInitializers.test.ts index 8104b08874..86ece09996 100644 --- a/src/domain/javascript/javascriptSemanticMutationInitializers.test.ts +++ b/src/domain/javascript/javascriptSemanticMutationInitializers.test.ts @@ -3,11 +3,16 @@ import { describe, expect, it } from "vitest"; import { semanticBindingAtRead, + semanticEffectTiming, semanticMutationInitializers, semanticTopLevelEffectFollowsCapture, } from "./javascriptSemanticMutationInitializers.js"; import type { JavaScriptSemanticBindingState } from "./javascriptSemanticState.js"; -import { traverseJavaScriptAst } from "./javascriptSemanticTraversal.js"; +import { indexJavaScriptBindingsWithReceiversSteps } from "./javascriptSemanticAnalysis.js"; +import { + completeSemanticSteps, + traverseJavaScriptAst, +} from "./javascriptSemanticTraversal.js"; import { parseJavaScriptSource } from "./javascriptSourceParser.js"; it.each([ @@ -152,3 +157,51 @@ describe("parameter references copied into function body bindings", () => { }, ); }); + +// A literal loop at program level orders its body against program-level code +// by source position. Code in a function body or an instance field value runs +// later, on a call or a construction, so source position proves nothing. +it.each([ + ["const value = read(); for (const item of [1]) target = item;", "after"], + ["for (const item of [1]) target = item; const value = read();", "before"], + [ + "class Box { static value = read(); } for (const item of [1]) target = item;", + "after", + ], + [ + "class Box { value = read(); } for (const item of [1]) target = item; new Box();", + "unknown", + ], + [ + "for (const item of [1]) { var Box = class { value = (target = item); }; } const value = read(); new Box();", + "unknown", + ], +] as const)( + "orders a literal loop write against %s as %s", + (source, timing) => { + const parsed = parseJavaScriptSource(source); + if (parsed === null) throw new Error("Expected a valid fixture"); + const index = completeSemanticSteps( + indexJavaScriptBindingsWithReceiversSteps(parsed.program), + ); + let read: t.CallExpression | undefined; + let write: t.AssignmentExpression | undefined; + traverseJavaScriptAst(parsed.program, { + enter: (node) => { + if ( + t.isCallExpression(node) && + t.isIdentifier(node.callee, { name: "read" }) + ) + read = node; + if ( + t.isAssignmentExpression(node) && + t.isIdentifier(node.left, { name: "target" }) + ) + write = node; + }, + }); + if (read === undefined || write === undefined) + throw new Error("Expected a read and a write"); + expect(semanticEffectTiming(read, write, index.bindings)).toBe(timing); + }, +); diff --git a/src/domain/javascript/javascriptSemanticMutationInitializers.ts b/src/domain/javascript/javascriptSemanticMutationInitializers.ts index 9a1e441417..db5a75c0b4 100644 --- a/src/domain/javascript/javascriptSemanticMutationInitializers.ts +++ b/src/domain/javascript/javascriptSemanticMutationInitializers.ts @@ -292,7 +292,10 @@ export const semanticEffectFollowsReference = ( let current: t.Node | undefined = reference; let child: t.Node | undefined; while (current !== undefined && current !== effect) { - if (t.isFunction(current)) break; + // A function body or instance field value runs later, on its own + // activation, even when it is written inside the effect. + if (t.isFunction(current) || delayedInstanceFieldValue(current, child)) + break; child = current; current = parents.get(current); } @@ -358,17 +361,9 @@ export const semanticEffectTiming = ( if (cardinality === "empty") return "after"; if (cardinality === "nonempty") { const loopParent = state.parentsByNode.get(loop); - const crossesFunction = (node: t.Node): boolean => { - let current: t.Node | undefined = node; - while (current !== undefined) { - if (t.isFunction(current)) return true; - current = state.parentsByNode.get(current); - } - return false; - }; if ( loopParent === state.program && - !crossesFunction(site) && + runsInProgramActivation(site, state.program, state.parentsByNode) && typeof loop.start === "number" && typeof loop.end === "number" && typeof site.start === "number" @@ -385,16 +380,45 @@ export const semanticEffectTiming = ( return "unknown"; }; +/** + * Whether `node` runs when the program body itself runs: no function body or + * instance field value, which run later, on a call or a construction, lies + * between it and the program. + */ +const runsInProgramActivation = ( + node: t.Node, + program: t.Node, + parents: WeakMap, +): boolean => { + let child: t.Node | undefined; + for ( + let current: t.Node | undefined = node; + current !== undefined && current !== program; + current = parents.get(current) + ) { + if (t.isFunction(current) || delayedInstanceFieldValue(current, child)) + return false; + child = current; + } + return true; +}; + +/** The for-in/of loop whose body runs `node` in the same activation. */ const semanticEnclosingIteration = ( node: t.Node, state: JavaScriptSemanticAnalysisState, ): t.ForInStatement | t.ForOfStatement | undefined => { - let current: t.Node | undefined = node; - while (current !== undefined) { + let child: t.Node | undefined; + for ( + let current: t.Node | undefined = node; + current !== undefined; + current = state.parentsByNode.get(current) + ) { if (t.isForInStatement(current) || t.isForOfStatement(current)) return current; - if (t.isFunction(current)) return undefined; - current = state.parentsByNode.get(current); + if (t.isFunction(current) || delayedInstanceFieldValue(current, child)) + return undefined; + child = current; } return undefined; }; diff --git a/src/domain/javascript/javascriptSemanticProjectedSources.ts b/src/domain/javascript/javascriptSemanticProjectedSources.ts index 304658bee7..5d639a32a3 100644 --- a/src/domain/javascript/javascriptSemanticProjectedSources.ts +++ b/src/domain/javascript/javascriptSemanticProjectedSources.ts @@ -10,23 +10,14 @@ import { semanticArrayPropertyReferences, } from "./javascriptSemanticObjectReferences.js"; import type { JavaScriptSemanticObjectReference } from "./javascriptSemanticObjectReference.js"; -import { - semanticBindingAtRead, - semanticDefiniteEffectPrecedesReference, - semanticEffectTiming, - semanticTopLevelEffectFollowsCapture, -} from "./javascriptSemanticMutationInitializers.js"; +import { semanticBindingAtRead } from "./javascriptSemanticMutationInitializers.js"; +import { semanticBindingMemberWritesObservedAt } from "./javascriptSemanticEffectLog.js"; import { semanticReferenceIterationUnknown, semanticReferenceSourcesInactive, } from "./javascriptSemanticReferenceSources.js"; import { semanticCopiedReferencePath } from "./javascriptSemanticCopiedReferencePaths.js"; import { semanticStoredSourceSelection } from "./javascriptSemanticIterationSources.js"; -import { - semanticEffectPathCandidates, - semanticEffectPathMatches, - semanticSomeEffectPathCandidate, -} from "./javascriptSemanticEffectPathIndex.js"; import { semanticAliasSelectionChoices, type SemanticAliasSelectionPath, @@ -98,7 +89,7 @@ interface ProjectedSource { } /** A fresh parent does not prove a descendant receiver is a fresh allocation. */ -const hasFreshProjectedReceiver = ( +export const hasFreshProjectedReceiver = ( root: t.Node, path: JavaScriptSemanticPropertyPath, state: JavaScriptSemanticAnalysisState, @@ -243,139 +234,20 @@ const projectedSourceFactsAtRead = ( normalizedCaptures?.find((capture) => capture.depth >= depth)?.at; const selectedAt = (depth: number): t.Node => captureForDepth(depth) ?? memberReadAt; - const samePrefix = ( - prefix: JavaScriptSemanticBindingState["memberWrites"][number], - effect: JavaScriptSemanticBindingState["memberWrites"][number], - ): boolean => - prefix.path.length < effect.path.length && - prefix.path.every( - (key, index) => - key !== null && - typeof key !== "object" && - effect.path[index] !== null && - typeof effect.path[index] !== "object" && - String(key) === String(effect.path[index]), - ); - // Only a definite assignment can replace an already captured receiver. - // Select candidates once per query rather than rescanning every propagated - // may-write for each matching effect. - type PrefixReplacement = - JavaScriptSemanticBindingState["memberWrites"][number] & { - readonly receiverIdentity: "definite"; - readonly node: t.AssignmentExpression; - }; - const prefixReplacements = - normalizedCaptures === undefined - ? [] - : binding.memberWrites.filter( - (replacement): replacement is PrefixReplacement => - replacement.receiverIdentity === "definite" && - t.isAssignmentExpression(replacement.node, { operator: "=" }) && - replacement.path.length < path.length, - ); - const targetsReplacedPrefix = ( - effect: JavaScriptSemanticBindingState["memberWrites"][number], - ): boolean => - capturedSelections.length > 0 && - prefixReplacements.some((replacement) => { - const capture = captureForDepth(replacement.path.length); - const receiverAt = effect.receiverAt ?? effect.node; - return ( - capture !== undefined && - receiverAt !== undefined && - samePrefix(replacement, effect) && - semanticTopLevelEffectFollowsCapture( - capture, - replacement.node, - state.parentsByNode, - ) && - semanticDefiniteEffectPrecedesReference( - replacement.node, - receiverAt, - state.parentsByNode, - ) && - hasFreshProjectedReceiver( - replacement.node.right, - effect.path.slice(replacement.path.length, -1), - state, - ) - ); - }); - // Every condition is a pure query. Test the selected path first: it is the - // cheapest and rejects most records of a binding with many member paths. - const matches = ( - effect: JavaScriptSemanticBindingState["memberWrites"][number], - ) => - semanticEffectPathMatches(effect.path, path) && - (effect.receiverInitializers === undefined || - effect.receiverInitializers.length === 0 || - captured.length === 0 || - effect.receiverInitializers.some((receiver) => - captured.some( - (source) => - source.node === receiver.node && - JSON.stringify(source.projection) === - JSON.stringify(receiver.projection), - ), - )) && - semanticEffectTiming(selectedAt(effect.path.length), effect.node, state) !== - "after" && - !targetsReplacedPrefix(effect); - const writes = semanticEffectPathCandidates( - binding.memberWrites, - path, - ).filter(matches); - let replacement: - | JavaScriptSemanticBindingState["memberWrites"][number] - | undefined; - for (const effect of writes) { - if ( - effect.receiverIdentity === "definite" && - t.isAssignmentExpression(effect.node, { operator: "=" }) && - effect.path.every( - (key, index) => - key !== null && - typeof key !== "object" && - path[index] !== null && - typeof path[index] !== "object" && - String(key) === String(path[index]), - ) && - semanticDefiniteEffectPrecedesReference( - effect.node, - selectedAt(effect.path.length), - state.parentsByNode, - ) && - (replacement === undefined || - semanticDefiniteEffectPrecedesReference( - replacement.node!, - effect.node, - state.parentsByNode, - ) || - (replacement.node === effect.node && - (replacement.assignedSources?.[0]?.target?.start ?? -1) < - (effect.assignedSources?.[0]?.target?.start ?? -1))) - ) - replacement = effect; - } - const superseded = ( - effect: JavaScriptSemanticBindingState["memberWrites"][number], - ) => - replacement !== undefined && - effect.node !== undefined && - effect !== replacement && - (semanticEffectTiming(replacement.node!, effect.node, state) === "before" || - (effect.node === replacement.node && - (effect.assignedSources?.[0]?.target?.start ?? -1) < - (replacement.assignedSources?.[0]?.target?.start ?? -1))); - if ( - includeUnknown && - valueEscapesHideSources && - semanticSomeEffectPathCandidate( - binding.escapedPaths, + const { writes, replaced, superseded, escapeMayPrecede } = + semanticBindingMemberWritesObservedAt( + state, + binding, path, - (effect) => matches(effect) && !superseded(effect), - ) - ) + { + at: selectedAt, + capturedAt: + normalizedCaptures === undefined ? undefined : captureForDepth, + captured, + }, + hasFreshProjectedReceiver, + ); + if (includeUnknown && valueEscapesHideSources && escapeMayPrecede()) return undefined; const sources: ProjectedSource[] = []; const sourceKeys = new WeakMap>(); @@ -446,7 +318,7 @@ const projectedSourceFactsAtRead = ( } } const storedSources = [...sources]; - if (!retainEligibility && replacement !== undefined) + if (!retainEligibility && replaced) return { sources, storedSources, retainsOrigin: false }; for (const initializer of read.binding.initializers) { if (semanticReferenceIterationUnknown(initializer, state)) return undefined; diff --git a/src/domain/javascript/javascriptSemanticProjection.ts b/src/domain/javascript/javascriptSemanticProjection.ts index c4c1eba31f..0095a37070 100644 --- a/src/domain/javascript/javascriptSemanticProjection.ts +++ b/src/domain/javascript/javascriptSemanticProjection.ts @@ -13,6 +13,7 @@ import { type JavaScriptSemanticScopeState, } from "./javascriptSemanticState.js"; import { semanticRequireOriginCandidate } from "./javascriptSemanticRequireOrigin.js"; +import { bindingHasMemberWrites } from "./javascriptSemanticEffectLog.js"; import { compareUnicodeCodePoints } from "../unicodeCodePointOrder.js"; import { completeSemanticSteps, @@ -269,7 +270,7 @@ export const immutableSemanticBindings = ( mutable: binding.mutable, definitions: binding.definitions, value: evaluateSemanticBinding(binding, state), - ...(binding.memberWrites.length > 0 && + ...(bindingHasMemberWrites(binding) && binding.initializers.length === 1 && binding.initializers[0]?.projection.length === 0 ? { diff --git a/src/domain/javascript/javascriptSemanticRequireOrigin.ts b/src/domain/javascript/javascriptSemanticRequireOrigin.ts index 8431984a43..b8b17e7212 100644 --- a/src/domain/javascript/javascriptSemanticRequireOrigin.ts +++ b/src/domain/javascript/javascriptSemanticRequireOrigin.ts @@ -4,9 +4,14 @@ import { semanticProjectedSourceEligibilityAtRead, semanticNormalizeCapturedSelections, semanticShiftCapturedSelections, + hasFreshProjectedReceiver, type SemanticCapturedSelection, } from "./javascriptSemanticProjectedSources.js"; import * as t from "@babel/types"; +import { + createGlobalPathReader, + type GlobalReferenceRead, +} from "./javascriptSemanticAmbientWrites.js"; import { traverseJavaScriptValueAst } from "./javascriptSemanticTraversal.js"; import { BUILTIN_RESOURCE_METHODS } from "./javascriptBuiltinResourceMethods.js"; @@ -24,7 +29,6 @@ import { isUnmodifiedCommonJsWrapperBinding, semanticBindingAtRead, semanticBindingHasPriorValueWrite, - semanticEffectTiming, } from "./javascriptSemanticMutationInitializers.js"; import { semanticStaticPropertyKey, @@ -45,6 +49,7 @@ import { import { ambientBindingMayObserveWriteAt, ambientPathMayObserveAt, + semanticEffectMayPrecede, } from "./javascriptSemanticEffectLog.js"; import { createSemanticReturnedReferences } from "./javascriptSemanticReturnedReferences.js"; import { semanticIterationSources } from "./javascriptSemanticIterationSources.js"; @@ -122,8 +127,8 @@ const commonJsDelegateChangedAt = ( node: t.Node, state: JavaScriptSemanticAnalysisState, ): boolean => - writesModuleRequire(state).some( - (write) => semanticEffectTiming(node, write.node, state) !== "after", + writesModuleRequire(state).some((write) => + semanticEffectMayPrecede(state, node, write.node), ); const commonJsLoaderAt = ( @@ -1619,23 +1624,62 @@ export const semanticOriginalGlobalProcessAt = ( node: t.Node, state: JavaScriptSemanticAnalysisState, ): boolean => - !ambientPathMayObserveAt( - state, - node, - ["global", "process"], - "value", - "top-level", - ) && !ambientBindingMayObserveWriteAt(state, node, "process", "top-level"); + !ambientPathMayObserveAt(state, node, ["global", "process"]) && + !ambientBindingMayObserveWriteAt(state, node, "process"); + +const globalProcessReaders = new WeakMap< + JavaScriptSemanticAnalysisState, + (site: t.Node) => GlobalReferenceRead +>(); + +/** + * Whether a write or escape through the global process object at this source + * site may still reach the original object: the global path reader does not + * prove a replacement with a fresh object before it. Reads that need a + * definite identity ask `semanticOriginalGlobalProcessAt` instead. + */ +export const semanticEffectMayReachOriginalGlobalProcess = ( + node: t.Node, + state: JavaScriptSemanticAnalysisState, +): boolean => { + let read = globalProcessReaders.get(state); + if (read === undefined) { + read = createGlobalPathReader(state, ["process"]); + globalProcessReaders.set(state, read); + } + const fact = read(node); + return ( + fact.status !== "replacement" || + !hasFreshProjectedReceiver(fact.value, [], state) + ); +}; + +/** + * What a global process reference is used for. A `"read"` needs the original + * object for certain; an `"effect"` (a write or an escape through the + * reference) is attributed to it while it may still be the original. + */ +export type SemanticGlobalProcessUse = "read" | "effect"; + +const globalProcessAt = ( + node: t.Node, + state: JavaScriptSemanticAnalysisState, + use: SemanticGlobalProcessUse, +): boolean => + use === "read" + ? semanticOriginalGlobalProcessAt(node, state) + : semanticEffectMayReachOriginalGlobalProcess(node, state); /** Node's unbound process reference shares the cached process exports object. */ export const semanticGlobalProcessOrigin = ( node: t.Node, state: JavaScriptSemanticAnalysisState, + use: SemanticGlobalProcessUse = "read", ): JavaScriptModuleOrigin | undefined => t.isIdentifier(node, { name: "process" }) && resolveSemanticBindingState(state, node, "process") === undefined && !semanticResolutionBlocked(state, node, "process") && - semanticOriginalGlobalProcessAt(node, state) + globalProcessAt(node, state, use) ? { specifier: "node:process", importedPath: [], resolutionMode: "require" } : undefined; @@ -1643,6 +1687,7 @@ export const semanticGlobalProcessOrigin = ( export const semanticGlobalProcessPathOrigin = ( node: t.Node, state: JavaScriptSemanticAnalysisState, + use: SemanticGlobalProcessUse = "read", ): JavaScriptModuleOrigin | undefined => { const chain = semanticMemberChain(node); if (chain === undefined) return undefined; @@ -1653,7 +1698,7 @@ export const semanticGlobalProcessPathOrigin = ( resolveSemanticBindingState(state, current, current.name) !== undefined || semanticResolutionBlocked(state, current, current.name) || members[0] !== "process" || - !semanticOriginalGlobalProcessAt(node, state) + !globalProcessAt(node, state, use) ) return undefined; return { @@ -1693,7 +1738,7 @@ const isConditionalRequireAt = ( ))) ) return false; - return !ambientBindingMayObserveWriteAt(state, node, "require", "top-level"); + return !ambientBindingMayObserveWriteAt(state, node, "require"); }; /** Recover an unshadowed literal require origin and its exact member path. */ @@ -1735,7 +1780,7 @@ export const semanticRequireOriginCandidate = ( (binding === undefined && (javaScriptRuntimeContext(state.program) === "module" || semanticResolutionBlocked(state, current.callee, "require"))) || - ambientBindingMayObserveWriteAt(state, current, "require", "top-level") + ambientBindingMayObserveWriteAt(state, current, "require") ) return undefined; const specifier = stringValue(current.arguments[0]); diff --git a/src/domain/javascript/javascriptSourceParserContext.test.ts b/src/domain/javascript/javascriptSourceParserContext.test.ts index bfc85c1e79..41760a169e 100644 --- a/src/domain/javascript/javascriptSourceParserContext.test.ts +++ b/src/domain/javascript/javascriptSourceParserContext.test.ts @@ -13,6 +13,7 @@ import { import { semanticRequireLoaderAt, semanticRequireOrigin, + semanticRequireOriginCandidate, } from "./javascriptSemanticRequireOrigin.js"; import { completeSemanticSteps, @@ -271,3 +272,130 @@ it("detects JavaScript module syntax within an observed unspecified package", () expect(file.errors).toEqual([]); expect(javaScriptRuntimeContext(file.program)).toBe("module"); }); + +// The loader and the origin of one unbound require call answer one question, +// with activation timing: may an assignment to `require` run before the call? +// Each "after" row states why the call is proven to run before every +// assignment; each "may precede" row states why that proof is missing. +const unboundRequireCases = [ + { + source: 'const fs = require("fs"); for (const x of [1]) require = custom;', + precedes: false, + why: "the loop is a later top-level statement, so it starts only after the call", + }, + { + source: 'const fs = require("fs"); for (const x of []) require = custom;', + precedes: false, + why: "an empty array literal with unchanged array iteration never runs the body", + }, + { + source: 'require = wrap(require("fs"));', + precedes: false, + why: "the right-hand side is evaluated before the assignment stores its value", + }, + { + source: 'const fs = require("fs"); require = custom;', + precedes: false, + why: "a later straight-line top-level statement runs after the call", + }, + { + source: + 'Array.prototype[Symbol.iterator] = gen; const fs = require("fs"); for (const x of []) require = custom;', + precedes: true, + why: "a replaced array iterator can yield from an empty literal", + }, + { + source: 'for (const x of [1]) require = custom; const fs = require("fs");', + precedes: true, + why: "the loop body runs before the call", + }, + { + source: 'const fs = require("fs"); while (ready) require = custom;', + precedes: true, + why: "a while body is not placed by top-level statement order", + }, + { + source: 'function load() { require = custom; } const fs = require("fs");', + precedes: true, + why: "a hoisted function may run before the call", + }, + { + source: + 'class C { fs = require("fs"); } for (const x of [1]) require = custom; const c = new C();', + precedes: true, + why: "an instance field runs on construction, after the loop", + }, + { + source: 'require = class { fs = require("fs"); }; new require();', + precedes: true, + why: "a field of the replacement class runs on construction, after the replacement", + }, +] as const; + +const unboundRequireCall = ( + source: string, + sourcePath: string, + packageType?: "commonjs", +) => { + const { file } = parseRecoveredJavaScript( + source, + sourcePath, + {}, + packageType, + ); + const index = completeSemanticSteps( + indexJavaScriptBindingsWithReceiversSteps(file.program), + ); + completeSemanticSteps(index.indexMemberMutationFactsSteps()); + let required: t.CallExpression | undefined; + traverseJavaScriptAst(file.program, { + enter: (node) => { + if ( + t.isCallExpression(node) && + t.isIdentifier(node.callee, { name: "require" }) && + t.isStringLiteral(node.arguments[0], { value: "fs" }) + ) + required = node; + }, + }); + if (required === undefined) throw new Error("Expected a require call"); + return { program: file.program, required, state: index.bindings }; +}; + +it.each(unboundRequireCases)( + "agrees on loader and origin for a CommonJS require in $source ($why)", + ({ source, precedes }) => { + const { program, required, state } = unboundRequireCall( + source, + "entry.cjs", + "commonjs", + ); + expect(javaScriptRuntimeContext(program)).toBe("commonjs"); + expect(semanticRequireLoaderAt(required, state, new Set())).toBe( + precedes ? "unknown" : "commonjs-loader", + ); + expect(semanticRequireOrigin(required, state)?.origin.specifier).toBe( + precedes ? undefined : "fs", + ); + }, +); + +it.each(unboundRequireCases)( + "keeps an unknown-runtime require a candidate in $source ($why)", + ({ source, precedes }) => { + const { program, required, state } = unboundRequireCall(source, "entry.js"); + expect(javaScriptRuntimeContext(program)).toBe("unknown"); + expect(semanticRequireLoaderAt(required, state, new Set())).toBe("unknown"); + expect(semanticRequireOriginCandidate(required, state)?.origin).toEqual( + precedes + ? undefined + : { + specifier: "fs", + importedPath: [], + resolutionMode: "require", + loaderAuthority: "candidate", + }, + ); + expect(semanticRequireOrigin(required, state)).toBeUndefined(); + }, +);