[Pkg-javascript-commits] [uglifyjs] 177/228: optimize try-catch-finally (#1731)

Jonas Smedegaard dr at jones.dk
Sat Apr 15 14:25:28 UTC 2017


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

js pushed a commit to branch master
in repository uglifyjs.

commit 7bea38a05dbe357434001fe59dbe06bb659a585f
Author: Alex Lam S.L <alexlamsl at gmail.com>
Date:   Thu Mar 30 12:16:58 2017 +0800

    optimize try-catch-finally (#1731)
    
    - eliminate empty blocks
    - flatten out if try-block does not throw
---
 lib/compress.js              |  9 +++++++++
 test/compress/dead-code.js   | 42 ++++++++++++++++++++++++++++++++++++++++++
 test/compress/drop-unused.js | 32 ++++++++++++++++++++++++--------
 test/compress/issue-1673.js  |  2 ++
 test/compress/screw-ie8.js   |  8 ++++----
 5 files changed, 81 insertions(+), 12 deletions(-)

diff --git a/lib/compress.js b/lib/compress.js
index e36ff89..be76015 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -2606,6 +2606,15 @@ merge(Compressor.prototype, {
 
     OPT(AST_Try, function(self, compressor){
         self.body = tighten_body(self.body, compressor);
+        if (self.bcatch && self.bfinally && all(self.bfinally.body, is_empty)) self.bfinally = null;
+        if (all(self.body, is_empty)) {
+            var body = [];
+            if (self.bcatch) extract_declarations_from_unreachable_code(compressor, self.bcatch, body);
+            if (self.bfinally) body = body.concat(self.bfinally.body);
+            return body.length > 0 ? make_node(AST_BlockStatement, self, {
+                body: body
+            }).optimize(compressor) : make_node(AST_EmptyStatement, self);
+        }
         return self;
     });
 
diff --git a/test/compress/dead-code.js b/test/compress/dead-code.js
index 7772e76..bb72451 100644
--- a/test/compress/dead-code.js
+++ b/test/compress/dead-code.js
@@ -214,3 +214,45 @@ dead_code_const_annotation_complex_scope: {
     }
     expect_stdout: true
 }
+
+try_catch_finally: {
+    options = {
+        conditionals: true,
+        dead_code: true,
+        evaluate: true,
+    }
+    input: {
+        var a = 1;
+        !function() {
+            try {
+                if (false) throw x;
+            } catch (a) {
+                var a = 2;
+                console.log("FAIL");
+            } finally {
+                a = 3;
+                console.log("PASS");
+            }
+        }();
+        try {
+            console.log(a);
+        } finally {
+        }
+    }
+    expect: {
+        var a = 1;
+        !function() {
+            var a;
+            a = 3;
+            console.log("PASS");
+        }();
+        try {
+            console.log(a);
+        } finally {
+        }
+    }
+    expect_stdout: [
+        "PASS",
+        "1",
+    ]
+}
diff --git a/test/compress/drop-unused.js b/test/compress/drop-unused.js
index d0b8776..28118fc 100644
--- a/test/compress/drop-unused.js
+++ b/test/compress/drop-unused.js
@@ -853,7 +853,9 @@ issue_1715_1: {
         var a = 1;
         function f() {
             a++;
-            try {} catch (a) {
+            try {
+                x();
+            } catch (a) {
                 var a;
             }
         }
@@ -864,7 +866,9 @@ issue_1715_1: {
         var a = 1;
         function f() {
             a++;
-            try {} catch (a) {
+            try {
+                x();
+            } catch (a) {
                 var a;
             }
         }
@@ -882,7 +886,9 @@ issue_1715_2: {
         var a = 1;
         function f() {
             a++;
-            try {} catch (a) {
+            try {
+                x();
+            } catch (a) {
                 var a = 2;
             }
         }
@@ -893,7 +899,9 @@ issue_1715_2: {
         var a = 1;
         function f() {
             a++;
-            try {} catch (a) {
+            try {
+                x();
+            } catch (a) {
                 var a;
             }
         }
@@ -911,7 +919,9 @@ issue_1715_3: {
         var a = 1;
         function f() {
             a++;
-            try {} catch (a) {
+            try {
+                console;
+            } catch (a) {
                 var a = 2 + x();
             }
         }
@@ -922,7 +932,9 @@ issue_1715_3: {
         var a = 1;
         function f() {
             a++;
-            try {} catch (a) {
+            try {
+                console;
+            } catch (a) {
                 var a = x();
             }
         }
@@ -940,7 +952,9 @@ issue_1715_4: {
         var a = 1;
         !function a() {
             a++;
-            try {} catch (a) {
+            try {
+                x();
+            } catch (a) {
                 var a;
             }
         }();
@@ -950,7 +964,9 @@ issue_1715_4: {
         var a = 1;
         !function() {
             a++;
-            try {} catch (a) {
+            try {
+                x();
+            } catch (a) {
                 var a;
             }
         }();
diff --git a/test/compress/issue-1673.js b/test/compress/issue-1673.js
index 4628e37..081b0e5 100644
--- a/test/compress/issue-1673.js
+++ b/test/compress/issue-1673.js
@@ -70,6 +70,7 @@ side_effects_finally: {
         function f() {
             function g() {
                 try {
+                    x();
                 } catch (e) {
                 } finally {
                     console.log("PASS");
@@ -83,6 +84,7 @@ side_effects_finally: {
         function f() {
             (function() {
                 try {
+                    x();
                 } catch (e) {
                 } finally {
                     console.log("PASS");
diff --git a/test/compress/screw-ie8.js b/test/compress/screw-ie8.js
index fc49211..68d1a36 100644
--- a/test/compress/screw-ie8.js
+++ b/test/compress/screw-ie8.js
@@ -204,13 +204,13 @@ issue_1586_1: {
     input: {
         function f() {
             try {
+                x();
             } catch (err) {
                 console.log(err.message);
             }
         }
     }
-    expect_exact: "function f(){try{}catch(c){console.log(c.message)}}"
-    expect_stdout: true
+    expect_exact: "function f(){try{x()}catch(c){console.log(c.message)}}"
 }
 
 issue_1586_2: {
@@ -223,11 +223,11 @@ issue_1586_2: {
     input: {
         function f() {
             try {
+                x();
             } catch (err) {
                 console.log(err.message);
             }
         }
     }
-    expect_exact: "function f(){try{}catch(c){console.log(c.message)}}"
-    expect_stdout: true
+    expect_exact: "function f(){try{x()}catch(c){console.log(c.message)}}"
 }

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