From ca1190a3d8338424e7746519118a5bbc44d42fab Mon Sep 17 00:00:00 2001 From: hanstzou Date: Fri, 26 Jun 2020 05:42:47 +0800 Subject: [TS] Use proper TypedArray in create*Vector (#5991) * [TS] Use proper TypedArray in create*Vector This commit adds TypeScript function overloads to create*Vector for proper TypedArray types, effectively resolves #5373. * Add @deprecated to old Uint8Array overloads --- src/idl_gen_js_ts.cpp | 26 ++++++++++++++++++++++---- tests/monster_test_generated.ts | 27 +++++++++++++++++++++------ 2 files changed, 43 insertions(+), 10 deletions(-) diff --git a/src/idl_gen_js_ts.cpp b/src/idl_gen_js_ts.cpp index ad1d8a0c..101fa29b 100644 --- a/src/idl_gen_js_ts.cpp +++ b/src/idl_gen_js_ts.cpp @@ -1804,11 +1804,29 @@ class JsTsGenerator : public BaseGenerator { false)); if (lang_.language == IDLOptions::kTs) { - code += "static create" + MakeCamel(field.name); + const std::string sig_begin = + "static create" + MakeCamel(field.name) + + "Vector(builder:flatbuffers.Builder, data:"; + const std::string sig_end = "):flatbuffers.Offset"; std::string type = GenTypeName(vector_type, true) + "[]"; - if (type == "number[]") { type += " | Uint8Array"; } - code += "Vector(builder:flatbuffers.Builder, data:" + type + - "):flatbuffers.Offset {\n"; + if (type == "number[]") { + const auto &array_type = GenType(vector_type); + // the old type should be deprecated in the future + std::string type_old = "number[]|Uint8Array"; + std::string type_new = "number[]|" + array_type + "Array"; + if (type_old == type_new) { + type = type_new; + } else { + // add function overloads + code += sig_begin + type_new + sig_end + ";\n"; + code += + "/**\n * @deprecated This Uint8Array overload will " + "be removed in the future.\n */\n"; + code += sig_begin + type_old + sig_end + ";\n"; + type = type_new + "|Uint8Array"; + } + } + code += sig_begin + type + sig_end + " {\n"; } else { code += object_name + ".create" + MakeCamel(field.name); code += "Vector = function(builder, data) {\n"; diff --git a/tests/monster_test_generated.ts b/tests/monster_test_generated.ts index fdf75892..1130761d 100644 --- a/tests/monster_test_generated.ts +++ b/tests/monster_test_generated.ts @@ -2191,7 +2191,7 @@ static addInventory(builder:flatbuffers.Builder, inventoryOffset:flatbuffers.Off * @param Array. data * @returns flatbuffers.Offset */ -static createInventoryVector(builder:flatbuffers.Builder, data:number[] | Uint8Array):flatbuffers.Offset { +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]); @@ -2326,7 +2326,7 @@ static addTestnestedflatbuffer(builder:flatbuffers.Builder, testnestedflatbuffer * @param Array. data * @returns flatbuffers.Offset */ -static createTestnestedflatbufferVector(builder:flatbuffers.Builder, data:number[] | Uint8Array):flatbuffers.Offset { +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]); @@ -2533,7 +2533,7 @@ static addFlex(builder:flatbuffers.Builder, flexOffset:flatbuffers.Offset) { * @param Array. data * @returns flatbuffers.Offset */ -static createFlexVector(builder:flatbuffers.Builder, data:number[] | Uint8Array):flatbuffers.Offset { +static createFlexVector(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]); @@ -2607,7 +2607,12 @@ static addVectorOfDoubles(builder:flatbuffers.Builder, vectorOfDoublesOffset:fla * @param Array. data * @returns flatbuffers.Offset */ -static createVectorOfDoublesVector(builder:flatbuffers.Builder, data:number[] | Uint8Array):flatbuffers.Offset { +static createVectorOfDoublesVector(builder:flatbuffers.Builder, data:number[]|Float64Array):flatbuffers.Offset; +/** + * @deprecated This Uint8Array overload will be removed in the future. + */ +static createVectorOfDoublesVector(builder:flatbuffers.Builder, data:number[]|Uint8Array):flatbuffers.Offset; +static createVectorOfDoublesVector(builder:flatbuffers.Builder, data:number[]|Float64Array|Uint8Array):flatbuffers.Offset { builder.startVector(8, data.length, 8); for (var i = data.length - 1; i >= 0; i--) { builder.addFloat64(data[i]); @@ -3633,7 +3638,12 @@ static addV8(builder:flatbuffers.Builder, v8Offset:flatbuffers.Offset) { * @param Array. data * @returns flatbuffers.Offset */ -static createV8Vector(builder:flatbuffers.Builder, data:number[] | Uint8Array):flatbuffers.Offset { +static createV8Vector(builder:flatbuffers.Builder, data:number[]|Int8Array):flatbuffers.Offset; +/** + * @deprecated This Uint8Array overload will be removed in the future. + */ +static createV8Vector(builder:flatbuffers.Builder, data:number[]|Uint8Array):flatbuffers.Offset; +static createV8Vector(builder:flatbuffers.Builder, data:number[]|Int8Array|Uint8Array):flatbuffers.Offset { builder.startVector(1, data.length, 1); for (var i = data.length - 1; i >= 0; i--) { builder.addInt8(data[i]); @@ -3662,7 +3672,12 @@ static addVf64(builder:flatbuffers.Builder, vf64Offset:flatbuffers.Offset) { * @param Array. data * @returns flatbuffers.Offset */ -static createVf64Vector(builder:flatbuffers.Builder, data:number[] | Uint8Array):flatbuffers.Offset { +static createVf64Vector(builder:flatbuffers.Builder, data:number[]|Float64Array):flatbuffers.Offset; +/** + * @deprecated This Uint8Array overload will be removed in the future. + */ +static createVf64Vector(builder:flatbuffers.Builder, data:number[]|Uint8Array):flatbuffers.Offset; +static createVf64Vector(builder:flatbuffers.Builder, data:number[]|Float64Array|Uint8Array):flatbuffers.Offset { builder.startVector(8, data.length, 8); for (var i = data.length - 1; i >= 0; i--) { builder.addFloat64(data[i]); -- cgit v1.2.3