From 3d2cf554d728905a62119982539385f5d6d2238e Mon Sep 17 00:00:00 2001 From: Kamil Rojewski Date: Thu, 18 May 2017 19:30:30 +0200 Subject: [PATCH] Fix for invalid null returns (#4318) * Eclipse ignore * TypeScript support * Prefixing enums * Test results * Merged JS and TS generators * Fixed AppVeyor build problems * Fixed more AppVeyor build problems * Fixed more AppVeyor build problems * Changed TS flag to options struct * Storing options by value * Removed unneeded const * Re-export support for unions * Uint support * Casting bools to numbers for mutation * TS shell tests * Reverted generates js test file to original version * Backing up js tests and properly generating test data * Not importing flatbuffers for TS test generation * Not overwriting generated js for tests * AppVeyor test fixes * Generating the most strict TS code possible * Not returning null when creating vectors * Not returning null from struct contructors --- src/idl_gen_js.cpp | 5 +-- tests/monster_test.bfbs | Bin 3696 -> 4288 bytes tests/monster_test_generated.ts | 36 +++++------------- .../namespace_test1_generated.ts | 2 +- 4 files changed, 12 insertions(+), 31 deletions(-) diff --git a/src/idl_gen_js.cpp b/src/idl_gen_js.cpp index edcb780f0..9fac55038 100644 --- a/src/idl_gen_js.cpp +++ b/src/idl_gen_js.cpp @@ -955,7 +955,7 @@ void GenStruct(const Parser &parser, StructDef &struct_def, if (lang_.language == IDLOptions::kTs) { code += "static create" + struct_def.name + "(builder:flatbuffers.Builder"; - code += arguments + "):flatbuffers.Offset|null {\n"; + code += arguments + "):flatbuffers.Offset {\n"; } else { code += object_name + ".create" + struct_def.name + " = function(builder"; code += arguments + ") {\n"; @@ -1049,8 +1049,7 @@ void GenStruct(const Parser &parser, StructDef &struct_def, type += " | Uint8Array"; } code += "Vector(builder:flatbuffers.Builder, data:" + type + - "):flatbuffers.Offset|null {\n"; - code += "if(!data){\n return null;\n}\n"; + "):flatbuffers.Offset {\n"; } else { code += object_name + ".create" + MakeCamel(field.name); code += "Vector = function(builder, data) {\n"; diff --git a/tests/monster_test.bfbs b/tests/monster_test.bfbs index b91ff4db6e47c2fdbb5eb1ae117ee85caf24b898..78871d504af00b89b914a99875abdce6801b38b7 100644 GIT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wEe?$IHGGU zG0M?@>)%htzr+E<)xUAf)6XoZLvGKwOOqNumL4at!Ttb#9rQ71A9VGEy@h(1XZEd0 zaMlu-?(?(2#lwCQ?F$pAN8Yd*rRgD}d+rkO-MRwp-bTHw{ToJa>J~6|jTtBF<2>kl zGe7Vn=4~GBJU7eZaQ+VFZH_5>PWOh%Vd%@iHDD+^1tMlAXkGm+z^zVKYsF`QXFoFc z1k-!;Q}|} data * @returns {flatbuffers.Offset} */ -static createInventoryVector(builder:flatbuffers.Builder, data:number[] | Uint8Array):flatbuffers.Offset|null { -if(!data){ - return null; -} +static createInventoryVector(builder:flatbuffers.Builder, data:number[] | Uint8Array):flatbuffers.Offset { builder.startVector(1, data.length, 1); for (var i = data.length - 1; i >= 0; i--) { builder.addInt8(data[i]); @@ -1386,10 +1383,7 @@ static addTestarrayofstring(builder:flatbuffers.Builder, testarrayofstringOffset * @param {Array.} data * @returns {flatbuffers.Offset} */ -static createTestarrayofstringVector(builder:flatbuffers.Builder, data:flatbuffers.Offset[]):flatbuffers.Offset|null { -if(!data){ - return null; -} +static createTestarrayofstringVector(builder:flatbuffers.Builder, data:flatbuffers.Offset[]):flatbuffers.Offset { builder.startVector(4, data.length, 4); for (var i = data.length - 1; i >= 0; i--) { builder.addOffset(data[i]); @@ -1418,10 +1412,7 @@ static addTestarrayoftables(builder:flatbuffers.Builder, testarrayoftablesOffset * @param {Array.} data * @returns {flatbuffers.Offset} */ -static createTestarrayoftablesVector(builder:flatbuffers.Builder, data:flatbuffers.Offset[]):flatbuffers.Offset|null { -if(!data){ - return null; -} +static createTestarrayoftablesVector(builder:flatbuffers.Builder, data:flatbuffers.Offset[]):flatbuffers.Offset { builder.startVector(4, data.length, 4); for (var i = data.length - 1; i >= 0; i--) { builder.addOffset(data[i]); @@ -1458,10 +1449,7 @@ static addTestnestedflatbuffer(builder:flatbuffers.Builder, testnestedflatbuffer * @param {Array.} data * @returns {flatbuffers.Offset} */ -static createTestnestedflatbufferVector(builder:flatbuffers.Builder, data:number[] | Uint8Array):flatbuffers.Offset|null { -if(!data){ - return null; -} +static createTestnestedflatbufferVector(builder:flatbuffers.Builder, data:number[] | Uint8Array):flatbuffers.Offset { builder.startVector(1, data.length, 1); for (var i = data.length - 1; i >= 0; i--) { builder.addInt8(data[i]); @@ -1570,10 +1558,7 @@ static addTestarrayofbools(builder:flatbuffers.Builder, testarrayofboolsOffset:f * @param {Array.} data * @returns {flatbuffers.Offset} */ -static createTestarrayofboolsVector(builder:flatbuffers.Builder, data:boolean[]):flatbuffers.Offset|null { -if(!data){ - return null; -} +static createTestarrayofboolsVector(builder:flatbuffers.Builder, data:boolean[]):flatbuffers.Offset { builder.startVector(1, data.length, 1); for (var i = data.length - 1; i >= 0; i--) { builder.addInt8(+data[i]); @@ -1626,10 +1611,7 @@ static addTestarrayofstring2(builder:flatbuffers.Builder, testarrayofstring2Offs * @param {Array.} data * @returns {flatbuffers.Offset} */ -static createTestarrayofstring2Vector(builder:flatbuffers.Builder, data:flatbuffers.Offset[]):flatbuffers.Offset|null { -if(!data){ - return null; -} +static createTestarrayofstring2Vector(builder:flatbuffers.Builder, data:flatbuffers.Offset[]):flatbuffers.Offset { builder.startVector(4, data.length, 4); for (var i = data.length - 1; i >= 0; i--) { builder.addOffset(data[i]); diff --git a/tests/namespace_test/namespace_test1_generated.ts b/tests/namespace_test/namespace_test1_generated.ts index 59b6f5aee..54d935a4e 100644 --- a/tests/namespace_test/namespace_test1_generated.ts +++ b/tests/namespace_test/namespace_test1_generated.ts @@ -168,7 +168,7 @@ mutate_b(value:number):boolean { * @param {number} b * @returns {flatbuffers.Offset} */ -static createStructInNestedNS(builder:flatbuffers.Builder, a: number, b: number):flatbuffers.Offset|null { +static createStructInNestedNS(builder:flatbuffers.Builder, a: number, b: number):flatbuffers.Offset { builder.prep(4, 8); builder.writeInt32(b); builder.writeInt32(a);