mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-02 04:04:19 +00:00
JS- support clear() method on builder (#5109)
* support clearing flatBuffer builder in js * remove unused member reset force_defaults dont actually need to clear data in bytebuffer
This commit is contained in:
committed by
Wouter van Oortmerssen
parent
b99332efd7
commit
46208b1e91
@@ -226,6 +226,19 @@ flatbuffers.Builder = function(opt_initial_size) {
|
||||
this.force_defaults = false;
|
||||
};
|
||||
|
||||
flatbuffers.Builder.prototype.clear = function() {
|
||||
this.bb.clear();
|
||||
this.space = this.bb.capacity();
|
||||
this.minalign = 1;
|
||||
this.vtable = null;
|
||||
this.vtable_in_use = 0;
|
||||
this.isNested = false;
|
||||
this.object_start = 0;
|
||||
this.vtables = [];
|
||||
this.vector_num_elems = 0;
|
||||
this.force_defaults = false;
|
||||
};
|
||||
|
||||
/**
|
||||
* In order to save space, fields that are set to their default value
|
||||
* don't get serialized into the buffer. Forcing defaults provides a
|
||||
@@ -816,6 +829,7 @@ flatbuffers.ByteBuffer = function(bytes) {
|
||||
* @private
|
||||
*/
|
||||
this.position_ = 0;
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -828,6 +842,10 @@ flatbuffers.ByteBuffer.allocate = function(byte_size) {
|
||||
return new flatbuffers.ByteBuffer(new Uint8Array(byte_size));
|
||||
};
|
||||
|
||||
flatbuffers.ByteBuffer.prototype.clear = function() {
|
||||
this.position_ = 0;
|
||||
};
|
||||
|
||||
/**
|
||||
* Get the underlying `Uint8Array`.
|
||||
*
|
||||
|
||||
@@ -21,7 +21,28 @@ function main() {
|
||||
// normally a size larger than the typical FlatBuffer you generate would be
|
||||
// better for performance.
|
||||
var fbb = new flatbuffers.Builder(1);
|
||||
createMonster(fbb);
|
||||
serializeAndTest(fbb);
|
||||
|
||||
// clear the builder, repeat tests
|
||||
var clearIterations = 100;
|
||||
var startingCapacity = fbb.bb.capacity();
|
||||
for (var i = 0; i < clearIterations; i++) {
|
||||
fbb.clear();
|
||||
createMonster(fbb);
|
||||
serializeAndTest(fbb);
|
||||
}
|
||||
// the capacity of our buffer shouldn't increase with the same size payload
|
||||
assert.strictEqual(fbb.bb.capacity(), startingCapacity);
|
||||
|
||||
test64bit();
|
||||
testUnicode();
|
||||
fuzzTest1();
|
||||
|
||||
console.log('FlatBuffers test: completed successfully');
|
||||
}
|
||||
|
||||
function createMonster(fbb) {
|
||||
// We set up the same values as monsterdata.json:
|
||||
|
||||
var str = fbb.createString('MyMonster');
|
||||
@@ -56,7 +77,9 @@ function main() {
|
||||
var mon = MyGame.Example.Monster.endMonster(fbb);
|
||||
|
||||
MyGame.Example.Monster.finishMonsterBuffer(fbb, mon);
|
||||
}
|
||||
|
||||
function serializeAndTest(fbb) {
|
||||
// Write the result to a file for debugging purposes:
|
||||
// Note that the binaries are not necessarily identical, since the JSON
|
||||
// parser may serialize in a slightly different order than the above
|
||||
@@ -69,12 +92,6 @@ function main() {
|
||||
testMutation(fbb.dataBuffer());
|
||||
|
||||
testBuffer(fbb.dataBuffer());
|
||||
|
||||
test64bit();
|
||||
testUnicode();
|
||||
fuzzTest1();
|
||||
|
||||
console.log('FlatBuffers test: completed successfully');
|
||||
}
|
||||
|
||||
function testMutation(bb) {
|
||||
|
||||
Reference in New Issue
Block a user