| 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 to see if resolving a hidden native class's method interferes with |
| 6 // subsequent resolving the subclass's method. This might happen if the |
| 7 // superclass caches the method in the prototype, so shadowing the dispatcher |
| 8 // stored on Object.prototype. |
| 9 |
| 10 // Version 1: It might be possible to call foo directly. |
| 11 class A1 native "*A1" { |
| 12 foo() native; |
| 13 } |
| 14 |
| 15 class B1 extends A1 native "*B1" { |
| 16 foo() native; |
| 17 } |
| 18 |
| 19 makeA1() native; |
| 20 makeB1() native; |
| 21 |
| 22 |
| 23 // Version 2: foo needs some kind of trampoline. |
| 24 class A2 native "*A2" { |
| 25 foo([a=99]) native; |
| 26 } |
| 27 |
| 28 class B2 extends A2 native "*B2" { |
| 29 foo([z=1000]) native; |
| 30 } |
| 31 |
| 32 makeA2() native; |
| 33 makeB2() native; |
| 34 |
| 35 void setup() native """ |
| 36 // This code is all inside 'setup' and so not accesible from the global scope. |
| 37 function inherits(child, parent) { |
| 38 if (child.prototype.__proto__) { |
| 39 child.prototype.__proto__ = parent.prototype; |
| 40 } else { |
| 41 function tmp() {}; |
| 42 tmp.prototype = parent.prototype; |
| 43 child.prototype = new tmp(); |
| 44 child.prototype.constructor = child; |
| 45 } |
| 46 } |
| 47 function A1(){} |
| 48 function B1(){} |
| 49 inherits(B1, A1); |
| 50 A1.prototype.foo = function(){return 100;} |
| 51 B1.prototype.foo = function(){return 200;} |
| 52 |
| 53 makeA1 = function(){return new A1}; |
| 54 makeB1 = function(){return new B1}; |
| 55 |
| 56 function A2(){} |
| 57 function B2(){} |
| 58 inherits(B2, A2); |
| 59 A2.prototype.foo = function(a){return a + 10000;} |
| 60 B2.prototype.foo = function(z){return z + 20000;} |
| 61 |
| 62 makeA2 = function(){return new A2}; |
| 63 makeB2 = function(){return new B2}; |
| 64 """; |
| 65 |
| 66 |
| 67 main() { |
| 68 setup(); |
| 69 |
| 70 var a1 = makeA1(); |
| 71 var b1 = makeB1(); |
| 72 Expect.equals(100, a1.foo()); |
| 73 Expect.equals(200, b1.foo()); |
| 74 |
| 75 var a2 = makeA2(); |
| 76 var b2 = makeB2(); |
| 77 Expect.equals(10000 + 99, a2.foo()); |
| 78 Expect.equals(20000 + 1000, b2.foo()); |
| 79 |
| 80 Expect.equals(10000 + 1, a2.foo(1)); |
| 81 Expect.equals(20000 + 2, b2.foo(2)); |
| 82 |
| 83 bool caught = false; |
| 84 try { |
| 85 a1.foo(20); |
| 86 } catch (var ex) { |
| 87 caught = true; |
| 88 Expect.isTrue(ex is NoSuchMethodException); |
| 89 } |
| 90 Expect.isTrue(caught, 'a1.foo(20) should throw'); |
| 91 |
| 92 caught = false; |
| 93 try { |
| 94 var x = 123; |
| 95 x.foo(20); |
| 96 } catch (var ex) { |
| 97 caught = true; |
| 98 Expect.isTrue(ex is NoSuchMethodException); |
| 99 } |
| 100 Expect.isTrue(caught, "x.foo(20) should throw"); |
| 101 } |
| OLD | NEW |