OLD | NEW |
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 library kernel.ast_from_binary; | 4 library kernel.ast_from_binary; |
5 | 5 |
6 import 'dart:convert'; | 6 import 'dart:convert'; |
7 import 'dart:typed_data'; | 7 import 'dart:typed_data'; |
8 | 8 |
9 import '../ast.dart'; | 9 import '../ast.dart'; |
10 import '../transformations/flags.dart'; | 10 import '../transformations/flags.dart'; |
(...skipping 12 matching lines...) Expand all Loading... |
23 | 23 |
24 class BinaryBuilder { | 24 class BinaryBuilder { |
25 final List<VariableDeclaration> variableStack = <VariableDeclaration>[]; | 25 final List<VariableDeclaration> variableStack = <VariableDeclaration>[]; |
26 final List<LabeledStatement> labelStack = <LabeledStatement>[]; | 26 final List<LabeledStatement> labelStack = <LabeledStatement>[]; |
27 int labelStackBase = 0; | 27 int labelStackBase = 0; |
28 final List<SwitchCase> switchCaseStack = <SwitchCase>[]; | 28 final List<SwitchCase> switchCaseStack = <SwitchCase>[]; |
29 final List<TypeParameter> typeParameterStack = <TypeParameter>[]; | 29 final List<TypeParameter> typeParameterStack = <TypeParameter>[]; |
30 final String filename; | 30 final String filename; |
31 final List<int> _bytes; | 31 final List<int> _bytes; |
32 int _byteIndex = 0; | 32 int _byteIndex = 0; |
33 List<String> _stringTable; | 33 final List<String> _stringTable = <String>[]; |
34 List<String> _sourceUriTable; | 34 final List<String> _sourceUriTable = <String>[]; |
35 List<CanonicalName> _linkTable; | 35 List<CanonicalName> _linkTable; |
36 int _transformerFlags = 0; | 36 int _transformerFlags = 0; |
37 Library _currentLibrary; | 37 Library _currentLibrary; |
38 | 38 |
39 // If something goes wrong, this list should indicate what library, | 39 // If something goes wrong, this list should indicate what library, |
40 // class, and member was being built. | 40 // class, and member was being built. |
41 List<String> debugPath = <String>[]; | 41 List<String> debugPath = <String>[]; |
42 | 42 |
43 bool _isReadingLibraryImplementation = false; | 43 bool _isReadingLibraryImplementation = false; |
44 | 44 |
(...skipping 30 matching lines...) Expand all Loading... |
75 readByte(); | 75 readByte(); |
76 } | 76 } |
77 | 77 |
78 List<int> readUtf8Bytes() { | 78 List<int> readUtf8Bytes() { |
79 List<int> bytes = new Uint8List(readUInt()); | 79 List<int> bytes = new Uint8List(readUInt()); |
80 bytes.setRange(0, bytes.length, _bytes, _byteIndex); | 80 bytes.setRange(0, bytes.length, _bytes, _byteIndex); |
81 _byteIndex += bytes.length; | 81 _byteIndex += bytes.length; |
82 return bytes; | 82 return bytes; |
83 } | 83 } |
84 | 84 |
85 String readStringEntry() { | 85 String readStringEntry(int numBytes) { |
86 int numBytes = readUInt(); | |
87 // Utf8Decoder will skip leading BOM characters, but we must preserve them. | 86 // Utf8Decoder will skip leading BOM characters, but we must preserve them. |
88 // Collect leading BOMs before passing the bytes onto Utf8Decoder. | 87 // Collect leading BOMs before passing the bytes onto Utf8Decoder. |
89 int numByteOrderMarks = 0; | 88 int numByteOrderMarks = 0; |
90 while (_byteIndex + 2 < _bytes.length && | 89 while (_byteIndex + 2 < _bytes.length && |
91 _bytes[_byteIndex] == 0xef && | 90 _bytes[_byteIndex] == 0xef && |
92 _bytes[_byteIndex + 1] == 0xbb && | 91 _bytes[_byteIndex + 1] == 0xbb && |
93 _bytes[_byteIndex + 2] == 0xbf) { | 92 _bytes[_byteIndex + 2] == 0xbf) { |
94 ++numByteOrderMarks; | 93 ++numByteOrderMarks; |
95 _byteIndex += 3; | 94 _byteIndex += 3; |
96 numBytes -= 3; | 95 numBytes -= 3; |
97 } | 96 } |
98 String string = | 97 String string = |
99 const Utf8Decoder().convert(_bytes, _byteIndex, _byteIndex + numBytes); | 98 const Utf8Decoder().convert(_bytes, _byteIndex, _byteIndex + numBytes); |
100 _byteIndex += numBytes; | 99 _byteIndex += numBytes; |
101 if (numByteOrderMarks > 0) { | 100 if (numByteOrderMarks > 0) { |
102 return '\ufeff' * numByteOrderMarks + string; | 101 return '\ufeff' * numByteOrderMarks + string; |
103 } | 102 } |
104 return string; | 103 return string; |
105 } | 104 } |
106 | 105 |
107 void readStringTable() { | 106 void readStringTable(List<String> table) { |
| 107 // Read the table of end offsets. |
108 int length = readUInt(); | 108 int length = readUInt(); |
109 _stringTable = new List<String>(length); | 109 List<int> endOffsets = new List<int>(length); |
110 for (int i = 0; i < length; ++i) { | 110 for (int i = 0; i < length; ++i) { |
111 _stringTable[i] = readStringEntry(); | 111 endOffsets[i] = readUInt(); |
| 112 } |
| 113 // Read the UTF-8 encoded strings. |
| 114 table.length = length; |
| 115 int startOffset = 0; |
| 116 for (int i = 0; i < length; ++i) { |
| 117 table[i] = readStringEntry(endOffsets[i] - startOffset); |
| 118 startOffset = endOffsets[i]; |
112 } | 119 } |
113 } | 120 } |
114 | 121 |
115 String readUriReference() { | 122 String readUriReference() { |
116 return _sourceUriTable[readUInt()]; | 123 return _sourceUriTable[readUInt()]; |
117 } | 124 } |
118 | 125 |
119 void readSourceUriTable() { | |
120 int length = readUInt(); | |
121 _sourceUriTable = new List<String>(length); | |
122 for (int i = 0; i < length; ++i) { | |
123 _sourceUriTable[i] = readStringEntry(); | |
124 } | |
125 } | |
126 | |
127 String readStringReference() { | 126 String readStringReference() { |
128 return _stringTable[readUInt()]; | 127 return _stringTable[readUInt()]; |
129 } | 128 } |
130 | 129 |
131 String readStringOrNullIfEmpty() { | 130 String readStringOrNullIfEmpty() { |
132 var string = readStringReference(); | 131 var string = readStringReference(); |
133 return string.isEmpty ? null : string; | 132 return string.isEmpty ? null : string; |
134 } | 133 } |
135 | 134 |
136 bool readAndCheckOptionTag() { | 135 bool readAndCheckOptionTag() { |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
243 throw 'Unrecognized bytes following program data'; | 242 throw 'Unrecognized bytes following program data'; |
244 } | 243 } |
245 } | 244 } |
246 | 245 |
247 void _readOneProgram(Program program) { | 246 void _readOneProgram(Program program) { |
248 int magic = readMagicWord(); | 247 int magic = readMagicWord(); |
249 if (magic != Tag.ProgramFile) { | 248 if (magic != Tag.ProgramFile) { |
250 throw fail('This is not a binary dart file. ' | 249 throw fail('This is not a binary dart file. ' |
251 'Magic number was: ${magic.toRadixString(16)}'); | 250 'Magic number was: ${magic.toRadixString(16)}'); |
252 } | 251 } |
253 readStringTable(); | 252 readStringTable(_stringTable); |
254 Map<String, Source> uriToSource = readUriToSource(); | 253 Map<String, Source> uriToSource = readUriToSource(); |
255 program.uriToSource.addAll(uriToSource); | 254 program.uriToSource.addAll(uriToSource); |
256 readLinkTable(program.root); | 255 readLinkTable(program.root); |
257 int numberOfLibraries = readUInt(); | 256 int numberOfLibraries = readUInt(); |
258 List<Library> libraries = new List<Library>(numberOfLibraries); | 257 List<Library> libraries = new List<Library>(numberOfLibraries); |
259 for (int i = 0; i < numberOfLibraries; ++i) { | 258 for (int i = 0; i < numberOfLibraries; ++i) { |
260 libraries[i] = readLibrary(program); | 259 libraries[i] = readLibrary(program); |
261 } | 260 } |
262 var mainMethod = readMemberReference(allowNull: true); | 261 var mainMethod = readMemberReference(allowNull: true); |
263 program.mainMethodName ??= mainMethod; | 262 program.mainMethodName ??= mainMethod; |
264 } | 263 } |
265 | 264 |
266 Map<String, Source> readUriToSource() { | 265 Map<String, Source> readUriToSource() { |
267 readSourceUriTable(); | 266 readStringTable(_sourceUriTable); |
268 int length = _sourceUriTable.length; | 267 int length = _sourceUriTable.length; |
269 Map<String, Source> uriToSource = <String, Source>{}; | 268 Map<String, Source> uriToSource = <String, Source>{}; |
270 for (int i = 0; i < length; ++i) { | 269 for (int i = 0; i < length; ++i) { |
271 String uri = _sourceUriTable[i]; | 270 String uri = _sourceUriTable[i]; |
272 List<int> sourceCode = readUtf8Bytes(); | 271 List<int> sourceCode = readUtf8Bytes(); |
273 int lineCount = readUInt(); | 272 int lineCount = readUInt(); |
274 List<int> lineStarts = new List<int>(lineCount); | 273 List<int> lineStarts = new List<int>(lineCount); |
275 int previousLineStart = 0; | 274 int previousLineStart = 0; |
276 for (int j = 0; j < lineCount; ++j) { | 275 for (int j = 0; j < lineCount; ++j) { |
277 int lineStart = readUInt() + previousLineStart; | 276 int lineStart = readUInt() + previousLineStart; |
(...skipping 847 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1125 ..fileOffset = offset | 1124 ..fileOffset = offset |
1126 ..fileEqualsOffset = fileEqualsOffset; | 1125 ..fileEqualsOffset = fileEqualsOffset; |
1127 } | 1126 } |
1128 | 1127 |
1129 int readOffset() { | 1128 int readOffset() { |
1130 // Offset is saved as unsigned, | 1129 // Offset is saved as unsigned, |
1131 // but actually ranges from -1 and up (thus the -1) | 1130 // but actually ranges from -1 and up (thus the -1) |
1132 return readUInt() - 1; | 1131 return readUInt() - 1; |
1133 } | 1132 } |
1134 } | 1133 } |
OLD | NEW |