[Pkg-javascript-commits] [uglifyjs] 346/491: compress `apply()` & `call()` of `function` (#2613)

Jonas Smedegaard dr at jones.dk
Wed Feb 14 19:51:51 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 8ddcbc39e617a3ce53a340303fd9ef3226ee0065
Author: Alex Lam S.L <alexlamsl at gmail.com>
Date:   Mon Dec 18 16:23:39 2017 +0800

    compress `apply()` & `call()` of `function` (#2613)
    
    - `fn.apply(a, [ ... ])` => `fn.call(a, ...)`
    - `fn.call(a, ... )` => `a, fn(...)`
    
    where `fn` can be `function` literal or symbol reference linked through `reduce_vars`
---
 lib/compress.js            |  28 ++++++++++
 test/compress/functions.js | 126 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 154 insertions(+)

diff --git a/lib/compress.js b/lib/compress.js
index 735b4d2..af1195d 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -3857,6 +3857,34 @@ merge(Compressor.prototype, {
                     }
                 }
                 break;
+              case "apply":
+                if (self.args.length == 2 && self.args[1] instanceof AST_Array) {
+                    var args = self.args[1].elements.slice();
+                    args.unshift(self.args[0]);
+                    return make_node(AST_Call, self, {
+                        expression: make_node(AST_Dot, exp, {
+                            expression: exp.expression,
+                            property: "call"
+                        }),
+                        args: args
+                    }).optimize(compressor);
+                }
+                break;
+              case "call":
+                var func = exp.expression;
+                if (func instanceof AST_SymbolRef) {
+                    func = func.fixed_value();
+                }
+                if (func instanceof AST_Function && !func.contains_this()) {
+                    return make_sequence(this, [
+                        self.args[0],
+                        make_node(AST_Call, self, {
+                            expression: exp.expression,
+                            args: self.args.slice(1)
+                        })
+                    ]).optimize(compressor);
+                }
+                break;
             }
         }
         if (compressor.option("unsafe_Func")
diff --git a/test/compress/functions.js b/test/compress/functions.js
index 5f7fba6..23ed22d 100644
--- a/test/compress/functions.js
+++ b/test/compress/functions.js
@@ -923,3 +923,129 @@ issue_2604_2: {
     }
     expect_stdout: "PASS"
 }
+
+unsafe_apply_1: {
+    options = {
+        inline: true,
+        passes: 2,
+        reduce_vars: true,
+        side_effects: true,
+        unsafe: true,
+        unused: true,
+    }
+    input: {
+        (function(a, b) {
+            console.log(a, b);
+        }).apply("foo", [ "bar" ]);
+        (function(a, b) {
+            console.log(this, a, b);
+        }).apply("foo", [ "bar" ]);
+        (function(a, b) {
+            console.log(a, b);
+        }).apply("foo", [ "bar" ], "baz");
+    }
+    expect: {
+        console.log("bar", void 0);
+        (function(a, b) {
+            console.log(this, a, b);
+        }).call("foo", "bar");
+        (function(a, b) {
+            console.log(a, b);
+        }).apply("foo", [ "bar" ], "baz");
+    }
+    expect_stdout: true
+}
+
+unsafe_apply_2: {
+    options = {
+        reduce_vars: true,
+        side_effects: true,
+        toplevel: true,
+        unsafe: true,
+    }
+    input: {
+        function foo() {
+            console.log(a, b);
+        }
+        var bar = function(a, b) {
+            console.log(this, a, b);
+        }
+        (function() {
+            foo.apply("foo", [ "bar" ]);
+            bar.apply("foo", [ "bar" ]);
+        })();
+    }
+    expect: {
+        function foo() {
+            console.log(a, b);
+        }
+        var bar = function(a, b) {
+            console.log(this, a, b);
+        }
+        (function() {
+            foo("bar");
+            bar.call("foo", "bar");
+        })();
+    }
+    expect_stdout: true
+}
+
+unsafe_call_1: {
+    options = {
+        inline: true,
+        passes: 2,
+        reduce_vars: true,
+        side_effects: true,
+        unsafe: true,
+        unused: true,
+    }
+    input: {
+        (function(a, b) {
+            console.log(a, b);
+        }).call("foo", "bar");
+        (function(a, b) {
+            console.log(this, a, b);
+        }).call("foo", "bar");
+    }
+    expect: {
+        console.log("bar", void 0);
+        (function(a, b) {
+            console.log(this, a, b);
+        }).call("foo", "bar");
+    }
+    expect_stdout: true
+}
+
+unsafe_call_2: {
+    options = {
+        reduce_vars: true,
+        side_effects: true,
+        toplevel: true,
+        unsafe: true,
+    }
+    input: {
+        function foo() {
+            console.log(a, b);
+        }
+        var bar = function(a, b) {
+            console.log(this, a, b);
+        }
+        (function() {
+            foo.call("foo", "bar");
+            bar.call("foo", "bar");
+        })();
+    }
+    expect: {
+        function foo() {
+            console.log(a, b);
+        }
+        var bar = function(a, b) {
+            console.log(this, a, b);
+        }
+        (function() {
+            foo("bar");
+            bar.call("foo", "bar");
+        })();
+    }
+    expect_stdout: true
+}

-- 
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