Fix shadowing of names declared in %%raw
Why
A binding named like a function or variable that %%raw declares made the compiler read that name as globalThis.<name>. A declaration in a module is no property of globalThis, so the read threw. Fixes #9456.
The names a %%raw program declares at its toplevel (functions, classes, variables, imports, also when exported) are now recorded when it is classified. No binding takes such a name; it gets a $N suffix, and the name is read as it is. A real global shadowed by a binding still goes through globalThis.
modifiedJS shadowed %%raw declaration
%%raw(`function helper() { return 1 }`)@val external helper: unit => int = "helper" let f = () => { let helper = helper() + 1 helper * helper}
js
Before
After
function helper() { return 1 }function helper() { return 1 };; function f() {function f() { let helper = globalThis.helper() + 1 | 0; return helper * helper | 0; let helper$1 = helper() + 1 | 0; return helper$1 * helper$1 | 0;}}
modifiedJS toplevel binding named like a %%raw declaration
%%raw(`function helper() { return 1 }`)module G = { @val external helper: unit => int = "helper" }let helper = () => G.helper() + 1
js
Before a second
helper declaration, a SyntaxError in an ES module; in CommonJS it replaces the raw one and the read throwsAfter
function helper() { return 1 }function helper() { return 1 };; function helper() { return globalThis.helper() + 1 | 0;function helper$1() { return helper() + 1 | 0;}} exports.helper = helper;exports.helper = helper$1;
Review
- Why are the names collected for the whole program, not per scope? A
%%rawin a local module declares its names only in that block. Keeping every binding off those names everywhere is simpler and still right: a plain read then never meets a binding of that name. - Why does
Ext_pp_scope.mergeneed~js?mergeregisters a closure's free JS globals like bindings. Without the flag, the globalhelperitself avoided its own name and took a phantomhelper$1, so the renamed binding becamehelper$2.