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