diff options
author | Mihai Bazon <mihai@bazon.net> | 2012-10-02 13:20:07 +0300 |
---|---|---|
committer | Mihai Bazon <mihai@bazon.net> | 2012-10-02 13:20:07 +0300 |
commit | dde5b22b5e99d4a75c8918659b7f73abed436ce2 (patch) | |
tree | b724402dab6a58618bcf418c0a6c1014db8a6afb /lib | |
parent | e1098b04a7e8b3ebd4faf598b2b8b30bceba14d6 (diff) | |
download | tracifyjs-dde5b22b5e99d4a75c8918659b7f73abed436ce2.tar.gz tracifyjs-dde5b22b5e99d4a75c8918659b7f73abed436ce2.zip |
support defines
Diffstat (limited to 'lib')
-rw-r--r-- | lib/compress.js | 71 | ||||
-rw-r--r-- | lib/scope.js | 8 |
2 files changed, 46 insertions, 33 deletions
diff --git a/lib/compress.js b/lib/compress.js index ecd9c955..d790a284 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -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; }); diff --git a/lib/scope.js b/lib/scope.js index 4166ac25..997e6575 100644 --- a/lib/scope.js +++ b/lib/scope.js @@ -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; }); |