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_to_binary; | 4 library kernel.ast_to_binary; |
5 | 5 |
6 import '../ast.dart'; | 6 import '../ast.dart'; |
7 import '../import_table.dart'; | 7 import '../import_table.dart'; |
8 import 'tag.dart'; | 8 import 'tag.dart'; |
9 import 'dart:convert'; | 9 import 'dart:convert'; |
10 import 'dart:typed_data'; | 10 import 'dart:typed_data'; |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
67 writeByte((value >> 16) & 0xFF); | 67 writeByte((value >> 16) & 0xFF); |
68 writeByte((value >> 8) & 0xFF); | 68 writeByte((value >> 8) & 0xFF); |
69 writeByte(value & 0xFF); | 69 writeByte(value & 0xFF); |
70 } | 70 } |
71 | 71 |
72 void writeUtf8Bytes(List<int> utf8Bytes) { | 72 void writeUtf8Bytes(List<int> utf8Bytes) { |
73 writeUInt30(utf8Bytes.length); | 73 writeUInt30(utf8Bytes.length); |
74 writeBytes(utf8Bytes); | 74 writeBytes(utf8Bytes); |
75 } | 75 } |
76 | 76 |
77 void writeStringTableEntry(String string) { | |
78 List<int> utf8Bytes = const Utf8Encoder().convert(string); | |
79 writeUInt30(utf8Bytes.length); | |
80 writeBytes(utf8Bytes); | |
81 } | |
82 | |
83 void writeStringTable(StringIndexer indexer) { | 77 void writeStringTable(StringIndexer indexer) { |
| 78 // Write the end offsets. |
84 writeUInt30(indexer.numberOfStrings); | 79 writeUInt30(indexer.numberOfStrings); |
| 80 int endOffset = 0; |
85 for (var entry in indexer.entries) { | 81 for (var entry in indexer.entries) { |
86 writeStringTableEntry(entry.value); | 82 endOffset += entry.utf8Bytes.length; |
| 83 writeUInt30(endOffset); |
| 84 } |
| 85 // Write the UTF-8 encoded strings. |
| 86 for (var entry in indexer.entries) { |
| 87 writeBytes(entry.utf8Bytes); |
87 } | 88 } |
88 } | 89 } |
89 | 90 |
90 void writeStringReference(String string) { | 91 void writeStringReference(String string) { |
91 writeUInt30(_stringIndexer[string]); | 92 writeUInt30(_stringIndexer[string]); |
92 } | 93 } |
93 | 94 |
94 void writeUriReference(String string) { | 95 void writeUriReference(String string) { |
95 int index = _sourceUriIndexer[string]; | 96 int index = _sourceUriIndexer[string]; |
96 if (index == null) { | 97 if (index == null) { |
(...skipping 992 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1089 | 1090 |
1090 void exit(List<TypeParameter> typeParameters) { | 1091 void exit(List<TypeParameter> typeParameters) { |
1091 stackHeight -= typeParameters.length; | 1092 stackHeight -= typeParameters.length; |
1092 } | 1093 } |
1093 | 1094 |
1094 int operator [](TypeParameter parameter) => index[parameter]; | 1095 int operator [](TypeParameter parameter) => index[parameter]; |
1095 } | 1096 } |
1096 | 1097 |
1097 class StringTableEntry implements Comparable<StringTableEntry> { | 1098 class StringTableEntry implements Comparable<StringTableEntry> { |
1098 final String value; | 1099 final String value; |
| 1100 final List<int> utf8Bytes; |
1099 int frequency = 0; | 1101 int frequency = 0; |
1100 | 1102 |
1101 StringTableEntry(this.value); | 1103 StringTableEntry(String value) |
| 1104 : value = value, |
| 1105 utf8Bytes = const Utf8Encoder().convert(value); |
1102 | 1106 |
1103 int compareTo(StringTableEntry other) => other.frequency - frequency; | 1107 int compareTo(StringTableEntry other) => other.frequency - frequency; |
1104 } | 1108 } |
1105 | 1109 |
1106 class StringIndexer extends RecursiveVisitor<Null> { | 1110 class StringIndexer extends RecursiveVisitor<Null> { |
1107 final List<StringTableEntry> entries = <StringTableEntry>[]; | 1111 final List<StringTableEntry> entries = <StringTableEntry>[]; |
1108 final LinkedHashMap<String, int> index = new LinkedHashMap<String, int>(); | 1112 final LinkedHashMap<String, int> index = new LinkedHashMap<String, int>(); |
1109 | 1113 |
1110 StringIndexer() { | 1114 StringIndexer() { |
1111 put(''); | 1115 put(''); |
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1304 void flush() { | 1308 void flush() { |
1305 _sink.add(_buffer.sublist(0, length)); | 1309 _sink.add(_buffer.sublist(0, length)); |
1306 _buffer = new Uint8List(SIZE); | 1310 _buffer = new Uint8List(SIZE); |
1307 length = 0; | 1311 length = 0; |
1308 } | 1312 } |
1309 | 1313 |
1310 void flushAndDestroy() { | 1314 void flushAndDestroy() { |
1311 _sink.add(_buffer.sublist(0, length)); | 1315 _sink.add(_buffer.sublist(0, length)); |
1312 } | 1316 } |
1313 } | 1317 } |
OLD | NEW |