From c927cea6322788b654128f9bbbf3a06441b964c2 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Sat, 21 Oct 2017 04:08:26 +0800 Subject: [PATCH] `unsafe` fix-ups for #2351 (#2379) --- lib/compress.js | 55 +++++++++++++++++-------------- test/compress/reduce_vars.js | 64 ++++++++++++++++++++++++++++++++++-- 2 files changed, 92 insertions(+), 27 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index 374456ac..1f58b390 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -151,7 +151,7 @@ merge(Compressor.prototype, { var last_count = 1 / 0; for (var pass = 0; pass < passes; pass++) { if (pass > 0 || this.option("reduce_vars")) - node.reset_opt_flags(this, true); + node.reset_opt_flags(this); node = node.transform(this); if (passes > 1) { var count = 0; @@ -283,8 +283,9 @@ merge(Compressor.prototype, { self.transform(tt); }); - AST_Node.DEFMETHOD("reset_opt_flags", function(compressor, rescan) { - var reduce_vars = rescan && compressor.option("reduce_vars"); + AST_Node.DEFMETHOD("reset_opt_flags", function(compressor) { + var reduce_vars = compressor.option("reduce_vars"); + var unused = compressor.option("unused"); // Stack of look-up tables to keep track of whether a `SymbolDef` has been // properly assigned before use: // - `push()` & `pop()` when visiting conditional branches @@ -308,16 +309,31 @@ merge(Compressor.prototype, { if (node instanceof AST_SymbolRef) { var d = node.definition(); d.references.push(node); - if (d.fixed === undefined || !safe_to_read(d) - || is_modified(node, 0, is_immutable(node))) { + if (d.fixed === undefined || !safe_to_read(d) || d.single_use == "m") { d.fixed = false; } else { - var parent = tw.parent(); - if (parent instanceof AST_Assign && parent.operator == "=" && node === parent.right - || parent instanceof AST_Call && node !== parent.expression - || parent instanceof AST_Return && node === parent.value && node.scope !== d.scope - || parent instanceof AST_VarDef && node === parent.value) { - d.escaped = true; + var value = node.fixed_value(); + if (unused) { + d.single_use = value + && d.references.length == 1 + && loop_ids[d.id] === in_loop + && d.scope === node.scope + && value.is_constant_expression(); + } + if (is_modified(node, 0, is_immutable(value))) { + if (d.single_use) { + d.single_use = "m"; + } else { + d.fixed = false; + } + } else { + var parent = tw.parent(); + if (parent instanceof AST_Assign && parent.operator == "=" && node === parent.right + || parent instanceof AST_Call && node !== parent.expression + || parent instanceof AST_Return && node === parent.value && node.scope !== d.scope + || parent instanceof AST_VarDef && node === parent.value) { + d.escaped = true; + } } } } @@ -550,20 +566,8 @@ merge(Compressor.prototype, { def.single_use = undefined; } - function is_immutable(node) { - var value = node.fixed_value(); - if (!value) return false; - if (value.is_constant()) return true; - if (compressor.option("unused")) { - var d = node.definition(); - if (d.single_use === undefined) { - d.single_use = loop_ids[d.id] === in_loop - && d.scope === node.scope - && value.is_constant_expression(); - } - if (d.references.length == 1 && d.single_use) return true; - } - return value instanceof AST_Lambda; + function is_immutable(value) { + return value && (value.is_constant() || value instanceof AST_Lambda); } function is_modified(node, level, immutable) { @@ -4107,6 +4111,7 @@ merge(Compressor.prototype, { d.fixed = fixed = make_node(AST_Function, fixed, fixed); } if (compressor.option("unused") + && fixed && d.references.length == 1 && (d.single_use || fixed instanceof AST_Function && !(d.scope.uses_arguments && d.orig[0] instanceof AST_SymbolFunarg) diff --git a/test/compress/reduce_vars.js b/test/compress/reduce_vars.js index d93f0cd1..a03bc1c8 100644 --- a/test/compress/reduce_vars.js +++ b/test/compress/reduce_vars.js @@ -1190,10 +1190,10 @@ defun_label: { !function() { console.log(function(a) { L: { - if (a) break L; + if (2) break L; return 1; } - }(2)); + }()); }(); } expect_stdout: true @@ -2894,3 +2894,63 @@ array_forin_2: { } expect_stdout: "3" } + +const_expr_1: { + options = { + evaluate: true, + reduce_vars: true, + toplevel: true, + unsafe: true, + unused: true, + } + input: { + var o = { + a: 1, + b: 2 + }; + o.a++; + console.log(o.a, o.b); + } + expect: { + var o = { + a: 1, + b: 2 + }; + o.a++; + console.log(o.a, o.b); + } + expect_stdout: "2 2" +} + +const_expr_2: { + options = { + evaluate: true, + reduce_vars: true, + toplevel: true, + unsafe: true, + unused: true, + } + input: { + Object.prototype.c = function() { + this.a++; + }; + var o = { + a: 1, + b: 2 + }; + o.c(); + console.log(o.a, o.b); + } + expect: { + Object.prototype.c = function() { + this.a++; + }; + var o = { + a: 1, + b: 2 + }; + o.c(); + console.log(o.a, o.b); + } + expect_stdout: "2 2" +} -- 2.34.1