summaryrefslogtreecommitdiff
path: root/js
diff options
context:
space:
mode:
authorKhoi Dinh Trinh <khoidinhtrinh@gmail.com>2020-04-09 09:53:16 -0700
committerGitHub <noreply@github.com>2020-04-09 09:53:16 -0700
commit003e164057e6d4908d87c379c79a9ebdcb8bbc26 (patch)
tree7c88e490dc117fbd320481a6dd2f0333ec64aa46 /js
parent21cf300f4cc4f282dec869b23bf288e61578f0d0 (diff)
downloadflatbuffers-003e164057e6d4908d87c379c79a9ebdcb8bbc26.tar.gz
flatbuffers-003e164057e6d4908d87c379c79a9ebdcb8bbc26.tar.bz2
flatbuffers-003e164057e6d4908d87c379c79a9ebdcb8bbc26.zip
[TS] Add Obj API (#5788)
* added basic code * backup work * got class property to work * backup progress * implementented fmt for creating code * added docs for genFieldUtils * back up work * added base helper js func * added union js code * added unpackTo and base for pack * added pack code * added null check for packing struct list * passes compile test * fixed some spacing of generated functions * added annotations for constructors * added obj api unpack test * tested pack to work * merge branch * separated js and ts test * fixed union signature to include string * fixed generator to support string union * hardcoded fb builder name * refactored struct vector creation * work around createLong * handle default value in constructor * update typescript docs * added notes about import flag * fixed formatting stuffs * undo TypescriptTest change * refactored fmt * updated generated code * remove ignoring union_vector for js * revert changes for .project * revert changes for package.json * don't generate js in ts test * fixed android project file * removed unused js function * removed package-lock.json * adjust createObjList to new signature * changed regex to callback style * fixed package.json * used existing func for generating annotation * changed ternary to !! * added return type for lambda * removed callback style for obj api generator * fixed js file indentation * removed unused header * added tests for string only union * handle string only union and refactor union conv func * updated generated ts files * renamed union conv func * made js test create files like other languages * removed union string only handling * don't allow null in createObjectOffsetList * updated generated ts code * changed the line that triggers Windows build errors * hopefully fix CI error
Diffstat (limited to 'js')
-rw-r--r--js/flatbuffers.js115
1 files changed, 115 insertions, 0 deletions
diff --git a/js/flatbuffers.js b/js/flatbuffers.js
index 74cd35ab..596da800 100644
--- a/js/flatbuffers.js
+++ b/js/flatbuffers.js
@@ -55,6 +55,15 @@ flatbuffers.FILE_IDENTIFIER_LENGTH = 4;
flatbuffers.SIZE_PREFIX_LENGTH = 4;
/**
+ * @param {number} low
+ * @param {number} high
+ * @returns {flatbuffers.Long}
+ */
+flatbuffers.createLong = function(low, high) {
+ return flatbuffers.Long.create(low, high);
+};
+
+/**
* @enum {number}
*/
flatbuffers.Encoding = {
@@ -830,6 +839,52 @@ flatbuffers.Builder.prototype.createString = function(s) {
flatbuffers.Builder.prototype.createLong = function(low, high) {
return flatbuffers.Long.create(low, high);
};
+
+/**
+ * A helper function to pack an object
+ *
+ * @returns offset of obj
+ */
+flatbuffers.Builder.prototype.createObjectOffset = function(obj) {
+ if(obj === null) {
+ return 0
+ }
+
+ if(typeof obj === 'string') {
+ return this.createString(obj);
+ } else {
+ return obj.pack(this);
+ }
+}
+
+/**
+ * A helper function to pack a list of object
+ *
+ * @returns list of offsets of each non null object
+ */
+flatbuffers.Builder.prototype.createObjectOffsetList = function(list) {
+ let ret = [];
+
+ for(let i = 0; i < list.length; ++i) {
+ let val = list[i];
+
+ if(val !== null) {
+ ret.push(this.createObjectOffset(val));
+ } else {
+ throw new Error(
+ 'FlatBuffers: Argument for createObjectOffsetList cannot contain null.');
+ }
+ }
+
+ return ret;
+};
+
+flatbuffers.Builder.prototype.createStructOffsetList = function(list, startFunc) {
+ startFunc(this, list.length);
+ this.createObjectOffsetList(list);
+ return this.endVector();
+}
+
////////////////////////////////////////////////////////////////////////////////
/// @cond FLATBUFFERS_INTERNAL
/**
@@ -1196,6 +1251,24 @@ flatbuffers.ByteBuffer.prototype.__string = function(offset, opt_encoding) {
};
/**
+ * Handle unions that can contain string as its member, if a Table-derived type then initialize it,
+ * if a string then return a new one
+ *
+ * WARNING: strings are immutable in JS so we can't change the string that the user gave us, this
+ * makes the behaviour of __union_with_string different compared to __union
+ *
+ * @param {flatbuffers.Table|string} o
+ * @param {number} offset
+ * @returns {flatbuffers.Table|string}
+ */
+flatbuffers.ByteBuffer.prototype.__union_with_string = function(o, offset) {
+ if(typeof o === 'string') {
+ return this.__string(offset);
+ }
+ return this.__union(o, offset);
+};
+
+/**
* Retrieve the relative offset stored at "offset"
* @param {number} offset
* @returns {number}
@@ -1252,6 +1325,48 @@ flatbuffers.ByteBuffer.prototype.createLong = function(low, high) {
return flatbuffers.Long.create(low, high);
};
+/**
+ * A helper function for generating list for obj api
+ * @param listAccessor function that accepts an index and return data at that index
+ * @param {number} listLength
+ * @returns {any[]}
+ */
+flatbuffers.ByteBuffer.prototype.createScalarList = function(listAccessor, listLength) {
+ let ret = [];
+ for(let i = 0; i < listLength; ++i) {
+ if(listAccessor(i) !== null) {
+ ret.push(listAccessor(i));
+ }
+ }
+
+ return ret;
+};
+
+/**
+ * This function is here only to get around typescript type system
+ */
+flatbuffers.ByteBuffer.prototype.createStringList = function(listAccessor, listLength) {
+ return this.createScalarList(listAccessor, listLength);
+};
+
+/**
+ * A helper function for generating list for obj api
+ * @param listAccessor function that accepts an index and return data at that index
+ * @param listLength {number} listLength
+ * @param res any[] result list
+ */
+flatbuffers.ByteBuffer.prototype.createObjList = function(listAccessor, listLength) {
+ let ret = [];
+ for(let i = 0; i < listLength; ++i) {
+ let val = listAccessor(i);
+ if(val !== null) {
+ ret.push(val.unpack());
+ }
+ }
+
+ return ret;
+};
+
// Exports for Node.js and RequireJS
this.flatbuffers = flatbuffers;