MCPcopy Create free account
hub / github.com/emscripten-core/emscripten / iteration

Function iteration

tools/acorn-optimizer.mjs:219–404  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

217
218function JSDCE(ast, aggressive) {
219 function iteration() {
220 let removed = 0;
221 const scopes = [{}]; // begin with empty toplevel scope
222 function ensureData(scope, name) {
223 if (Object.prototype.hasOwnProperty.call(scope, name)) return scope[name];
224 scope[name] = {
225 def: 0,
226 use: 0,
227 param: 0, // true for function params, which cannot be eliminated
228 };
229 return scope[name];
230 }
231 function cleanUp(ast, names) {
232 recursiveWalk(ast, {
233 ForStatement(node, c) {
234 visitChildren(node, c);
235 // If we had `for (var x = ...; ...)` and we removed `x`, we need to change to `for (; ...)`.
236 if (node.init?.type === 'EmptyStatement') {
237 node.init = null;
238 }
239 },
240 ForInStatement(node, c) {
241 // We can't remove the var in a for-in, as that would result in an invalid syntax. Skip the LHS.
242 c(node.right);
243 c(node.body);
244 },
245 ForOfStatement(node, c) {
246 // We can't remove the var in a for-of, as that would result in an invalid syntax. Skip the LHS.
247 c(node.right);
248 c(node.body);
249 },
250 VariableDeclaration(node, _c) {
251 let removedHere = 0;
252 node.declarations = node.declarations.filter((node) => {
253 assert(node.type === 'VariableDeclarator');
254 let keep = node.init && hasSideEffects(node.init);
255 walkPattern(
256 node.id,
257 (value) => {
258 keep ||= hasSideEffects(value);
259 },
260 (boundName) => {
261 keep ||= !names.has(boundName);
262 },
263 );
264 if (!keep) removedHere = 1;
265 return keep;
266 });
267 removed += removedHere;
268 if (node.declarations.length === 0) {
269 emptyOut(node);
270 }
271 },
272 ExpressionStatement(node, _c) {
273 if (aggressive && !hasSideEffects(node)) {
274 emptyOut(node);
275 removed++;
276 }

Callers 1

JSDCEFunction · 0.85

Calls 5

recursiveWalkFunction · 0.85
cleanUpFunction · 0.85
assertFunction · 0.50
popMethod · 0.45
addMethod · 0.45

Tested by

no test coverage detected