diff options
-rw-r--r-- | lib/compress.js | 18 | ||||
-rw-r--r-- | test/compress/collapse_vars.js | 73 |
2 files changed, 89 insertions, 2 deletions
diff --git a/lib/compress.js b/lib/compress.js index a161b457..50843f6b 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -914,6 +914,7 @@ merge(Compressor.prototype, { function tighten_body(statements, compressor) { var scope = compressor.find_parent(AST_Scope); + var in_loop = is_in_loop(); var CHANGED, max_iter = 10; do { CHANGED = false; @@ -936,6 +937,14 @@ merge(Compressor.prototype, { } } while (CHANGED && max_iter-- > 0); + function is_in_loop() { + for (var node, level = 0; node = compressor.parent(level); level++) { + if (node instanceof AST_IterationStatement) return true; + if (node instanceof AST_Scope) break; + } + return false; + } + // Search from right to left for assignment-like expressions: // - `var a = x;` // - `a = x;` @@ -1096,9 +1105,9 @@ merge(Compressor.prototype, { var stop_if_hit = null; var lhs = get_lhs(candidate); if (!lhs || is_lhs_read_only(lhs) || lhs.has_side_effects(compressor)) continue; - var lhs_local = is_lhs_local(lhs); // Locate symbols which may execute code outside of scanning range var lvalues = get_lvalues(candidate); + var lhs_local = is_lhs_local(lhs); if (lhs instanceof AST_SymbolRef) lvalues[lhs.name] = false; var side_effects = value_has_side_effects(candidate); var replace_all = replace_all_symbols(); @@ -1393,7 +1402,12 @@ merge(Compressor.prototype, { function is_lhs_local(lhs) { while (lhs instanceof AST_PropAccess) lhs = lhs.expression; - return lhs instanceof AST_SymbolRef && lhs.definition().scope === scope; + return lhs instanceof AST_SymbolRef + && lhs.definition().scope === scope + && !(in_loop + && (lhs.name in lvalues + || candidate instanceof AST_Unary + || candidate instanceof AST_Assign && candidate.operator != "=")); } function value_has_side_effects(expr) { diff --git a/test/compress/collapse_vars.js b/test/compress/collapse_vars.js index 2c7bbdeb..bcb9cb91 100644 --- a/test/compress/collapse_vars.js +++ b/test/compress/collapse_vars.js @@ -4546,3 +4546,76 @@ issue_2908: { } expect_stdout: "2" } + +issue_2914_1: { + options = { + collapse_vars: true, + } + input: { + function read(input) { + var i = 0; + var e = 0; + var t = 0; + while (e < 32) { + var n = input[i++]; + t |= (127 & n) << e; + if (0 === (128 & n)) + return t; + e += 7; + } + } + console.log(read([129])); + } + expect: { + function read(input) { + var i = 0; + var e = 0; + var t = 0; + while (e < 32) { + var n = input[i++]; + t |= (127 & n) << e; + if (0 === (128 & n)) + return t; + e += 7; + } + } + console.log(read([129])); + } + expect_stdout: "1" +} + +issue_2914_2: { + options = { + collapse_vars: true, + } + input: { + function read(input) { + var i = 0; + var e = 0; + var t = 0; + while (e < 32) { + var n = input[i++]; + t = (127 & n) << e; + if (0 === (128 & n)) + return t; + e += 7; + } + } + console.log(read([129])); + } + expect: { + function read(input) { + var i = 0; + var e = 0; + var t = 0; + while (e < 32) { + var n = input[i++]; + if (0 === (128 & n)) + return t = (127 & n) << e; + e += 7; + } + } + console.log(read([129])); + } + expect_stdout: "0" +} |