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

Unified Diff: pkg/compiler/lib/src/serialization/system.dart

Issue 1975153002: Support (de)serialization from command-line (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: Updated cf. comments. 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « pkg/compiler/lib/src/options.dart ('k') | pkg/compiler/lib/src/serialization/task.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/compiler/lib/src/serialization/system.dart
diff --git a/tests/compiler/dart2js/serialization/helper.dart b/pkg/compiler/lib/src/serialization/system.dart
similarity index 56%
copy from tests/compiler/dart2js/serialization/helper.dart
copy to pkg/compiler/lib/src/serialization/system.dart
index 8a4aba5f16698f4b3acbebf02121f37f6222fa2b..8db5c4bede2fa7c1552477b06f50802af58745e6 100644
--- a/tests/compiler/dart2js/serialization/helper.dart
+++ b/pkg/compiler/lib/src/serialization/system.dart
@@ -2,177 +2,31 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
-library dart2js.serialization_helper;
+library dart2js.serialization_system;
import 'dart:async';
-import 'dart:io';
-
-import 'package:compiler/src/commandline_options.dart';
-import 'package:compiler/src/common.dart';
-import 'package:compiler/src/common/backend_api.dart';
-import 'package:compiler/src/common/names.dart';
-import 'package:compiler/src/common/resolution.dart';
-import 'package:compiler/src/compiler.dart';
-import 'package:compiler/src/elements/elements.dart';
-import 'package:compiler/src/io/source_file.dart';
-import 'package:compiler/src/scanner/scanner.dart';
-import 'package:compiler/src/script.dart';
-import 'package:compiler/src/serialization/impact_serialization.dart';
-import 'package:compiler/src/serialization/json_serializer.dart';
-import 'package:compiler/src/serialization/modelz.dart';
-import 'package:compiler/src/serialization/resolved_ast_serialization.dart';
-import 'package:compiler/src/serialization/serialization.dart';
-import 'package:compiler/src/serialization/task.dart';
-import 'package:compiler/src/tokens/token.dart';
-import 'package:compiler/src/universe/call_structure.dart';
-import 'package:compiler/src/universe/world_impact.dart';
-import 'package:compiler/src/universe/use.dart';
-
-import '../memory_compiler.dart';
-
-class Arguments {
- final String filename;
- final int index;
- final bool loadSerializedData;
- final bool saveSerializedData;
- final String serializedDataFileName;
- final bool verbose;
-
- const Arguments({
- this.filename,
- this.index,
- this.loadSerializedData: false,
- this.saveSerializedData: false,
- this.serializedDataFileName: 'out.data',
- this.verbose: false});
-
- factory Arguments.from(List<String> arguments) {
- String filename;
- int index;
- for (String arg in arguments) {
- if (!arg.startsWith('-')) {
- index = int.parse(arg);
- if (index == null) {
- filename = arg;
- }
- }
- }
- bool verbose = arguments.contains('-v');
- bool loadSerializedData = arguments.contains('-l');
- bool saveSerializedData = arguments.contains('-s');
- return new Arguments(
- filename: filename,
- index: index,
- verbose: verbose,
- loadSerializedData: loadSerializedData,
- saveSerializedData: saveSerializedData);
- }
-}
-
-
-Future<String> serializeDartCore(
- {Arguments arguments: const Arguments()}) async {
- print('------------------------------------------------------------------');
- print('serialize dart:core');
- print('------------------------------------------------------------------');
- String serializedData;
- if (arguments.loadSerializedData) {
- File file = new File(arguments.serializedDataFileName);
- if (file.existsSync()) {
- print('Loading data from $file');
- serializedData = file.readAsStringSync();
- }
- }
- if (serializedData == null) {
- Compiler compiler = compilerFor(
- options: [Flags.analyzeAll]);
- compiler.serialization.supportSerialization = true;
- await compiler.run(Uris.dart_core);
- serializedData = serialize(
- compiler,
- compiler.libraryLoader.libraries)
- .toText(const JsonSerializationEncoder());
- if (arguments.saveSerializedData) {
- File file = new File(arguments.serializedDataFileName);
- print('Saving data to $file');
- file.writeAsStringSync(serializedData);
- }
- }
- return serializedData;
-}
-
-Serializer serialize(
- Compiler compiler,
- Iterable<LibraryElement> libraries) {
- assert(compiler.serialization.supportSerialization);
-
- Serializer serializer = new Serializer();
- SerializerPlugin backendSerializer =
- compiler.backend.serialization.serializer;
- serializer.plugins.add(backendSerializer);
- serializer.plugins.add(new ResolutionImpactSerializer(
- compiler.resolution, backendSerializer));
- serializer.plugins.add(new ResolvedAstSerializerPlugin(
- compiler.resolution, backendSerializer));
-
- for (LibraryElement library in libraries) {
- serializer.serialize(library);
- }
- return serializer;
-}
-
-void deserialize(Compiler compiler,
- String serializedData) {
- Deserializer deserializer = new Deserializer.fromText(
- new DeserializationContext(),
- serializedData,
- const JsonSerializationDecoder());
- deserializer.plugins.add(compiler.backend.serialization.deserializer);
- compiler.serialization.deserializer =
- new _DeserializerSystem(
- compiler,
- deserializer,
- compiler.backend.impactTransformer);
-}
-
-
-const String WORLD_IMPACT_TAG = 'worldImpact';
-
-class ResolutionImpactSerializer extends SerializerPlugin {
- final Resolution resolution;
- final SerializerPlugin nativeDataSerializer;
-
- ResolutionImpactSerializer(this.resolution, this.nativeDataSerializer);
-
- @override
- void onElement(Element element, ObjectEncoder createEncoder(String tag)) {
- if (resolution.hasBeenResolved(element)) {
- ResolutionImpact impact = resolution.getResolutionImpact(element);
- ObjectEncoder encoder = createEncoder(WORLD_IMPACT_TAG);
- new ImpactSerializer(element, encoder, nativeDataSerializer)
- .serialize(impact);
- }
- }
-}
-
-class ResolutionImpactDeserializer extends DeserializerPlugin {
- Map<Element, ResolutionImpact> impactMap = <Element, ResolutionImpact>{};
- final DeserializerPlugin nativeDataDeserializer;
-
- ResolutionImpactDeserializer(this.nativeDataDeserializer);
-
- @override
- void onElement(Element element, ObjectDecoder getDecoder(String tag)) {
- ObjectDecoder decoder = getDecoder(WORLD_IMPACT_TAG);
- if (decoder != null) {
- impactMap[element] =
- ImpactDeserializer.deserializeImpact(
- element, decoder, nativeDataDeserializer);
- }
- }
-}
-
-class _DeserializerSystem extends DeserializerSystem {
+import '../commandline_options.dart';
+import '../common.dart';
+import '../common/backend_api.dart';
+import '../common/names.dart';
+import '../common/resolution.dart';
+import '../compiler.dart';
+import '../elements/elements.dart';
+import '../io/source_file.dart';
+import '../scanner/scanner.dart';
+import '../script.dart';
+import '../serialization/impact_serialization.dart';
+import '../tokens/token.dart';
+import '../universe/call_structure.dart';
+import '../universe/world_impact.dart';
+import '../universe/use.dart';
+import 'json_serializer.dart';
+import 'modelz.dart';
+import 'resolved_ast_serialization.dart';
+import 'serialization.dart';
+import 'task.dart';
+
+class DeserializerSystemImpl extends DeserializerSystem {
final Compiler _compiler;
final Deserializer _deserializer;
final List<LibraryElement> deserializedLibraries = <LibraryElement>[];
@@ -180,9 +34,7 @@ class _DeserializerSystem extends DeserializerSystem {
final ResolvedAstDeserializerPlugin _resolvedAstDeserializer;
final ImpactTransformer _impactTransformer;
- factory _DeserializerSystem(
- Compiler compiler,
- Deserializer deserializer,
+ factory DeserializerSystemImpl(Compiler compiler, Deserializer deserializer,
ImpactTransformer impactTransformer) {
List<DeserializerPlugin> plugins = <DeserializerPlugin>[];
DeserializerPlugin backendDeserializer =
@@ -191,11 +43,11 @@ class _DeserializerSystem extends DeserializerSystem {
ResolutionImpactDeserializer resolutionImpactDeserializer =
new ResolutionImpactDeserializer(backendDeserializer);
deserializer.plugins.add(resolutionImpactDeserializer);
- ResolvedAstDeserializerPlugin resolvedAstDeserializer
- = new ResolvedAstDeserializerPlugin(
+ ResolvedAstDeserializerPlugin resolvedAstDeserializer =
+ new ResolvedAstDeserializerPlugin(
compiler.parsingContext, backendDeserializer);
deserializer.plugins.add(resolvedAstDeserializer);
- return new _DeserializerSystem._(
+ return new DeserializerSystemImpl._(
compiler,
deserializer,
impactTransformer,
@@ -203,14 +55,13 @@ class _DeserializerSystem extends DeserializerSystem {
resolvedAstDeserializer);
}
- _DeserializerSystem._(
+ DeserializerSystemImpl._(
this._compiler,
this._deserializer,
this._impactTransformer,
this._resolutionImpactDeserializer,
this._resolvedAstDeserializer);
-
@override
Future<LibraryElement> readLibrary(Uri resolvedUri) {
LibraryElement library = _deserializer.lookupLibrary(resolvedUri);
@@ -219,7 +70,8 @@ class _DeserializerSystem extends DeserializerSystem {
return Future.forEach(library.compilationUnits,
(CompilationUnitElement compilationUnit) {
ScriptZ script = compilationUnit.script;
- return _compiler.readScript(script.readableUri)
+ return _compiler
+ .readScript(script.readableUri)
.then((Script newScript) {
script.file = newScript.file;
_resolvedAstDeserializer.sourceFiles[script.resourceUri] =
@@ -244,7 +96,7 @@ class _DeserializerSystem extends DeserializerSystem {
@override
bool hasResolutionImpact(Element element) {
if (element.isConstructor &&
- element.enclosingClass.isUnnamedMixinApplication) {
+ element.enclosingClass.isUnnamedMixinApplication) {
return true;
}
return _resolutionImpactDeserializer.impactMap.containsKey(element);
@@ -254,18 +106,19 @@ class _DeserializerSystem extends DeserializerSystem {
ResolutionImpact getResolutionImpact(Element element) {
if (element.isConstructor &&
element.enclosingClass.isUnnamedMixinApplication) {
- ClassElement superclass = element.enclosingClass.superclass;
+ ClassElement superclass = element.enclosingClass.superclass;
ConstructorElement superclassConstructor =
superclass.lookupConstructor(element.name);
assert(invariant(element, superclassConstructor != null,
message: "Superclass constructor '${element.name}' called from "
- "${element} not found in ${superclass}."));
+ "${element} not found in ${superclass}."));
// TODO(johnniwinther): Compute callStructure. Currently not used.
CallStructure callStructure;
return _resolutionImpactDeserializer.impactMap.putIfAbsent(element, () {
- return new DeserializedResolutionImpact(
- staticUses: <StaticUse>[new StaticUse.superConstructorInvoke(
- superclassConstructor, callStructure)]);
+ return new DeserializedResolutionImpact(staticUses: <StaticUse>[
+ new StaticUse.superConstructorInvoke(
+ superclassConstructor, callStructure)
+ ]);
});
}
return _resolutionImpactDeserializer.impactMap[element];
@@ -288,6 +141,41 @@ class _DeserializerSystem extends DeserializerSystem {
}
}
+const String WORLD_IMPACT_TAG = 'worldImpact';
+
+class ResolutionImpactSerializer extends SerializerPlugin {
+ final Resolution resolution;
+ final SerializerPlugin nativeDataSerializer;
+
+ ResolutionImpactSerializer(this.resolution, this.nativeDataSerializer);
+
+ @override
+ void onElement(Element element, ObjectEncoder createEncoder(String tag)) {
+ if (resolution.hasBeenResolved(element)) {
+ ResolutionImpact impact = resolution.getResolutionImpact(element);
+ ObjectEncoder encoder = createEncoder(WORLD_IMPACT_TAG);
+ new ImpactSerializer(element, encoder, nativeDataSerializer)
+ .serialize(impact);
+ }
+ }
+}
+
+class ResolutionImpactDeserializer extends DeserializerPlugin {
+ Map<Element, ResolutionImpact> impactMap = <Element, ResolutionImpact>{};
+ final DeserializerPlugin nativeDataDeserializer;
+
+ ResolutionImpactDeserializer(this.nativeDataDeserializer);
+
+ @override
+ void onElement(Element element, ObjectDecoder getDecoder(String tag)) {
+ ObjectDecoder decoder = getDecoder(WORLD_IMPACT_TAG);
+ if (decoder != null) {
+ impactMap[element] = ImpactDeserializer.deserializeImpact(
+ element, decoder, nativeDataDeserializer);
+ }
+ }
+}
+
const String RESOLVED_AST_TAG = 'resolvedAst';
class ResolvedAstSerializerPlugin extends SerializerPlugin {
@@ -306,9 +194,8 @@ class ResolvedAstSerializerPlugin extends SerializerPlugin {
ResolvedAst resolvedAst = resolution.getResolvedAst(element);
ObjectEncoder objectEncoder = createEncoder(RESOLVED_AST_TAG);
new ResolvedAstSerializer(
- objectEncoder,
- resolvedAst,
- nativeDataSerializer).serialize();
+ objectEncoder, resolvedAst, nativeDataSerializer)
+ .serialize();
}
}
}
@@ -336,9 +223,8 @@ class ResolvedAstDeserializerPlugin extends DeserializerPlugin {
ObjectDecoder decoder = _decoderMap[element.memberContext];
if (decoder != null) {
- ResolvedAstDeserializer.deserialize(
- element.memberContext, decoder, parsingContext, findToken,
- nativeDataDeserializer);
+ ResolvedAstDeserializer.deserialize(element.memberContext, decoder,
+ parsingContext, findToken, nativeDataDeserializer);
_decoderMap.remove(element);
assert(invariant(element, element.hasResolvedAst,
message: "ResolvedAst not computed for $element."));
@@ -352,7 +238,7 @@ class ResolvedAstDeserializerPlugin extends DeserializerPlugin {
SourceFile sourceFile = sourceFiles[uri];
if (sourceFile == null) {
throw 'No source file found for $uri in:\n '
- '${sourceFiles.keys.join('\n ')}';
+ '${sourceFiles.keys.join('\n ')}';
}
return new Scanner(sourceFile).tokenize();
});
@@ -367,4 +253,3 @@ class ResolvedAstDeserializerPlugin extends DeserializerPlugin {
}
}
}
-
« no previous file with comments | « pkg/compiler/lib/src/options.dart ('k') | pkg/compiler/lib/src/serialization/task.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698