OLD | NEW |
1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2017, 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 import 'dart:async'; | 5 import 'dart:async'; |
6 import 'dart:io'; | 6 import 'dart:io'; |
7 | 7 |
8 import 'package:front_end/src/scanner/token.dart' show Token; | 8 import 'package:front_end/src/scanner/token.dart' show Token; |
9 import 'package:kernel/ast.dart'; | 9 import 'package:kernel/ast.dart'; |
10 import 'package:kernel/binary/ast_to_binary.dart'; | 10 import 'package:kernel/binary/ast_to_binary.dart'; |
| 11 import 'package:kernel/binary/limited_ast_to_binary.dart'; |
11 import 'package:kernel/text/ast_to_text.dart'; | 12 import 'package:kernel/text/ast_to_text.dart'; |
12 | 13 |
13 /// A null-aware alternative to `token.offset`. If [token] is `null`, returns | 14 /// A null-aware alternative to `token.offset`. If [token] is `null`, returns |
14 /// `TreeNode.noOffset`. | 15 /// `TreeNode.noOffset`. |
15 int offsetForToken(Token token) => | 16 int offsetForToken(Token token) => |
16 token == null ? TreeNode.noOffset : token.offset; | 17 token == null ? TreeNode.noOffset : token.offset; |
17 | 18 |
18 /// Print the given [program]. Do nothing if it is `null`. If the | 19 /// Print the given [program]. Do nothing if it is `null`. If the |
19 /// [libraryFilter] is provided, then only libraries that satisfy it are | 20 /// [libraryFilter] is provided, then only libraries that satisfy it are |
20 /// printed. | 21 /// printed. |
(...skipping 11 matching lines...) Expand all Loading... |
32 Future<Null> writeProgramToFile(Program program, Uri uri) async { | 33 Future<Null> writeProgramToFile(Program program, Uri uri) async { |
33 File output = new File.fromUri(uri); | 34 File output = new File.fromUri(uri); |
34 IOSink sink = output.openWrite(); | 35 IOSink sink = output.openWrite(); |
35 try { | 36 try { |
36 new BinaryPrinter(sink).writeProgramFile(program); | 37 new BinaryPrinter(sink).writeProgramFile(program); |
37 program.unbindCanonicalNames(); | 38 program.unbindCanonicalNames(); |
38 } finally { | 39 } finally { |
39 await sink.close(); | 40 await sink.close(); |
40 } | 41 } |
41 } | 42 } |
| 43 |
| 44 /// Serialize the libraries in [program] that match [filter]. |
| 45 List<int> serializeProgram(Program program, |
| 46 {bool filter(Library library), bool excludeUriToSource: false}) { |
| 47 ByteSink byteSink = new ByteSink(); |
| 48 BinaryPrinter printer = filter == null && !excludeUriToSource |
| 49 ? new BinaryPrinter(byteSink) |
| 50 : new LimitedBinaryPrinter( |
| 51 byteSink, filter ?? (_) => true, excludeUriToSource); |
| 52 printer.writeProgramFile(program); |
| 53 return byteSink.builder.takeBytes(); |
| 54 } |
| 55 |
| 56 /// A [Sink] that directly writes data into a byte builder. |
| 57 class ByteSink implements Sink<List<int>> { |
| 58 final BytesBuilder builder = new BytesBuilder(); |
| 59 |
| 60 void add(List<int> data) { |
| 61 builder.add(data); |
| 62 } |
| 63 |
| 64 void close() {} |
| 65 } |
OLD | NEW |