OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2016, 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 library dart2js.serialization_helper; |
| 6 |
| 7 import 'dart:async'; |
| 8 import 'package:async_helper/async_helper.dart'; |
| 9 import 'package:expect/expect.dart'; |
| 10 import 'package:compiler/src/commandline_options.dart'; |
| 11 import 'package:compiler/src/common/backend_api.dart'; |
| 12 import 'package:compiler/src/common/names.dart'; |
| 13 import 'package:compiler/src/common/resolution.dart'; |
| 14 import 'package:compiler/src/compiler.dart'; |
| 15 import 'package:compiler/src/elements/elements.dart'; |
| 16 import 'package:compiler/src/filenames.dart'; |
| 17 import 'package:compiler/src/serialization/element_serialization.dart'; |
| 18 import 'package:compiler/src/serialization/impact_serialization.dart'; |
| 19 import 'package:compiler/src/serialization/json_serializer.dart'; |
| 20 import 'package:compiler/src/serialization/serialization.dart'; |
| 21 import 'package:compiler/src/serialization/task.dart'; |
| 22 import 'package:compiler/src/universe/world_impact.dart'; |
| 23 import 'memory_compiler.dart'; |
| 24 |
| 25 |
| 26 Future<String> serializeDartCore() async { |
| 27 Compiler compiler = compilerFor( |
| 28 options: [Flags.analyzeAll]); |
| 29 compiler.serialization.supportSerialization = true; |
| 30 await compiler.run(Uris.dart_core); |
| 31 return serialize(compiler, compiler.libraryLoader.libraries) |
| 32 .toText(const JsonSerializationEncoder()); |
| 33 } |
| 34 |
| 35 Serializer serialize(Compiler compiler, Iterable<LibraryElement> libraries) { |
| 36 assert(compiler.serialization.supportSerialization); |
| 37 |
| 38 Serializer serializer = new Serializer(); |
| 39 serializer.plugins.add(compiler.backend.serialization.serializer); |
| 40 serializer.plugins.add(new ResolutionImpactSerializer(compiler.resolution)); |
| 41 |
| 42 for (LibraryElement library in libraries) { |
| 43 serializer.serialize(library); |
| 44 } |
| 45 return serializer; |
| 46 } |
| 47 |
| 48 void deserialize(Compiler compiler, String serializedData) { |
| 49 Deserializer deserializer = new Deserializer.fromText( |
| 50 new DeserializationContext(), |
| 51 serializedData, |
| 52 const JsonSerializationDecoder()); |
| 53 deserializer.plugins.add(compiler.backend.serialization.deserializer); |
| 54 compiler.serialization.deserializer = |
| 55 new _DeserializerSystem( |
| 56 deserializer, |
| 57 compiler.backend.impactTransformer); |
| 58 } |
| 59 |
| 60 |
| 61 const String WORLD_IMPACT_TAG = 'worldImpact'; |
| 62 |
| 63 class ResolutionImpactSerializer extends SerializerPlugin { |
| 64 final Resolution resolution; |
| 65 |
| 66 ResolutionImpactSerializer(this.resolution); |
| 67 |
| 68 @override |
| 69 void onElement(Element element, ObjectEncoder createEncoder(String tag)) { |
| 70 if (resolution.hasBeenResolved(element)) { |
| 71 ResolutionImpact impact = resolution.getResolutionImpact(element); |
| 72 ObjectEncoder encoder = createEncoder(WORLD_IMPACT_TAG); |
| 73 new ImpactSerializer(encoder).serialize(impact); |
| 74 } |
| 75 } |
| 76 } |
| 77 |
| 78 class ResolutionImpactDeserializer extends DeserializerPlugin { |
| 79 Map<Element, ResolutionImpact> impactMap = <Element, ResolutionImpact>{}; |
| 80 |
| 81 @override |
| 82 void onElement(Element element, ObjectDecoder getDecoder(String tag)) { |
| 83 ObjectDecoder decoder = getDecoder(WORLD_IMPACT_TAG); |
| 84 if (decoder != null) { |
| 85 impactMap[element] = ImpactDeserializer.deserializeImpact(decoder); |
| 86 } |
| 87 } |
| 88 } |
| 89 |
| 90 class _DeserializerSystem extends DeserializerSystem { |
| 91 final Deserializer _deserializer; |
| 92 final List<LibraryElement> deserializedLibraries = <LibraryElement>[]; |
| 93 final ResolutionImpactDeserializer _resolutionImpactDeserializer = |
| 94 new ResolutionImpactDeserializer(); |
| 95 final ImpactTransformer _impactTransformer; |
| 96 |
| 97 _DeserializerSystem(this._deserializer, this._impactTransformer) { |
| 98 _deserializer.plugins.add(_resolutionImpactDeserializer); |
| 99 } |
| 100 |
| 101 LibraryElement readLibrary(Uri resolvedUri) { |
| 102 LibraryElement library = _deserializer.lookupLibrary(resolvedUri); |
| 103 if (library != null) { |
| 104 deserializedLibraries.add(library); |
| 105 } |
| 106 return library; |
| 107 } |
| 108 |
| 109 ResolutionImpact getResolutionImpact(Element element) { |
| 110 return _resolutionImpactDeserializer.impactMap[element]; |
| 111 } |
| 112 |
| 113 @override |
| 114 WorldImpact computeWorldImpact(Element element) { |
| 115 ResolutionImpact resolutionImpact = getResolutionImpact(element); |
| 116 if (resolutionImpact == null) { |
| 117 print('No impact found for $element (${element.library})'); |
| 118 return const WorldImpact(); |
| 119 } else { |
| 120 return _impactTransformer.transformResolutionImpact(resolutionImpact); |
| 121 } |
| 122 } |
| 123 |
| 124 @override |
| 125 bool isDeserialized(Element element) { |
| 126 return deserializedLibraries.contains(element.library); |
| 127 } |
| 128 } |
OLD | NEW |