diff options
author | Kamil Rojewski <kamil.rojewski@gmail.com> | 2021-03-25 18:25:00 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-03-25 10:25:00 -0700 |
commit | 124654ffc4ef8ee8a67aca8af3d9a6e58bd38f59 (patch) | |
tree | 37fc3686f25348207555749b9f275005549c67d7 | |
parent | 3b7d1e86b46e2e79715e0aab915e1ceaca958a8e (diff) | |
download | flatbuffers-124654ffc4ef8ee8a67aca8af3d9a6e58bd38f59.tar.gz flatbuffers-124654ffc4ef8ee8a67aca8af3d9a6e58bd38f59.tar.bz2 flatbuffers-124654ffc4ef8ee8a67aca8af3d9a6e58bd38f59.zip |
fixed packing structs (#6530)
* fixed packing structs in nested buffers
* fixed packing structs
-rw-r--r-- | src/idl_gen_ts.cpp | 8 | ||||
-rw-r--r-- | tests/my-game/example/monster.ts | 3 | ||||
-rw-r--r-- | tests/namespace_test/namespace-a/table-in-first-n-s.ts | 3 | ||||
-rw-r--r-- | tests/ts/my-game/example/monster.ts | 9 |
4 files changed, 15 insertions, 8 deletions
diff --git a/src/idl_gen_ts.cpp b/src/idl_gen_ts.cpp index a6d4ef6c..b9e32f56 100644 --- a/src/idl_gen_ts.cpp +++ b/src/idl_gen_ts.cpp @@ -856,10 +856,16 @@ class TsGenerator : public BaseGenerator { const std::string field_accessor = "this." + field_name + "()"; field_val = GenNullCheckConditional(field_accessor, field_accessor + "!.unpack()"); - field_offset_decl = GenNullCheckConditional( + auto packing = GenNullCheckConditional( "this." + field_name, "this." + field_name + "!.pack(builder)", "0"); + if (sd.fixed) { + field_offset_val = std::move(packing); + } else { + field_offset_decl = std::move(packing); + } + break; } diff --git a/tests/my-game/example/monster.ts b/tests/my-game/example/monster.ts index 33e256f7..26481d23 100644 --- a/tests/my-game/example/monster.ts +++ b/tests/my-game/example/monster.ts @@ -1284,7 +1284,6 @@ 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); @@ -1314,7 +1313,7 @@ pack(builder:flatbuffers.Builder): flatbuffers.Offset { const scalarKeySortedTables = Monster.createScalarKeySortedTablesVector(builder, builder.createObjectOffsetList(this.scalarKeySortedTables)); Monster.startMonster(builder); - Monster.addPos(builder, pos); + Monster.addPos(builder, (this.pos !== null ? this.pos!.pack(builder) : 0)); Monster.addMana(builder, this.mana); Monster.addHp(builder, this.hp); Monster.addName(builder, name); diff --git a/tests/namespace_test/namespace-a/table-in-first-n-s.ts b/tests/namespace_test/namespace-a/table-in-first-n-s.ts index 72563a75..774a2f16 100644 --- a/tests/namespace_test/namespace-a/table-in-first-n-s.ts +++ b/tests/namespace_test/namespace-a/table-in-first-n-s.ts @@ -137,14 +137,13 @@ 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, fooTable); TableInFirstNS.addFooEnum(builder, this.fooEnum); TableInFirstNS.addFooUnionType(builder, this.fooUnionType); TableInFirstNS.addFooUnion(builder, fooUnion); - TableInFirstNS.addFooStruct(builder, fooStruct); + TableInFirstNS.addFooStruct(builder, (this.fooStruct !== null ? this.fooStruct!.pack(builder) : 0)); return TableInFirstNS.endTableInFirstNS(builder); } diff --git a/tests/ts/my-game/example/monster.ts b/tests/ts/my-game/example/monster.ts index 49e528e6..b4ee6e3f 100644 --- a/tests/ts/my-game/example/monster.ts +++ b/tests/ts/my-game/example/monster.ts @@ -1294,7 +1294,9 @@ pack(builder:flatbuffers.Builder): flatbuffers.Offset { 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); @@ -1302,6 +1304,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)); @@ -1325,9 +1328,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); @@ -1347,7 +1350,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); |