From 46208b1e918cfa7e92abc109264e2bc3b3627711 Mon Sep 17 00:00:00 2001 From: unintellisense Date: Mon, 14 Jan 2019 09:21:42 -0800 Subject: [PATCH] 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 --- js/flatbuffers.js | 18 ++++++++++++++++++ tests/JavaScriptTest.js | 29 +++++++++++++++++++++++------ 2 files changed, 41 insertions(+), 6 deletions(-) diff --git a/js/flatbuffers.js b/js/flatbuffers.js index 580020a49..0c970bd8b 100644 --- a/js/flatbuffers.js +++ b/js/flatbuffers.js @@ -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`. * diff --git a/tests/JavaScriptTest.js b/tests/JavaScriptTest.js index 1186cdd41..f581b1d10 100644 --- a/tests/JavaScriptTest.js +++ b/tests/JavaScriptTest.js @@ -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) {