[Pkg-javascript-commits] [uglifyjs] 260/491: extend function inlining safety checks (#2430)

Jonas Smedegaard dr at jones.dk
Wed Feb 14 19:51:42 UTC 2018


This is an automated email from the git hooks/post-receive script.

js pushed a commit to annotated tag debian/3.3.10-1
in repository uglifyjs.

commit 5b4b07e9a7d67e593c6ae8d54dc77d174afd25ac
Author: Alex Lam S.L <alexlamsl at gmail.com>
Date:   Sun Nov 5 06:18:45 2017 +0800

    extend function inlining safety checks (#2430)
---
 lib/compress.js              | 46 +++++++++++++++-------
 test/compress/drop-unused.js | 55 ++++++++++++++++++++------
 test/compress/reduce_vars.js | 93 +++++++++++++++++++++++++++++++++++++++++++-
 3 files changed, 167 insertions(+), 27 deletions(-)

diff --git a/lib/compress.js b/lib/compress.js
index ba90f00..454c166 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -319,7 +319,7 @@ merge(Compressor.prototype, {
                             if (value instanceof AST_Lambda) {
                                 d.single_use = d.scope === node.scope
                                         && !(d.orig[0] instanceof AST_SymbolFunarg)
-                                    || value.is_constant_expression();
+                                    || value.is_constant_expression(node.scope);
                             } else {
                                 d.single_use = d.scope === node.scope
                                     && loop_ids[d.id] === in_loop
@@ -385,7 +385,7 @@ merge(Compressor.prototype, {
                         mark(d, true);
                         if (unused && d.references.length == 1) {
                             d.single_use = d.scope === d.references[0].scope
-                                || node.is_constant_expression();
+                                || node.is_constant_expression(d.references[0].scope);
                         }
                     }
                     var save_ids = safe_ids;
@@ -2176,18 +2176,22 @@ merge(Compressor.prototype, {
         }
         def(AST_Node, return_false);
         def(AST_Constant, return_true);
-        def(AST_Lambda, function(){
+        def(AST_Lambda, function(scope){
             var self = this;
             var result = true;
             self.walk(new TreeWalker(function(node) {
                 if (!result) return true;
                 if (node instanceof AST_SymbolRef) {
                     var def = node.definition();
-                    if (self.enclosed.indexOf(def) >= 0
-                        && self.variables.get(def.name) !== def) {
+                    if (member(def, self.enclosed)
+                        && !self.variables.has(def.name)) {
+                        if (scope) {
+                            var scope_def = scope.find_variable(node);
+                            if (def.undeclared ? !scope_def : scope_def === def) return true;
+                        }
                         result = false;
-                        return true;
                     }
+                    return true;
                 }
             }));
             return result;
@@ -2330,7 +2334,10 @@ merge(Compressor.prototype, {
                     });
                     return true;
                 }
-                if (assign_as_unused(node) instanceof AST_SymbolRef && scope === self) {
+                var sym;
+                if (scope === self
+                    && (sym = assign_as_unused(node)) instanceof AST_SymbolRef
+                    && self.variables.get(sym.name) === sym.definition()) {
                     if (node instanceof AST_Assign) node.right.walk(tw);
                     return true;
                 }
@@ -2403,8 +2410,10 @@ merge(Compressor.prototype, {
                     }
                 }
                 if (drop_funcs && node instanceof AST_Defun && node !== self) {
-                    if (!(node.name.definition().id in in_use_ids)) {
+                    var def = node.name.definition();
+                    if (!(def.id in in_use_ids)) {
                         compressor[node.name.unreferenced() ? "warn" : "info"]("Dropping unused function {name} [{file}:{line},{col}]", template(node.name));
+                        drop_decl(def, node.name);
                         return make_node(AST_EmptyStatement, node);
                     }
                     return node;
@@ -2426,7 +2435,7 @@ merge(Compressor.prototype, {
                                 if (var_defs.length > 1 && !def.value) {
                                     compressor.warn("Dropping duplicated definition of variable {name} [{file}:{line},{col}]", template(def.name));
                                     remove(var_defs, def);
-                                    remove(sym.orig, def.name);
+                                    drop_decl(sym, def.name);
                                     return;
                                 }
                             }
@@ -2459,7 +2468,7 @@ merge(Compressor.prototype, {
                             } else {
                                 compressor[def.name.unreferenced() ? "warn" : "info"]("Dropping unused variable {name} [{file}:{line},{col}]", template(def.name));
                             }
-                            remove(sym.orig, def.name);
+                            drop_decl(sym, def.name);
                         }
                     });
                     if (head.length == 0 && tail.length == 1 && tail[0].name instanceof AST_SymbolVar) {
@@ -2468,7 +2477,7 @@ merge(Compressor.prototype, {
                             var def = tail.pop();
                             compressor.warn("Converting duplicated definition of variable {name} to assignment [{file}:{line},{col}]", template(def.name));
                             remove(var_defs, def);
-                            remove(def.name.definition().orig, def.name);
+                            drop_decl(def.name.definition(), def.name);
                             side_effects.unshift(make_node(AST_Assign, def, {
                                 operator: "=",
                                 left: make_node(AST_SymbolRef, def.name, def.name),
@@ -2497,10 +2506,9 @@ merge(Compressor.prototype, {
                     }
                 }
                 if (drop_vars) {
-                    var def = assign_as_unused(node);
-                    if (def instanceof AST_SymbolRef
-                        && !((def = def.definition()).id in in_use_ids)
-                        && self.variables.get(def.name) === def) {
+                    var sym = assign_as_unused(node);
+                    if (sym instanceof AST_SymbolRef
+                        && !(sym.definition().id in in_use_ids)) {
                         if (node instanceof AST_Assign) {
                             return maintain_this_binding(parent, node, node.right.transform(tt));
                         }
@@ -2551,6 +2559,14 @@ merge(Compressor.prototype, {
                         col  : sym.start.col
                     };
                 }
+
+                function drop_decl(def, decl) {
+                    remove(def.orig, decl);
+                    if (!def.orig.length) {
+                        def.scope.functions.del(def.name);
+                        def.scope.variables.del(def.name);
+                    }
+                }
             }
         );
         self.transform(tt);
diff --git a/test/compress/drop-unused.js b/test/compress/drop-unused.js
index 4ce8d2e..af6a7a4 100644
--- a/test/compress/drop-unused.js
+++ b/test/compress/drop-unused.js
@@ -1109,11 +1109,11 @@ var_catch_toplevel: {
     }
 }
 
-issue_2105: {
+issue_2105_1: {
     options = {
         collapse_vars: true,
         inline: true,
-        passes: 3,
+        passes: 2,
         reduce_vars: true,
         side_effects: true,
         unused: true,
@@ -1139,17 +1139,50 @@ issue_2105: {
         });
     }
     expect: {
-        (function() {
-            var quux = function() {
+        ({
+            prop: function() {
+                console.log;
                 console.log("PASS");
-            };
-            return {
-                prop: function() {
-                    console.log;
-                    quux();
+            }
+        }).prop();
+    }
+    expect_stdout: "PASS"
+}
+
+issue_2105_2: {
+    options = {
+        collapse_vars: true,
+        inline: true,
+        passes: 2,
+        properties: true,
+        pure_getters: "strict",
+        reduce_vars: true,
+        side_effects: true,
+        unsafe: true,
+        unused: true,
+    }
+    input: {
+        !function(factory) {
+            factory();
+        }( function() {
+            return function(fn) {
+                fn()().prop();
+            }( function() {
+                function bar() {
+                    var quux = function() {
+                        console.log("PASS");
+                    }, foo = function() {
+                        console.log;
+                        quux();
+                    };
+                    return { prop: foo };
                 }
-            };
-        })().prop();
+                return bar;
+            } );
+        });
+    }
+    expect: {
+        console.log("PASS");
     }
     expect_stdout: "PASS"
 }
diff --git a/test/compress/reduce_vars.js b/test/compress/reduce_vars.js
index a18d425..7714ad5 100644
--- a/test/compress/reduce_vars.js
+++ b/test/compress/reduce_vars.js
@@ -3397,6 +3397,10 @@ issue_2423_1: {
         p();
         p();
     }
+    expect_stdout: [
+        "1",
+        "1",
+    ]
 }
 
 issue_2423_2: {
@@ -3417,6 +3421,10 @@ issue_2423_2: {
         p();
         p();
     }
+    expect_stdout: [
+        "1",
+        "1",
+    ]
 }
 
 issue_2423_3: {
@@ -3433,12 +3441,14 @@ issue_2423_3: {
     expect: {
         (function() { console.log(function() { return 1; }()); })();
     }
+    expect_stdout: "1"
 }
 
 issue_2423_4: {
     options = {
         inline: true,
         reduce_vars: true,
+        side_effects: true,
         toplevel: true,
         unused: true,
     }
@@ -3448,6 +3458,87 @@ issue_2423_4: {
         p();
     }
     expect: {
-        void console.log(1);
+        console.log(1);
+    }
+    expect_stdout: "1"
+}
+
+issue_2423_5: {
+    options = {
+        inline: true,
+        passes: 2,
+        reduce_vars: true,
+        side_effects: true,
+        toplevel: true,
+        unused: true,
+    }
+    input: {
+        function x() {
+            y();
+        }
+        function y() {
+            console.log(1);
+        }
+        function z() {
+            function y() {
+                console.log(2);
+            }
+            x();
+        }
+        z();
+        z();
+    }
+    expect: {
+        function z() {
+            console.log(1);
+        }
+        z();
+        z();
     }
+    expect_stdout: [
+        "1",
+        "1",
+    ]
+}
+
+issue_2423_6: {
+    options = {
+        inline: true,
+        passes: 2,
+        reduce_vars: true,
+        side_effects: true,
+        toplevel: true,
+        unused: true,
+    }
+    input: {
+        function x() {
+            y();
+        }
+        function y() {
+            console.log(1);
+        }
+        function z() {
+            function y() {
+                console.log(2);
+            }
+            x();
+            y();
+        }
+        z();
+        z();
+    }
+    expect: {
+        function z(){
+            console.log(1);
+            console.log(2);
+        }
+        z();
+        z();
+    }
+    expect_stdout: [
+        "1",
+        "2",
+        "1",
+        "2",
+    ]
 }

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/uglifyjs.git



More information about the Pkg-javascript-commits mailing list