OLD | NEW |
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 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 | 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_helper; | 5 library dart2js.serialization_helper; |
6 | 6 |
| 7 import 'dart:io'; |
7 import 'dart:async'; | 8 import 'dart:async'; |
8 import 'package:async_helper/async_helper.dart'; | 9 import 'package:async_helper/async_helper.dart'; |
9 import 'package:expect/expect.dart'; | 10 import 'package:expect/expect.dart'; |
10 import 'package:compiler/compiler_new.dart'; | 11 import 'package:compiler/compiler_new.dart'; |
| 12 import 'package:compiler/src/common.dart'; |
11 import 'package:compiler/src/commandline_options.dart'; | 13 import 'package:compiler/src/commandline_options.dart'; |
12 import 'package:compiler/src/common/backend_api.dart'; | 14 import 'package:compiler/src/common/backend_api.dart'; |
13 import 'package:compiler/src/common/names.dart'; | 15 import 'package:compiler/src/common/names.dart'; |
14 import 'package:compiler/src/common/resolution.dart'; | 16 import 'package:compiler/src/common/resolution.dart'; |
15 import 'package:compiler/src/compiler.dart'; | 17 import 'package:compiler/src/compiler.dart'; |
16 import 'package:compiler/src/elements/elements.dart'; | 18 import 'package:compiler/src/elements/elements.dart'; |
17 import 'package:compiler/src/filenames.dart'; | 19 import 'package:compiler/src/filenames.dart'; |
18 import 'package:compiler/src/io/source_file.dart'; | 20 import 'package:compiler/src/io/source_file.dart'; |
19 import 'package:compiler/src/scanner/scanner.dart'; | 21 import 'package:compiler/src/scanner/scanner.dart'; |
20 import 'package:compiler/src/serialization/element_serialization.dart'; | 22 import 'package:compiler/src/serialization/element_serialization.dart'; |
21 import 'package:compiler/src/serialization/impact_serialization.dart'; | 23 import 'package:compiler/src/serialization/impact_serialization.dart'; |
22 import 'package:compiler/src/serialization/json_serializer.dart'; | 24 import 'package:compiler/src/serialization/json_serializer.dart'; |
23 import 'package:compiler/src/serialization/resolved_ast_serialization.dart'; | 25 import 'package:compiler/src/serialization/resolved_ast_serialization.dart'; |
24 import 'package:compiler/src/serialization/serialization.dart'; | 26 import 'package:compiler/src/serialization/serialization.dart'; |
25 import 'package:compiler/src/serialization/modelz.dart'; | 27 import 'package:compiler/src/serialization/modelz.dart'; |
26 import 'package:compiler/src/serialization/task.dart'; | 28 import 'package:compiler/src/serialization/task.dart'; |
27 import 'package:compiler/src/tokens/token.dart'; | 29 import 'package:compiler/src/tokens/token.dart'; |
28 import 'package:compiler/src/script.dart'; | 30 import 'package:compiler/src/script.dart'; |
29 import 'package:compiler/src/universe/world_impact.dart'; | 31 import 'package:compiler/src/universe/world_impact.dart'; |
30 import 'memory_compiler.dart'; | 32 import 'memory_compiler.dart'; |
31 | 33 |
| 34 class Arguments { |
| 35 final String filename; |
| 36 final bool loadSerializedData; |
| 37 final bool saveSerializedData; |
| 38 final String serializedDataFileName; |
| 39 final bool verbose; |
32 | 40 |
33 Future<String> serializeDartCore({bool serializeResolvedAst: false}) async { | 41 const Arguments({ |
34 Compiler compiler = compilerFor( | 42 this.filename, |
35 options: [Flags.analyzeAll]); | 43 this.loadSerializedData: false, |
36 compiler.serialization.supportSerialization = true; | 44 this.saveSerializedData: false, |
37 await compiler.run(Uris.dart_core); | 45 this.serializedDataFileName: 'out.data', |
38 return serialize( | 46 this.verbose: false}); |
39 compiler, | 47 |
40 compiler.libraryLoader.libraries, | 48 factory Arguments.from(List<String> arguments) { |
41 serializeResolvedAst: serializeResolvedAst) | 49 String filename; |
42 .toText(const JsonSerializationEncoder()); | 50 for (String arg in arguments) { |
| 51 if (!arg.startsWith('-')) { |
| 52 filename = arg; |
| 53 } |
| 54 } |
| 55 bool verbose = arguments.contains('-v'); |
| 56 bool loadSerializedData = arguments.contains('-l'); |
| 57 bool saveSerializedData = arguments.contains('-s'); |
| 58 return new Arguments( |
| 59 filename: filename, |
| 60 verbose: verbose, |
| 61 loadSerializedData: loadSerializedData, |
| 62 saveSerializedData: saveSerializedData); |
| 63 } |
| 64 } |
| 65 |
| 66 |
| 67 Future<String> serializeDartCore( |
| 68 {Arguments arguments: const Arguments(), |
| 69 bool serializeResolvedAst: false}) async { |
| 70 print('------------------------------------------------------------------'); |
| 71 print('serialize dart:core'); |
| 72 print('------------------------------------------------------------------'); |
| 73 String serializedData; |
| 74 if (arguments.loadSerializedData) { |
| 75 File file = new File(arguments.serializedDataFileName); |
| 76 if (file.existsSync()) { |
| 77 print('Loading data from $file'); |
| 78 serializedData = file.readAsStringSync(); |
| 79 } |
| 80 } |
| 81 if (serializedData == null) { |
| 82 Compiler compiler = compilerFor( |
| 83 options: [Flags.analyzeAll]); |
| 84 compiler.serialization.supportSerialization = true; |
| 85 await compiler.run(Uris.dart_core); |
| 86 serializedData = serialize( |
| 87 compiler, |
| 88 compiler.libraryLoader.libraries, |
| 89 serializeResolvedAst: serializeResolvedAst) |
| 90 .toText(const JsonSerializationEncoder()); |
| 91 if (arguments.saveSerializedData) { |
| 92 File file = new File(arguments.serializedDataFileName); |
| 93 print('Saving data to $file'); |
| 94 file.writeAsStringSync(serializedData); |
| 95 } |
| 96 } |
| 97 return serializedData; |
43 } | 98 } |
44 | 99 |
45 Serializer serialize( | 100 Serializer serialize( |
46 Compiler compiler, | 101 Compiler compiler, |
47 Iterable<LibraryElement> libraries, | 102 Iterable<LibraryElement> libraries, |
48 {bool serializeResolvedAst: false}) { | 103 {bool serializeResolvedAst: false}) { |
49 assert(compiler.serialization.supportSerialization); | 104 assert(compiler.serialization.supportSerialization); |
50 | 105 |
51 Serializer serializer = new Serializer(); | 106 Serializer serializer = new Serializer(); |
52 serializer.plugins.add(compiler.backend.serialization.serializer); | 107 serializer.plugins.add(compiler.backend.serialization.serializer); |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
147 _resolvedAstDeserializer.sourceFiles[script.resourceUri] = | 202 _resolvedAstDeserializer.sourceFiles[script.resourceUri] = |
148 newScript.file; | 203 newScript.file; |
149 }); | 204 }); |
150 }).then((_) => library); | 205 }).then((_) => library); |
151 } | 206 } |
152 } | 207 } |
153 return new Future<LibraryElement>.value(library); | 208 return new Future<LibraryElement>.value(library); |
154 } | 209 } |
155 | 210 |
156 @override | 211 @override |
| 212 bool hasResolvedAst(Element element) { |
| 213 if (_resolvedAstDeserializer != null) { |
| 214 return _resolvedAstDeserializer.hasResolvedAst(element); |
| 215 } |
| 216 return false; |
| 217 } |
| 218 |
| 219 @override |
157 ResolvedAst getResolvedAst(Element element) { | 220 ResolvedAst getResolvedAst(Element element) { |
158 if (_resolvedAstDeserializer != null) { | 221 if (_resolvedAstDeserializer != null) { |
159 return _resolvedAstDeserializer.getResolvedAst(element); | 222 return _resolvedAstDeserializer.getResolvedAst(element); |
160 } | 223 } |
161 return null; | 224 return null; |
162 } | 225 } |
163 | 226 |
164 @override | 227 @override |
165 ResolutionImpact getResolutionImpact(Element element) { | 228 ResolutionImpact getResolutionImpact(Element element) { |
166 return _resolutionImpactDeserializer.impactMap[element]; | 229 return _resolutionImpactDeserializer.impactMap[element]; |
(...skipping 18 matching lines...) Expand all Loading... |
185 | 248 |
186 const String RESOLVED_AST_TAG = 'resolvedAst'; | 249 const String RESOLVED_AST_TAG = 'resolvedAst'; |
187 | 250 |
188 class ResolvedAstSerializerPlugin extends SerializerPlugin { | 251 class ResolvedAstSerializerPlugin extends SerializerPlugin { |
189 final Resolution resolution; | 252 final Resolution resolution; |
190 | 253 |
191 ResolvedAstSerializerPlugin(this.resolution); | 254 ResolvedAstSerializerPlugin(this.resolution); |
192 | 255 |
193 @override | 256 @override |
194 void onElement(Element element, ObjectEncoder createEncoder(String tag)) { | 257 void onElement(Element element, ObjectEncoder createEncoder(String tag)) { |
195 if (element is MemberElement && resolution.hasResolvedAst(element)) { | 258 assert(invariant(element, element.isDeclaration, |
| 259 message: "Element $element must be the declaration")); |
| 260 if (element is MemberElement) { |
| 261 assert(invariant(element, resolution.hasResolvedAst(element), |
| 262 message: "Element $element must have a resolved ast")); |
196 ResolvedAst resolvedAst = resolution.getResolvedAst(element); | 263 ResolvedAst resolvedAst = resolution.getResolvedAst(element); |
197 ObjectEncoder objectEncoder = createEncoder(RESOLVED_AST_TAG); | 264 ObjectEncoder objectEncoder = createEncoder(RESOLVED_AST_TAG); |
198 new ResolvedAstSerializer(objectEncoder, resolvedAst).serialize(); | 265 new ResolvedAstSerializer(objectEncoder, resolvedAst).serialize(); |
199 } | 266 } |
200 } | 267 } |
201 } | 268 } |
202 | 269 |
203 class ResolvedAstDeserializerPlugin extends DeserializerPlugin { | 270 class ResolvedAstDeserializerPlugin extends DeserializerPlugin { |
204 final Parsing parsing; | 271 final Parsing parsing; |
205 final Map<Uri, SourceFile> sourceFiles = <Uri, SourceFile>{}; | 272 final Map<Uri, SourceFile> sourceFiles = <Uri, SourceFile>{}; |
206 | 273 |
207 Map<Element, ResolvedAst> _resolvedAstMap = <Element, ResolvedAst>{}; | 274 Map<Element, ResolvedAst> _resolvedAstMap = <Element, ResolvedAst>{}; |
208 Map<Element, ObjectDecoder> _decoderMap = <Element, ObjectDecoder>{}; | 275 Map<Element, ObjectDecoder> _decoderMap = <Element, ObjectDecoder>{}; |
209 Map<Uri, Token> beginTokenMap = <Uri, Token>{}; | 276 Map<Uri, Token> beginTokenMap = <Uri, Token>{}; |
210 | 277 |
211 ResolvedAstDeserializerPlugin(this.parsing); | 278 ResolvedAstDeserializerPlugin(this.parsing); |
212 | 279 |
| 280 bool hasResolvedAst(Element element) { |
| 281 return _resolvedAstMap.containsKey(element) || |
| 282 _decoderMap.containsKey(element); |
| 283 } |
| 284 |
213 ResolvedAst getResolvedAst(Element element) { | 285 ResolvedAst getResolvedAst(Element element) { |
214 ResolvedAst resolvedAst = _resolvedAstMap[element]; | 286 ResolvedAst resolvedAst = _resolvedAstMap[element]; |
215 if (resolvedAst == null) { | 287 if (resolvedAst == null) { |
216 ObjectDecoder decoder = _decoderMap[element]; | 288 ObjectDecoder decoder = _decoderMap[element]; |
217 if (decoder != null) { | 289 if (decoder != null) { |
218 resolvedAst = _resolvedAstMap[element] = | 290 resolvedAst = _resolvedAstMap[element] = |
219 ResolvedAstDeserializer.deserialize( | 291 ResolvedAstDeserializer.deserialize( |
220 element, decoder, parsing, findToken); | 292 element, decoder, parsing, findToken); |
221 _decoderMap.remove(element); | 293 _decoderMap.remove(element); |
222 } | 294 } |
(...skipping 15 matching lines...) Expand all Loading... |
238 | 310 |
239 @override | 311 @override |
240 void onElement(Element element, ObjectDecoder getDecoder(String tag)) { | 312 void onElement(Element element, ObjectDecoder getDecoder(String tag)) { |
241 ObjectDecoder decoder = getDecoder(RESOLVED_AST_TAG); | 313 ObjectDecoder decoder = getDecoder(RESOLVED_AST_TAG); |
242 if (decoder != null) { | 314 if (decoder != null) { |
243 _decoderMap[element] = decoder; | 315 _decoderMap[element] = decoder; |
244 } | 316 } |
245 } | 317 } |
246 } | 318 } |
247 | 319 |
OLD | NEW |