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