Chromium Code Reviews| 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 | 4 |
| 5 library analyzer.src.summary.flat_buffers; | 5 library analyzer.src.summary.flat_buffers; |
| 6 | 6 |
| 7 import 'dart:collection'; | 7 import 'dart:collection'; |
| 8 import 'dart:convert'; | 8 import 'dart:convert'; |
| 9 import 'dart:math'; | 9 import 'dart:math'; |
| 10 import 'dart:typed_data'; | 10 import 'dart:typed_data'; |
| 11 | 11 |
| 12 /** | 12 /** |
| 13 * The reader of booleans. | |
| 14 */ | |
| 15 class BoolReader extends Reader<bool> { | |
| 16 const BoolReader() : super(); | |
| 17 | |
| 18 @override | |
| 19 int get size => 1; | |
| 20 | |
| 21 @override | |
| 22 bool read(BufferPointer bp) => bp._getInt8() == 1; | |
|
Paul Berry
2016/01/09 14:38:46
Nit: I'd prefer to do `bp._getInt8() != 0`, for co
scheglov
2016/01/09 18:26:14
Done.
| |
| 23 } | |
| 24 | |
| 25 /** | |
| 13 * A pointer to some data. | 26 * A pointer to some data. |
| 14 */ | 27 */ |
| 15 class BufferPointer { | 28 class BufferPointer { |
| 16 final ByteData _buffer; | 29 final ByteData _buffer; |
| 17 final int _offset; | 30 final int _offset; |
| 18 | 31 |
| 19 factory BufferPointer.fromBytes(List<int> byteList, [int offset = 0]) { | 32 factory BufferPointer.fromBytes(List<int> byteList, [int offset = 0]) { |
| 20 Uint8List uint8List = _asUint8List(byteList); | 33 Uint8List uint8List = _asUint8List(byteList); |
| 21 ByteData buf = new ByteData.view(uint8List.buffer); | 34 ByteData buf = new ByteData.view(uint8List.buffer); |
| 22 return new BufferPointer._(buf, uint8List.offsetInBytes + offset); | 35 return new BufferPointer._(buf, uint8List.offsetInBytes + offset); |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 95 */ | 108 */ |
| 96 int _currentTableEndTail; | 109 int _currentTableEndTail; |
| 97 | 110 |
| 98 _VTable _currentVTable; | 111 _VTable _currentVTable; |
| 99 | 112 |
| 100 Builder({this.initialSize: 1024}) { | 113 Builder({this.initialSize: 1024}) { |
| 101 reset(); | 114 reset(); |
| 102 } | 115 } |
| 103 | 116 |
| 104 /** | 117 /** |
| 118 * Add the [field] with the given boolean [value]. The field is not added if | |
| 119 * the [value] is equal to [def]. Booleans are stored as 8-bit fields with | |
| 120 * `0` for `false` and `1` for `true`. | |
| 121 */ | |
| 122 void addBool(int field, bool value, [bool def]) { | |
| 123 if (_currentVTable == null) { | |
| 124 throw new StateError('Start a table before adding values.'); | |
| 125 } | |
| 126 if (value != null && value != def) { | |
| 127 int size = 1; | |
| 128 _prepare(size, 1); | |
| 129 _trackField(field); | |
| 130 _buf.setInt8(_buf.lengthInBytes - _tail, value ? 1 : 0); | |
| 131 } | |
| 132 } | |
| 133 | |
| 134 /** | |
| 105 * Add the [field] with the given 32-bit signed integer [value]. The field is | 135 * Add the [field] with the given 32-bit signed integer [value]. The field is |
| 106 * not added if the [value] is equal to [def]. | 136 * not added if the [value] is equal to [def]. |
| 107 */ | 137 */ |
| 108 void addInt32(int field, int value, [int def]) { | 138 void addInt32(int field, int value, [int def]) { |
| 109 if (_currentVTable == null) { | 139 if (_currentVTable == null) { |
| 110 throw new StateError('Start a table before adding values.'); | 140 throw new StateError('Start a table before adding values.'); |
| 111 } | 141 } |
| 112 if (value != def) { | 142 if (value != null && value != def) { |
| 113 int size = 4; | 143 int size = 4; |
| 114 _prepare(size, 1); | 144 _prepare(size, 1); |
| 115 _trackField(field); | 145 _trackField(field); |
| 116 _setInt32AtTail(_buf, _tail, value); | 146 _setInt32AtTail(_buf, _tail, value); |
| 117 } | 147 } |
| 118 } | 148 } |
| 119 | 149 |
| 120 /** | 150 /** |
| 121 * Add the [field] with the given 64-bit signed integer [value]. The field is | 151 * Add the [field] with the given 64-bit signed integer [value]. The field is |
| 122 * not added if the [value] is equal to [def]. | 152 * not added if the [value] is equal to [def]. |
| 123 */ | 153 */ |
| 124 void addInt64(int field, int value, [int def]) { | 154 void addInt64(int field, int value, [int def]) { |
| 125 if (_currentVTable == null) { | 155 if (_currentVTable == null) { |
| 126 throw new StateError('Start a table before adding values.'); | 156 throw new StateError('Start a table before adding values.'); |
| 127 } | 157 } |
| 128 if (value != def) { | 158 if (value != null && value != def) { |
| 129 int size = 8; | 159 int size = 8; |
| 130 _prepare(size, 1); | 160 _prepare(size, 1); |
| 131 _trackField(field); | 161 _trackField(field); |
| 132 _setInt64AtTail(_buf, _tail, value); | 162 _setInt64AtTail(_buf, _tail, value); |
| 133 } | 163 } |
| 134 } | 164 } |
| 135 | 165 |
| 136 /** | 166 /** |
| 137 * Add the [field] with the given 8-bit signed integer [value]. The field is | 167 * Add the [field] with the given 8-bit signed integer [value]. The field is |
| 138 * not added if the [value] is equal to [def]. | 168 * not added if the [value] is equal to [def]. |
| 139 */ | 169 */ |
| 140 void addInt8(int field, int value, [int def]) { | 170 void addInt8(int field, int value, [int def]) { |
| 141 if (_currentVTable == null) { | 171 if (_currentVTable == null) { |
| 142 throw new StateError('Start a table before adding values.'); | 172 throw new StateError('Start a table before adding values.'); |
| 143 } | 173 } |
| 144 if (value != def) { | 174 if (value != null && value != def) { |
| 145 int size = 1; | 175 int size = 1; |
| 146 _prepare(size, 1); | 176 _prepare(size, 1); |
| 147 _trackField(field); | 177 _trackField(field); |
| 148 _buf.setInt8(_buf.lengthInBytes - _tail, value); | 178 _buf.setInt8(_buf.lengthInBytes - _tail, value); |
| 149 } | 179 } |
| 150 } | 180 } |
| 151 | 181 |
| 152 /** | 182 /** |
| 153 * Add the [field] referencing an object with the given [offset]. | 183 * Add the [field] referencing an object with the given [offset]. |
| 154 */ | 184 */ |
| (...skipping 473 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 628 // Table size. | 658 // Table size. |
| 629 buf.setUint16(bufOffset, tableSize, Endianness.LITTLE_ENDIAN); | 659 buf.setUint16(bufOffset, tableSize, Endianness.LITTLE_ENDIAN); |
| 630 bufOffset += 2; | 660 bufOffset += 2; |
| 631 // Field offsets. | 661 // Field offsets. |
| 632 for (int fieldOffset in fieldOffsets) { | 662 for (int fieldOffset in fieldOffsets) { |
| 633 buf.setUint16(bufOffset, fieldOffset, Endianness.LITTLE_ENDIAN); | 663 buf.setUint16(bufOffset, fieldOffset, Endianness.LITTLE_ENDIAN); |
| 634 bufOffset += 2; | 664 bufOffset += 2; |
| 635 } | 665 } |
| 636 } | 666 } |
| 637 } | 667 } |
| OLD | NEW |