| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 // Test that hidden native exception classes can be marked as existing. | 5 // Test that hidden native exception classes can be marked as existing. |
| 6 // | 6 // |
| 7 // To infer which native hidden types exist, we need | 7 // To infer which native hidden types exist, we need |
| 8 // (1) return types of native methods and getters | 8 // (1) return types of native methods and getters |
| 9 // (2) argument types of callbacks | 9 // (2) argument types of callbacks |
| 10 // (3) exceptions thrown by the operation. | 10 // (3) exceptions thrown by the operation. |
| 11 // | 11 // |
| 12 // (1) and (2) can be achieved by having nicely typed native methods, but there | 12 // (1) and (2) can be achieved by having nicely typed native methods, but there |
| 13 // is no place in the Dart language to communicate (3). So we use the following | 13 // is no place in the Dart language to communicate (3). So we use the following |
| 14 // fake body technique. | 14 // fake body technique. |
| 15 | 15 |
| 16 | 16 |
| 17 // The exception type. | 17 // The exception type. |
| 18 class E native "*E" { | 18 @native("*E") |
| 19 E._used() native; // Bogus native constructor, called only from fake body. | 19 class E { |
| 20 @native E._used(); // Bogus native constructor, called only from fake body. |
| 20 | 21 |
| 21 final int code; | 22 final int code; |
| 22 } | 23 } |
| 23 | 24 |
| 24 // Type with exception-throwing methods. | 25 // Type with exception-throwing methods. |
| 25 class A native "*A" { | 26 @native("*A") |
| 26 op(int x) native { | 27 class A { |
| 28 @native op(int x) { |
| 27 // Fake body calls constructor to mark the exception class (E) as used. | 29 // Fake body calls constructor to mark the exception class (E) as used. |
| 28 throw new E._used(); | 30 throw new E._used(); |
| 29 } | 31 } |
| 30 } | 32 } |
| 31 | 33 |
| 32 // This class is here just so that a dynamic context is polymorphic. | 34 // This class is here just so that a dynamic context is polymorphic. |
| 33 class B { | 35 class B { |
| 34 int get code => 666; | 36 int get code => 666; |
| 35 op(String x) => 123; | 37 op(String x) => 123; |
| 36 } | 38 } |
| 37 | 39 |
| 38 makeA() native; | 40 @native makeA(); |
| 39 | 41 |
| 40 void setup1() native """ | 42 @native(""" |
| 41 // Ensure we are not relying on global names 'A' and 'E'. | 43 // Ensure we are not relying on global names 'A' and 'E'. |
| 42 A = null; | 44 A = null; |
| 43 E = null; | 45 E = null; |
| 44 """; | 46 """) |
| 47 void setup1(); |
| 45 | 48 |
| 46 void setup2() native """ | 49 @native(""" |
| 47 // This code is all inside 'setup2' and so not accesible from the global scope. | 50 // This code is all inside 'setup2' and so not accesible from the global scope. |
| 48 function E(x){ this.code = x; } | 51 function E(x){ this.code = x; } |
| 49 | 52 |
| 50 function A(){} | 53 function A(){} |
| 51 A.prototype.op = function (x) { | 54 A.prototype.op = function (x) { |
| 52 if (x & 1) throw new E(100); | 55 if (x & 1) throw new E(100); |
| 53 return x / 2; | 56 return x / 2; |
| 54 }; | 57 }; |
| 55 makeA = function(){return new A}; | 58 makeA = function(){return new A}; |
| 56 """; | 59 """) |
| 60 void setup2(); |
| 57 | 61 |
| 58 int inscrutable(int x) => x == 0 ? 0 : x | inscrutable(x & (x - 1)); | 62 int inscrutable(int x) => x == 0 ? 0 : x | inscrutable(x & (x - 1)); |
| 59 | 63 |
| 60 main() { | 64 main() { |
| 61 setup1(); | 65 setup1(); |
| 62 setup2(); | 66 setup2(); |
| 63 | 67 |
| 64 var things = [makeA(), new B()]; | 68 var things = [makeA(), new B()]; |
| 65 var a = things[inscrutable(0)]; | 69 var a = things[inscrutable(0)]; |
| 66 var b = things[inscrutable(1)]; | 70 var b = things[inscrutable(1)]; |
| (...skipping 23 matching lines...) Expand all Loading... |
| 90 threw = false; | 94 threw = false; |
| 91 try { | 95 try { |
| 92 var x = aa.op(51); | 96 var x = aa.op(51); |
| 93 } catch (E e) { | 97 } catch (E e) { |
| 94 threw = true; | 98 threw = true; |
| 95 Expect.equals(100, e.code); | 99 Expect.equals(100, e.code); |
| 96 Expect.isTrue(e is E); | 100 Expect.isTrue(e is E); |
| 97 } | 101 } |
| 98 Expect.isTrue(threw); | 102 Expect.isTrue(threw); |
| 99 } | 103 } |
| OLD | NEW |