fix(javascript): count escapes of Function.prototype before trusting call and apply (#11501)

* fix(javascript): count escapes of Function.prototype before trusting call and apply

* fix(javascript): key cached returned references on ambient escapes too

* fix(javascript): track arguments of directly invoked functions through their parameters (#11538)

* fix(javascript): track arguments of directly invoked functions through their parameters

Passing a value to a function expression invoked where it is defined, such
as a UMD header (function(root, factory){...})(this, ...), recorded an
escape of the argument to unevaluated code although the callee's body is
source code whose effects are indexed. javascriptSemanticDirectInvocations
owns which arguments reach such a callee only through a plain parameter;
the ambient path resolver and member mutation indexing follow that
parameter back to its argument, and neither records an escape for it.
Spread and rest lists, pattern or default parameters, arguments and direct
eval keep the escape, as do unknown callees.

Refs #11499

* fix(javascript): escape values thrown out of a function and scan arguments once

A value thrown out of a function or class body reaches handlers the
throw-to-catch index does not link to it. Arguments of a directly invoked
function used to hide this behind the call escape; they no longer do, and
captured values had the same gap. The binding pass records such throws,
and ambient and member effect indexing escape their values at the throw.

Which functions name 'arguments' is now found in one program walk instead
of one subtree scan per directly invoked function.

* perf(javascript): find shadowed direct-invocation parameters in one pass

* fix(javascript): settle Object and Reflect static call receivers in one owner (#11543)

* fix(javascript): settle Object and Reflect static call receivers in one owner

Ambient write indexing skipped the receiver escape of any resolved Object
or Reflect static call, even when source code had replaced the method,
while member mutation indexing escaped the constructor for every such
call. The second escape (global.Object contents) made each later
Function.prototype.call/apply check uncertain in bundles that call
Object.defineProperty or Object.freeze. Ambient indexing now settles these
receivers once all ambient writes are known: the constructor escapes only
when a write or escape may replace the method before the call, and member
mutation reuses that decision through retainedGlobalCallReceivers.

Refs #11535

* fix(javascript): retain the receiver only for native Object and Reflect statics

A resolved Object member that is not one of the language's own static
methods (for example one Object inherits from Function.prototype, or an
unknown name) may be user code that receives the constructor, so its call
keeps the receiver escape.

* fix(javascript): look up intrinsic and builtin method tables by own key only

NATIVE_STATIC_METHODS and BUILTIN_RECEIVER_METHODS were plain objects, so
an owner such as globalThis.constructor or a module named 'constructor' or
'toString' found an inherited Object.prototype function and analysis threw
a TypeError on '.has'. Both are Maps now.

* fix(javascript): escape values thrown uncaught at the top level (#11556)

A value thrown uncaught at the top level reaches the host's uncaught-exception handlers (process.on('uncaughtException'), window.onerror), which may change it before a timer or callback reads it. The binding pass recorded escaping throws only when the walk reached a function or class body; a throw that reaches the program without a handler now escapes the same way.

* fix(javascript): follow direct-invocation arguments through returned references and function arguments properties

A callable argument of a directly invoked function no longer escapes at the call, but the shared returned-reference owner could not follow the parameter to it, so patch(get()) inside the callee lost the escape of the value get returns. The returned-reference walk now follows the parameter to its argument like the write and escape walkers.

A sloppy function's arguments property (f["arguments"], g.caller.arguments from a function it calls) exposes its activation's arguments. A non-strict callee is no longer tracked through its parameters when the program reads an arguments or caller property by a static key. The strictness reader moves beside javaScriptProgramStrictness so both modules share it.
This commit is contained in:
morluto authored and GitHub committed 2026-11-16 07:37:59 +08:00
1 parent ff6467ca97
commit a86a668718
14 files changed
+826 -68

No files matched your search

@@ -385,3 +385,136 @@ it.each([
expect(statuses).toEqual(expected);
},
);
// An argument of a directly invoked source function expression reaches only
// that function's parameter, whose effects are indexed like any other source
// effect. Only arguments the body may reach another way escape.
function expectAmbientEffectPaths({
source,
writes,
escapes,
}: {
readonly source: string;
readonly writes: readonly (readonly unknown[])[];
readonly escapes: readonly (readonly unknown[])[];
}): void {
const parsed = parseJavaScriptSource(source, "app.js");
if (parsed === null) throw new Error("Expected ambient effect fixture");
const index = completeSemanticSteps(
indexJavaScriptBindingsWithReceiversSteps(parsed.program),
);
completeSemanticSteps(index.indexMemberMutationFactsSteps());
const paths = (effects: readonly { readonly path: unknown }[]) => [
...new Set(effects.map(({ path }) => JSON.stringify(path))),
];
expect({
writes: paths(index.bindings.ambientWriteEffects),
escapes: paths(index.bindings.ambientEscapeEffects),
}).toEqual({
writes: writes.map((path) => JSON.stringify(path)),
escapes: escapes.map((path) => JSON.stringify(path)),
});
}
it.each([
{ source: "(function(g){})(this);", writes: [], escapes: [] },
{ source: "(function(){})(this);", writes: [], escapes: [] },
{
source: "(function(g){g.x=1})(this);",
writes: [["global", "x"]],
escapes: [],
},
{
source: "(function(g){var h=g;h.y=2})(this);",
writes: [["global", "y"]],
escapes: [],
},
{
source: "(function(g){later(function(){g.w=1})})(this);",
writes: [["global", "w"]],
escapes: [],
},
{
source: "(function(g){return g})(this).z=1;",
writes: [["global", "z"]],
escapes: [],
},
{
source: "(function(g,y){y(1)})(this,function(g){});",
writes: [],
escapes: [],
},
{
source: "(function(g){patch(g)})(this);",
writes: [],
escapes: [["global", null]],
},
{
source: "(function(g){arguments[0].x=1})(this);",
writes: [],
escapes: [["global", null]],
},
{
source: "(function(g){})(...[this]);",
writes: [],
escapes: [["global", null]],
},
{
source: "(function({x}){})(this);",
writes: [],
escapes: [["global", null]],
},
{
source: "(function(g=d){})(this);",
writes: [],
escapes: [["global", null]],
},
{
source: "(function(...r){})(this);",
writes: [],
escapes: [["global", null]],
},
{ source: "unknown(this);", writes: [], escapes: [["global", null]] },
{
source: "(function(g){g.x=1})(globalThis);",
writes: [["global", "x"]],
escapes: [],
},
{
source: "(function(m){m.exports=1})(module);",
writes: [["commonjs-module", "exports"]],
escapes: [],
},
{
source: "(function(g){patch(g)})(window);",
writes: [],
escapes: [["global", null]],
},
{
source: "(function(g){throw g})(this);",
writes: [],
escapes: [["global", null]],
},
])(
"tracks arguments of directly invoked functions through parameters: $source",
expectAmbientEffectPaths,
);
// A value thrown uncaught at the top level reaches the host's
// uncaught-exception handlers, which may change it.
it.each([
...[
"throw this;",
"{ throw this; }",
"try { throw this; } finally {}",
"try {} catch { throw this; }",
].map((source) => ({
source,
writes: [],
escapes: [["global", null]],
})),
{ source: "try { throw this; } catch {}", writes: [], escapes: [] },
])(
"escapes values thrown uncaught at the top level: $source",
expectAmbientEffectPaths,
);