Skip to content

Commit 2c778e5

Browse files
committed
fix
1 parent 57fa5e0 commit 2c778e5

File tree

1 file changed

+33
-8
lines changed

1 file changed

+33
-8
lines changed

tools/acorn-optimizer.mjs

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,13 @@ function simpleWalk(node, cs) {
6565
}
6666
}
6767

68-
// Full post-order walk, calling a single function for all types.
69-
function fullWalk(node, c) {
70-
visitChildren(node, (child) => fullWalk(child, c));
68+
// Full post-order walk, calling a single function for all types. If |pre| is
69+
// provided, it is called in pre-order (before children).
70+
function fullWalk(node, c, pre) {
71+
if (pre) {
72+
pre(node);
73+
}
74+
visitChildren(node, (child) => fullWalk(child, c, pre));
7175
c(node);
7276
}
7377

@@ -699,6 +703,13 @@ function emitDCEGraph(ast) {
699703
}
700704
}
701705

706+
// We track defined functions very carefully, so that we can remove them and
707+
// the things they call, but other function scopes (like arrow functions and
708+
// object methods) are trickier to track (object methods require knowing what
709+
// object a function name is called on), so we do not track those. We consider
710+
// all content inside them as top-level, which means it is used.
711+
var specialScopes = 0;
712+
702713
fullWalk(ast, (node) => {
703714
if (isWasmImportsAssign(node)) {
704715
const assignedObject = getWasmImportsValue(node);
@@ -788,11 +799,14 @@ function emitDCEGraph(ast) {
788799
emptyOut(node);
789800
}
790801
} else if (node.type === 'FunctionDeclaration') {
791-
defuns.push(node);
792-
const name = node.id.name;
793-
nameToGraphName[name] = getGraphName(name, 'defun');
794-
emptyOut(node); // ignore this in the second pass; we scan defuns separately
802+
if (!specialScopes) {
803+
defuns.push(node);
804+
const name = node.id.name;
805+
nameToGraphName[name] = getGraphName(name, 'defun');
806+
emptyOut(node); // ignore this in the second pass; we scan defuns separately
807+
}
795808
} else if (node.type === 'ArrowFunctionExpression') {
809+
specialScopes--;
796810
// Check if this is the minimal runtime exports function, which looks like
797811
// (output) => { var wasmExports = output.instance.exports;
798812
if (
@@ -857,9 +871,19 @@ function emitDCEGraph(ast) {
857871
}
858872
}
859873
}
874+
} else if (node.type === 'Property' && node.method) {
875+
specialScopes--;
876+
}
877+
}, (node) => {
878+
// Pre-walking logic. We note special scopes (see above).
879+
if (node.type === 'ArrowFunctionExpression' ||
880+
(node.type === 'Property' && node.method)) {
881+
specialScopes++;
860882
}
861883
});
862-
// must find the info we need
884+
// Scoping must balance out.
885+
assert(specialScopes === 0);
886+
// We must have found the info we need.
863887
assert(
864888
foundWasmImportsAssign,
865889
'could not find the assignment to "wasmImports". perhaps --pre-js or --post-js code moved it out of the global scope? (things like that should be done after emcc runs, as they do not need to be run through the optimizer which is the special thing about --pre-js/--post-js code)',
@@ -870,6 +894,7 @@ function emitDCEGraph(ast) {
870894
saveAsmExport(exp[0], exp[1]);
871895
}
872896
}
897+
873898
// Second pass: everything used in the toplevel scope is rooted;
874899
// things used in defun scopes create links
875900
function getGraphName(name, what) {

0 commit comments

Comments
 (0)