fixed packing structs in nested buffers (#6509)
authorKamil Rojewski <kamil.rojewski@gmail.com>
Thu, 11 Mar 2021 19:14:23 +0000 (20:14 +0100)
committerGitHub <noreply@github.com>
Thu, 11 Mar 2021 19:14:23 +0000 (11:14 -0800)
src/idl_gen_ts.cpp
tests/my-game/example/monster.ts
tests/namespace_test/namespace-a/second-table-in-a.ts
tests/namespace_test/namespace-a/table-in-first-n-s.ts
tests/namespace_test/namespace-c/table-in-c.ts

index 89dd8c5..a6d4ef6 100644 (file)
@@ -856,7 +856,7 @@ class TsGenerator : public BaseGenerator {
             const std::string field_accessor = "this." + field_name + "()";
             field_val = GenNullCheckConditional(field_accessor,
                                                 field_accessor + "!.unpack()");
-            field_offset_val = GenNullCheckConditional(
+            field_offset_decl = GenNullCheckConditional(
                 "this." + field_name, "this." + field_name + "!.pack(builder)",
                 "0");
 
index 69bfa58..33e256f 100644 (file)
@@ -1284,13 +1284,16 @@ constructor(
 
 
 pack(builder:flatbuffers.Builder): flatbuffers.Offset {
+  const pos = (this.pos !== null ? this.pos!.pack(builder) : 0);
   const name = (this.name !== null ? builder.createString(this.name!) : 0);
   const inventory = Monster.createInventoryVector(builder, this.inventory);
   const test = builder.createObjectOffset(this.test);
   const test4 = builder.createStructOffsetList(this.test4, Monster.startTest4Vector);
   const testarrayofstring = Monster.createTestarrayofstringVector(builder, builder.createObjectOffsetList(this.testarrayofstring));
   const testarrayoftables = Monster.createTestarrayoftablesVector(builder, builder.createObjectOffsetList(this.testarrayoftables));
+  const enemy = (this.enemy !== null ? this.enemy!.pack(builder) : 0);
   const testnestedflatbuffer = Monster.createTestnestedflatbufferVector(builder, this.testnestedflatbuffer);
+  const testempty = (this.testempty !== null ? this.testempty!.pack(builder) : 0);
   const testarrayofbools = Monster.createTestarrayofboolsVector(builder, this.testarrayofbools);
   const testarrayofstring2 = Monster.createTestarrayofstring2Vector(builder, builder.createObjectOffsetList(this.testarrayofstring2));
   const testarrayofsortedstruct = builder.createStructOffsetList(this.testarrayofsortedstruct, Monster.startTestarrayofsortedstructVector);
@@ -1298,6 +1301,7 @@ pack(builder:flatbuffers.Builder): flatbuffers.Offset {
   const test5 = builder.createStructOffsetList(this.test5, Monster.startTest5Vector);
   const vectorOfLongs = Monster.createVectorOfLongsVector(builder, this.vectorOfLongs);
   const vectorOfDoubles = Monster.createVectorOfDoublesVector(builder, this.vectorOfDoubles);
+  const parentNamespaceTest = (this.parentNamespaceTest !== null ? this.parentNamespaceTest!.pack(builder) : 0);
   const vectorOfReferrables = Monster.createVectorOfReferrablesVector(builder, builder.createObjectOffsetList(this.vectorOfReferrables));
   const vectorOfWeakReferences = Monster.createVectorOfWeakReferencesVector(builder, this.vectorOfWeakReferences);
   const vectorOfStrongReferrables = Monster.createVectorOfStrongReferrablesVector(builder, builder.createObjectOffsetList(this.vectorOfStrongReferrables));
@@ -1310,7 +1314,7 @@ pack(builder:flatbuffers.Builder): flatbuffers.Offset {
   const scalarKeySortedTables = Monster.createScalarKeySortedTablesVector(builder, builder.createObjectOffsetList(this.scalarKeySortedTables));
 
   Monster.startMonster(builder);
-  Monster.addPos(builder, (this.pos !== null ? this.pos!.pack(builder) : 0));
+  Monster.addPos(builder, pos);
   Monster.addMana(builder, this.mana);
   Monster.addHp(builder, this.hp);
   Monster.addName(builder, name);
@@ -1321,9 +1325,9 @@ pack(builder:flatbuffers.Builder): flatbuffers.Offset {
   Monster.addTest4(builder, test4);
   Monster.addTestarrayofstring(builder, testarrayofstring);
   Monster.addTestarrayoftables(builder, testarrayoftables);
-  Monster.addEnemy(builder, (this.enemy !== null ? this.enemy!.pack(builder) : 0));
+  Monster.addEnemy(builder, enemy);
   Monster.addTestnestedflatbuffer(builder, testnestedflatbuffer);
-  Monster.addTestempty(builder, (this.testempty !== null ? this.testempty!.pack(builder) : 0));
+  Monster.addTestempty(builder, testempty);
   Monster.addTestbool(builder, this.testbool);
   Monster.addTesthashs32Fnv1(builder, this.testhashs32Fnv1);
   Monster.addTesthashu32Fnv1(builder, this.testhashu32Fnv1);
@@ -1343,7 +1347,7 @@ pack(builder:flatbuffers.Builder): flatbuffers.Offset {
   Monster.addTest5(builder, test5);
   Monster.addVectorOfLongs(builder, vectorOfLongs);
   Monster.addVectorOfDoubles(builder, vectorOfDoubles);
-  Monster.addParentNamespaceTest(builder, (this.parentNamespaceTest !== null ? this.parentNamespaceTest!.pack(builder) : 0));
+  Monster.addParentNamespaceTest(builder, parentNamespaceTest);
   Monster.addVectorOfReferrables(builder, vectorOfReferrables);
   Monster.addSingleWeakReference(builder, this.singleWeakReference);
   Monster.addVectorOfWeakReferences(builder, vectorOfWeakReferences);
index 2d50a83..9be3402 100644 (file)
@@ -70,8 +70,10 @@ constructor(
 
 
 pack(builder:flatbuffers.Builder): flatbuffers.Offset {
+  const referToC = (this.referToC !== null ? this.referToC!.pack(builder) : 0);
+
   return SecondTableInA.createSecondTableInA(builder,
-    (this.referToC !== null ? this.referToC!.pack(builder) : 0)
+    referToC
   );
 }
 }
index 7a8832e..72563a7 100644 (file)
@@ -135,14 +135,16 @@ constructor(
 
 
 pack(builder:flatbuffers.Builder): flatbuffers.Offset {
+  const fooTable = (this.fooTable !== null ? this.fooTable!.pack(builder) : 0);
   const fooUnion = builder.createObjectOffset(this.fooUnion);
+  const fooStruct = (this.fooStruct !== null ? this.fooStruct!.pack(builder) : 0);
 
   TableInFirstNS.startTableInFirstNS(builder);
-  TableInFirstNS.addFooTable(builder, (this.fooTable !== null ? this.fooTable!.pack(builder) : 0));
+  TableInFirstNS.addFooTable(builder, fooTable);
   TableInFirstNS.addFooEnum(builder, this.fooEnum);
   TableInFirstNS.addFooUnionType(builder, this.fooUnionType);
   TableInFirstNS.addFooUnion(builder, fooUnion);
-  TableInFirstNS.addFooStruct(builder, (this.fooStruct !== null ? this.fooStruct!.pack(builder) : 0));
+  TableInFirstNS.addFooStruct(builder, fooStruct);
 
   return TableInFirstNS.endTableInFirstNS(builder);
 }
index 7fd559b..7b924b8 100644 (file)
@@ -78,9 +78,12 @@ constructor(
 
 
 pack(builder:flatbuffers.Builder): flatbuffers.Offset {
+  const referToA1 = (this.referToA1 !== null ? this.referToA1!.pack(builder) : 0);
+  const referToA2 = (this.referToA2 !== null ? this.referToA2!.pack(builder) : 0);
+
   TableInC.startTableInC(builder);
-  TableInC.addReferToA1(builder, (this.referToA1 !== null ? this.referToA1!.pack(builder) : 0));
-  TableInC.addReferToA2(builder, (this.referToA2 !== null ? this.referToA2!.pack(builder) : 0));
+  TableInC.addReferToA1(builder, referToA1);
+  TableInC.addReferToA2(builder, referToA2);
 
   return TableInC.endTableInC(builder);
 }