scope.js 30 KB

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