Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(179)

Side by Side Diff: pkg/compiler/lib/src/serialization/task.dart

Issue 1975153002: Support (de)serialization from command-line (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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.task; 5 library dart2js.serialization.task;
6 6
7 import 'dart:async' show Future; 7 import 'dart:async' show EventSink, Future;
8 import '../common/resolution.dart' show ResolutionImpact, ResolutionWorkItem; 8 import '../common/resolution.dart' show ResolutionImpact, ResolutionWorkItem;
9 import '../common/tasks.dart' show CompilerTask; 9 import '../common/tasks.dart' show CompilerTask;
10 import '../common/work.dart' show ItemCompilationContext; 10 import '../common/work.dart' show ItemCompilationContext;
11 import '../compiler.dart' show Compiler; 11 import '../compiler.dart' show Compiler;
12 import '../elements/elements.dart'; 12 import '../elements/elements.dart';
13 import '../enqueue.dart' show ResolutionEnqueuer; 13 import '../enqueue.dart' show ResolutionEnqueuer;
14 import '../universe/world_impact.dart' show WorldImpact; 14 import '../universe/world_impact.dart' show WorldImpact;
15 import 'json_serializer.dart';
16 import 'serialization.dart';
17 import 'system.dart';
15 18
16 /// A deserializer that can load a library element by reading it's information 19 /// A deserializer that can load a library element by reading it's information
17 /// from a serialized form. 20 /// from a serialized form.
18 abstract class LibraryDeserializer { 21 abstract class LibraryDeserializer {
19 /// Loads the [LibraryElement] associated with a library under [uri], or null 22 /// Loads the [LibraryElement] associated with a library under [uri], or null
20 /// if no serialized information is available for the given library. 23 /// if no serialized information is available for the given library.
21 Future<LibraryElement> readLibrary(Uri uri); 24 Future<LibraryElement> readLibrary(Uri uri);
22 } 25 }
23 26
24 /// Task that supports deserialization of elements. 27 /// Task that supports deserialization of elements.
25 class SerializationTask extends CompilerTask implements LibraryDeserializer { 28 class SerializationTask extends CompilerTask implements LibraryDeserializer {
26 SerializationTask(Compiler compiler) : super(compiler); 29 SerializationTask(Compiler compiler) : super(compiler);
27 30
28 DeserializerSystem deserializer; 31 DeserializerSystem system;
Siggi Cherem (dart-lang) 2016/05/14 01:10:31 I sort of prefer the old name here.
Johnni Winther 2016/05/17 12:37:33 Done.
29 32
30 String get name => 'Serialization'; 33 String get name => 'Serialization';
31 34
32 /// If `true`, data must be retained to support serialization. 35 /// If `true`, data must be retained to support serialization.
33 // TODO(johnniwinther): Make this more precise in terms of what needs to be 36 // TODO(johnniwinther): Make this more precise in terms of what needs to be
34 // retained, for instance impacts, resolution data etc. 37 // retained, for instance impacts, resolution data etc.
35 bool supportSerialization = false; 38 bool supportSerialization = false;
36 39
37 /// If `true`, deserialized data is supported. 40 /// If `true`, deserialized data is supported.
38 bool get supportsDeserialization => deserializer != null; 41 bool get supportsDeserialization => system != null;
39 42
40 /// Returns the [LibraryElement] for [resolvedUri] if available from 43 /// Returns the [LibraryElement] for [resolvedUri] if available from
41 /// serialization. 44 /// serialization.
42 Future<LibraryElement> readLibrary(Uri resolvedUri) { 45 Future<LibraryElement> readLibrary(Uri resolvedUri) {
43 if (deserializer == null) return new Future<LibraryElement>.value(); 46 if (system == null) return new Future<LibraryElement>.value();
44 return deserializer.readLibrary(resolvedUri); 47 return system.readLibrary(resolvedUri);
45 } 48 }
46 49
47 /// Returns `true` if [element] has been deserialized. 50 /// Returns `true` if [element] has been deserialized.
48 bool isDeserialized(Element element) { 51 bool isDeserialized(Element element) {
49 return deserializer != null && deserializer.isDeserialized(element); 52 return system != null && system.isDeserialized(element);
50 } 53 }
51 54
52 bool hasResolutionImpact(Element element) { 55 bool hasResolutionImpact(Element element) {
53 return deserializer != null && deserializer.hasResolutionImpact(element); 56 return system != null && system.hasResolutionImpact(element);
54 } 57 }
55 58
56 ResolutionImpact getResolutionImpact(Element element) { 59 ResolutionImpact getResolutionImpact(Element element) {
57 return deserializer != null 60 return system != null ? system.getResolutionImpact(element) : null;
58 ? deserializer.getResolutionImpact(element)
59 : null;
60 } 61 }
61 62
62 /// Creates the [ResolutionWorkItem] for the deserialized [element]. 63 /// Creates the [ResolutionWorkItem] for the deserialized [element].
63 ResolutionWorkItem createResolutionWorkItem( 64 ResolutionWorkItem createResolutionWorkItem(
64 Element element, ItemCompilationContext context) { 65 Element element, ItemCompilationContext context) {
65 assert(deserializer != null); 66 assert(system != null);
66 assert(isDeserialized(element)); 67 assert(isDeserialized(element));
67 return new DeserializedResolutionWorkItem( 68 return new DeserializedResolutionWorkItem(
68 element, context, deserializer.computeWorldImpact(element)); 69 element, context, system.computeWorldImpact(element));
69 } 70 }
70 71
71 bool hasResolvedAst(ExecutableElement element) { 72 bool hasResolvedAst(ExecutableElement element) {
72 return deserializer != null ? deserializer.hasResolvedAst(element) : false; 73 return system != null ? system.hasResolvedAst(element) : false;
73 } 74 }
74 75
75 ResolvedAst getResolvedAst(ExecutableElement element) { 76 ResolvedAst getResolvedAst(ExecutableElement element) {
76 return deserializer != null ? deserializer.getResolvedAst(element) : null; 77 return system != null ? system.getResolvedAst(element) : null;
78 }
79
80 Serializer createSerializer(Iterable<LibraryElement> libraries) {
81 return measure(() {
82 assert(supportSerialization);
83
84 Serializer serializer = new Serializer();
85 SerializerPlugin backendSerializer =
86 compiler.backend.serialization.serializer;
87 serializer.plugins.add(backendSerializer);
88 serializer.plugins.add(new ResolutionImpactSerializer(
89 compiler.resolution, backendSerializer));
90 serializer.plugins.add(new ResolvedAstSerializerPlugin(
91 compiler.resolution, backendSerializer));
92
93 for (LibraryElement library in libraries) {
94 serializer.serialize(library);
95 }
96 return serializer;
97 });
98 }
99
100 void serializeToSink(
101 EventSink<String> sink, Iterable<LibraryElement> libraries) {
102 measure(() {
103 sink
104 ..add(createSerializer(libraries)
105 .toText(const JsonSerializationEncoder()))
106 ..close();
107 });
108 }
109
110 void deserializeFromText(String serializedData) {
111 measure(() {
112 Deserializer deserializer = new Deserializer.fromText(
113 new DeserializationContext(),
114 serializedData,
115 const JsonSerializationDecoder());
116 deserializer.plugins.add(compiler.backend.serialization.deserializer);
117 system = new DeserializerSystemImpl(
118 compiler, deserializer, compiler.backend.impactTransformer);
119 });
77 } 120 }
78 } 121 }
79 122
80 /// A [ResolutionWorkItem] for a deserialized element. 123 /// A [ResolutionWorkItem] for a deserialized element.
81 /// 124 ///
82 /// This will not resolve the element but only compute the [WorldImpact]. 125 /// This will not resolve the element but only compute the [WorldImpact].
83 class DeserializedResolutionWorkItem implements ResolutionWorkItem { 126 class DeserializedResolutionWorkItem implements ResolutionWorkItem {
84 final Element element; 127 final Element element;
85 final ItemCompilationContext compilationContext; 128 final ItemCompilationContext compilationContext;
86 final WorldImpact worldImpact; 129 final WorldImpact worldImpact;
(...skipping 17 matching lines...) Expand all
104 /// elements. 147 /// elements.
105 abstract class DeserializerSystem { 148 abstract class DeserializerSystem {
106 Future<LibraryElement> readLibrary(Uri resolvedUri); 149 Future<LibraryElement> readLibrary(Uri resolvedUri);
107 bool isDeserialized(Element element); 150 bool isDeserialized(Element element);
108 bool hasResolvedAst(ExecutableElement element); 151 bool hasResolvedAst(ExecutableElement element);
109 ResolvedAst getResolvedAst(ExecutableElement element); 152 ResolvedAst getResolvedAst(ExecutableElement element);
110 bool hasResolutionImpact(Element element); 153 bool hasResolutionImpact(Element element);
111 ResolutionImpact getResolutionImpact(Element element); 154 ResolutionImpact getResolutionImpact(Element element);
112 WorldImpact computeWorldImpact(Element element); 155 WorldImpact computeWorldImpact(Element element);
113 } 156 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698