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 import "dart:_js_helper"; | 5 import "dart:_js_helper"; |
6 import "package:expect/expect.dart"; | 6 import "package:expect/expect.dart"; |
7 | 7 |
8 // Check that native fields are not incorrectly renamed. | 8 // Check that native fields are not incorrectly renamed. |
9 | 9 |
10 @Native("A") | 10 @Native("A") |
11 class A { | 11 class A { |
12 int myLongPropertyName; | 12 int myLongPropertyName; |
13 int getValue; | 13 int getValue; |
14 | 14 |
15 int method(int z) => myLongPropertyName; | 15 int method(int z) => myLongPropertyName; |
16 } | 16 } |
17 | 17 |
18 | |
19 // This code is inside the setup function, so the function names are not | 18 // This code is inside the setup function, so the function names are not |
20 // accessible, but the makeA variable is global through the magic of JS scoping. | 19 // accessible, but the makeA variable is global through the magic of JS scoping. |
21 // The contents of this are of course not analyzable by the compiler. | 20 // The contents of this are of course not analyzable by the compiler. |
22 void setup() native r""" | 21 void setup() native r""" |
23 function getter() { | 22 function getter() { |
24 return ++this.getValue; | 23 return ++this.getValue; |
25 } | 24 } |
26 | 25 |
27 function setter(x) { | 26 function setter(x) { |
28 this.getValue += 10; | 27 this.getValue += 10; |
29 } | 28 } |
30 | 29 |
31 function A(){ | 30 function A(){ |
32 var a = Object.create( | 31 var a = Object.create( |
33 { constructor: { name: 'A'}}, | 32 { constructor: { name: 'A'}}, |
34 { myLongPropertyName: { get: getter, | 33 { myLongPropertyName: { get: getter, |
35 set: setter, | 34 set: setter, |
36 configurable: false, | 35 configurable: false, |
37 writeable: false | 36 writeable: false |
38 } | 37 } |
39 }); | 38 }); |
40 a.getValue = 0; | 39 a.getValue = 0; |
41 return a; | 40 return a; |
42 } | 41 } |
43 | 42 |
44 makeA = function(){return new A;}; | 43 makeA = function(){return new A;}; |
45 """; | 44 """; |
46 | 45 |
47 A makeA() native; | 46 A makeA() native ; |
48 | 47 |
49 main() { | 48 main() { |
50 setup(); | 49 setup(); |
51 var a = makeA(); | 50 var a = makeA(); |
52 a.myLongPropertyName = 21; | 51 a.myLongPropertyName = 21; |
53 int gotten = a.myLongPropertyName; | 52 int gotten = a.myLongPropertyName; |
54 Expect.equals(11, gotten); | 53 Expect.equals(11, gotten); |
55 | 54 |
56 var a2 = makeA(); | 55 var a2 = makeA(); |
57 if (a2 is A) { | 56 if (a2 is A) { |
58 // Inside this 'if' the compiler knows that a2 is an A, so it is tempted | 57 // Inside this 'if' the compiler knows that a2 is an A, so it is tempted |
59 // to access myLongPropertyName directly, using its minified name. But | 58 // to access myLongPropertyName directly, using its minified name. But |
60 // renaming of native properties can only work using getters and setters | 59 // renaming of native properties can only work using getters and setters |
61 // that access the original name. | 60 // that access the original name. |
62 a2.myLongPropertyName = 21; | 61 a2.myLongPropertyName = 21; |
63 int gotten = a2.myLongPropertyName; | 62 int gotten = a2.myLongPropertyName; |
64 Expect.equals(11, gotten); | 63 Expect.equals(11, gotten); |
65 } | 64 } |
66 } | 65 } |
OLD | NEW |