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

Unified Diff: pkg/front_end/tool/_fasta/dump_partial.dart

Issue 2985113002: Add dump-partial tool. (Closed)
Patch Set: Created 3 years, 5 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 | « no previous file | pkg/front_end/tool/fasta » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/front_end/tool/_fasta/dump_partial.dart
diff --git a/pkg/front_end/tool/_fasta/dump_partial.dart b/pkg/front_end/tool/_fasta/dump_partial.dart
new file mode 100644
index 0000000000000000000000000000000000000000..c800523b01457af9ef9048522de46e91e4f881f8
--- /dev/null
+++ b/pkg/front_end/tool/_fasta/dump_partial.dart
@@ -0,0 +1,72 @@
+// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
+// 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.
+
+/// Utility to dump a truncated .dill file generated by the incremental kernel
+/// generator or by the VM's kernel service.
+import 'dart:io';
+import 'package:kernel/kernel.dart';
+import 'package:front_end/src/fasta/kernel/utils.dart';
+
+main(List<String> args) {
+ if (args.length == 0) {
+ print('usage: pkg/front_end/tool/fasta dump_partial '
+ 'partial.dill [extra1.dill] ... [extraN.dill]');
+ exitCode = 1;
+ }
+
+ var program = _loadProgram(args);
+ writeProgramToText(program);
+}
+
+/// Creates a program that contains all of the context code marked as external,
+/// and all libraries defined in partial.dill as they are written in that file.
+Program _loadProgram(List<String> args) {
+ List<int> partialInput = new File(args[0]).readAsBytesSync();
+
+ var context = new Program();
+ for (var i = 1; i < args.length; i++) {
+ loadProgramFromBinary(args[i], context);
+ }
+
+ Set<Uri> libraries = _definedLibraries(partialInput, context);
+
+ // By default `package:kernel/binary/ast_from_binary.dart` merges the contents
+ // of libraries that are mentioned in more than one .dill file. In order to
+ // keep the contents of partial.dill intact, we build a new context that
+ // excludes those libraries defined in partial.dill.
+ List<int> contextBytes = serializeProgram(context,
+ filter: (l) => !libraries.contains(l.importUri));
+ var program = new Program();
+ loadProgramFromBytes(contextBytes, program);
+ _updateIsExternal(program, true);
+ loadProgramFromBytes(partialInput, program);
+ return program;
+}
+
+/// Compute the set of libraries defined in [partialDill].
+///
+/// The [context] program contains all other libraries that may be needed to
+/// properly deserialize partialDill.
+///
+/// Note: This function will mutate [context] in place.
+// TODO(sigmund): simplify and get rid of [context]. We could do that with a
+// custom deserialization, but it will be easier to do once .dill has
+// random-access support.
+Set<Uri> _definedLibraries(List<int> partialDill, Program context) {
+ _updateIsExternal(context, true);
+
+ // This implicitly sets `isExternal = false` on all libraries defined in the
+ // partial.dill file.
+ loadProgramFromBytes(partialDill, context);
+ var result = context.libraries
+ .where((l) => !l.isExternal)
+ .map((l) => l.importUri)
+ .toSet();
+ _updateIsExternal(context, false);
+ return result;
+}
+
+void _updateIsExternal(Program program, bool toValue) {
+ program.libraries.forEach((lib) => lib.isExternal = toValue);
+}
« no previous file with comments | « no previous file | pkg/front_end/tool/fasta » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698