Home | History | Annotate | Download | only in compiler
      1 // Copyright 2016 the V8 project authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 // Flags: --allow-natives-syntax
      6 
      7 (function TryBlockCatch() {
      8   var global = 0;
      9   function f(a) {
     10     var x = a + 23
     11     try {
     12       let y = a + 42;
     13       function capture() { return x + y }
     14       throw "boom!";
     15     } catch(e) {
     16       global = x;
     17     }
     18     return x;
     19   }
     20   assertEquals(23, f(0));
     21   assertEquals(24, f(1));
     22   %OptimizeFunctionOnNextCall(f);
     23   assertEquals(25, f(2));
     24   assertEquals(25, global);
     25 })();
     26 
     27 (function TryBlockFinally() {
     28   var global = 0;
     29   function f(a) {
     30     var x = a + 23
     31     try {
     32       let y = a + 42;
     33       function capture() { return x + y }
     34       throw "boom!";
     35     } finally {
     36       global = x;
     37     }
     38     return x;
     39   }
     40   assertThrows(function() { f(0) });
     41   assertThrows(function() { f(1) });
     42   %OptimizeFunctionOnNextCall(f);
     43   assertThrows(function() { f(2) });
     44   assertEquals(25, global);
     45 })();
     46 
     47 (function TryCatchCatch() {
     48   var global = 0;
     49   function f(a) {
     50     var x = a + 23
     51     try {
     52       try {
     53         throw "boom!";
     54       } catch(e2) {
     55         function capture() { return x + y }
     56         throw "boom!";
     57       }
     58     } catch(e) {
     59       global = x;
     60     }
     61     return x;
     62   }
     63   assertEquals(23, f(0));
     64   assertEquals(24, f(1));
     65   %OptimizeFunctionOnNextCall(f);
     66   assertEquals(25, f(2));
     67   assertEquals(25, global);
     68 })();
     69 
     70 (function TryWithCatch() {
     71   var global = 0;
     72   function f(a) {
     73     var x = a + 23
     74     try {
     75       with ({ y : a + 42 }) {
     76         function capture() { return x + y }
     77         throw "boom!";
     78       }
     79     } catch(e) {
     80       global = x;
     81     }
     82     return x;
     83   }
     84   assertEquals(23, f(0));
     85   assertEquals(24, f(1));
     86   %OptimizeFunctionOnNextCall(f);
     87   assertEquals(25, f(2));
     88   assertEquals(25, global);
     89 })();
     90