aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/compress.js32
-rw-r--r--test/compress/collapse_vars.js29
2 files changed, 51 insertions, 10 deletions
diff --git a/lib/compress.js b/lib/compress.js
index 2b796dcc..ad691f7a 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -972,13 +972,13 @@ merge(Compressor.prototype, {
|| node instanceof AST_Try
|| node instanceof AST_With
|| parent instanceof AST_For && node !== parent.init
- || (side_effects || !replace_all)
+ || !replace_all
&& (node instanceof AST_SymbolRef && !node.is_declared(compressor))) {
abort = true;
return node;
}
// Stop only if candidate is found within conditional branches
- if (!stop_if_hit && (side_effects || !replace_all)
+ if (!stop_if_hit && (!lhs_local || !replace_all)
&& (parent instanceof AST_Binary && lazy_op(parent.operator) && parent.left !== node
|| parent instanceof AST_Conditional && parent.condition !== node
|| parent instanceof AST_If && parent.condition !== node)) {
@@ -1096,17 +1096,12 @@ 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);
if (lhs instanceof AST_SymbolRef) lvalues[lhs.name] = false;
- var replace_all = value_def;
- if (!replace_all && lhs instanceof AST_SymbolRef) {
- var def = lhs.definition();
- if (def.references.length - def.replaced == (candidate instanceof AST_VarDef ? 1 : 2)) {
- replace_all = true;
- }
- }
var side_effects = value_has_side_effects(candidate);
+ var replace_all = replace_all_symbols();
var may_throw = candidate.may_throw(compressor);
var funarg = candidate.name instanceof AST_SymbolFunarg;
var hit = funarg;
@@ -1151,7 +1146,7 @@ merge(Compressor.prototype, {
hit_index++;
}
branch.expression = branch.expression.transform(scanner);
- if (side_effects || !replace_all) break;
+ if (!replace_all) break;
}
}
abort = true;
@@ -1393,11 +1388,28 @@ 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;
+ }
+
function value_has_side_effects(expr) {
if (expr instanceof AST_Unary) return false;
return get_rvalue(expr).has_side_effects(compressor);
}
+ function replace_all_symbols() {
+ if (side_effects) return false;
+ if (value_def) return true;
+ if (lhs instanceof AST_SymbolRef) {
+ var def = lhs.definition();
+ if (def.references.length - def.replaced == (candidate instanceof AST_VarDef ? 1 : 2)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
function may_modify(sym) {
var def = sym.definition();
if (def.orig.length == 1 && def.orig[0] instanceof AST_SymbolDefun) return false;
diff --git a/test/compress/collapse_vars.js b/test/compress/collapse_vars.js
index ed1d8d97..2c7bbdeb 100644
--- a/test/compress/collapse_vars.js
+++ b/test/compress/collapse_vars.js
@@ -4517,3 +4517,32 @@ issue_2891_2: {
}
expect_stdout: true
}
+
+issue_2908: {
+ options = {
+ collapse_vars: true,
+ }
+ input: {
+ var a = 0, b = 0;
+ function f(c) {
+ if (1 == c) return;
+ a++;
+ if (2 == c) b = a;
+ }
+ f(0);
+ f(2);
+ console.log(b);
+ }
+ expect: {
+ var a = 0, b = 0;
+ function f(c) {
+ if (1 == c) return;
+ a++;
+ if (2 == c) b = a;
+ }
+ f(0);
+ f(2);
+ console.log(b);
+ }
+ expect_stdout: "2"
+}
adjust caller. * gnu/installer/timezone.scm (locate-childrens): Rename to… (locate-children): …this. Adjust all callers. Tobias Geerinckx-Rice 2019-01-17installer: "formating" → "formatting"....* gnu/installer/newt/partition.scm, gnu/installer/parted.scm: Replace "formating" with "formatting". Ludovic Courtès 2019-01-17installer: parted: Do not call BLKRRPART on loop devices....* gnu/installer/parted.scm (with-delay-device-in-use?): Return immediately if the file-name passed as argument designates a loop device. Mathieu Othacehe 2019-01-17installer: Various renamins follow-up....s/path/file and s/crypt/encrypt. * gnu/installer/newt/partition.scm: Apply renamings. * gnu/installer/parted.scm: Ditto. Mathieu Othacehe 2019-01-17installer: Various renamings....1. s/partitionment/partitioning/ 2. s/crypted/encrypted/ * gnu/installer.scm (installer-steps): Apply renamings. * gnu/installer/newt/partition.scm (run-disk-page): ditto, * gnu/installer/parted.scm (auto-partition): ditto, (luks-format-and-open): ditto, (luks-close): ditto, (user-partitions->configuration): ditto. Mathieu Othacehe 2019-01-17installer: parted: Use read-luks-partition-uuid instead of...find-partition-by-luks-uuid. * gnu/installer/parted.scm (user-partition->mapped-device): Replace read-luks-partition-uuid by find-partition-by-luks-uuid, (user-partition->file-system): only compute uuid if the partition is not encrypted. Mathieu Othacehe 2019-01-17installer: partionment: Add encryption support....* gnu/installer.scm (set-installer-path): Add cryptsetup. * gnu/installer/newt/partition.scm (prompt-luks-passwords): New procedure, (run-partioning-page): Add the possibility to set encryption to "On" on a partition and choose a label, add a new partition scheme: "Guided - using the entire disk with encryption", prompt for encryption passwords before proceeding to formating. * gnu/installer/parted.scm (<user-partition>)[crypt-label], [crypt-password]: New fields, (partition-description): add the encryption label, (user-partition-description): add an encryption field, (auto-partition): add two partitioning schemes: entire-crypted-root and entire-crypted-root-home, (call-with-luks-key-file): new procedure, (user-partition-upper-path): new procedure, (luks-format-and-open): new procedure, (luks-close): new procedure, (format-user-partitions): format and open luks partitions before creating file-system. (mount-user-partitions): use the path returned by user-partition-upper-path, (umount-user-partitions): close the luks partitions, (user-partition->file-system): set device field to label for luks partitions and to uuid for the rest, (user-partition->mapped-device): new procedure, (user-partitions->configuration): add mapped-devices field. Mathieu Othacehe 2019-01-17installer: partition: Fix swaping and use syscalls....* gnu/installer/parted.scm (start-swaping): Remove it, (stop-swaping): Remove it, (start-swapping): New procedure using swapon syscall, (stop-swapping): New procedure using swapoff syscall, (with-mounted-partitions): Use previous start-swapping and stop-swapping procedures. Mathieu Othacehe 2019-01-17installer: Add partitioning support....* gnu/installer.scm (installer-steps): Add partitioning step. * gnu/installer/newt.scm (newt-installer): Add partition-page field. * gnu/installer/newt/partition.scm: New file. * gnu/installer/parted.scm: New file. * gnu/installer/record (installer): New partition-page field. * gnu/local.mk (GNU_SYSTEM_MODULES): Add new files. * po/guix/POTFILES.in: Add new files. Mathieu Othacehe