aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authoralexlamsl <alexlamsl@gmail.com>2017-02-18 19:19:55 +0800
committeralexlamsl <alexlamsl@gmail.com>2017-02-21 13:29:58 +0800
commita0f4fd390a0a1af80964aab9754bf5358db575e2 (patch)
treeb07e7f034909cb2b234fb8963575b6e0f8ff2ef5 /lib
parentb8b133d91a7a65f3375d391a036623901d1e357f (diff)
downloadtracifyjs-a0f4fd390a0a1af80964aab9754bf5358db575e2.tar.gz
tracifyjs-a0f4fd390a0a1af80964aab9754bf5358db575e2.zip
improve reduce_vars and fix a bug
- update modified flag between compress() passes - support IIFE arguments - fix corner case with multiple definitions closes #1473
Diffstat (limited to 'lib')
-rw-r--r--lib/compress.js41
-rw-r--r--lib/scope.js26
2 files changed, 39 insertions, 28 deletions
diff --git a/lib/compress.js b/lib/compress.js
index 345a414a..72afe92a 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -108,7 +108,8 @@ merge(Compressor.prototype, {
compress: function(node) {
var passes = +this.options.passes || 1;
for (var pass = 0; pass < passes && pass < 3; ++pass) {
- if (pass > 0) node.clear_opt_flags();
+ if (pass > 0 || this.option("reduce_vars"))
+ node.reset_opt_flags(this);
node = node.transform(this);
}
return node;
@@ -167,19 +168,45 @@ merge(Compressor.prototype, {
return this.print_to_string() == node.print_to_string();
});
- AST_Node.DEFMETHOD("clear_opt_flags", function(){
- this.walk(new TreeWalker(function(node){
+ AST_Node.DEFMETHOD("reset_opt_flags", function(compressor){
+ var reduce_vars = compressor.option("reduce_vars");
+ var tw = new TreeWalker(function(node){
+ if (reduce_vars && node instanceof AST_Scope) {
+ node.variables.each(function(def) {
+ delete def.modified;
+ });
+ }
if (node instanceof AST_SymbolRef) {
var d = node.definition();
- if (d && d.init) {
+ if (d.init) {
delete d.init._evaluated;
}
+ if (reduce_vars && (d.orig.length > 1 || isModified(node, 0))) {
+ d.modified = true;
+ }
+ }
+ if (reduce_vars && node instanceof AST_Call && node.expression instanceof AST_Function) {
+ node.expression.argnames.forEach(function(arg, i) {
+ arg.definition().init = node.args[i] || make_node(AST_Undefined, node);
+ });
}
if (!(node instanceof AST_Directive || node instanceof AST_Constant)) {
node._squeezed = false;
node._optimized = false;
}
- }));
+ });
+ this.walk(tw);
+
+ function isModified(node, level) {
+ var parent = tw.parent(level);
+ if (parent instanceof AST_Unary && (parent.operator === "++" || parent.operator === "--")
+ || parent instanceof AST_Assign && parent.left === node
+ || parent instanceof AST_Call && parent.expression === node) {
+ return true;
+ } else if (parent instanceof AST_PropAccess && parent.expression === node) {
+ return isModified(parent, level + 1);
+ }
+ }
});
function make_node(ctor, orig, props) {
@@ -459,7 +486,7 @@ merge(Compressor.prototype, {
var_defs_removed = true;
}
// Further optimize statement after substitution.
- stat.clear_opt_flags();
+ stat.reset_opt_flags(compressor);
compressor.warn("Replacing " + (is_constant ? "constant" : "variable") +
" " + var_name + " [{file}:{line},{col}]", node.start);
@@ -1158,7 +1185,7 @@ merge(Compressor.prototype, {
this._evaluating = true;
try {
var d = this.definition();
- if (d && (d.constant || compressor.option("reduce_vars") && !d.modified) && d.init) {
+ if ((d.constant || compressor.option("reduce_vars") && !d.modified) && d.init) {
if (compressor.option("unsafe")) {
if (!HOP(d.init, '_evaluated')) {
d.init._evaluated = ev(d.init, compressor);
diff --git a/lib/scope.js b/lib/scope.js
index d5cadd34..6ad12616 100644
--- a/lib/scope.js
+++ b/lib/scope.js
@@ -183,17 +183,6 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(options){
var func = null;
var globals = self.globals = new Dictionary();
var tw = new TreeWalker(function(node, descend){
- function isModified(node, level) {
- var parent = tw.parent(level);
- if (parent instanceof AST_Unary && (parent.operator === "++" || parent.operator === "--")
- || parent instanceof AST_Assign && parent.left === node
- || parent instanceof AST_Call && parent.expression === node) {
- return true;
- } else if (parent instanceof AST_PropAccess && parent.expression === node) {
- return isModified(parent, level + 1);
- }
- }
-
if (node instanceof AST_Lambda) {
var prev_func = func;
func = node;
@@ -217,21 +206,16 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(options){
node.scope.uses_arguments = true;
}
if (!sym) {
- var g;
if (globals.has(name)) {
- g = globals.get(name);
+ sym = globals.get(name);
} else {
- g = new SymbolDef(self, globals.size(), node);
- g.undeclared = true;
- g.global = true;
- globals.set(name, g);
+ sym = new SymbolDef(self, globals.size(), node);
+ sym.undeclared = true;
+ sym.global = true;
+ globals.set(name, sym);
}
- sym = g;
}
node.thedef = sym;
- if (isModified(node, 0)) {
- sym.modified = true;
- }
node.reference(options);
return true;
}