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