Compare commits
21 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
39f8a62703 | ||
|
|
46be3f2bf1 | ||
|
|
258b46f4dc | ||
|
|
80da21dab4 | ||
|
|
bb0e4d7126 | ||
|
|
5276a4a873 | ||
|
|
a1ae0c8609 | ||
|
|
a90c1aeafe | ||
|
|
ff388a8d2d | ||
|
|
5346fb94bb | ||
|
|
a4f6d46118 | ||
|
|
7f5f4d60b7 | ||
|
|
ffccb233e5 | ||
|
|
fba0c1aafe | ||
|
|
774f2ded94 | ||
|
|
85af942d64 | ||
|
|
8413787efc | ||
|
|
dde57452aa | ||
|
|
cf409800be | ||
|
|
18270dd9f3 | ||
|
|
d4c25c571b |
@@ -252,7 +252,7 @@ if (SCOPE_IS_NEEDED) {
|
|||||||
time_it("scope", function(){
|
time_it("scope", function(){
|
||||||
TOPLEVEL.figure_out_scope();
|
TOPLEVEL.figure_out_scope();
|
||||||
if (MANGLE) {
|
if (MANGLE) {
|
||||||
TOPLEVEL.compute_char_frequency();
|
TOPLEVEL.compute_char_frequency(MANGLE);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -345,6 +345,10 @@ var AST_Lambda = DEFNODE("Lambda", "name argnames uses_arguments", {
|
|||||||
}
|
}
|
||||||
}, AST_Scope);
|
}, AST_Scope);
|
||||||
|
|
||||||
|
var AST_Accessor = DEFNODE("Accessor", null, {
|
||||||
|
$documentation: "A setter/getter function"
|
||||||
|
}, AST_Lambda);
|
||||||
|
|
||||||
var AST_Function = DEFNODE("Function", null, {
|
var AST_Function = DEFNODE("Function", null, {
|
||||||
$documentation: "A function expression"
|
$documentation: "A function expression"
|
||||||
}, AST_Lambda);
|
}, AST_Lambda);
|
||||||
@@ -758,6 +762,10 @@ var AST_Symbol = DEFNODE("Symbol", "scope name thedef", {
|
|||||||
$documentation: "Base class for all symbols",
|
$documentation: "Base class for all symbols",
|
||||||
});
|
});
|
||||||
|
|
||||||
|
var AST_SymbolAccessor = DEFNODE("SymbolAccessor", null, {
|
||||||
|
$documentation: "The name of a property accessor (setter/getter function)"
|
||||||
|
}, AST_Symbol);
|
||||||
|
|
||||||
var AST_SymbolDeclaration = DEFNODE("SymbolDeclaration", "init", {
|
var AST_SymbolDeclaration = DEFNODE("SymbolDeclaration", "init", {
|
||||||
$documentation: "A declaration symbol (symbol in var/const, function name or argument, symbol in catch)",
|
$documentation: "A declaration symbol (symbol in var/const, function name or argument, symbol in catch)",
|
||||||
$propdoc: {
|
$propdoc: {
|
||||||
|
|||||||
@@ -53,6 +53,7 @@ function Compressor(options, false_by_default) {
|
|||||||
dead_code : !false_by_default,
|
dead_code : !false_by_default,
|
||||||
drop_debugger : !false_by_default,
|
drop_debugger : !false_by_default,
|
||||||
unsafe : !false_by_default,
|
unsafe : !false_by_default,
|
||||||
|
unsafe_comps : false,
|
||||||
conditionals : !false_by_default,
|
conditionals : !false_by_default,
|
||||||
comparisons : !false_by_default,
|
comparisons : !false_by_default,
|
||||||
evaluate : !false_by_default,
|
evaluate : !false_by_default,
|
||||||
@@ -703,7 +704,7 @@ merge(Compressor.prototype, {
|
|||||||
});
|
});
|
||||||
def(AST_Binary, function(compressor){
|
def(AST_Binary, function(compressor){
|
||||||
var self = this.clone(), op = this.operator;
|
var self = this.clone(), op = this.operator;
|
||||||
if (compressor.option("comparisons") && compressor.option("unsafe")) {
|
if (compressor.option("unsafe_comps")) {
|
||||||
switch (op) {
|
switch (op) {
|
||||||
case "<=" : self.operator = ">" ; return self;
|
case "<=" : self.operator = ">" ; return self;
|
||||||
case "<" : self.operator = ">=" ; return self;
|
case "<" : self.operator = ">=" ; return self;
|
||||||
@@ -1025,7 +1026,7 @@ merge(Compressor.prototype, {
|
|||||||
if (hoist_funs || hoist_vars) {
|
if (hoist_funs || hoist_vars) {
|
||||||
var dirs = [];
|
var dirs = [];
|
||||||
var hoisted = [];
|
var hoisted = [];
|
||||||
var vars = {}, vars_found = 0, var_decl = 0;
|
var vars = new Dictionary(), vars_found = 0, var_decl = 0;
|
||||||
// let's count var_decl first, we seem to waste a lot of
|
// let's count var_decl first, we seem to waste a lot of
|
||||||
// space if we hoist `var` when there's only one.
|
// space if we hoist `var` when there's only one.
|
||||||
self.walk(new TreeWalker(function(node){
|
self.walk(new TreeWalker(function(node){
|
||||||
@@ -1050,7 +1051,7 @@ merge(Compressor.prototype, {
|
|||||||
}
|
}
|
||||||
if (node instanceof AST_Var && hoist_vars) {
|
if (node instanceof AST_Var && hoist_vars) {
|
||||||
node.definitions.forEach(function(def){
|
node.definitions.forEach(function(def){
|
||||||
vars[def.name.name] = def;
|
vars.set(def.name.name, def);
|
||||||
++vars_found;
|
++vars_found;
|
||||||
});
|
});
|
||||||
var seq = node.to_assignments();
|
var seq = node.to_assignments();
|
||||||
@@ -1074,8 +1075,8 @@ merge(Compressor.prototype, {
|
|||||||
);
|
);
|
||||||
self = self.transform(tt);
|
self = self.transform(tt);
|
||||||
if (vars_found > 0) hoisted.unshift(make_node(AST_Var, self, {
|
if (vars_found > 0) hoisted.unshift(make_node(AST_Var, self, {
|
||||||
definitions: Object.keys(vars).map(function(name){
|
definitions: vars.map(function(def){
|
||||||
var def = vars[name].clone();
|
def = def.clone();
|
||||||
def.value = null;
|
def.value = null;
|
||||||
return def;
|
return def;
|
||||||
})
|
})
|
||||||
@@ -1117,6 +1118,15 @@ merge(Compressor.prototype, {
|
|||||||
return self;
|
return self;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
OPT(AST_While, function(self, compressor) {
|
||||||
|
if (!compressor.option("loops")) return self;
|
||||||
|
self = AST_DWLoop.prototype.optimize.call(self, compressor);
|
||||||
|
if (self instanceof AST_While) {
|
||||||
|
self = make_node(AST_For, self, self);
|
||||||
|
}
|
||||||
|
return self;
|
||||||
|
});
|
||||||
|
|
||||||
OPT(AST_For, function(self, compressor){
|
OPT(AST_For, function(self, compressor){
|
||||||
var cond = self.condition;
|
var cond = self.condition;
|
||||||
if (cond) {
|
if (cond) {
|
||||||
@@ -1445,9 +1455,9 @@ merge(Compressor.prototype, {
|
|||||||
compressor.warn("Boolean expression always true [{file}:{line},{col}]", self.start);
|
compressor.warn("Boolean expression always true [{file}:{line},{col}]", self.start);
|
||||||
return make_node(AST_True, self);
|
return make_node(AST_True, self);
|
||||||
}
|
}
|
||||||
}
|
if (e instanceof AST_Binary && self.operator == "!") {
|
||||||
if (e instanceof AST_Binary) {
|
self = best_of(self, e.negate(compressor));
|
||||||
self = best_of(self, e.negate(compressor));
|
}
|
||||||
}
|
}
|
||||||
return self.evaluate(compressor)[0];
|
return self.evaluate(compressor)[0];
|
||||||
});
|
});
|
||||||
@@ -1476,7 +1486,21 @@ merge(Compressor.prototype, {
|
|||||||
return this;
|
return this;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
var commutativeOperators = makePredicate("== === != !== * & | ^");
|
||||||
|
|
||||||
OPT(AST_Binary, function(self, compressor){
|
OPT(AST_Binary, function(self, compressor){
|
||||||
|
function reverse(op) {
|
||||||
|
if (op) self.operator = op;
|
||||||
|
var tmp = self.left;
|
||||||
|
self.left = self.right;
|
||||||
|
self.right = tmp;
|
||||||
|
};
|
||||||
|
if (commutativeOperators(self.operator)) {
|
||||||
|
if (self.right instanceof AST_Constant
|
||||||
|
&& !(self.left instanceof AST_Constant)) {
|
||||||
|
reverse();
|
||||||
|
}
|
||||||
|
}
|
||||||
self = self.lift_sequences(compressor);
|
self = self.lift_sequences(compressor);
|
||||||
if (compressor.option("comparisons")) switch (self.operator) {
|
if (compressor.option("comparisons")) switch (self.operator) {
|
||||||
case "===":
|
case "===":
|
||||||
@@ -1488,21 +1512,10 @@ merge(Compressor.prototype, {
|
|||||||
// XXX: intentionally falling down to the next case
|
// XXX: intentionally falling down to the next case
|
||||||
case "==":
|
case "==":
|
||||||
case "!=":
|
case "!=":
|
||||||
if (self.left instanceof AST_UnaryPrefix
|
if (self.left instanceof AST_String
|
||||||
&& self.left.operator == "typeof"
|
&& self.left.value == "undefined"
|
||||||
&& self.right instanceof AST_String
|
&& self.right instanceof AST_UnaryPrefix
|
||||||
&& self.right.value == "undefined") {
|
&& self.right.operator == "typeof") {
|
||||||
if (!(self.left.expression instanceof AST_SymbolRef)
|
|
||||||
|| !self.left.expression.undeclared()) {
|
|
||||||
self.left = self.left.expression;
|
|
||||||
self.right = make_node(AST_Undefined, self.right).optimize(compressor);
|
|
||||||
if (self.operator.length == 2) self.operator += "=";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (self.left instanceof AST_String
|
|
||||||
&& self.left.value == "undefined"
|
|
||||||
&& self.right instanceof AST_UnaryPrefix
|
|
||||||
&& self.right.operator == "typeof") {
|
|
||||||
if (!(self.right.expression instanceof AST_SymbolRef)
|
if (!(self.right.expression instanceof AST_SymbolRef)
|
||||||
|| !self.right.expression.undeclared()) {
|
|| !self.right.expression.undeclared()) {
|
||||||
self.left = self.right.expression;
|
self.left = self.right.expression;
|
||||||
@@ -1565,12 +1578,6 @@ merge(Compressor.prototype, {
|
|||||||
});
|
});
|
||||||
self = best_of(self, negated);
|
self = best_of(self, negated);
|
||||||
}
|
}
|
||||||
var reverse = function(op) {
|
|
||||||
self.operator = op;
|
|
||||||
var tmp = self.left;
|
|
||||||
self.left = self.right;
|
|
||||||
self.right = tmp;
|
|
||||||
};
|
|
||||||
switch (self.operator) {
|
switch (self.operator) {
|
||||||
case "<": reverse(">"); break;
|
case "<": reverse(">"); break;
|
||||||
case "<=": reverse(">="); break;
|
case "<=": reverse(">="); break;
|
||||||
|
|||||||
@@ -137,7 +137,7 @@ function OutputStream(options) {
|
|||||||
str = String(str);
|
str = String(str);
|
||||||
var ch = str.charAt(0);
|
var ch = str.charAt(0);
|
||||||
if (might_need_semicolon) {
|
if (might_need_semicolon) {
|
||||||
if (";}".indexOf(ch) < 0 && !/[;]$/.test(last)) {
|
if ((!ch || ";}".indexOf(ch) < 0) && !/[;]$/.test(last)) {
|
||||||
if (options.semicolons || requireSemicolonChars(ch)) {
|
if (options.semicolons || requireSemicolonChars(ch)) {
|
||||||
OUTPUT += ";";
|
OUTPUT += ";";
|
||||||
current_col++;
|
current_col++;
|
||||||
@@ -410,6 +410,11 @@ function OutputStream(options) {
|
|||||||
return first_in_statement(output);
|
return first_in_statement(output);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
PARENS(AST_Unary, function(output){
|
||||||
|
var p = output.parent();
|
||||||
|
return p instanceof AST_PropAccess && p.expression === this;
|
||||||
|
});
|
||||||
|
|
||||||
PARENS(AST_Seq, function(output){
|
PARENS(AST_Seq, function(output){
|
||||||
var p = output.parent();
|
var p = output.parent();
|
||||||
return p instanceof AST_Call // (foo, bar)() or foo(1, (2, 3), 4)
|
return p instanceof AST_Call // (foo, bar)() or foo(1, (2, 3), 4)
|
||||||
@@ -463,10 +468,36 @@ function OutputStream(options) {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
PARENS(AST_PropAccess, function(output){
|
||||||
|
var p = output.parent();
|
||||||
|
if (p instanceof AST_New && p.expression === this) {
|
||||||
|
// i.e. new (foo.bar().baz)
|
||||||
|
//
|
||||||
|
// if there's one call into this subtree, then we need
|
||||||
|
// parens around it too, otherwise the call will be
|
||||||
|
// interpreted as passing the arguments to the upper New
|
||||||
|
// expression.
|
||||||
|
try {
|
||||||
|
this.walk(new TreeWalker(function(node){
|
||||||
|
if (node instanceof AST_Call) throw p;
|
||||||
|
}));
|
||||||
|
} catch(ex) {
|
||||||
|
if (ex !== p) throw ex;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
PARENS(AST_Call, function(output){
|
||||||
|
var p = output.parent();
|
||||||
|
return p instanceof AST_New && p.expression === this;
|
||||||
|
});
|
||||||
|
|
||||||
PARENS(AST_New, function(output){
|
PARENS(AST_New, function(output){
|
||||||
var p = output.parent();
|
var p = output.parent();
|
||||||
// (new Date).getTime();
|
if (no_constructor_parens(this, output)
|
||||||
if (p instanceof AST_Dot && no_constructor_parens(this, output))
|
&& (p instanceof AST_Dot // (new Date).getTime()
|
||||||
|
|| p instanceof AST_Call && p.expression === this)) // (new foo)(bar)
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -529,6 +560,7 @@ function OutputStream(options) {
|
|||||||
});
|
});
|
||||||
DEFPRINT(AST_Toplevel, function(self, output){
|
DEFPRINT(AST_Toplevel, function(self, output){
|
||||||
display_body(self.body, true, output);
|
display_body(self.body, true, output);
|
||||||
|
output.print("");
|
||||||
});
|
});
|
||||||
DEFPRINT(AST_LabeledStatement, function(self, output){
|
DEFPRINT(AST_LabeledStatement, function(self, output){
|
||||||
self.label.print(output);
|
self.label.print(output);
|
||||||
|
|||||||
@@ -883,6 +883,8 @@ function parse($TEXT, options) {
|
|||||||
var function_ = function(in_statement, ctor) {
|
var function_ = function(in_statement, ctor) {
|
||||||
var name = is("name") ? as_symbol(in_statement
|
var name = is("name") ? as_symbol(in_statement
|
||||||
? AST_SymbolDefun
|
? AST_SymbolDefun
|
||||||
|
: ctor === AST_Accessor
|
||||||
|
? AST_SymbolAccessor
|
||||||
: AST_SymbolLambda) : null;
|
: AST_SymbolLambda) : null;
|
||||||
if (in_statement && !name)
|
if (in_statement && !name)
|
||||||
unexpected();
|
unexpected();
|
||||||
@@ -1158,7 +1160,7 @@ function parse($TEXT, options) {
|
|||||||
a.push(new AST_ObjectGetter({
|
a.push(new AST_ObjectGetter({
|
||||||
start : start,
|
start : start,
|
||||||
key : name,
|
key : name,
|
||||||
value : function_(false, AST_Lambda),
|
value : function_(false, AST_Accessor),
|
||||||
end : prev()
|
end : prev()
|
||||||
}));
|
}));
|
||||||
continue;
|
continue;
|
||||||
@@ -1167,7 +1169,7 @@ function parse($TEXT, options) {
|
|||||||
a.push(new AST_ObjectSetter({
|
a.push(new AST_ObjectSetter({
|
||||||
start : start,
|
start : start,
|
||||||
key : name,
|
key : name,
|
||||||
value : function_(false, AST_Lambda),
|
value : function_(false, AST_Accessor),
|
||||||
end : prev()
|
end : prev()
|
||||||
}));
|
}));
|
||||||
continue;
|
continue;
|
||||||
|
|||||||
95
lib/scope.js
95
lib/scope.js
@@ -43,7 +43,7 @@
|
|||||||
|
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
function SymbolDef(scope, orig) {
|
function SymbolDef(scope, index, orig) {
|
||||||
this.name = orig.name;
|
this.name = orig.name;
|
||||||
this.orig = [ orig ];
|
this.orig = [ orig ];
|
||||||
this.scope = scope;
|
this.scope = scope;
|
||||||
@@ -52,15 +52,18 @@ function SymbolDef(scope, orig) {
|
|||||||
this.mangled_name = null;
|
this.mangled_name = null;
|
||||||
this.undeclared = false;
|
this.undeclared = false;
|
||||||
this.constant = false;
|
this.constant = false;
|
||||||
|
this.index = index;
|
||||||
};
|
};
|
||||||
|
|
||||||
SymbolDef.prototype = {
|
SymbolDef.prototype = {
|
||||||
unmangleable: function() {
|
unmangleable: function(options) {
|
||||||
return this.global || this.undeclared || this.scope.uses_eval || this.scope.uses_with;
|
return this.global
|
||||||
|
|| this.undeclared
|
||||||
|
|| (!options.eval && (this.scope.uses_eval || this.scope.uses_with));
|
||||||
},
|
},
|
||||||
mangle: function() {
|
mangle: function(options) {
|
||||||
if (!this.mangled_name && !this.unmangleable())
|
if (!this.mangled_name && !this.unmangleable(options))
|
||||||
this.mangled_name = this.scope.next_mangled();
|
this.mangled_name = this.scope.next_mangled(options);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -75,14 +78,17 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(){
|
|||||||
// pass 1: setup scope chaining and handle definitions
|
// pass 1: setup scope chaining and handle definitions
|
||||||
var self = this;
|
var self = this;
|
||||||
var scope = self.parent_scope = null;
|
var scope = self.parent_scope = null;
|
||||||
var labels = Object.create(null);
|
var labels = new Dictionary();
|
||||||
|
var nesting = 0;
|
||||||
var tw = new TreeWalker(function(node, descend){
|
var tw = new TreeWalker(function(node, descend){
|
||||||
if (node instanceof AST_Scope) {
|
if (node instanceof AST_Scope) {
|
||||||
node.init_scope_vars();
|
node.init_scope_vars(nesting);
|
||||||
var save_scope = node.parent_scope = scope;
|
var save_scope = node.parent_scope = scope;
|
||||||
|
++nesting;
|
||||||
scope = node;
|
scope = node;
|
||||||
descend();
|
descend();
|
||||||
scope = save_scope;
|
scope = save_scope;
|
||||||
|
--nesting;
|
||||||
return true; // don't descend again in TreeWalker
|
return true; // don't descend again in TreeWalker
|
||||||
}
|
}
|
||||||
if (node instanceof AST_Directive) {
|
if (node instanceof AST_Directive) {
|
||||||
@@ -97,11 +103,11 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(){
|
|||||||
}
|
}
|
||||||
if (node instanceof AST_LabeledStatement) {
|
if (node instanceof AST_LabeledStatement) {
|
||||||
var l = node.label;
|
var l = node.label;
|
||||||
if (labels[l.name])
|
if (labels.has(l.name))
|
||||||
throw new Error(string_template("Label {name} defined twice", l));
|
throw new Error(string_template("Label {name} defined twice", l));
|
||||||
labels[l.name] = l;
|
labels.set(l.name, l);
|
||||||
descend();
|
descend();
|
||||||
delete labels[l.name];
|
labels.del(l.name);
|
||||||
return true; // no descend again
|
return true; // no descend again
|
||||||
}
|
}
|
||||||
if (node instanceof AST_SymbolDeclaration) {
|
if (node instanceof AST_SymbolDeclaration) {
|
||||||
@@ -151,7 +157,7 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(){
|
|||||||
scope.def_variable(node);
|
scope.def_variable(node);
|
||||||
}
|
}
|
||||||
if (node instanceof AST_LabelRef) {
|
if (node instanceof AST_LabelRef) {
|
||||||
var sym = labels[node.name];
|
var sym = labels.get(node.name);
|
||||||
if (!sym) throw new Error(string_template("Undefined label {name} [{line},{col}]", {
|
if (!sym) throw new Error(string_template("Undefined label {name} [{line},{col}]", {
|
||||||
name: node.name,
|
name: node.name,
|
||||||
line: node.start.line,
|
line: node.start.line,
|
||||||
@@ -164,7 +170,7 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(){
|
|||||||
|
|
||||||
// pass 2: find back references and eval
|
// pass 2: find back references and eval
|
||||||
var func = null;
|
var func = null;
|
||||||
var globals = self.globals = Object.create(null);
|
var globals = self.globals = new Dictionary();
|
||||||
var tw = new TreeWalker(function(node, descend){
|
var tw = new TreeWalker(function(node, descend){
|
||||||
if (node instanceof AST_Lambda) {
|
if (node instanceof AST_Lambda) {
|
||||||
var prev_func = func;
|
var prev_func = func;
|
||||||
@@ -182,12 +188,12 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(){
|
|||||||
var sym = node.scope.find_variable(name);
|
var sym = node.scope.find_variable(name);
|
||||||
if (!sym) {
|
if (!sym) {
|
||||||
var g;
|
var g;
|
||||||
if (globals[name]) {
|
if (globals.has(name)) {
|
||||||
g = globals[name];
|
g = globals.get(name);
|
||||||
} else {
|
} else {
|
||||||
g = new SymbolDef(self, node);
|
g = new SymbolDef(self, globals.size(), node);
|
||||||
g.undeclared = true;
|
g.undeclared = true;
|
||||||
globals[name] = g;
|
globals.set(name, g);
|
||||||
}
|
}
|
||||||
node.thedef = g;
|
node.thedef = g;
|
||||||
if (name == "eval" && tw.parent() instanceof AST_Call) {
|
if (name == "eval" && tw.parent() instanceof AST_Call) {
|
||||||
@@ -207,15 +213,16 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(){
|
|||||||
self.walk(tw);
|
self.walk(tw);
|
||||||
});
|
});
|
||||||
|
|
||||||
AST_Scope.DEFMETHOD("init_scope_vars", function(){
|
AST_Scope.DEFMETHOD("init_scope_vars", function(nesting){
|
||||||
this.directives = []; // contains the directives defined in this scope, i.e. "use strict"
|
this.directives = []; // contains the directives defined in this scope, i.e. "use strict"
|
||||||
this.variables = Object.create(null); // map name to AST_SymbolVar (variables defined in this scope; includes functions)
|
this.variables = new Dictionary(); // map name to AST_SymbolVar (variables defined in this scope; includes functions)
|
||||||
this.functions = Object.create(null); // map name to AST_SymbolDefun (functions defined in this scope)
|
this.functions = new Dictionary(); // map name to AST_SymbolDefun (functions defined in this scope)
|
||||||
this.uses_with = false; // will be set to true if this or some nested scope uses the `with` statement
|
this.uses_with = false; // will be set to true if this or some nested scope uses the `with` statement
|
||||||
this.uses_eval = false; // will be set to true if this or nested scope uses the global `eval`
|
this.uses_eval = false; // will be set to true if this or nested scope uses the global `eval`
|
||||||
this.parent_scope = null; // the parent scope
|
this.parent_scope = null; // the parent scope
|
||||||
this.enclosed = []; // a list of variables from this or outer scope(s) that are referenced from this or inner scopes
|
this.enclosed = []; // a list of variables from this or outer scope(s) that are referenced from this or inner scopes
|
||||||
this.cname = -1; // the current index for mangling functions/variables
|
this.cname = -1; // the current index for mangling functions/variables
|
||||||
|
this.nesting = nesting; // the nesting level of this scope (0 means toplevel)
|
||||||
});
|
});
|
||||||
|
|
||||||
AST_Scope.DEFMETHOD("strict", function(){
|
AST_Scope.DEFMETHOD("strict", function(){
|
||||||
@@ -223,7 +230,7 @@ AST_Scope.DEFMETHOD("strict", function(){
|
|||||||
});
|
});
|
||||||
|
|
||||||
AST_Lambda.DEFMETHOD("init_scope_vars", function(){
|
AST_Lambda.DEFMETHOD("init_scope_vars", function(){
|
||||||
AST_Scope.prototype.init_scope_vars.call(this);
|
AST_Scope.prototype.init_scope_vars.apply(this, arguments);
|
||||||
this.uses_arguments = false;
|
this.uses_arguments = false;
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -236,6 +243,7 @@ AST_SymbolRef.DEFMETHOD("reference", function() {
|
|||||||
if (s === def.scope) break;
|
if (s === def.scope) break;
|
||||||
s = s.parent_scope;
|
s = s.parent_scope;
|
||||||
}
|
}
|
||||||
|
this.frame = this.scope.nesting - def.scope.nesting;
|
||||||
});
|
});
|
||||||
|
|
||||||
AST_SymbolDeclaration.DEFMETHOD("init_scope_vars", function(){
|
AST_SymbolDeclaration.DEFMETHOD("init_scope_vars", function(){
|
||||||
@@ -252,7 +260,7 @@ AST_LabelRef.DEFMETHOD("reference", function(){
|
|||||||
|
|
||||||
AST_Scope.DEFMETHOD("find_variable", function(name){
|
AST_Scope.DEFMETHOD("find_variable", function(name){
|
||||||
if (name instanceof AST_Symbol) name = name.name;
|
if (name instanceof AST_Symbol) name = name.name;
|
||||||
return this.variables[name]
|
return this.variables.get(name)
|
||||||
|| (this.parent_scope && this.parent_scope.find_variable(name));
|
|| (this.parent_scope && this.parent_scope.find_variable(name));
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -262,23 +270,23 @@ AST_Scope.DEFMETHOD("has_directive", function(value){
|
|||||||
});
|
});
|
||||||
|
|
||||||
AST_Scope.DEFMETHOD("def_function", function(symbol){
|
AST_Scope.DEFMETHOD("def_function", function(symbol){
|
||||||
this.functions[symbol.name] = this.def_variable(symbol);
|
this.functions.set(symbol.name, this.def_variable(symbol));
|
||||||
});
|
});
|
||||||
|
|
||||||
AST_Scope.DEFMETHOD("def_variable", function(symbol){
|
AST_Scope.DEFMETHOD("def_variable", function(symbol){
|
||||||
var def;
|
var def;
|
||||||
if (!this.variables[symbol.name]) {
|
if (!this.variables.has(symbol.name)) {
|
||||||
def = new SymbolDef(this, symbol);
|
def = new SymbolDef(this, this.variables.size(), symbol);
|
||||||
this.variables[symbol.name] = def;
|
this.variables.set(symbol.name, def);
|
||||||
def.global = !this.parent_scope;
|
def.global = !this.parent_scope;
|
||||||
} else {
|
} else {
|
||||||
def = this.variables[symbol.name];
|
def = this.variables.get(symbol.name);
|
||||||
def.orig.push(symbol);
|
def.orig.push(symbol);
|
||||||
}
|
}
|
||||||
return symbol.thedef = def;
|
return symbol.thedef = def;
|
||||||
});
|
});
|
||||||
|
|
||||||
AST_Scope.DEFMETHOD("next_mangled", function(){
|
AST_Scope.DEFMETHOD("next_mangled", function(options){
|
||||||
var ext = this.enclosed, n = ext.length;
|
var ext = this.enclosed, n = ext.length;
|
||||||
out: while (true) {
|
out: while (true) {
|
||||||
var m = base54(++this.cname);
|
var m = base54(++this.cname);
|
||||||
@@ -288,7 +296,7 @@ AST_Scope.DEFMETHOD("next_mangled", function(){
|
|||||||
// inner scopes.
|
// inner scopes.
|
||||||
for (var i = n; --i >= 0;) {
|
for (var i = n; --i >= 0;) {
|
||||||
var sym = ext[i];
|
var sym = ext[i];
|
||||||
var name = sym.mangled_name || (sym.unmangleable() && sym.name);
|
var name = sym.mangled_name || (sym.unmangleable(options) && sym.name);
|
||||||
if (m == name) continue out;
|
if (m == name) continue out;
|
||||||
}
|
}
|
||||||
return m;
|
return m;
|
||||||
@@ -300,8 +308,13 @@ AST_Scope.DEFMETHOD("references", function(sym){
|
|||||||
return this.enclosed.indexOf(sym) < 0 ? null : sym;
|
return this.enclosed.indexOf(sym) < 0 ? null : sym;
|
||||||
});
|
});
|
||||||
|
|
||||||
AST_Symbol.DEFMETHOD("unmangleable", function(){
|
AST_Symbol.DEFMETHOD("unmangleable", function(options){
|
||||||
return this.definition().unmangleable();
|
return this.definition().unmangleable(options);
|
||||||
|
});
|
||||||
|
|
||||||
|
// property accessors are not mangleable
|
||||||
|
AST_SymbolAccessor.DEFMETHOD("unmangleable", function(){
|
||||||
|
return true;
|
||||||
});
|
});
|
||||||
|
|
||||||
// labels are always mangleable
|
// labels are always mangleable
|
||||||
@@ -336,7 +349,8 @@ AST_Symbol.DEFMETHOD("global", function(){
|
|||||||
|
|
||||||
AST_Toplevel.DEFMETHOD("mangle_names", function(options){
|
AST_Toplevel.DEFMETHOD("mangle_names", function(options){
|
||||||
options = defaults(options, {
|
options = defaults(options, {
|
||||||
except : []
|
except : [],
|
||||||
|
eval : false,
|
||||||
});
|
});
|
||||||
// We only need to mangle declaration nodes. Special logic wired
|
// We only need to mangle declaration nodes. Special logic wired
|
||||||
// into the code generator will display the mangled name if it's
|
// into the code generator will display the mangled name if it's
|
||||||
@@ -354,16 +368,11 @@ AST_Toplevel.DEFMETHOD("mangle_names", function(options){
|
|||||||
}
|
}
|
||||||
if (node instanceof AST_Scope) {
|
if (node instanceof AST_Scope) {
|
||||||
var p = tw.parent();
|
var p = tw.parent();
|
||||||
var is_setget = p instanceof AST_ObjectSetter || p instanceof AST_ObjectGetter;
|
node.variables.each(function(symbol){
|
||||||
var a = node.variables;
|
if (options.except.indexOf(symbol.name) < 0) {
|
||||||
for (var i in a) {
|
to_mangle.push(symbol);
|
||||||
var symbol = a[i];
|
|
||||||
if (!(is_setget && symbol instanceof AST_SymbolLambda)) {
|
|
||||||
if (options.except.indexOf(symbol.name) < 0) {
|
|
||||||
to_mangle.push(symbol);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (node instanceof AST_Label) {
|
if (node instanceof AST_Label) {
|
||||||
@@ -377,7 +386,7 @@ AST_Toplevel.DEFMETHOD("mangle_names", function(options){
|
|||||||
to_mangle.forEach(function(def){ def.mangle(options) });
|
to_mangle.forEach(function(def){ def.mangle(options) });
|
||||||
});
|
});
|
||||||
|
|
||||||
AST_Toplevel.DEFMETHOD("compute_char_frequency", function(){
|
AST_Toplevel.DEFMETHOD("compute_char_frequency", function(options){
|
||||||
var tw = new TreeWalker(function(node){
|
var tw = new TreeWalker(function(node){
|
||||||
if (node instanceof AST_Constant)
|
if (node instanceof AST_Constant)
|
||||||
base54.consider(node.print_to_string());
|
base54.consider(node.print_to_string());
|
||||||
@@ -435,7 +444,7 @@ AST_Toplevel.DEFMETHOD("compute_char_frequency", function(){
|
|||||||
base54.consider("catch");
|
base54.consider("catch");
|
||||||
else if (node instanceof AST_Finally)
|
else if (node instanceof AST_Finally)
|
||||||
base54.consider("finally");
|
base54.consider("finally");
|
||||||
else if (node instanceof AST_Symbol && node.unmangleable())
|
else if (node instanceof AST_Symbol && node.unmangleable(options))
|
||||||
base54.consider(node.name);
|
base54.consider(node.name);
|
||||||
else if (node instanceof AST_Unary || node instanceof AST_Binary)
|
else if (node instanceof AST_Unary || node instanceof AST_Binary)
|
||||||
base54.consider(node.operator);
|
base54.consider(node.operator);
|
||||||
|
|||||||
34
lib/utils.js
34
lib/utils.js
@@ -244,3 +244,37 @@ function makePredicate(words) {
|
|||||||
}
|
}
|
||||||
return new Function("str", f);
|
return new Function("str", f);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function Dictionary() {
|
||||||
|
this._values = Object.create(null);
|
||||||
|
this._size = 0;
|
||||||
|
};
|
||||||
|
Dictionary.prototype = {
|
||||||
|
set: function(key, val) {
|
||||||
|
if (!this.has(key)) ++this._size;
|
||||||
|
this._values["$" + key] = val;
|
||||||
|
return this;
|
||||||
|
},
|
||||||
|
get: function(key) { return this._values["$" + key] },
|
||||||
|
del: function(key) {
|
||||||
|
if (this.has(key)) {
|
||||||
|
--this._size;
|
||||||
|
delete this._values["$" + key];
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
},
|
||||||
|
has: function(key) { return ("$" + key) in this._values },
|
||||||
|
each: function(f) {
|
||||||
|
for (var i in this._values)
|
||||||
|
f(this._values[i], i.substr(1));
|
||||||
|
},
|
||||||
|
size: function() {
|
||||||
|
return this._size;
|
||||||
|
},
|
||||||
|
map: function(f) {
|
||||||
|
var ret = [];
|
||||||
|
for (var i in this._values)
|
||||||
|
ret.push(f(this._values[i], i.substr(1)));
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
"description": "JavaScript parser, mangler/compressor and beautifier toolkit",
|
"description": "JavaScript parser, mangler/compressor and beautifier toolkit",
|
||||||
"homepage": "http://lisperator.net/uglifyjs",
|
"homepage": "http://lisperator.net/uglifyjs",
|
||||||
"main": "tools/node.js",
|
"main": "tools/node.js",
|
||||||
"version": "2.1.5",
|
"version": "2.1.9",
|
||||||
"engines": { "node" : ">=0.4.0" },
|
"engines": { "node" : ">=0.4.0" },
|
||||||
"maintainers": [{
|
"maintainers": [{
|
||||||
"name": "Mihai Bazon",
|
"name": "Mihai Bazon",
|
||||||
|
|||||||
@@ -1,18 +1,5 @@
|
|||||||
var path = require("path");
|
var path = require("path");
|
||||||
var fs = require("fs");
|
var fs = require("fs");
|
||||||
|
|
||||||
// Avoid NodeJS warning.
|
|
||||||
//
|
|
||||||
// There's a --no-deprecation command line argument supported by
|
|
||||||
// NodeJS, but that's tricky to use, so I'd like to set it from the
|
|
||||||
// program itself. Turns out you need to set `process.noDeprecation`,
|
|
||||||
// but by the time you can set that the `path` module is already
|
|
||||||
// loaded and `path.existsSync` is already changed to display that
|
|
||||||
// warning, therefore here's the poor solution:
|
|
||||||
if (fs.existsSync) {
|
|
||||||
path.existsSync = fs.existsSync;
|
|
||||||
}
|
|
||||||
|
|
||||||
var vm = require("vm");
|
var vm = require("vm");
|
||||||
var sys = require("util");
|
var sys = require("util");
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user