| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 // Test that hidden native exception classes can be marked as existing. | |
| 6 // | |
| 7 // To infer which native hidden types exist, we need | |
| 8 // (1) return types of native methods and getters | |
| 9 // (2) argument types of callbacks | |
| 10 // (3) exceptions thrown by the operation. | |
| 11 // | |
| 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 | |
| 14 // fake body technique. | |
| 15 | |
| 16 import "package:expect/expect.dart"; | |
| 17 import 'native_metadata.dart'; | |
| 18 | |
| 19 // The exception type. | |
| 20 @Native("*E") | |
| 21 class E { | |
| 22 @native E._used(); // Bogus native constructor, called only from fake body. | |
| 23 | |
| 24 final int code; | |
| 25 } | |
| 26 | |
| 27 // Type with exception-throwing methods. | |
| 28 @Native("*A") | |
| 29 class A { | |
| 30 @native op(int x) { | |
| 31 // Fake body calls constructor to mark the exception class (E) as used. | |
| 32 throw new E._used(); | |
| 33 } | |
| 34 } | |
| 35 | |
| 36 // This class is here just so that a dynamic context is polymorphic. | |
| 37 class B { | |
| 38 int get code => 666; | |
| 39 op(String x) => 123; | |
| 40 } | |
| 41 | |
| 42 @native makeA(); | |
| 43 | |
| 44 @Native(""" | |
| 45 // Ensure we are not relying on global names 'A' and 'E'. | |
| 46 A = null; | |
| 47 E = null; | |
| 48 """) | |
| 49 void setup1(); | |
| 50 | |
| 51 @Native(""" | |
| 52 // This code is all inside 'setup2' and so not accesible from the global scope. | |
| 53 function E(x){ this.code = x; } | |
| 54 | |
| 55 function A(){} | |
| 56 A.prototype.op = function (x) { | |
| 57 if (x & 1) throw new E(100); | |
| 58 return x / 2; | |
| 59 }; | |
| 60 makeA = function(){return new A}; | |
| 61 """) | |
| 62 void setup2(); | |
| 63 | |
| 64 int inscrutable(int x) => x == 0 ? 0 : x | inscrutable(x & (x - 1)); | |
| 65 | |
| 66 main() { | |
| 67 setup1(); | |
| 68 setup2(); | |
| 69 | |
| 70 var things = [makeA(), new B()]; | |
| 71 var a = things[inscrutable(0)]; | |
| 72 var b = things[inscrutable(1)]; | |
| 73 | |
| 74 Expect.equals(25, a.op(50)); | |
| 75 Expect.equals(123, b.op('hello')); | |
| 76 Expect.equals(666, b.code); | |
| 77 | |
| 78 bool threw = false; | |
| 79 try { | |
| 80 var x = a.op(51); | |
| 81 } catch (e) { | |
| 82 threw = true; | |
| 83 Expect.equals(100, e.code); | |
| 84 Expect.isTrue(e is E); | |
| 85 } | |
| 86 Expect.isTrue(threw); | |
| 87 | |
| 88 // Again, but with statically typed receivers. | |
| 89 A aa = a; | |
| 90 B bb = b; | |
| 91 | |
| 92 Expect.equals(25, aa.op(50)); | |
| 93 Expect.equals(123, bb.op('hello')); | |
| 94 Expect.equals(666, bb.code); | |
| 95 | |
| 96 threw = false; | |
| 97 try { | |
| 98 var x = aa.op(51); | |
| 99 } on E catch (e) { | |
| 100 threw = true; | |
| 101 Expect.equals(100, e.code); | |
| 102 Expect.isTrue(e is E); | |
| 103 } | |
| 104 Expect.isTrue(threw); | |
| 105 } | |
| OLD | NEW |