summaryrefslogtreecommitdiff
path: root/dart/lib/src/builder.dart
diff options
context:
space:
mode:
authorMaxim Zaks <maxim.zaks@gmail.com>2020-09-10 21:36:37 +0200
committerGitHub <noreply@github.com>2020-09-10 12:36:37 -0700
commit92a8c1a0f2519d6dcfaf3df9116cf9e92af1dd3c (patch)
tree610c8a2561214a42e11c5e2ed11688e8827f871d /dart/lib/src/builder.dart
parent6cea45dcd3ad84c94c0062d0211736bba53188f2 (diff)
downloadflatbuffers-92a8c1a0f2519d6dcfaf3df9116cf9e92af1dd3c.tar.gz
flatbuffers-92a8c1a0f2519d6dcfaf3df9116cf9e92af1dd3c.tar.bz2
flatbuffers-92a8c1a0f2519d6dcfaf3df9116cf9e92af1dd3c.zip
[JS] FlexBuffers Fix for wrong type of offset and length values (#6107)
* Adding FlexBuffers support for Dart language * Introduce snapshot method. * Fix docu * Replacing extension methods with static methods in order to support older Dart version * Improving code based on PR feedback. Mainly rename refactoring. * Addressing all PR feedback which does not need clarification * exchange dynamic type with Object * Adds better API documentation. [] operator throws a very descriptive exception in case of a bad key. * Implementation of JavaScript FlexBuffers decoder * implements JS FlexBuffers builder * replacing _toF32 with Math.fround * Introducing test for BigInt number * Moving functions from BitWitdth & ValueType object into BitWidthUtil and ValueTypeUtil accordingly. Removing defensive checks. Introducing fix for large int numbers by converting them to BigInt type. Introducing handling for BigInt type in `add` method. Using TextEncoder and Decoder to handle string / utf8 conversion. * rename variable * Lets user turn deduplication strategies for strings, keys and vector of keys off while building FlexBuffer. Implements quick sort and choses quick sort if the number of keys is bigger then 20. Removes unnecessary dict lookups in BitWidthUtil helper functions * make iwidth and uwidth computation simpler and faster * Making redInt and readUint a bit faster and shim the BigInt / BigUint usage * Fixing a bug in FlexBuffers JS, where offsets and lengths are stored and read as int and not as uint values. * Fixing a bug in FlexBuffers Dart, where offset and length values are stored and read as int values instead of uint values
Diffstat (limited to 'dart/lib/src/builder.dart')
-rw-r--r--dart/lib/src/builder.dart46
1 files changed, 32 insertions, 14 deletions
diff --git a/dart/lib/src/builder.dart b/dart/lib/src/builder.dart
index afdf7232..5ce46dcb 100644
--- a/dart/lib/src/builder.dart
+++ b/dart/lib/src/builder.dart
@@ -111,9 +111,9 @@ class Builder {
}
final utf8String = utf8.encode(value);
final length = utf8String.length;
- final bitWidth = BitWidthUtil.width(length);
+ final bitWidth = BitWidthUtil.uwidth(length);
final byteWidth = _align(bitWidth);
- _writeInt(length, byteWidth);
+ _writeUInt(length, byteWidth);
final stringOffset = _offset;
final newOffset = _newOffset(length + 1);
_pushBuffer(utf8String);
@@ -149,9 +149,9 @@ class Builder {
void addBlob(ByteBuffer value) {
_integrityCheckOnValueAddition();
final length = value.lengthInBytes;
- final bitWidth = BitWidthUtil.width(length);
+ final bitWidth = BitWidthUtil.uwidth(length);
final byteWidth = _align(bitWidth);
- _writeInt(length, byteWidth);
+ _writeUInt(length, byteWidth);
final blobOffset = _offset;
final newOffset = _newOffset(length);
_pushBuffer(value.asUint8List());
@@ -295,13 +295,13 @@ class Builder {
final value = _stack[0];
final byteWidth = _align(value.elementWidth(_offset, 0));
_writeStackValue(value, byteWidth);
- _writeInt(value.storedPackedType(), 1);
- _writeInt(byteWidth, 1);
+ _writeUInt(value.storedPackedType(), 1);
+ _writeUInt(byteWidth, 1);
_finished = true;
}
_StackValue _createVector(int start, int vecLength, int step, [_StackValue keys]) {
- var bitWidth = BitWidthUtil.width(vecLength);
+ var bitWidth = BitWidthUtil.uwidth(vecLength);
var prefixElements = 1;
if (keys != null) {
var elemWidth = keys.elementWidth(_offset, 0);
@@ -330,10 +330,10 @@ class Builder {
final fix = typed & ValueTypeUtils.isNumber(vectorType) && vecLength >= 2 && vecLength <= 4;
if (keys != null) {
_writeStackValue(keys, byteWidth);
- _writeInt(1 << keys.width.index, byteWidth);
+ _writeUInt(1 << keys.width.index, byteWidth);
}
if (fix == false) {
- _writeInt(vecLength, byteWidth);
+ _writeUInt(vecLength, byteWidth);
}
final vecOffset = _offset;
for (var i = start; i < _stack.length; i += step) {
@@ -341,7 +341,7 @@ class Builder {
}
if (typed == false) {
for (var i = start; i < _stack.length; i += step) {
- _writeInt(_stack[i].storedPackedType(), 1);
+ _writeUInt(_stack[i].storedPackedType(), 1);
}
}
if (keys != null) {
@@ -442,7 +442,7 @@ class Builder {
if (value.isOffset) {
final relativeOffset = _offset - value.offset;
if (byteWidth == 8 || relativeOffset < (1 << (byteWidth * 8))) {
- _writeInt(relativeOffset, byteWidth);
+ _writeUInt(relativeOffset, byteWidth);
} else {
throw StateError('Unexpected size $byteWidth. This might be a bug. Please create an issue https://github.com/google/flatbuffers/issues/new');
}
@@ -452,9 +452,9 @@ class Builder {
_offset = newOffset;
}
- void _writeInt(int value, int byteWidth) {
+ void _writeUInt(int value, int byteWidth) {
final newOffset = _newOffset(byteWidth);
- _pushInt(value, BitWidthUtil.fromByteWidth(byteWidth));
+ _pushUInt(value, BitWidthUtil.fromByteWidth(byteWidth));
_offset = newOffset;
}
@@ -492,6 +492,24 @@ class Builder {
}
}
+ void _pushUInt(int value, BitWidth width) {
+ switch (width) {
+
+ case BitWidth.width8:
+ _buffer.setUint8(_offset, value);
+ break;
+ case BitWidth.width16:
+ _buffer.setUint16(_offset, value, Endian.little);
+ break;
+ case BitWidth.width32:
+ _buffer.setUint32(_offset, value, Endian.little);
+ break;
+ case BitWidth.width64:
+ _buffer.setUint64(_offset, value, Endian.little);
+ break;
+ }
+ }
+
void _pushBuffer(List<int> value) {
_buffer.buffer.asUint8List().setAll(_offset, value);
}
@@ -541,7 +559,7 @@ class _StackValue {
final width = 1 << i;
final offsetLoc = size + BitWidthUtil.paddingSize(size, width) + index * width;
final offset = offsetLoc - _offset;
- final bitWidth = BitWidthUtil.width(offset);
+ final bitWidth = BitWidthUtil.uwidth(offset);
if (1 << bitWidth.index == width) {
return bitWidth;
}