support defines
This commit is contained in:
@@ -65,7 +65,8 @@ function Compressor(options, false_by_default) {
|
||||
join_vars : !false_by_default,
|
||||
cascade : !false_by_default,
|
||||
|
||||
warnings : true
|
||||
warnings : true,
|
||||
global_defs : {}
|
||||
}, true);
|
||||
};
|
||||
|
||||
@@ -132,6 +133,30 @@ merge(Compressor.prototype, {
|
||||
return new ctor(props);
|
||||
};
|
||||
|
||||
function make_node_from_constant(compressor, val, orig) {
|
||||
switch (typeof val) {
|
||||
case "string":
|
||||
return make_node(AST_String, orig, {
|
||||
value: val
|
||||
}).optimize(compressor);
|
||||
case "number":
|
||||
return make_node(isNaN(val) ? AST_NaN : AST_Number, orig, {
|
||||
value: val
|
||||
}).optimize(compressor);
|
||||
case "boolean":
|
||||
return make_node(val ? AST_True : AST_False, orig);
|
||||
case "undefined":
|
||||
return make_node(AST_Undefined, orig).optimize(compressor);
|
||||
default:
|
||||
if (val === null) {
|
||||
return make_node(AST_Null, orig).optimize(compressor);
|
||||
}
|
||||
throw new Error(string_template("Can't handle constant of type: {type}", {
|
||||
type: typeof val
|
||||
}));
|
||||
}
|
||||
};
|
||||
|
||||
function as_statement_array(thing) {
|
||||
if (thing === null) return [];
|
||||
if (thing instanceof AST_BlockStatement) return thing.body;
|
||||
@@ -493,33 +518,7 @@ merge(Compressor.prototype, {
|
||||
AST_Node.DEFMETHOD("evaluate", function(compressor){
|
||||
if (!compressor.option("evaluate")) return [ this ];
|
||||
try {
|
||||
var val = this._eval(), ast;
|
||||
switch (typeof val) {
|
||||
case "string":
|
||||
ast = make_node(AST_String, this, {
|
||||
value: val
|
||||
}).optimize(compressor);
|
||||
break;
|
||||
case "number":
|
||||
ast = make_node(isNaN(val) ? AST_NaN : AST_Number, this, {
|
||||
value: val
|
||||
}).optimize(compressor);
|
||||
break;
|
||||
case "boolean":
|
||||
ast = make_node(val ? AST_True : AST_False, this);
|
||||
break;
|
||||
case "undefined":
|
||||
ast = make_node(AST_Undefined, this).optimize(compressor);
|
||||
break;
|
||||
default:
|
||||
if (val === null) {
|
||||
ast = make_node(AST_Null, this).optimize(compressor);
|
||||
break;
|
||||
}
|
||||
throw new Error(string_template("Can't handle constant of type: {type}", {
|
||||
type: typeof val
|
||||
}));
|
||||
}
|
||||
var val = this._eval(), ast = make_node_from_constant(compressor, val, this);
|
||||
return [ best_of(ast, this), val ];
|
||||
} catch(ex) {
|
||||
if (ex !== def) throw ex;
|
||||
@@ -1439,11 +1438,17 @@ merge(Compressor.prototype, {
|
||||
});
|
||||
|
||||
OPT(AST_SymbolRef, function(self, compressor){
|
||||
if (self.undeclared()) switch (self.name) {
|
||||
case "undefined":
|
||||
return make_node(AST_Undefined, self);
|
||||
case "NaN":
|
||||
return make_node(AST_NaN, self);
|
||||
if (self.undeclared()) {
|
||||
var defines = compressor.option("global_defs");
|
||||
if (defines && HOP(defines, self.name)) {
|
||||
return make_node_from_constant(compressor, defines[self.name], self);
|
||||
}
|
||||
switch (self.name) {
|
||||
case "undefined":
|
||||
return make_node(AST_Undefined, self);
|
||||
case "NaN":
|
||||
return make_node(AST_NaN, self);
|
||||
}
|
||||
}
|
||||
return self;
|
||||
});
|
||||
|
||||
@@ -331,6 +331,14 @@ AST_Symbol.DEFMETHOD("undeclared", function(){
|
||||
return this.definition().undeclared;
|
||||
});
|
||||
|
||||
AST_LabelRef.DEFMETHOD("undeclared", function(){
|
||||
return false;
|
||||
});
|
||||
|
||||
AST_Label.DEFMETHOD("undeclared", function(){
|
||||
return false;
|
||||
});
|
||||
|
||||
AST_Symbol.DEFMETHOD("definition", function(){
|
||||
return this.thedef;
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user