}
function get_rhs(expr) {
- if (!(candidate instanceof AST_Assign && candidate.operator == "=")) return;
- return candidate.right;
+ return candidate instanceof AST_Assign && candidate.operator == "=" && candidate.right;
}
function get_rvalue(expr) {
return expr[expr instanceof AST_Assign ? "right" : "value"];
}
+ function invariant(expr) {
+ if (expr instanceof AST_Array) return false;
+ if (expr instanceof AST_Binary && lazy_op[expr.operator]) {
+ return invariant(expr.left) && invariant(expr.right);
+ }
+ if (expr instanceof AST_Call) return false;
+ if (expr instanceof AST_Conditional) {
+ return invariant(expr.consequent) && invariant(expr.alternative);
+ }
+ if (expr instanceof AST_Object) return false;
+ return !expr.has_side_effects(compressor);
+ }
+
function foldable(expr) {
if (expr instanceof AST_SymbolRef) {
var value = expr.evaluate(compressor);
return rhs_fuzzy_match(expr.evaluate(compressor), rhs_exact_match);
}
if (!(lhs instanceof AST_SymbolRef)) return false;
- if (expr.has_side_effects(compressor)) return false;
+ if (!invariant(expr)) return false;
var circular;
var def = lhs.definition();
expr.walk(new TreeWalker(function(node) {
// determine if expression has side effects
(function(def) {
- def(AST_Node, return_true);
-
- def(AST_EmptyStatement, return_false);
- def(AST_Constant, return_false);
- def(AST_This, return_false);
-
function any(list, compressor) {
for (var i = list.length; --i >= 0;)
if (list[i].has_side_effects(compressor))
return true;
return false;
}
-
+ def(AST_Node, return_true);
+ def(AST_Array, function(compressor) {
+ return any(this.elements, compressor);
+ });
+ def(AST_Assign, return_true);
+ def(AST_Binary, function(compressor) {
+ return this.left.has_side_effects(compressor)
+ || this.right.has_side_effects(compressor);
+ });
def(AST_Block, function(compressor) {
return any(this.body, compressor);
});
}
return any(this.args, compressor);
});
- def(AST_Switch, function(compressor) {
- return this.expression.has_side_effects(compressor)
- || any(this.body, compressor);
- });
def(AST_Case, function(compressor) {
return this.expression.has_side_effects(compressor)
|| any(this.body, compressor);
});
- def(AST_Try, function(compressor) {
- return any(this.body, compressor)
- || this.bcatch && this.bcatch.has_side_effects(compressor)
- || this.bfinally && this.bfinally.has_side_effects(compressor);
+ def(AST_Conditional, function(compressor) {
+ return this.condition.has_side_effects(compressor)
+ || this.consequent.has_side_effects(compressor)
+ || this.alternative.has_side_effects(compressor);
});
+ def(AST_Constant, return_false);
+ def(AST_Definitions, function(compressor) {
+ return any(this.definitions, compressor);
+ });
+ def(AST_Dot, function(compressor) {
+ return this.expression.may_throw_on_access(compressor)
+ || this.expression.has_side_effects(compressor);
+ });
+ def(AST_EmptyStatement, return_false);
def(AST_If, function(compressor) {
return this.condition.has_side_effects(compressor)
|| this.body && this.body.has_side_effects(compressor)
def(AST_LabeledStatement, function(compressor) {
return this.body.has_side_effects(compressor);
});
- def(AST_SimpleStatement, function(compressor) {
- return this.body.has_side_effects(compressor);
- });
def(AST_Lambda, return_false);
- def(AST_Binary, function(compressor) {
- return this.left.has_side_effects(compressor)
- || this.right.has_side_effects(compressor);
- });
- def(AST_Assign, return_true);
- def(AST_Conditional, function(compressor) {
- return this.condition.has_side_effects(compressor)
- || this.consequent.has_side_effects(compressor)
- || this.alternative.has_side_effects(compressor);
- });
- def(AST_Unary, function(compressor) {
- return unary_side_effects[this.operator]
- || this.expression.has_side_effects(compressor);
- });
- def(AST_SymbolRef, function(compressor) {
- return !this.is_declared(compressor);
- });
- def(AST_SymbolDeclaration, return_false);
def(AST_Object, function(compressor) {
return any(this.properties, compressor);
});
def(AST_ObjectProperty, function(compressor) {
return this.value.has_side_effects(compressor);
});
- def(AST_Array, function(compressor) {
- return any(this.elements, compressor);
- });
- def(AST_Dot, function(compressor) {
- return this.expression.may_throw_on_access(compressor)
- || this.expression.has_side_effects(compressor);
- });
def(AST_Sub, function(compressor) {
return this.expression.may_throw_on_access(compressor)
|| this.expression.has_side_effects(compressor)
def(AST_Sequence, function(compressor) {
return any(this.expressions, compressor);
});
- def(AST_Definitions, function(compressor) {
- return any(this.definitions, compressor);
+ def(AST_SimpleStatement, function(compressor) {
+ return this.body.has_side_effects(compressor);
+ });
+ def(AST_Switch, function(compressor) {
+ return this.expression.has_side_effects(compressor)
+ || any(this.body, compressor);
+ });
+ def(AST_SymbolDeclaration, return_false);
+ def(AST_SymbolRef, function(compressor) {
+ return !this.is_declared(compressor);
+ });
+ def(AST_This, return_false);
+ def(AST_Try, function(compressor) {
+ return any(this.body, compressor)
+ || this.bcatch && this.bcatch.has_side_effects(compressor)
+ || this.bfinally && this.bfinally.has_side_effects(compressor);
+ });
+ def(AST_Unary, function(compressor) {
+ return unary_side_effects[this.operator]
+ || this.expression.has_side_effects(compressor);
});
def(AST_VarDef, function(compressor) {
return this.value;