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

Function minifyLocals

tools/acorn-optimizer.mjs:1370–1498  ·  view source on GitHub ↗
(ast)

Source from the content-addressed store, hash-verified

1368}
1369
1370function minifyLocals(ast) {
1371 // We are given a mapping of global names to their minified forms.
1372 assert(extraInfo?.globals);
1373
1374 for (const fun of ast.body) {
1375 if (fun.type !== 'FunctionDeclaration') {
1376 continue;
1377 }
1378 // Find the list of local names, including params.
1379 const localNames = new Set();
1380 for (const param of fun.params) {
1381 localNames.add(param.name);
1382 }
1383 simpleWalk(fun, {
1384 VariableDeclaration(node, _c) {
1385 for (const dec of node.declarations) {
1386 localNames.add(dec.id.name);
1387 }
1388 },
1389 });
1390
1391 function isLocalName(name) {
1392 return localNames.has(name);
1393 }
1394
1395 // Names old to new names.
1396 const newNames = new Map();
1397
1398 // The names in use, that must not be collided with.
1399 const usedNames = new Set();
1400
1401 // Put the function name aside. We don't want to traverse it as it is not
1402 // in the scope of itself.
1403 const funId = fun.id;
1404 fun.id = null;
1405
1406 // Find all the globals that we need to minify using pre-assigned names.
1407 // Don't actually minify them yet as that might interfere with local
1408 // variable names; just mark them as used, and what their new name will be.
1409 simpleWalk(fun, {
1410 Identifier(node, _c) {
1411 const name = node.name;
1412 if (!isLocalName(name)) {
1413 const minified = extraInfo.globals[name];
1414 if (minified) {
1415 newNames.set(name, minified);
1416 usedNames.add(minified);
1417 }
1418 }
1419 },
1420 CallExpression(node, _c) {
1421 // We should never call a local name, as in asm.js-style code our
1422 // locals are just numbers, not functions; functions are all declared
1423 // in the outer scope. If a local is called, that is a bug.
1424 if (node.callee.type === 'Identifier') {
1425 assertAt(!isLocalName(node.callee.name), node.callee, 'cannot call a local');
1426 }
1427 },

Callers

nothing calls this directly

Calls 6

simpleWalkFunction · 0.85
getNextMinifiedNameFunction · 0.85
recursiveWalkFunction · 0.85
setMethod · 0.65
assertFunction · 0.50
addMethod · 0.45

Tested by

no test coverage detected