summaryrefslogtreecommitdiff
path: root/js
diff options
context:
space:
mode:
authorBjörn Harrtell <bjornharrtell@users.noreply.github.com>2019-05-16 20:43:31 +0200
committerWouter van Oortmerssen <aardappel@gmail.com>2019-05-16 11:43:31 -0700
commit0bb3ce69353d94f88da4658768fbac2934a035f0 (patch)
tree732f5b91cc59f014c5eb71eefc89857b691751bd /js
parentb56d60f058cbe0c9b41d884857e977f38e9b0205 (diff)
downloadflatbuffers-0bb3ce69353d94f88da4658768fbac2934a035f0.tar.gz
flatbuffers-0bb3ce69353d94f88da4658768fbac2934a035f0.tar.bz2
flatbuffers-0bb3ce69353d94f88da4658768fbac2934a035f0.zip
[JS/TS] Size prefix support (#5326)
* WIP size prefix support * Consider size prefix in overloaded variant * Work on code gen * Disabled helper functions in code gen * Enabled helper functions in code gen * Fix size prefixed test * Fix bad function call * Add SIZE_PREFIX_LENGTH * Fix review comments
Diffstat (limited to 'js')
-rw-r--r--js/flatbuffers.js27
1 files changed, 24 insertions, 3 deletions
diff --git a/js/flatbuffers.js b/js/flatbuffers.js
index 41608520..461cd7af 100644
--- a/js/flatbuffers.js
+++ b/js/flatbuffers.js
@@ -49,6 +49,12 @@ flatbuffers.SIZEOF_INT = 4;
flatbuffers.FILE_IDENTIFIER_LENGTH = 4;
/**
+ * @type {number}
+ * @const
+ */
+flatbuffers.SIZE_PREFIX_LENGTH = 4;
+
+/**
* @enum {number}
*/
flatbuffers.Encoding = {
@@ -676,12 +682,14 @@ outer_loop:
*
* @param {flatbuffers.Offset} root_table
* @param {string=} opt_file_identifier
+ * @param {boolean=} opt_size_prefix
*/
-flatbuffers.Builder.prototype.finish = function(root_table, opt_file_identifier) {
+flatbuffers.Builder.prototype.finish = function(root_table, opt_file_identifier, opt_size_prefix) {
+ var size_prefix = opt_size_prefix ? flatbuffers.SIZE_PREFIX_LENGTH : 0;
if (opt_file_identifier) {
var file_identifier = opt_file_identifier;
this.prep(this.minalign, flatbuffers.SIZEOF_INT +
- flatbuffers.FILE_IDENTIFIER_LENGTH);
+ flatbuffers.FILE_IDENTIFIER_LENGTH + size_prefix);
if (file_identifier.length != flatbuffers.FILE_IDENTIFIER_LENGTH) {
throw new Error('FlatBuffers: file identifier must be length ' +
flatbuffers.FILE_IDENTIFIER_LENGTH);
@@ -690,11 +698,24 @@ flatbuffers.Builder.prototype.finish = function(root_table, opt_file_identifier)
this.writeInt8(file_identifier.charCodeAt(i));
}
}
- this.prep(this.minalign, flatbuffers.SIZEOF_INT);
+ this.prep(this.minalign, flatbuffers.SIZEOF_INT + size_prefix);
this.addOffset(root_table);
+ if (size_prefix) {
+ this.addInt32(this.bb.capacity() - this.space);
+ }
this.bb.setPosition(this.space);
};
+/**
+ * Finalize a size prefixed buffer, pointing to the given `root_table`.
+ *
+ * @param {flatbuffers.Offset} root_table
+ * @param {string=} opt_file_identifier
+ */
+flatbuffers.Builder.prototype.finishSizePrefixed = function (root_table, opt_file_identifier) {
+ this.finish(root_table, opt_file_identifier, true);
+};
+
/// @cond FLATBUFFERS_INTERNAL
/**
* This checks a required field has been set in a given table that has