Index: pkg/front_end/lib/src/fasta/kernel/utils.dart |
diff --git a/pkg/front_end/lib/src/fasta/kernel/utils.dart b/pkg/front_end/lib/src/fasta/kernel/utils.dart |
index df9748ba387553c27d01cb8690ec00f48eefa775..f77b16dd9d11fd3388a66c7f431f6c0c2d40ca24 100644 |
--- a/pkg/front_end/lib/src/fasta/kernel/utils.dart |
+++ b/pkg/front_end/lib/src/fasta/kernel/utils.dart |
@@ -8,6 +8,7 @@ import 'dart:io'; |
import 'package:front_end/src/scanner/token.dart' show Token; |
import 'package:kernel/ast.dart'; |
import 'package:kernel/binary/ast_to_binary.dart'; |
+import 'package:kernel/binary/limited_ast_to_binary.dart'; |
import 'package:kernel/text/ast_to_text.dart'; |
/// A null-aware alternative to `token.offset`. If [token] is `null`, returns |
@@ -39,3 +40,26 @@ Future<Null> writeProgramToFile(Program program, Uri uri) async { |
await sink.close(); |
} |
} |
+ |
+/// Serialize the libraries in [program] that match [filter]. |
+List<int> serializeProgram(Program program, |
+ {bool filter(Library library), bool excludeUriToSource: false}) { |
+ ByteSink byteSink = new ByteSink(); |
+ BinaryPrinter printer = filter == null && !excludeUriToSource |
+ ? new BinaryPrinter(byteSink) |
+ : new LimitedBinaryPrinter( |
+ byteSink, filter ?? (_) => true, excludeUriToSource); |
+ printer.writeProgramFile(program); |
+ return byteSink.builder.takeBytes(); |
+} |
+ |
+/// A [Sink] that directly writes data into a byte builder. |
+class ByteSink implements Sink<List<int>> { |
+ final BytesBuilder builder = new BytesBuilder(); |
+ |
+ void add(List<int> data) { |
+ builder.add(data); |
+ } |
+ |
+ void close() {} |
+} |