From 3a01d269799768f4614e94063eceb699fdc83d2d Mon Sep 17 00:00:00 2001 From: alexlamsl Date: Sat, 13 Jun 2020 20:55:08 +0800 Subject: [PATCH] enhance `evaluate` --- lib/compress.js | 36 ++++++++++++++++++------------------ test/compress/evaluate.js | 33 +++++++++++++++++++++++++++++++++ test/ufuzz/index.js | 4 ++++ 3 files changed, 55 insertions(+), 18 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index a3a4a63d22a..6106d37dc56 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -1485,7 +1485,7 @@ merge(Compressor.prototype, { } function is_last_node(node, parent) { - if (node.TYPE == "Binary") return node.operator == "in" && !is_object(node.right.tail_node()); + if (node.TYPE == "Binary") return node.operator == "in" && !is_object(node.right); if (node instanceof AST_Call) { var def, fn = node.expression; if (fn instanceof AST_SymbolRef) { @@ -3383,16 +3383,11 @@ merge(Compressor.prototype, { for (var i = 0; i < this.properties.length; i++) { var prop = this.properties[i]; var key = prop.key; - if (key instanceof AST_Symbol) { - key = key.name; - } else if (key instanceof AST_Node) { - key = key._eval(compressor, ignore_side_effects, cached, depth); - if (key === prop.key) return this; - } - if (typeof Object.prototype[key] === "function") { - return this; + if (key instanceof AST_Symbol) key = key.name; + if (prop.value instanceof AST_Function) { + if (typeof Object.prototype[key] == "function") return this; + continue; } - if (prop.value instanceof AST_Function) continue; val[key] = prop.value._eval(compressor, ignore_side_effects, cached, depth); if (val[key] === prop.value) return this; } @@ -3479,10 +3474,10 @@ merge(Compressor.prototype, { case "&" : result = left & right; break; case "^" : result = left ^ right; break; case "+" : result = left + right; break; + case "-" : result = left - right; break; case "*" : result = left * right; break; case "/" : result = left / right; break; case "%" : result = left % right; break; - case "-" : result = left - right; break; case "<<" : result = left << right; break; case ">>" : result = left >> right; break; case ">>>": result = left >>> right; break; @@ -3494,7 +3489,13 @@ merge(Compressor.prototype, { case "<=" : result = left <= right; break; case ">" : result = left > right; break; case ">=" : result = left >= right; break; - default : return this; + case "in": + if (right && typeof right == "object" && HOP(right, left)) { + result = true; + break; + } + default: + return this; } if (isNaN(result)) return compressor.find_parent(AST_With) ? this : result; if (compressor.option("unsafe_math") @@ -3848,7 +3849,7 @@ merge(Compressor.prototype, { def(AST_Binary, function(compressor) { return this.left.has_side_effects(compressor) || this.right.has_side_effects(compressor) - || this.operator == "in" && !is_object(this.right.tail_node()); + || this.operator == "in" && !is_object(this.right); }); def(AST_Block, function(compressor) { return any(this.body, compressor); @@ -3961,7 +3962,7 @@ merge(Compressor.prototype, { def(AST_Binary, function(compressor) { return this.left.may_throw(compressor) || this.right.may_throw(compressor) - || this.operator == "in" && !is_object(this.right.tail_node()); + || this.operator == "in" && !is_object(this.right); }); def(AST_Block, function(compressor) { return any(this.body, compressor); @@ -4056,7 +4057,7 @@ merge(Compressor.prototype, { def(AST_Binary, function() { return this.left.is_constant_expression() && this.right.is_constant_expression() - && (this.operator != "in" || is_object(this.right.tail_node())); + && (this.operator != "in" || is_object(this.right)); }); def(AST_Constant, return_true); def(AST_Lambda, function(scope) { @@ -5162,7 +5163,7 @@ merge(Compressor.prototype, { return this; }); def(AST_Binary, function(compressor, first_in_statement) { - if (this.operator == "in" && !is_object(this.right.tail_node())) { + if (this.operator == "in" && !is_object(this.right)) { var left = this.left.drop_side_effect_free(compressor, first_in_statement); if (left === this.left) return this; var node = this.clone(); @@ -6920,10 +6921,9 @@ merge(Compressor.prototype, { var indexFns = makePredicate("indexOf lastIndexOf"); var commutativeOperators = makePredicate("== === != !== * & | ^"); function is_object(node) { - while (node instanceof AST_SymbolRef) { + while ((node = node.tail_node()) instanceof AST_SymbolRef) { node = node.fixed_value(); if (!node) return false; - node = node.tail_node(); } return node instanceof AST_Array || node instanceof AST_Lambda diff --git a/test/compress/evaluate.js b/test/compress/evaluate.js index da928b3cc41..29ba09da876 100644 --- a/test/compress/evaluate.js +++ b/test/compress/evaluate.js @@ -2759,3 +2759,36 @@ issue_3988: { } expect_stdout: "0" } + +operator_in: { + options = { + evaluate: true, + unsafe: true, + } + input: { + Object.prototype.PASS = 0; + console.log(0 in [ 1 ]); + console.log(0 / 0 in { NaN: 2 }); + console.log("PASS" in { }); + console.log("FAIL" in { }); + console.log("toString" in { }); + console.log("toString" in { toString: 3 }); + } + expect: { + Object.prototype.PASS = 0; + console.log(true); + console.log(true); + console.log("PASS" in { }); + console.log("FAIL" in { }); + console.log("toString" in { }); + console.log(true); + } + expect_stdout: [ + "true", + "true", + "true", + "false", + "true", + "true", + ] +} diff --git a/test/ufuzz/index.js b/test/ufuzz/index.js index 006b4ca7590..1a8eaff55ef 100644 --- a/test/ufuzz/index.js +++ b/test/ufuzz/index.js @@ -767,6 +767,10 @@ function _createExpression(recurmax, noComma, stmtDepth, canThrow) { return createArrayLiteral(recurmax, stmtDepth, canThrow) + "." + getDotKey(); case p++: return createObjectLiteral(recurmax, stmtDepth, canThrow) + "." + getDotKey(); + case p++: + return createValue() + " in " + createArrayLiteral(recurmax, stmtDepth, canThrow); + case p++: + return createValue() + " in " + createObjectLiteral(recurmax, stmtDepth, canThrow); case p++: var name = getVarName(); var s = name + "[" + createExpression(recurmax, COMMA_OK, stmtDepth, canThrow) + "]";