[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
This commit is contained in:
Maxim Zaks
2020-09-10 21:36:37 +02:00
committed by GitHub
parent 6cea45dcd3
commit 92a8c1a0f2
6 changed files with 130 additions and 50 deletions

View File

@@ -15,6 +15,7 @@ function main() {
testRoundTrip();
testRoundTripWithBuilder();
testDeduplicationOff();
testBugWhereOffestWereStoredAsIntInsteadOfUInt();
}
function testSingleValueBuffers() {
@@ -360,5 +361,29 @@ function testGoldBuffer() {
});
}
function testBugWhereOffestWereStoredAsIntInsteadOfUInt() {
// Reported in https://github.com/google/flatbuffers/issues/5949#issuecomment-688421193
const object = {'channels_in': 64, 'dilation_height_factor': 1, 'dilation_width_factor': 1, 'fused_activation_function': 1, 'pad_values': 1, 'padding': 0, 'stride_height': 1, 'stride_width': 1};
let data1 = flexbuffers.encode(object);
const data = [99, 104, 97, 110, 110, 101, 108, 115, 95, 105, 110, 0,
100, 105, 108, 97, 116, 105, 111, 110, 95, 104, 101, 105, 103, 104, 116, 95, 102, 97, 99, 116, 111, 114, 0,
100, 105, 108, 97, 116, 105, 111, 110, 95, 119, 105, 100, 116, 104, 95, 102, 97, 99, 116, 111, 114, 0,
102, 117, 115, 101, 100, 95, 97, 99, 116, 105, 118, 97, 116, 105, 111, 110, 95, 102, 117, 110, 99, 116, 105, 111, 110, 0,
112, 97, 100, 95, 118, 97, 108, 117, 101, 115, 0, 112, 97, 100, 100, 105, 110, 103, 0,
115, 116, 114, 105, 100, 101, 95, 104, 101, 105, 103, 104, 116, 0,
115, 116, 114, 105, 100, 101, 95, 119, 105, 100, 116, 104, 0,
8, 130, 119, 97, 76, 51, 41, 34, 21, 8, 1, 8, 64, 1, 1, 1, 1, 0, 1, 1, 4, 4, 4, 4, 4, 4, 4, 4, 16, 36, 1];
let object2 = flexbuffers.toObject(new Uint8Array(data).buffer);
let object1 = flexbuffers.toObject(new Uint8Array(data1).buffer);
assert.deepStrictEqual(object, object2);
assert.deepStrictEqual(object, object1);
assert.strictEqual(data.length, data1.length);
let ref = flexbuffers.toReference(new Uint8Array(data).buffer);
assert.strictEqual(ref.isMap(), true);
assert.strictEqual(ref.length(), 8);
assert.strictEqual(ref.get("channels_in").numericValue(), 64);
assert.strictEqual(ref.get("padding").isNumber(), true);
}
main();