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

Side by Side Diff: pkg/kernel/lib/kernel.dart

Issue 2784303003: Allow dartk to print out binary Kernel IR results to stdout if requested. (Closed)
Patch Set: . Created 3 years, 8 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 unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 /// Conventions for paths: 5 /// Conventions for paths:
6 /// 6 ///
7 /// - Use the [Uri] class for paths that may have the `file`, `dart` or 7 /// - Use the [Uri] class for paths that may have the `file`, `dart` or
8 /// `package` scheme. Never use [Uri] for relative paths. 8 /// `package` scheme. Never use [Uri] for relative paths.
9 /// - Use [String]s for all filenames and paths that have no scheme prefix. 9 /// - Use [String]s for all filenames and paths that have no scheme prefix.
10 /// - Never translate a `dart:` or `package:` URI into a `file:` URI, instead 10 /// - Never translate a `dart:` or `package:` URI into a `file:` URI, instead
(...skipping 11 matching lines...) Expand all
22 22
23 export 'ast.dart'; 23 export 'ast.dart';
24 24
25 Program loadProgramFromBinary(String path, [Program program]) { 25 Program loadProgramFromBinary(String path, [Program program]) {
26 program ??= new Program(); 26 program ??= new Program();
27 new BinaryBuilder(new File(path).readAsBytesSync()).readProgram(program); 27 new BinaryBuilder(new File(path).readAsBytesSync()).readProgram(program);
28 return program; 28 return program;
29 } 29 }
30 30
31 Future writeProgramToBinary(Program program, String path) { 31 Future writeProgramToBinary(Program program, String path) {
32 var sink = new File(path).openWrite(); 32 var sink;
33 if (path == 'null' || path == 'stdout') {
34 sink = new _ByteBuffer();
asgerf 2017/03/31 08:40:16 Is the wrapper necessary? Could you try this inst
Emily Fortuna 2017/03/31 17:46:55 Ah, that's MUCH better. I didn't know about that.
35 } else {
36 sink = new File(path).openWrite();
37 }
38
33 var future; 39 var future;
34 try { 40 try {
35 new BinaryPrinter(sink).writeProgramFile(program); 41 new BinaryPrinter(sink).writeProgramFile(program);
36 } finally { 42 } finally {
37 future = sink.close(); 43 future = sink.close();
38 } 44 }
45
46 if (sink is _ByteBuffer) print(sink.takeBytes());
asgerf 2017/03/31 08:40:16 Calling toString on a List<int> normally emits som
Emily Fortuna 2017/03/31 17:46:56 touche. No I hadn't actually tested consuming this
39 return future; 47 return future;
40 } 48 }
41 49
42 void writeLibraryToText(Library library, {String path}) { 50 void writeLibraryToText(Library library, {String path}) {
43 StringBuffer buffer = new StringBuffer(); 51 StringBuffer buffer = new StringBuffer();
44 new Printer(buffer).writeLibraryFile(library); 52 new Printer(buffer).writeLibraryFile(library);
45 if (path == null) { 53 if (path == null) {
46 print(buffer); 54 print(buffer);
47 } else { 55 } else {
48 new File(path).writeAsStringSync('$buffer'); 56 new File(path).writeAsStringSync('$buffer');
49 } 57 }
50 } 58 }
51 59
52 void writeProgramToText(Program program, 60 void writeProgramToText(Program program,
53 {String path, bool showExternal: false, bool showOffsets: false}) { 61 {String path, bool showExternal: false, bool showOffsets: false}) {
54 StringBuffer buffer = new StringBuffer(); 62 StringBuffer buffer = new StringBuffer();
55 new Printer(buffer, showExternal: showExternal, showOffsets: showOffsets) 63 new Printer(buffer, showExternal: showExternal, showOffsets: showOffsets)
56 .writeProgramFile(program); 64 .writeProgramFile(program);
57 if (path == null) { 65 if (path == null) {
58 print(buffer); 66 print(buffer);
59 } else { 67 } else {
60 new File(path).writeAsStringSync('$buffer'); 68 new File(path).writeAsStringSync('$buffer');
61 } 69 }
62 } 70 }
71
72 /// Silly wrapper around BytesBuilder that conforms to the Sink interface.
Johnni Winther 2017/03/31 06:38:35 'Silly' -> 'Simple'
Emily Fortuna 2017/03/31 17:46:56 Acknowledged.
73 class _ByteBuffer implements Sink<List<int>> {
74 BytesBuilder _builder;
75 bool _open;
76
77 _ByteBuffer()
78 : _open = true,
79 _builder = new BytesBuilder();
asgerf 2017/03/31 08:40:16 You can pass `copy: false` to the BytesBuilder her
Emily Fortuna 2017/03/31 17:46:56 No longer needed once I took this out.
80
81 void add(List<int> data) {
82 if (_open) {
83 _builder.add(data);
84 }
85 }
86
87 void close() {
88 _open = false;
89 }
90
91 List<int> takeBytes() => _builder.takeBytes();
92 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698