| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 library dart2js.serialization_analysis_test; | 5 library dart2js.serialization_analysis_test; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'package:async_helper/async_helper.dart'; | 8 import 'package:async_helper/async_helper.dart'; |
| 9 import 'package:expect/expect.dart'; | 9 import 'package:expect/expect.dart'; |
| 10 import 'package:compiler/src/commandline_options.dart'; | 10 import 'package:compiler/src/commandline_options.dart'; |
| 11 import 'package:compiler/src/common/resolution.dart'; |
| 12 import 'package:compiler/src/compiler.dart'; |
| 11 import 'package:compiler/src/elements/elements.dart'; | 13 import 'package:compiler/src/elements/elements.dart'; |
| 12 import 'package:compiler/src/compiler.dart'; | |
| 13 import 'package:compiler/src/filenames.dart'; | 14 import 'package:compiler/src/filenames.dart'; |
| 15 import 'package:compiler/src/serialization/element_serialization.dart'; |
| 16 import 'package:compiler/src/serialization/impact_serialization.dart'; |
| 17 import 'package:compiler/src/serialization/json_serializer.dart'; |
| 14 import 'package:compiler/src/serialization/serialization.dart'; | 18 import 'package:compiler/src/serialization/serialization.dart'; |
| 15 import 'package:compiler/src/serialization/json_serializer.dart'; | |
| 16 import 'package:compiler/src/serialization/task.dart'; | 19 import 'package:compiler/src/serialization/task.dart'; |
| 17 import 'package:compiler/src/universe/world_impact.dart'; | 20 import 'package:compiler/src/universe/world_impact.dart'; |
| 18 import 'memory_compiler.dart'; | 21 import 'memory_compiler.dart'; |
| 19 | 22 |
| 20 const List<Test> TESTS = const <Test>[ | 23 const List<Test> TESTS = const <Test>[ |
| 21 const Test(const { | 24 const Test(const { |
| 22 'main.dart': 'main() => print("Hello World");' | 25 'main.dart': 'main() => print("Hello World");' |
| 23 }), | 26 }), |
| 24 | 27 |
| 25 const Test(const { | 28 const Test(const { |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 135 'main.dart': r''' | 138 'main.dart': r''' |
| 136 class Class implements Comparable { | 139 class Class implements Comparable { |
| 137 bool compareTo(a, b) => true; | 140 bool compareTo(a, b) => true; |
| 138 } | 141 } |
| 139 main() { | 142 main() { |
| 140 print(new Class().compareTo(null, null)); | 143 print(new Class().compareTo(null, null)); |
| 141 }''' | 144 }''' |
| 142 }, | 145 }, |
| 143 expectedWarningCount: 1, | 146 expectedWarningCount: 1, |
| 144 expectedInfoCount: 1), | 147 expectedInfoCount: 1), |
| 148 |
| 149 const Test(const { |
| 150 'main.dart': r''' |
| 151 import 'dart:math'; |
| 152 |
| 153 class MyRandom implements Random { |
| 154 int nextInt(int max) { |
| 155 return max.length; |
| 156 } |
| 157 bool nextBool() => true; |
| 158 double nextDouble() => 0.0; |
| 159 } |
| 160 main() { |
| 161 new MyRandom().nextInt(0); |
| 162 }''' |
| 163 }, |
| 164 expectedWarningCount: 1, |
| 165 expectedInfoCount: 0), |
| 166 |
| 167 const Test(const { |
| 168 'main.dart': r''' |
| 169 import 'dart:math'; |
| 170 |
| 171 class MyRandom implements Random { |
| 172 int nextInt(int max) { |
| 173 return max.length; |
| 174 } |
| 175 bool nextBool() => true; |
| 176 double nextDouble() => 0.0; |
| 177 } |
| 178 main() { |
| 179 new MyRandom(); |
| 180 }''' |
| 181 }), |
| 182 |
| 183 const Test(const { |
| 184 'main.dart': r''' |
| 185 import 'dart:math'; |
| 186 |
| 187 class MyRandom implements Random { |
| 188 int nextInt(int max) { |
| 189 return max.length; |
| 190 } |
| 191 bool nextBool() => true; |
| 192 double nextDouble() => 0.0; |
| 193 } |
| 194 main() { |
| 195 [].shuffle(new MyRandom()); |
| 196 }''' |
| 197 }, |
| 198 expectedWarningCount: 1, |
| 199 expectedInfoCount: 0), |
| 145 ]; | 200 ]; |
| 146 | 201 |
| 147 main(List<String> arguments) { | 202 main(List<String> arguments) { |
| 148 asyncTest(() async { | 203 asyncTest(() async { |
| 149 String serializedData = await serializeDartCore(); | 204 String serializedData = await serializeDartCore(); |
| 150 | 205 |
| 151 if (arguments.isNotEmpty) { | 206 if (arguments.isNotEmpty) { |
| 152 Uri entryPoint = Uri.base.resolve(nativeToUriPath(arguments.last)); | 207 Uri entryPoint = Uri.base.resolve(nativeToUriPath(arguments.last)); |
| 153 await analyze(serializedData, entryPoint, null); | 208 await analyze(serializedData, entryPoint, null); |
| 154 } else { | 209 } else { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 168 final int expectedInfoCount; | 223 final int expectedInfoCount; |
| 169 | 224 |
| 170 const Test(this.sourceFiles, { | 225 const Test(this.sourceFiles, { |
| 171 this.expectedErrorCount: 0, | 226 this.expectedErrorCount: 0, |
| 172 this.expectedWarningCount: 0, | 227 this.expectedWarningCount: 0, |
| 173 this.expectedHintCount: 0, | 228 this.expectedHintCount: 0, |
| 174 this.expectedInfoCount: 0}); | 229 this.expectedInfoCount: 0}); |
| 175 } | 230 } |
| 176 | 231 |
| 177 Future analyze(String serializedData, Uri entryPoint, Test test) async { | 232 Future analyze(String serializedData, Uri entryPoint, Test test) async { |
| 178 Deserializer deserializer = new Deserializer.fromText( | |
| 179 serializedData, const JsonSerializationDecoder()); | |
| 180 DiagnosticCollector diagnosticCollector = new DiagnosticCollector(); | 233 DiagnosticCollector diagnosticCollector = new DiagnosticCollector(); |
| 181 await runCompiler( | 234 await runCompiler( |
| 182 entryPoint: entryPoint, | 235 entryPoint: entryPoint, |
| 183 memorySourceFiles: test != null ? test.sourceFiles : const {}, | 236 memorySourceFiles: test != null ? test.sourceFiles : const {}, |
| 184 options: [Flags.analyzeOnly], | 237 options: [Flags.analyzeOnly], |
| 185 diagnosticHandler: diagnosticCollector, | 238 diagnosticHandler: diagnosticCollector, |
| 186 beforeRun: (Compiler compiler) { | 239 beforeRun: (Compiler compiler) { |
| 240 Deserializer deserializer = new Deserializer.fromText( |
| 241 serializedData, |
| 242 const JsonSerializationDecoder()); |
| 243 deserializer.plugins.add(compiler.backend.serialization.deserializer); |
| 187 compiler.serialization.deserializer = | 244 compiler.serialization.deserializer = |
| 188 new _DeserializerSystem(deserializer); | 245 new _DeserializerSystem(deserializer); |
| 189 }); | 246 }); |
| 190 if (test != null) { | 247 if (test != null) { |
| 191 Expect.equals(test.expectedErrorCount, diagnosticCollector.errors.length, | 248 Expect.equals(test.expectedErrorCount, diagnosticCollector.errors.length, |
| 192 "Unexpected error count."); | 249 "Unexpected error count."); |
| 193 Expect.equals( | 250 Expect.equals( |
| 194 test.expectedWarningCount, | 251 test.expectedWarningCount, |
| 195 diagnosticCollector.warnings.length, | 252 diagnosticCollector.warnings.length, |
| 196 "Unexpected warning count."); | 253 "Unexpected warning count."); |
| 197 Expect.equals(test.expectedHintCount, diagnosticCollector.hints.length, | 254 Expect.equals(test.expectedHintCount, diagnosticCollector.hints.length, |
| 198 "Unexpected hint count."); | 255 "Unexpected hint count."); |
| 199 Expect.equals(test.expectedInfoCount, diagnosticCollector.infos.length, | 256 Expect.equals(test.expectedInfoCount, diagnosticCollector.infos.length, |
| 200 "Unexpected info count."); | 257 "Unexpected info count."); |
| 201 } | 258 } |
| 202 } | 259 } |
| 203 | 260 |
| 204 Future<String> serializeDartCore() async { | 261 Future<String> serializeDartCore() async { |
| 205 Compiler compiler = compilerFor( | 262 Compiler compiler = compilerFor( |
| 206 options: ['--analyze-all']); | 263 options: ['--analyze-all']); |
| 207 await compiler.run(Uri.parse('dart:core')); | 264 await compiler.run(Uri.parse('dart:core')); |
| 208 return serialize(compiler.libraryLoader.libraries); | 265 return serialize(compiler); |
| 209 } | 266 } |
| 210 | 267 |
| 211 String serialize(Iterable<LibraryElement> libraries) { | 268 String serialize(Compiler compiler) { |
| 212 Serializer serializer = new Serializer(const JsonSerializationEncoder()); | 269 Serializer serializer = new Serializer(const JsonSerializationEncoder()); |
| 213 for (LibraryElement library in libraries) { | 270 serializer.plugins.add(compiler.backend.serialization.serializer); |
| 271 serializer.plugins.add(new WorldImpactSerializer(compiler.resolution)); |
| 272 |
| 273 for (LibraryElement library in compiler.libraryLoader.libraries) { |
| 214 serializer.serialize(library); | 274 serializer.serialize(library); |
| 215 } | 275 } |
| 216 return serializer.toText(); | 276 return serializer.toText(); |
| 217 } | 277 } |
| 218 | 278 |
| 279 const String WORLD_IMPACT_TAG = 'worldImpact'; |
| 280 |
| 281 class WorldImpactSerializer extends SerializerPlugin { |
| 282 final Resolution resolution; |
| 283 |
| 284 WorldImpactSerializer(this.resolution); |
| 285 |
| 286 @override |
| 287 void onElement(Element element, ObjectEncoder createEncoder(String tag)) { |
| 288 if (resolution.hasBeenResolved(element)) { |
| 289 WorldImpact impact = resolution.getWorldImpact(element); |
| 290 ObjectEncoder encoder = createEncoder(WORLD_IMPACT_TAG); |
| 291 impact.apply(new ImpactSerializer(encoder)); |
| 292 } |
| 293 } |
| 294 } |
| 295 |
| 296 class WorldImpactDeserializer extends DeserializerPlugin { |
| 297 Map<Element, WorldImpact> impactMap = <Element, WorldImpact>{}; |
| 298 |
| 299 @override |
| 300 void onElement(Element element, ObjectDecoder getDecoder(String tag)) { |
| 301 ObjectDecoder decoder = getDecoder(WORLD_IMPACT_TAG); |
| 302 if (decoder != null) { |
| 303 impactMap[element] = ImpactDeserializer.deserializeImpact(decoder); |
| 304 } |
| 305 } |
| 306 } |
| 307 |
| 219 class _DeserializerSystem extends DeserializerSystem { | 308 class _DeserializerSystem extends DeserializerSystem { |
| 220 final Deserializer _deserializer; | 309 final Deserializer _deserializer; |
| 221 final List<LibraryElement> deserializedLibraries = <LibraryElement>[]; | 310 final List<LibraryElement> deserializedLibraries = <LibraryElement>[]; |
| 311 final WorldImpactDeserializer _worldImpactDeserializer = |
| 312 new WorldImpactDeserializer(); |
| 222 | 313 |
| 223 _DeserializerSystem(this._deserializer); | 314 _DeserializerSystem(this._deserializer) { |
| 315 _deserializer.plugins.add(_worldImpactDeserializer); |
| 316 } |
| 224 | 317 |
| 225 LibraryElement readLibrary(Uri resolvedUri) { | 318 LibraryElement readLibrary(Uri resolvedUri) { |
| 226 LibraryElement library = _deserializer.lookupLibrary(resolvedUri); | 319 LibraryElement library = _deserializer.lookupLibrary(resolvedUri); |
| 227 if (library != null) { | 320 if (library != null) { |
| 228 deserializedLibraries.add(library); | 321 deserializedLibraries.add(library); |
| 229 } | 322 } |
| 230 return library; | 323 return library; |
| 231 } | 324 } |
| 232 | 325 |
| 233 @override | 326 @override |
| 234 WorldImpact computeWorldImpact(Element element) { | 327 WorldImpact computeWorldImpact(Element element) { |
| 235 return const WorldImpact(); | 328 WorldImpact impact = _worldImpactDeserializer.impactMap[element]; |
| 329 if (impact == null) { |
| 330 print('No impact found for $element (${element.library})'); |
| 331 impact = const WorldImpact(); |
| 332 } |
| 333 return impact; |
| 236 } | 334 } |
| 237 | 335 |
| 238 @override | 336 @override |
| 239 bool isDeserialized(Element element) { | 337 bool isDeserialized(Element element) { |
| 240 return deserializedLibraries.contains(element.library); | 338 return deserializedLibraries.contains(element.library); |
| 241 } | 339 } |
| 242 } | 340 } |
| OLD | NEW |