Skip to content

Commit 0dfb071

Browse files
authored
JS optimizer: Fix scoping issues with non-defined functions (#23159)
Other than simple defined functions (function foo() {}), JS also has arrow functions and object methods. We need to be aware of those in metadce. This fixes #22968 which was an issue with an object method that enclosed a function of the same name as another toplevel function. To fix this, this PR makes us aware of such scopes, and we do not optimize functions in them. That is, we only ever optimize defined functions at the toplevel scope, and anything enclosed is considered fixed.
1 parent ebd6f3c commit 0dfb071

File tree

4 files changed

+234
-147
lines changed

4 files changed

+234
-147
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
[
2+
{
3+
"name": "emcc$defun$arrowed",
4+
"reaches": [],
5+
"root": true
6+
},
7+
{
8+
"name": "emcc$defun$bar",
9+
"reaches": [],
10+
"root": true
11+
},
12+
{
13+
"name": "emcc$defun$caller",
14+
"reaches": [
15+
"emcc$defun$foo"
16+
],
17+
"root": true
18+
},
19+
{
20+
"name": "emcc$defun$foo",
21+
"reaches": []
22+
}
23+
]
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
function foo() {}
2+
3+
function bar() {}
4+
5+
// caller() calls foo(). There is also another function called "caller", down
6+
// below, which should not confuse us (if it does, nothing would refer to foo,
7+
// and instead we'd think the toplevel caller calls bar).
8+
function caller() {
9+
foo();
10+
}
11+
12+
caller();
13+
14+
var object = {
15+
method() {
16+
function caller(data) {
17+
bar();
18+
}
19+
}
20+
};
21+
22+
// Similar, with an arrow function. This should also not confuse us (it would
23+
// make "caller" refer to "arrowed".
24+
25+
function arrowed() {}
26+
27+
var arrow = () => {
28+
function caller(data) {
29+
arrowed();
30+
}
31+
}
32+
33+
wasmImports = {};

test/test_other.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2896,6 +2896,7 @@ def test_extern_prepost(self):
28962896
'emitDCEGraph3': (['emitDCEGraph', '--no-print'],),
28972897
'emitDCEGraph4': (['emitDCEGraph', '--no-print'],),
28982898
'emitDCEGraph5': (['emitDCEGraph', '--no-print'],),
2899+
'emitDCEGraph-scopes': (['emitDCEGraph', '--no-print'],),
28992900
'minimal-runtime-applyDCEGraphRemovals': (['applyDCEGraphRemovals'],),
29002901
'applyDCEGraphRemovals': (['applyDCEGraphRemovals'],),
29012902
'applyImportAndExportNameChanges': (['applyImportAndExportNameChanges'],),

0 commit comments

Comments
 (0)