Skip to content

Commit e1c10dc

Browse files
fix(library): keep exports live across writes made inside eval() (#22755)
A module calling eval() now syncs each exported binding once an eval() call returns, so a write the evaluated code makes reaches the library's export. Its bindings count as written, and the sync tolerates a call made before a binding is initialized. Only a function eval() creates and calls later still writes unseen, until the module's next write or eval() call. Claude-Session: https://claude.ai/code/session_01LuWrtfTSHhfowLVuYht8Zc Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 9f25e90 commit e1c10dc

7 files changed

Lines changed: 70 additions & 16 deletions

File tree

‎lib/dependencies/esm/ESMLiveExportDependency.js‎

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -174,18 +174,20 @@ ESMLiveExportDependency.Template = class ESMLiveExportDependencyTemplate extends
174174
helpers.push(helper);
175175
// Hoisted, so any write may call it; a value passed with `v` set is what
176176
// the binding is about to hold, read before the binding is initialized.
177+
const sync = hoisted.registered
178+
? `\tvar s = ${RuntimeGlobals.liveExportSetters};\n` +
179+
`\ts = s && s[${id}];\n` +
180+
`\ts = s && s${propertyAccess([name])};\n` +
181+
`\tif (s) for (var i = 0; i < s.length; i++) s[i](v ? r : ${name});\n`
182+
: `\t${hoisted.names[index]} = v ? r : ${name};\n`;
177183
source.insert(
178184
0,
179-
hoisted.registered
180-
? `function ${helper}(r, v) {\n` +
181-
`\tvar s = ${RuntimeGlobals.liveExportSetters};\n` +
182-
`\ts = s && s[${id}];\n` +
183-
`\ts = s && s${propertyAccess([name])};\n` +
184-
`\tif (s) for (var i = 0; i < s.length; i++) s[i](v ? r : ${name});\n` +
185-
"\treturn r;\n}\n"
186-
: `function ${helper}(r, v) {\n` +
187-
`\t${hoisted.names[index]} = v ? r : ${name};\n` +
188-
"\treturn r;\n}\n"
185+
`function ${helper}(r, v) {\n${
186+
// an eval() call may come before the binding is initialized
187+
dep.readByEval
188+
? `\ttry {\n\t${sync.replace(/\n\t/g, "\n\t\t")}\t} catch (e) {}\n`
189+
: sync
190+
}\treturn r;\n}\n`
189191
);
190192
}
191193
for (let i = 0; i < mirrors.length; i += 3) {

‎lib/dependencies/esm/ESMLiveExportParserPlugin.js‎

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,9 @@ class ESMLiveExportParserPlugin {
216216
let untagged = false;
217217
// a direct `eval` may read a binding by the name renaming takes away
218218
let usesEval = false;
219+
// flat pairs of where each direct `eval` call starts and ends, as it may write
220+
/** @type {number[]} */
221+
let evalCalls = [];
219222
// A hot update replaces the module where the chunk's scope is not, so the
220223
// module keeps its declarations and mirrors them, `const` included.
221224
let hot = false;
@@ -533,14 +536,17 @@ class ESMLiveExportParserPlugin {
533536
pendingPattern = undefined;
534537
untagged = false;
535538
usesEval = false;
539+
evalCalls = [];
536540
hot = Boolean(
537541
parser.state.module &&
538542
/** @type {NormalModule} */ (parser.state.module).hot
539543
);
540544
});
541545

542-
parser.hooks.call.for("eval").tap(PLUGIN_NAME, () => {
546+
parser.hooks.call.for("eval").tap(PLUGIN_NAME, (expression) => {
543547
usesEval = true;
548+
const range = /** @type {Range} */ (expression.range);
549+
evalCalls.push(range[0], range[1]);
544550
});
545551

546552
// Counted before anything is claimed: a `var` the walk does not report as a
@@ -804,8 +810,8 @@ class ESMLiveExportParserPlugin {
804810
pendingPattern = undefined;
805811
if (claimed === undefined) return;
806812
for (const group of claimed) {
807-
// a hot update may write a binding the module now leaves alone
808-
if (!group.written && !hot) continue;
813+
// a hot update or an eval() call may write a binding the module leaves alone
814+
if (!group.written && !hot && !usesEval) continue;
809815
if (
810816
group.declaration.type === "VariableDeclaration" &&
811817
group.declaration.kind !== "var" &&
@@ -832,6 +838,16 @@ class ESMLiveExportParserPlugin {
832838
) {
833839
continue;
834840
}
841+
for (const references of group.names) {
842+
for (let i = 0; i < evalCalls.length; i += 2) {
843+
recordOccurrence(
844+
references,
845+
evalCalls[i],
846+
evalCalls[i + 1],
847+
ESMLiveExportDependency.SYNC
848+
);
849+
}
850+
}
835851
parser.state.current.addDependency(
836852
new ESMLiveExportDependency(
837853
group.names.map((references) => references.name),

‎lib/library/ModuleLibraryPlugin.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ const getHoistRefusal = (module, entry, declaration) => {
167167
return "";
168168
}
169169
if (declaration.readByEval) {
170-
return "the module calls eval(), and a write made inside it is not seen";
170+
return "the module calls eval(), and a function it creates may write a binding later";
171171
}
172172
return null;
173173
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,21 @@
1+
// An eval() call before the declarations finds them uninitialized
2+
// eslint-disable-next-line no-eval
3+
eval("0");
14
export let evaluated = 1;
5+
// Written through eval() alone
6+
export let onlyEvaluated = 1;
27
export function readEvaluated() {
38
// eslint-disable-next-line no-eval
49
return eval("evaluated");
510
}
611
export function setEvaluated(value) {
712
evaluated = value;
813
}
14+
export function setByEval(value) {
15+
// eslint-disable-next-line no-eval
16+
eval("evaluated = value; onlyEvaluated = value");
17+
}
18+
export function makeLateWriter() {
19+
// eslint-disable-next-line no-eval
20+
return eval("(value) => { evaluated = value; }");
21+
}

‎test/configCases/library/module-live-bindings-bailouts/index.js‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,19 @@ it("should keep a binding live that a module reading it through eval writes", ()
1313
expect(library.readEvaluated()).toBe(2);
1414
expect(library.evaluated).toBe(2);
1515
});
16+
17+
it("should keep a binding live that eval() writes", () => {
18+
library.setByEval(3);
19+
expect(library.evaluated).toBe(3);
20+
expect(library.onlyEvaluated).toBe(3);
21+
});
22+
23+
it("should miss a write a function created by eval() makes later", () => {
24+
const write = library.makeLateWriter();
25+
write(4);
26+
// the known gap the warning names
27+
expect(library.evaluated).toBe(3);
28+
// until the module's next eval() call or write catches up
29+
expect(library.readEvaluated()).toBe(4);
30+
expect(library.evaluated).toBe(4);
31+
});
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,9 @@
11
export { shared, bumpShared } from "./shared.js";
2-
export { evaluated, readEvaluated, setEvaluated } from "./evaluated.js";
2+
export {
3+
evaluated,
4+
onlyEvaluated,
5+
readEvaluated,
6+
setEvaluated,
7+
setByEval,
8+
makeLateWriter
9+
} from "./evaluated.js";

‎test/configCases/library/module-live-bindings-bailouts/warnings.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
module.exports = [
44
[
5-
/a module library may export a stale binding, since the module calls eval\(\), and a write made inside it is not seen\n {4}\.\/evaluated\.js/
5+
/a module library may export a stale binding, since the module calls eval\(\), and a function it creates may write a binding later\n {4}\.\/evaluated\.js/
66
]
77
];

0 commit comments

Comments
 (0)