scope.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  1. /***********************************************************************
  2. A JavaScript tokenizer / parser / beautifier / compressor.
  3. https://github.com/mishoo/UglifyJS
  4. -------------------------------- (C) ---------------------------------
  5. Author: Mihai Bazon
  6. <mihai.bazon@gmail.com>
  7. http://mihai.bazon.net/blog
  8. Distributed under the BSD license:
  9. Copyright 2012 (c) Mihai Bazon <mihai.bazon@gmail.com>
  10. Redistribution and use in source and binary forms, with or without
  11. modification, are permitted provided that the following conditions
  12. are met:
  13. * Redistributions of source code must retain the above
  14. copyright notice, this list of conditions and the following
  15. disclaimer.
  16. * Redistributions in binary form must reproduce the above
  17. copyright notice, this list of conditions and the following
  18. disclaimer in the documentation and/or other materials
  19. provided with the distribution.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER “AS IS” AND ANY
  21. EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23. PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE
  24. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
  25. OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  29. TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  30. THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. SUCH DAMAGE.
  32. ***********************************************************************/
  33. "use strict";
  34. function SymbolDef(id, scope, orig, init) {
  35. this.eliminated = 0;
  36. this.exported = false;
  37. this.global = false;
  38. this.id = id;
  39. this.init = init;
  40. this.mangled_name = null;
  41. this.name = orig.name;
  42. this.orig = [ orig ];
  43. this.references = [];
  44. this.replaced = 0;
  45. this.scope = scope;
  46. this.undeclared = false;
  47. }
  48. SymbolDef.prototype = {
  49. forEach: function(fn) {
  50. this.orig.forEach(fn);
  51. this.references.forEach(fn);
  52. },
  53. mangle: function(options) {
  54. var cache = options.cache && options.cache.props;
  55. if (this.global && cache && cache.has(this.name)) {
  56. this.mangled_name = cache.get(this.name);
  57. } else if (!this.mangled_name && !this.unmangleable(options)) {
  58. var def = this.redefined();
  59. if (def) {
  60. this.mangled_name = def.mangled_name || def.name;
  61. } else {
  62. this.mangled_name = next_mangled_name(this, options);
  63. }
  64. if (this.global && cache) {
  65. cache.set(this.name, this.mangled_name);
  66. }
  67. }
  68. },
  69. redefined: function() {
  70. var scope = this.defun;
  71. if (!scope) return;
  72. var name = this.name;
  73. var def = scope.variables.get(name)
  74. || scope instanceof AST_Toplevel && scope.globals.get(name)
  75. || this.orig[0] instanceof AST_SymbolConst && find_if(function(def) {
  76. return def.name == name;
  77. }, scope.enclosed);
  78. if (def && def !== this) return def.redefined() || def;
  79. },
  80. unmangleable: function(options) {
  81. return this.global && !options.toplevel
  82. || this.exported
  83. || this.undeclared
  84. || !options.eval && this.scope.pinned()
  85. || options.keep_fnames
  86. && (this.orig[0] instanceof AST_SymbolClass
  87. || this.orig[0] instanceof AST_SymbolDefClass
  88. || this.orig[0] instanceof AST_SymbolDefun
  89. || this.orig[0] instanceof AST_SymbolLambda);
  90. },
  91. };
  92. var unary_side_effects = makePredicate("delete ++ --");
  93. function is_lhs(node, parent) {
  94. if (parent instanceof AST_Assign) return parent.left === node && node;
  95. if (parent instanceof AST_DefaultValue) return parent.name === node && node;
  96. if (parent instanceof AST_Destructured) return node;
  97. if (parent instanceof AST_DestructuredKeyVal) return node;
  98. if (parent instanceof AST_ForEnumeration) return parent.init === node && node;
  99. if (parent instanceof AST_Unary) return unary_side_effects[parent.operator] && parent.expression;
  100. }
  101. AST_Toplevel.DEFMETHOD("figure_out_scope", function(options) {
  102. options = defaults(options, {
  103. cache: null,
  104. ie8: false,
  105. });
  106. // pass 1: setup scope chaining and handle definitions
  107. var self = this;
  108. var defun = null;
  109. var exported = false;
  110. var next_def_id = 0;
  111. var scope = self.parent_scope = null;
  112. var tw = new TreeWalker(function(node, descend) {
  113. if (node instanceof AST_DefClass) {
  114. var save_exported = exported;
  115. exported = tw.parent() instanceof AST_ExportDeclaration;
  116. node.name.walk(tw);
  117. exported = save_exported;
  118. walk_scope(function() {
  119. if (node.extends) node.extends.walk(tw);
  120. node.properties.forEach(function(prop) {
  121. prop.walk(tw);
  122. });
  123. });
  124. return true;
  125. }
  126. if (node instanceof AST_Definitions) {
  127. var save_exported = exported;
  128. exported = tw.parent() instanceof AST_ExportDeclaration;
  129. descend();
  130. exported = save_exported;
  131. return true;
  132. }
  133. if (node instanceof AST_LambdaDefinition) {
  134. var save_exported = exported;
  135. exported = tw.parent() instanceof AST_ExportDeclaration;
  136. node.name.walk(tw);
  137. exported = save_exported;
  138. walk_scope(function() {
  139. node.argnames.forEach(function(argname) {
  140. argname.walk(tw);
  141. });
  142. if (node.rest) node.rest.walk(tw);
  143. walk_body(node, tw);
  144. });
  145. return true;
  146. }
  147. if (node instanceof AST_SwitchBranch) {
  148. node.init_vars(scope);
  149. descend();
  150. return true;
  151. }
  152. if (node instanceof AST_Try) {
  153. walk_scope(function() {
  154. walk_body(node, tw);
  155. });
  156. if (node.bcatch) node.bcatch.walk(tw);
  157. if (node.bfinally) node.bfinally.walk(tw);
  158. return true;
  159. }
  160. if (node instanceof AST_With) {
  161. var s = scope;
  162. do {
  163. s = s.resolve();
  164. if (s.uses_with) break;
  165. s.uses_with = true;
  166. } while (s = s.parent_scope);
  167. walk_scope(descend);
  168. return true;
  169. }
  170. if (node instanceof AST_BlockScope) {
  171. walk_scope(descend);
  172. return true;
  173. }
  174. if (node instanceof AST_Symbol) {
  175. node.scope = scope;
  176. }
  177. if (node instanceof AST_Label) {
  178. node.thedef = node;
  179. node.references = [];
  180. }
  181. if (node instanceof AST_SymbolCatch) {
  182. scope.def_variable(node).defun = defun;
  183. } else if (node instanceof AST_SymbolConst) {
  184. var def = scope.def_variable(node);
  185. def.defun = defun;
  186. if (exported) def.exported = true;
  187. } else if (node instanceof AST_SymbolDefun) {
  188. var def = defun.def_function(node, tw.parent());
  189. if (exported) def.exported = true;
  190. entangle(defun, scope);
  191. } else if (node instanceof AST_SymbolFunarg) {
  192. defun.def_variable(node);
  193. entangle(defun, scope);
  194. } else if (node instanceof AST_SymbolLambda) {
  195. var def = defun.def_function(node, node.name == "arguments" ? undefined : defun);
  196. if (options.ie8) def.defun = defun.parent_scope.resolve();
  197. } else if (node instanceof AST_SymbolLet) {
  198. var def = scope.def_variable(node);
  199. if (exported) def.exported = true;
  200. } else if (node instanceof AST_SymbolVar) {
  201. var def = defun.def_variable(node, node instanceof AST_SymbolImport ? undefined : null);
  202. if (exported) def.exported = true;
  203. entangle(defun, scope);
  204. }
  205. function walk_scope(descend) {
  206. node.init_vars(scope);
  207. var save_defun = defun;
  208. var save_scope = scope;
  209. if (node instanceof AST_Scope) defun = node;
  210. scope = node;
  211. descend();
  212. scope = save_scope;
  213. defun = save_defun;
  214. }
  215. function entangle(defun, scope) {
  216. if (defun === scope) return;
  217. node.mark_enclosed(options);
  218. var def = scope.find_variable(node.name);
  219. if (node.thedef === def) return;
  220. node.thedef = def;
  221. def.orig.push(node);
  222. node.mark_enclosed(options);
  223. }
  224. });
  225. self.make_def = function(orig, init) {
  226. return new SymbolDef(++next_def_id, this, orig, init);
  227. };
  228. self.walk(tw);
  229. // pass 2: find back references and eval
  230. self.globals = new Dictionary();
  231. var in_arg = [];
  232. var tw = new TreeWalker(function(node) {
  233. if (node instanceof AST_Catch) {
  234. if (!(node.argname instanceof AST_Destructured)) return;
  235. in_arg.push(node);
  236. node.argname.walk(tw);
  237. in_arg.pop();
  238. walk_body(node, tw);
  239. return true;
  240. }
  241. if (node instanceof AST_Lambda) {
  242. in_arg.push(node);
  243. node.argnames.forEach(function(argname) {
  244. argname.walk(tw);
  245. });
  246. if (node.rest) node.rest.walk(tw);
  247. in_arg.pop();
  248. walk_lambda(node, tw);
  249. return true;
  250. }
  251. if (node instanceof AST_LoopControl) {
  252. if (node.label) node.label.thedef.references.push(node);
  253. return true;
  254. }
  255. if (node instanceof AST_SymbolDeclaration) {
  256. var def = node.definition();
  257. def.preinit = def.references.length;
  258. if (node instanceof AST_SymbolCatch) {
  259. // ensure mangling works if `catch` reuses a scope variable
  260. var redef = def.redefined();
  261. if (redef) for (var s = node.scope; s; s = s.parent_scope) {
  262. push_uniq(s.enclosed, redef);
  263. if (s === redef.scope) break;
  264. }
  265. } else if (node instanceof AST_SymbolConst) {
  266. // ensure compression works if `const` reuses a scope variable
  267. var redef = def.redefined();
  268. if (redef) redef.const_redefs = true;
  269. }
  270. if (node.name != "arguments") return true;
  271. var parent = node instanceof AST_SymbolVar && tw.parent();
  272. if (parent instanceof AST_VarDef && !parent.value) return true;
  273. var sym = node.scope.resolve().find_variable("arguments");
  274. if (sym && is_arguments(sym)) sym.scope.uses_arguments = 3;
  275. return true;
  276. }
  277. if (node instanceof AST_SymbolRef) {
  278. var name = node.name;
  279. var sym = node.scope.find_variable(name);
  280. for (var i = in_arg.length; i > 0 && sym;) {
  281. i = in_arg.lastIndexOf(sym.scope, i - 1);
  282. if (i < 0) break;
  283. var decl = sym.orig[0];
  284. if (decl instanceof AST_SymbolCatch
  285. || decl instanceof AST_SymbolFunarg
  286. || decl instanceof AST_SymbolLambda) {
  287. node.in_arg = true;
  288. break;
  289. }
  290. sym = sym.scope.parent_scope.find_variable(name);
  291. }
  292. if (!sym) {
  293. sym = self.def_global(node);
  294. } else if (name == "arguments" && is_arguments(sym)) {
  295. var parent = tw.parent();
  296. if (is_lhs(node, parent)) {
  297. sym.scope.uses_arguments = 3;
  298. } else if (sym.scope.uses_arguments < 2
  299. && !(parent instanceof AST_PropAccess && parent.expression === node)) {
  300. sym.scope.uses_arguments = 2;
  301. } else if (!sym.scope.uses_arguments) {
  302. sym.scope.uses_arguments = true;
  303. }
  304. }
  305. if (name == "eval") {
  306. var parent = tw.parent();
  307. if (parent.TYPE == "Call" && parent.expression === node) {
  308. var s = node.scope;
  309. do {
  310. s = s.resolve();
  311. if (s.uses_eval) break;
  312. s.uses_eval = true;
  313. } while (s = s.parent_scope);
  314. } else if (sym.undeclared) {
  315. self.uses_eval = true;
  316. }
  317. }
  318. if (sym.init instanceof AST_LambdaDefinition && sym.scope !== sym.init.name.scope) {
  319. var scope = node.scope;
  320. do {
  321. if (scope === sym.init.name.scope) break;
  322. } while (scope = scope.parent_scope);
  323. if (!scope) sym.init = undefined;
  324. }
  325. node.thedef = sym;
  326. node.reference(options);
  327. return true;
  328. }
  329. });
  330. self.walk(tw);
  331. // pass 3: fix up any scoping issue with IE8
  332. if (options.ie8) self.walk(new TreeWalker(function(node) {
  333. if (node instanceof AST_SymbolCatch) {
  334. var scope = node.thedef.defun;
  335. if (scope.name instanceof AST_SymbolLambda && scope.name.name == node.name) {
  336. scope = scope.parent_scope.resolve();
  337. }
  338. redefine(node, scope);
  339. return true;
  340. }
  341. if (node instanceof AST_SymbolLambda) {
  342. var def = node.thedef;
  343. if (!redefine(node, node.scope.parent_scope.resolve())) {
  344. delete def.defun;
  345. } else if (typeof node.thedef.init !== "undefined") {
  346. node.thedef.init = false;
  347. } else if (def.init) {
  348. node.thedef.init = def.init;
  349. }
  350. return true;
  351. }
  352. }));
  353. function is_arguments(sym) {
  354. return sym.orig[0] instanceof AST_SymbolFunarg
  355. && !(sym.orig[1] instanceof AST_SymbolFunarg || sym.orig[2] instanceof AST_SymbolFunarg)
  356. && !is_arrow(sym.scope);
  357. }
  358. function redefine(node, scope) {
  359. var name = node.name;
  360. var old_def = node.thedef;
  361. if (!all(old_def.orig, function(sym) {
  362. return !(sym instanceof AST_SymbolConst || sym instanceof AST_SymbolLet);
  363. })) return false;
  364. var new_def = scope.find_variable(name);
  365. if (new_def) {
  366. var redef = new_def.redefined();
  367. if (redef) new_def = redef;
  368. } else {
  369. new_def = self.globals.get(name);
  370. }
  371. if (new_def) {
  372. new_def.orig.push(node);
  373. } else {
  374. new_def = scope.def_variable(node);
  375. }
  376. old_def.defun = new_def.scope;
  377. old_def.forEach(function(node) {
  378. node.redef = old_def;
  379. node.thedef = new_def;
  380. node.reference(options);
  381. });
  382. if (new_def.undeclared) self.variables.set(name, new_def);
  383. return true;
  384. }
  385. });
  386. AST_Toplevel.DEFMETHOD("def_global", function(node) {
  387. var globals = this.globals, name = node.name;
  388. if (globals.has(name)) {
  389. return globals.get(name);
  390. } else {
  391. var g = this.make_def(node);
  392. g.undeclared = true;
  393. g.global = true;
  394. globals.set(name, g);
  395. return g;
  396. }
  397. });
  398. function init_block_vars(scope, parent) {
  399. scope.enclosed = []; // variables from this or outer scope(s) that are referenced from this or inner scopes
  400. scope.parent_scope = parent; // the parent scope (null if this is the top level)
  401. scope.functions = new Dictionary(); // map name to AST_SymbolDefun (functions defined in this scope)
  402. scope.variables = new Dictionary(); // map name to AST_SymbolVar (variables defined in this scope; includes functions)
  403. if (parent) scope.make_def = parent.make_def; // top-level tracking of SymbolDef instances
  404. }
  405. function init_scope_vars(scope, parent) {
  406. init_block_vars(scope, parent);
  407. scope.uses_eval = false; // will be set to true if this or nested scope uses the global `eval`
  408. scope.uses_with = false; // will be set to true if this or some nested scope uses the `with` statement
  409. }
  410. AST_BlockScope.DEFMETHOD("init_vars", function(parent_scope) {
  411. init_block_vars(this, parent_scope);
  412. });
  413. AST_Scope.DEFMETHOD("init_vars", function(parent_scope) {
  414. init_scope_vars(this, parent_scope);
  415. });
  416. AST_Arrow.DEFMETHOD("init_vars", function(parent_scope) {
  417. init_scope_vars(this, parent_scope);
  418. return this;
  419. });
  420. AST_AsyncArrow.DEFMETHOD("init_vars", function(parent_scope) {
  421. init_scope_vars(this, parent_scope);
  422. });
  423. AST_Lambda.DEFMETHOD("init_vars", function(parent_scope) {
  424. init_scope_vars(this, parent_scope);
  425. this.uses_arguments = false;
  426. this.def_variable(new AST_SymbolFunarg({
  427. name: "arguments",
  428. start: this.start,
  429. end: this.end,
  430. }));
  431. return this;
  432. });
  433. AST_Symbol.DEFMETHOD("mark_enclosed", function(options) {
  434. var def = this.definition();
  435. for (var s = this.scope; s; s = s.parent_scope) {
  436. push_uniq(s.enclosed, def);
  437. if (!options) {
  438. delete s._var_names;
  439. } else if (options.keep_fnames) {
  440. s.functions.each(function(d) {
  441. push_uniq(def.scope.enclosed, d);
  442. });
  443. }
  444. if (s === def.scope) break;
  445. }
  446. });
  447. AST_Symbol.DEFMETHOD("reference", function(options) {
  448. this.definition().references.push(this);
  449. this.mark_enclosed(options);
  450. });
  451. AST_BlockScope.DEFMETHOD("find_variable", function(name) {
  452. return this.variables.get(name)
  453. || this.parent_scope && this.parent_scope.find_variable(name);
  454. });
  455. AST_BlockScope.DEFMETHOD("def_function", function(symbol, init) {
  456. var def = this.def_variable(symbol, init);
  457. if (!def.init || def.init instanceof AST_LambdaDefinition) def.init = init;
  458. this.functions.set(symbol.name, def);
  459. return def;
  460. });
  461. AST_BlockScope.DEFMETHOD("def_variable", function(symbol, init) {
  462. var def = this.variables.get(symbol.name);
  463. if (def) {
  464. def.orig.push(symbol);
  465. if (def.init instanceof AST_LambdaExpression) def.init = init;
  466. } else {
  467. def = this.make_def(symbol, init);
  468. this.variables.set(symbol.name, def);
  469. def.global = !this.parent_scope;
  470. }
  471. return symbol.thedef = def;
  472. });
  473. function names_in_use(scope, options) {
  474. var names = scope.names_in_use;
  475. if (!names) {
  476. scope.cname = -1;
  477. scope.cname_holes = [];
  478. scope.names_in_use = names = Object.create(null);
  479. var cache = options.cache && options.cache.props;
  480. scope.enclosed.forEach(function(def) {
  481. if (def.unmangleable(options)) names[def.name] = true;
  482. if (def.global && cache && cache.has(def.name)) {
  483. names[cache.get(def.name)] = true;
  484. }
  485. });
  486. }
  487. return names;
  488. }
  489. function next_mangled_name(def, options) {
  490. var scope = def.scope;
  491. var in_use = names_in_use(scope, options);
  492. var holes = scope.cname_holes;
  493. var names = Object.create(null);
  494. var scopes = [ scope ];
  495. def.forEach(function(sym) {
  496. var scope = sym.scope;
  497. do {
  498. if (scopes.indexOf(scope) < 0) {
  499. for (var name in names_in_use(scope, options)) {
  500. names[name] = true;
  501. }
  502. scopes.push(scope);
  503. } else break;
  504. } while (scope = scope.parent_scope);
  505. });
  506. var name;
  507. for (var i = 0; i < holes.length; i++) {
  508. name = base54(holes[i]);
  509. if (names[name]) continue;
  510. holes.splice(i, 1);
  511. in_use[name] = true;
  512. return name;
  513. }
  514. while (true) {
  515. name = base54(++scope.cname);
  516. if (in_use[name] || RESERVED_WORDS[name] || options.reserved.has[name]) continue;
  517. if (!names[name]) break;
  518. holes.push(scope.cname);
  519. }
  520. in_use[name] = true;
  521. return name;
  522. }
  523. AST_Symbol.DEFMETHOD("unmangleable", function(options) {
  524. var def = this.definition();
  525. return !def || def.unmangleable(options);
  526. });
  527. // labels are always mangleable
  528. AST_Label.DEFMETHOD("unmangleable", return_false);
  529. AST_Symbol.DEFMETHOD("definition", function() {
  530. return this.thedef;
  531. });
  532. function _default_mangler_options(options) {
  533. options = defaults(options, {
  534. eval : false,
  535. ie8 : false,
  536. keep_fnames : false,
  537. reserved : [],
  538. toplevel : false,
  539. v8 : false,
  540. webkit : false,
  541. });
  542. if (!Array.isArray(options.reserved)) options.reserved = [];
  543. // Never mangle arguments
  544. push_uniq(options.reserved, "arguments");
  545. options.reserved.has = makePredicate(options.reserved);
  546. return options;
  547. }
  548. AST_Toplevel.DEFMETHOD("mangle_names", function(options) {
  549. options = _default_mangler_options(options);
  550. // We only need to mangle declaration nodes. Special logic wired
  551. // into the code generator will display the mangled name if it's
  552. // present (and for AST_SymbolRef-s it'll use the mangled name of
  553. // the AST_SymbolDeclaration that it points to).
  554. var lname = -1;
  555. if (options.cache && options.cache.props) {
  556. var mangled_names = names_in_use(this, options);
  557. options.cache.props.each(function(mangled_name) {
  558. mangled_names[mangled_name] = true;
  559. });
  560. }
  561. var redefined = [];
  562. var tw = new TreeWalker(function(node, descend) {
  563. if (node instanceof AST_LabeledStatement) {
  564. // lname is incremented when we get to the AST_Label
  565. var save_nesting = lname;
  566. descend();
  567. if (!options.v8 || !in_label(tw)) lname = save_nesting;
  568. return true;
  569. }
  570. if (node instanceof AST_BlockScope) {
  571. if (options.webkit && node instanceof AST_IterationStatement && node.init instanceof AST_Let) {
  572. node.init.definitions.forEach(function(defn) {
  573. defn.name.match_symbol(function(sym) {
  574. if (!(sym instanceof AST_SymbolLet)) return;
  575. var def = sym.definition();
  576. var scope = sym.scope.parent_scope;
  577. var redef = scope.def_variable(sym);
  578. sym.thedef = def;
  579. scope.to_mangle.push(redef);
  580. def.redefined = function() {
  581. return redef;
  582. };
  583. });
  584. }, true);
  585. }
  586. node.to_mangle = [];
  587. node.variables.each(function(def) {
  588. if (!defer_redef(def)) node.to_mangle.push(def);
  589. });
  590. descend();
  591. if (options.cache && node instanceof AST_Toplevel) {
  592. node.globals.each(mangle);
  593. }
  594. if (node instanceof AST_Defun && tw.has_directive("use asm")) {
  595. var sym = new AST_SymbolRef(node.name);
  596. sym.scope = node;
  597. sym.reference(options);
  598. }
  599. node.to_mangle.forEach(mangle);
  600. return true;
  601. }
  602. if (node instanceof AST_Label) {
  603. var name;
  604. do {
  605. name = base54(++lname);
  606. } while (RESERVED_WORDS[name]);
  607. node.mangled_name = name;
  608. return true;
  609. }
  610. });
  611. this.walk(tw);
  612. redefined.forEach(mangle);
  613. function mangle(def) {
  614. if (options.reserved.has[def.name]) return;
  615. def.mangle(options);
  616. }
  617. function defer_redef(def) {
  618. var sym = def.orig[0];
  619. var redef = def.redefined();
  620. if (!redef) {
  621. if (!(sym instanceof AST_SymbolConst)) return false;
  622. var scope = def.scope.resolve();
  623. if (def.scope === scope) return false;
  624. if (def.scope.parent_scope.find_variable(sym.name)) return false;
  625. redef = scope.def_variable(sym);
  626. scope.to_mangle.push(redef);
  627. }
  628. redefined.push(def);
  629. def.references.forEach(reference);
  630. if (sym instanceof AST_SymbolCatch || sym instanceof AST_SymbolConst) reference(sym);
  631. return true;
  632. function reference(sym) {
  633. sym.thedef = redef;
  634. sym.reference(options);
  635. sym.thedef = def;
  636. }
  637. }
  638. function in_label(tw) {
  639. var level = 0, parent;
  640. while (parent = tw.parent(level++)) {
  641. if (parent instanceof AST_Block) return parent instanceof AST_Toplevel && !options.toplevel;
  642. if (parent instanceof AST_LabeledStatement) return true;
  643. }
  644. }
  645. });
  646. AST_Toplevel.DEFMETHOD("find_colliding_names", function(options) {
  647. var cache = options.cache && options.cache.props;
  648. var avoid = Object.create(RESERVED_WORDS);
  649. options.reserved.forEach(to_avoid);
  650. this.globals.each(add_def);
  651. this.walk(new TreeWalker(function(node) {
  652. if (node instanceof AST_BlockScope) node.variables.each(add_def);
  653. }));
  654. return avoid;
  655. function to_avoid(name) {
  656. avoid[name] = true;
  657. }
  658. function add_def(def) {
  659. var name = def.name;
  660. if (def.global && cache && cache.has(name)) name = cache.get(name);
  661. else if (!def.unmangleable(options)) return;
  662. to_avoid(name);
  663. }
  664. });
  665. AST_Toplevel.DEFMETHOD("expand_names", function(options) {
  666. base54.reset();
  667. base54.sort();
  668. options = _default_mangler_options(options);
  669. var avoid = this.find_colliding_names(options);
  670. var cname = 0;
  671. this.globals.each(rename);
  672. this.walk(new TreeWalker(function(node) {
  673. if (node instanceof AST_BlockScope) node.variables.each(rename);
  674. }));
  675. function next_name() {
  676. var name;
  677. do {
  678. name = base54(cname++);
  679. } while (avoid[name]);
  680. return name;
  681. }
  682. function rename(def) {
  683. if (def.global && options.cache) return;
  684. if (def.unmangleable(options)) return;
  685. if (options.reserved.has[def.name]) return;
  686. var redef = def.redefined();
  687. var name = redef ? redef.rename || redef.name : next_name();
  688. def.rename = name;
  689. def.forEach(function(sym) {
  690. if (sym.definition() === def) sym.name = name;
  691. });
  692. }
  693. });
  694. AST_Node.DEFMETHOD("tail_node", return_this);
  695. AST_Sequence.DEFMETHOD("tail_node", function() {
  696. return this.expressions[this.expressions.length - 1];
  697. });
  698. AST_Toplevel.DEFMETHOD("compute_char_frequency", function(options) {
  699. options = _default_mangler_options(options);
  700. base54.reset();
  701. var fn = AST_Symbol.prototype.add_source_map;
  702. try {
  703. AST_Symbol.prototype.add_source_map = function() {
  704. if (!this.unmangleable(options)) base54.consider(this.name, -1);
  705. };
  706. if (options.properties) {
  707. AST_Dot.prototype.add_source_map = function() {
  708. base54.consider(this.property, -1);
  709. };
  710. AST_Sub.prototype.add_source_map = function() {
  711. skip_string(this.property);
  712. };
  713. }
  714. base54.consider(this.print_to_string(), 1);
  715. } finally {
  716. AST_Symbol.prototype.add_source_map = fn;
  717. delete AST_Dot.prototype.add_source_map;
  718. delete AST_Sub.prototype.add_source_map;
  719. }
  720. base54.sort();
  721. function skip_string(node) {
  722. if (node instanceof AST_String) {
  723. base54.consider(node.value, -1);
  724. } else if (node instanceof AST_Conditional) {
  725. skip_string(node.consequent);
  726. skip_string(node.alternative);
  727. } else if (node instanceof AST_Sequence) {
  728. skip_string(node.tail_node());
  729. }
  730. }
  731. });
  732. var base54 = (function() {
  733. var freq = Object.create(null);
  734. function init(chars) {
  735. var array = [];
  736. for (var i = 0; i < chars.length; i++) {
  737. var ch = chars[i];
  738. array.push(ch);
  739. freq[ch] = -1e-2 * i;
  740. }
  741. return array;
  742. }
  743. var digits = init("0123456789");
  744. var leading = init("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_");
  745. var chars, frequency;
  746. function reset() {
  747. chars = null;
  748. frequency = Object.create(freq);
  749. }
  750. base54.consider = function(str, delta) {
  751. for (var i = str.length; --i >= 0;) {
  752. frequency[str[i]] += delta;
  753. }
  754. };
  755. function compare(a, b) {
  756. return frequency[b] - frequency[a];
  757. }
  758. base54.sort = function() {
  759. chars = leading.sort(compare).concat(digits).sort(compare);
  760. };
  761. base54.reset = reset;
  762. reset();
  763. function base54(num) {
  764. var ret = leading[num % 54];
  765. for (num = Math.floor(num / 54); --num >= 0; num >>= 6) {
  766. ret += chars[num & 0x3F];
  767. }
  768. return ret;
  769. }
  770. return base54;
  771. })();