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 // A native method prevents other members from having that name, including |
| 6 // fields. However, native fields keep their name. The implication: a getter |
| 7 // for the field must be based on the field's name, not the field's jsname. |
| 8 |
| 9 interface I { |
| 10 int key; |
| 11 } |
| 12 |
| 13 class A implements I native "*A" { |
| 14 int key; // jsname is 'key' |
| 15 int getKey() => key; |
| 16 } |
| 17 |
| 18 class B implements I { |
| 19 int key; // jsname is not 'key' |
| 20 B([this.key = 222]); |
| 21 int getKey() => key; |
| 22 } |
| 23 |
| 24 class X native "*X" { |
| 25 int native_key_method() native 'key'; |
| 26 // This should cause B.key to be renamed, but not A.key. |
| 27 int key() native 'key'; |
| 28 } |
| 29 |
| 30 A makeA() native; |
| 31 X makeX() native; |
| 32 |
| 33 |
| 34 void setup() native """ |
| 35 // This code is all inside 'setup' and so not accesible from the global scope. |
| 36 function A(){ this.key = 111; } |
| 37 A.prototype.getKey = function(){return this.key;}; |
| 38 |
| 39 function X(){} |
| 40 X.prototype.key = function(){return 666;}; |
| 41 |
| 42 makeA = function(){return new A}; |
| 43 makeX = function(){return new X}; |
| 44 """; |
| 45 |
| 46 testDynamic() { |
| 47 var things = [makeA(), new B(), makeX()]; |
| 48 var a = things[0]; |
| 49 var b = things[1]; |
| 50 var x = things[2]; |
| 51 |
| 52 Expect.equals(111, a.key); |
| 53 Expect.equals(222, b.key); |
| 54 Expect.equals(111, a.getKey()); |
| 55 Expect.equals(222, b.getKey()); |
| 56 |
| 57 Expect.equals(666, x.native_key_method()); |
| 58 Expect.equals(666, x.key()); |
| 59 var fn = x.key; |
| 60 Expect.equals(666, fn()); |
| 61 } |
| 62 |
| 63 testPartial() { |
| 64 var things = [makeA(), new B(), makeX()]; |
| 65 I a = things[0]; |
| 66 I b = things[1]; |
| 67 |
| 68 // All subtypes of I have a field 'key'. The compiler might be tempted to |
| 69 // generate a direct property access, but that will fail since one of the |
| 70 // fields is renamed. A getter call is required here. |
| 71 Expect.equals(111, a.key); |
| 72 Expect.equals(222, b.key); |
| 73 } |
| 74 |
| 75 testTyped() { |
| 76 A a = makeA(); |
| 77 B b = new B(); |
| 78 X x = makeX(); |
| 79 |
| 80 Expect.equals(666, x.native_key_method()); |
| 81 Expect.equals(111, a.key); |
| 82 Expect.equals(222, b.key); |
| 83 Expect.equals(111, a.getKey()); |
| 84 Expect.equals(222, b.getKey()); |
| 85 } |
| 86 |
| 87 main() { |
| 88 setup(); |
| 89 |
| 90 testTyped(); |
| 91 testPartial(); |
| 92 testDynamic(); |
| 93 } |
OLD | NEW |