aboutsummaryrefslog
aboutsummaryrefslogtreecommitdiff ;;; GNU Guix --- Functional package management for GNU ;;; Copyright © 2013 Nikita Karetnikov <nikita@karetnikov.org> ;;; Copyright © 2014, 2015 Mark H Weaver <mhw@netris.org> ;;; Copyright © 2017, 2019 Efraim Flashner <efraim@flashner.co.il> ;;; Copyright © 2022 Paul A. Patience <paul@apatience.com> ;;; Copyright © 2023 B. Wilson <elaexuotee@wilsonb.com> ;;; ;;; This file is part of GNU Guix. ;;; ;;; GNU Guix is free software; you can redistribute it and/or modify it ;;; under the terms of the GNU General Public License as published by ;;; the Free Software Foundation; either version 3 of the License, or (at ;;; your option) any later version. ;;; ;;; GNU Guix is distributed in the hope that it will be useful, but ;;; WITHOUT ANY WARRANTY; without even the implied warranty of ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;;; GNU General Public License for more details. ;;; ;;; You should have received a copy of the GNU General Public License ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>. (define-module (gnu packages apl) #:use-module ((guix licenses) #:prefix license:) #:use-module (guix git-download) #:use-module (guix svn-download) #:use-module (guix gexp) #:use-module (guix packages) #:use-module (guix build-system gnu) #:use-module (guix build-system trivial) #:use-module (gnu packages bash) #:use-module (gnu packages compression) #:use-module (gnu packages fontutils) #:use-module (gnu packages gettext) #:use-module (gnu packages java) #:use-module (gnu packages maths) #:use-module (gnu packages pcre) #:use-module (gnu packages readline) #:use-module (gnu packages sqlite)) (define-public apl (let ((revision 1550)) (package (name "apl") (version (string-append "1.8-r" (number->string revision))) (source (origin (method svn-fetch) (uri (svn-reference (url "svn://svn.savannah.gnu.org/apl/trunk") (revision revision))) (file-name (git-file-name name version)) (sha256 (base32 "1bgc3a09f35zrqq2irhm1hspppnxjqas0fmcw14hkc7910br9ip3")))) (build-system gnu-build-system) (home-page "https://www.gn
-rw-r--r--lib/compress.js42
-rw-r--r--test/compress/collapse_vars.js30
-rw-r--r--test/compress/reduce_vars.js32
3 files changed, 84 insertions, 20 deletions
diff --git a/lib/compress.js b/lib/compress.js
index 31747c92..bdce24bc 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -355,20 +355,18 @@ merge(Compressor.prototype, {
} else {
def.fixed = false;
}
- if (def.init instanceof AST_Defun && !all(def.references, same_defun_scope)) {
+ if (def.init instanceof AST_Defun && !all(def.references, function(ref) {
+ var scope = ref.scope;
+ do {
+ if (def.scope === scope) return true;
+ } while (scope instanceof AST_Function && (scope = scope.parent_scope));
+ })) {
tw.defun_ids[def.id] = undefined;
}
def.recursive_refs = 0;
def.references = [];
def.should_replace = undefined;
def.single_use = undefined;
-
- function same_defun_scope(ref) {
- var scope = ref.scope;
- do {
- if (def.scope === scope) return true;
- } while (scope instanceof AST_Function && (scope = scope.parent_scope));
- }
}
function reset_variables(tw, compressor, scope) {
@@ -384,11 +382,11 @@ merge(Compressor.prototype, {
});
}
- function walk_defun(tw, def) {
- if (def.id in tw.defun_ids) return;
+ function mark_defun(tw, def) {
+ if (def.id in tw.defun_ids) return def.fixed;
if (!tw.in_loop) {
- tw.defun_ids[def.id] = true;
- def.fixed.walk(tw);
+ tw.defun_ids[def.id] = tw.safe_ids;
+ return def.fixed;
} else if (tw.defun_ids[def.id] !== false) {
tw.defun_ids[def.id] = undefined;
}
@@ -397,7 +395,7 @@ merge(Compressor.prototype, {
function walk_defuns(tw, scope) {
scope.functions.each(function(def) {
if (def.init instanceof AST_Defun && tw.defun_ids[def.id] === undefined) {
- tw.defun_ids[def.id] = true;
+ tw.defun_ids[def.id] = tw.safe_ids;
def.init.walk(tw);
}
});
@@ -536,11 +534,14 @@ merge(Compressor.prototype, {
return true;
});
def(AST_Call, function(tw, descend) {
- descend();
var exp = this.expression;
- if (exp instanceof AST_SymbolRef && exp.fixed_value() instanceof AST_Defun) {
- walk_defun(tw, exp.definition());
- }
+ if (!(exp instanceof AST_SymbolRef)) return;
+ var def = exp.definition();
+ if (!(def.fixed instanceof AST_Defun)) return;
+ var defun = mark_defun(tw, def);
+ if (!defun) return;
+ descend();
+ defun.walk(tw);
return true;
});
def(AST_Case, function(tw) {
@@ -570,7 +571,7 @@ merge(Compressor.prototype, {
});
def(AST_Defun, function(tw, descend, compressor) {
var id = this.name.definition().id;
- if (!tw.defun_ids[id]) return true;
+ if (tw.defun_ids[id] !== tw.safe_ids) return true;
tw.defun_ids[id] = false;
this.inlined = false;
push(tw);
@@ -707,7 +708,8 @@ merge(Compressor.prototype, {
var parent;
if (d.fixed instanceof AST_Defun
&& !((parent = tw.parent()) instanceof AST_Call && parent.expression === this)) {
- walk_defun(tw, d);
+ var defun = mark_defun(tw, d);
+ if (defun) defun.walk(tw);
}
});
def(AST_Toplevel, function(tw, descend, compressor) {
@@ -1121,7 +1123,7 @@ merge(Compressor.prototype, {
if (is_lhs(node, multi_replacer.parent())) return node;
def.replaced++;
value_def.replaced--;
- return candidate.value;
+ return candidate.value.clone();
}
// Skip (non-executed) functions and (leading) default case in switch statements
if (node instanceof AST_Default || node instanceof AST_Scope) return node;
diff --git a/test/compress/collapse_vars.js b/test/compress/collapse_vars.js
index 950ebd07..c915041f 100644
--- a/test/compress/collapse_vars.js
+++ b/test/compress/collapse_vars.js
@@ -4056,6 +4056,36 @@ replace_all_var: {
expect_stdout: "PASS"
}
+replace_all_var_scope: {
+ rename = true;
+ options = {
+ collapse_vars: true,
+ unused: true,
+ }
+ mangle = {}
+ input: {
+ var a = 100, b = 10;
+ (function(r, a) {
+ switch (~a) {
+ case (b += a):
+ case a++:
+ }
+ })(--b, a);
+ console.log(a, b);
+ }
+ expect: {
+ var a = 100, b = 10;
+ (function(c, o) {
+ switch (~a) {
+ case (b += a):
+ case o++:
+ }
+ })(--b, a);
+ console.log(a, b);
+ }
+ expect_stdout: "100 109"
+}
+
cascade_statement: {
options = {
collapse_vars: true,
diff --git a/test/compress/reduce_vars.js b/test/compress/reduce_vars.js
index 1d6d1896..14b6a46f 100644
--- a/test/compress/reduce_vars.js
+++ b/test/compress/reduce_vars.js
@@ -5998,3 +5998,35 @@ issue_3113_5: {
"1",
]
}
+
+conditional_nested: {
+ options = {
+ evaluate: true,
+ reduce_vars: true,
+ }
+ input: {
+ var a = 1, b = 0;
+ (function f(c) {
+ function g() {
+ c && (c.a = 0);
+ c && (c.a = 0);
+ c && (c[b++] *= 0);
+ }
+ g(a-- && f(g(c = 42)));
+ })();
+ console.log(b);
+ }
+ expect: {
+ var a = 1, b = 0;
+ (function f(c) {
+ function g() {
+ c && (c.a = 0);
+ c && (c.a = 0);
+ c && (c[b++] *= 0);
+ }
+ g(a-- && f(g(c = 42)));
+ })();
+ console.log(b);
+ }
+ expect_stdout: "2"
+}