[Pkg-javascript-commits] [uglifyjs] 116/491: enhance `unsafe` `evaluate` (#2037)

Jonas Smedegaard dr at jones.dk
Wed Feb 14 19:51:27 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 17e73121fa641c14fd9f4cdd3310bf3e8ec037d0
Author: Alex Lam S.L <alexlamsl at gmail.com>
Date:   Thu Jun 1 00:56:28 2017 +0800

    enhance `unsafe` `evaluate` (#2037)
---
 lib/compress.js           | 25 +++++++++++++++++++++++++
 test/compress/evaluate.js | 28 ++++++++++++++++++++++++++++
 2 files changed, 53 insertions(+)

diff --git a/lib/compress.js b/lib/compress.js
index 32a4d60..a928d7b 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -1665,6 +1665,26 @@ merge(Compressor.prototype, {
             }
             throw def;
         });
+        def(AST_Call, function(compressor){
+            var exp = this.expression;
+            if (compressor.option("unsafe") && exp instanceof AST_PropAccess) {
+                var key = exp.property;
+                if (key instanceof AST_Node) {
+                    key = ev(key, compressor);
+                }
+                var val = ev(exp.expression, compressor);
+                var fn = val[key];
+                if (typeof fn == "function") {
+                    return fn.apply(val, this.args.map(function(arg) {
+                        return ev(arg, compressor);
+                    }));
+                }
+            }
+            throw def;
+        });
+        def(AST_New, function(compressor){
+            throw def;
+        });
     })(function(node, func){
         node.DEFMETHOD("_eval", func);
     });
@@ -3144,6 +3164,11 @@ merge(Compressor.prototype, {
             && is_iife_call(self)) {
             return self.negate(compressor, true);
         }
+        var ev = self.evaluate(compressor);
+        if (ev !== self) {
+            ev = make_node_from_constant(ev, self).optimize(compressor);
+            return best_of(compressor, ev, self);
+        }
         return self;
     });
 
diff --git a/test/compress/evaluate.js b/test/compress/evaluate.js
index a1e3d0b..020d7cf 100644
--- a/test/compress/evaluate.js
+++ b/test/compress/evaluate.js
@@ -1037,3 +1037,31 @@ issue_1964_2: {
     }
     expect_stdout: "b"
 }
+
+array_slice_index: {
+    options = {
+        evaluate: true,
+        unsafe: true,
+    }
+    input: {
+        console.log([1,2,3].slice(1)[1]);
+    }
+    expect: {
+        console.log(3);
+    }
+    expect_stdout: "3"
+}
+
+string_charCodeAt: {
+    options = {
+        evaluate: true,
+        unsafe: true,
+    }
+    input: {
+        console.log("foo".charCodeAt("bar".length));
+    }
+    expect: {
+        console.log(NaN);
+    }
+    expect_stdout: "NaN"
+}

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