OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 that the @MirrorsUsed annotation suppress hints and that only | 5 /// Test that the @MirrorsUsed annotation suppress hints and that only |
6 /// requested elements are retained for reflection. | 6 /// requested elements are retained for reflection. |
7 library dart2js.test.mirrors_used_test; | 7 library dart2js.test.mirrors_used_test; |
8 | 8 |
9 import 'package:expect/expect.dart'; | 9 import 'package:expect/expect.dart'; |
10 import "package:async_helper/async_helper.dart"; | 10 import "package:async_helper/async_helper.dart"; |
11 | 11 |
12 import 'memory_compiler.dart' show | 12 import 'memory_compiler.dart' show |
13 compilerFor; | 13 compilerFor; |
14 | 14 |
15 import '../../../sdk/lib/_internal/compiler/implementation/apiimpl.dart' show | 15 import '../../../sdk/lib/_internal/compiler/implementation/apiimpl.dart' show |
16 Compiler; | 16 Compiler; |
17 | 17 |
18 import '../../../sdk/lib/_internal/compiler/implementation/dart2jslib.dart' show | 18 import '../../../sdk/lib/_internal/compiler/implementation/dart2jslib.dart' show |
19 Constant, | 19 Constant, |
20 TypeConstant; | 20 TypeConstant; |
21 | 21 |
22 import | 22 import |
23 '../../../sdk/lib/_internal/compiler/implementation/elements/elements.dart' | 23 '../../../sdk/lib/_internal/compiler/implementation/elements/elements.dart' |
24 show | 24 show Element, Elements; |
25 Elements; | 25 import '../../../sdk/lib/_internal/compiler/implementation/js_backend/js_backend
.dart' |
| 26 show JavaScriptBackend; |
26 | 27 |
27 void expectOnlyVerboseInfo(Uri uri, int begin, int end, String message, kind) { | 28 void expectOnlyVerboseInfo(Uri uri, int begin, int end, String message, kind) { |
28 if (kind.name == 'verbose info') { | 29 if (kind.name == 'verbose info') { |
29 print(message); | 30 print(message); |
30 return; | 31 return; |
31 } | 32 } |
32 if (message.contains('methods retained for use by dart:mirrors out of')) { | 33 if (message.contains('methods retained for use by dart:mirrors out of')) { |
33 print(message); | 34 print(message); |
34 return; | 35 return; |
35 } | 36 } |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 [ | 75 [ |
75 'Foo_staticMethod', // The name of Foo.staticMethod. | 76 'Foo_staticMethod', // The name of Foo.staticMethod. |
76 r'instanceMethod$0']; // The name of Foo.instanceMethod. | 77 r'instanceMethod$0']; // The name of Foo.instanceMethod. |
77 | 78 |
78 // We always include the names of some native classes. | 79 // We always include the names of some native classes. |
79 List<Element> nativeClasses = [ | 80 List<Element> nativeClasses = [ |
80 compiler.intClass, compiler.doubleClass, compiler.numClass, | 81 compiler.intClass, compiler.doubleClass, compiler.numClass, |
81 compiler.stringClass, compiler.boolClass, compiler.nullClass, | 82 compiler.stringClass, compiler.boolClass, compiler.nullClass, |
82 compiler.listClass | 83 compiler.listClass |
83 ]; | 84 ]; |
84 | 85 JavaScriptBackend backend = compiler.backend; |
85 Iterable<String> nativeNames = | 86 Iterable<String> nativeNames = |
86 nativeClasses.map(compiler.backend.namer.getNameOfClass); | 87 nativeClasses.map(backend.namer.getNameOfClass); |
87 expectedNames.addAll(nativeNames); | 88 expectedNames.addAll(nativeNames); |
88 | 89 |
89 Set recordedNames = new Set() | 90 Set recordedNames = new Set() |
90 ..addAll(compiler.backend.emitter.recordedMangledNames) | 91 ..addAll(backend.emitter.recordedMangledNames) |
91 ..addAll(compiler.backend.emitter.mangledFieldNames.keys) | 92 ..addAll(backend.emitter.mangledFieldNames.keys) |
92 ..addAll(compiler.backend.emitter.mangledGlobalFieldNames.keys); | 93 ..addAll(backend.emitter.mangledGlobalFieldNames.keys); |
93 Expect.setEquals(new Set.from(expectedNames), recordedNames); | 94 Expect.setEquals(new Set.from(expectedNames), recordedNames); |
94 | 95 |
95 for (var library in compiler.libraries.values) { | 96 for (var library in compiler.libraries.values) { |
96 library.forEachLocalMember((member) { | 97 library.forEachLocalMember((member) { |
97 if (library == compiler.mainApp && member.name == 'Foo') { | 98 if (library == compiler.mainApp && member.name == 'Foo') { |
98 Expect.isTrue( | 99 Expect.isTrue( |
99 compiler.backend.isNeededForReflection(member), '$member'); | 100 compiler.backend.isNeededForReflection(member), '$member'); |
100 member.forEachLocalMember((classMember) { | 101 member.forEachLocalMember((classMember) { |
101 Expect.isTrue( | 102 Expect.isTrue( |
102 compiler.backend.isNeededForReflection(classMember), | 103 compiler.backend.isNeededForReflection(classMember), |
103 '$classMember'); | 104 '$classMember'); |
104 }); | 105 }); |
105 } else { | 106 } else { |
106 Expect.isFalse( | 107 Expect.isFalse( |
107 compiler.backend.isNeededForReflection(member), '$member'); | 108 compiler.backend.isNeededForReflection(member), '$member'); |
108 } | 109 } |
109 }); | 110 }); |
110 } | 111 } |
111 | 112 |
112 // There should at least be one metadata constant: | 113 // There should at least be one metadata constant: |
113 // 1. The constructed constant for 'MirrorsUsed'. | 114 // 1. The constructed constant for 'MirrorsUsed'. |
114 Expect.isTrue(compiler.backend.metadataConstants.length >= 1); | 115 Expect.isTrue(backend.metadataConstants.length >= 1); |
115 | 116 |
116 Set<Constant> compiledConstants = | 117 Set<Constant> compiledConstants = backend.constants.compiledConstants; |
117 compiler.backend.constants.compiledConstants; | |
118 // Make sure that most of the metadata constants aren't included in the | 118 // Make sure that most of the metadata constants aren't included in the |
119 // generated code. | 119 // generated code. |
120 for (var dependency in compiler.backend.metadataConstants) { | 120 for (var dependency in backend.metadataConstants) { |
121 Constant constant = dependency.constant; | 121 Constant constant = dependency.constant; |
122 Expect.isFalse(compiledConstants.contains(constant), '$constant'); | 122 Expect.isFalse(compiledConstants.contains(constant), '$constant'); |
123 } | 123 } |
124 | 124 |
125 // The type literal 'Foo' is both used as metadata, and as a plain value in | 125 // The type literal 'Foo' is both used as metadata, and as a plain value in |
126 // the program. Make sure that it isn't duplicated. | 126 // the program. Make sure that it isn't duplicated. |
127 int fooConstantCount = 0; | 127 int fooConstantCount = 0; |
128 for (Constant constant in compiledConstants) { | 128 for (Constant constant in compiledConstants) { |
129 if (constant is TypeConstant && '${constant.representedType}' == 'Foo') { | 129 if (constant is TypeConstant && '${constant.representedType}' == 'Foo') { |
130 fooConstantCount++; | 130 fooConstantCount++; |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
162 library lib; | 162 library lib; |
163 | 163 |
164 import 'dart:mirrors'; | 164 import 'dart:mirrors'; |
165 | 165 |
166 useReflect(type) { | 166 useReflect(type) { |
167 print(new Symbol('Foo')); | 167 print(new Symbol('Foo')); |
168 print(MirrorSystem.getName(reflectClass(type).owner.qualifiedName)); | 168 print(MirrorSystem.getName(reflectClass(type).owner.qualifiedName)); |
169 } | 169 } |
170 """, | 170 """, |
171 }; | 171 }; |
OLD | NEW |