From 4f93049133542f08f66be95edfb85c337ebaf9ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=BClent=20Vural?= Date: Fri, 21 Oct 2022 23:46:41 +0300 Subject: [PATCH] [TS] Add support for fixed length arrays on Typescript (#5864) (#7021) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Typescript / Javascript don't have fixed arrays but it is important to support these languages for compatibility. * Generated TS code checks the length of the given array and do truncating / padding to conform to the schema. * Supports the both standard API and Object Based API. * Added a test. Co-authored-by: Mehmet Baker Signed-off-by: Bulent Vural Signed-off-by: Bülent Vural --- .gitignore | 1 + src/idl_gen_ts.cpp | 313 ++++++++- src/idl_parser.cpp | 2 +- tests/ts/JavaScriptComplexArraysTest.js | 129 ++++ tests/ts/TypeScriptTest.py | 15 +- .../arrays_test_complex.fbs | 52 ++ .../arrays_test_complex_generated.js | 409 ++++++++++++ .../arrays_test_complex_generated.ts | 626 ++++++++++++++++++ tests/ts/reflection/enum-val.js | 32 +- tests/ts/reflection/enum-val.ts | 38 +- tests/ts/reflection_generated.js | 31 +- tests/ts/reflection_generated.ts | 37 +- tests/ts/tsconfig.json | 3 +- tests/ts/union_vector/movie.ts | 4 +- 14 files changed, 1664 insertions(+), 28 deletions(-) create mode 100644 tests/ts/JavaScriptComplexArraysTest.js create mode 100644 tests/ts/arrays_test_complex/arrays_test_complex.fbs create mode 100644 tests/ts/arrays_test_complex/arrays_test_complex_generated.js create mode 100644 tests/ts/arrays_test_complex/arrays_test_complex_generated.ts diff --git a/.gitignore b/.gitignore index 08daee191a7a..d7dc2705bb3c 100644 --- a/.gitignore +++ b/.gitignore @@ -150,3 +150,4 @@ flatbuffers.pc **/latex/** # https://cmake.org/cmake/help/latest/module/FetchContent.html#variable:FETCHCONTENT_BASE_DIR _deps/ +tests/ts/arrays_test_complex/arrays_test_complex.bfbs \ No newline at end of file diff --git a/src/idl_gen_ts.cpp b/src/idl_gen_ts.cpp index 44e8cb6cbf02..014f9d271e3f 100644 --- a/src/idl_gen_ts.cpp +++ b/src/idl_gen_ts.cpp @@ -406,6 +406,21 @@ class TsGenerator : public BaseGenerator { // return the bigint value directly since typescript does not support // enums with bigint backing types. switch (value.type.base_type) { + case BASE_TYPE_ARRAY: { + std::string ret = "["; + for (auto i = 0; i < value.type.fixed_length; ++i) { + ret += + AddImport(imports, *value.type.enum_def, *value.type.enum_def) + .name + + "." + + namer_.Variant( + *value.type.enum_def->FindByValue(value.constant)); + if (i < value.type.fixed_length - 1) { ret += ", "; } + } + ret += "]"; + return ret; + break; + } case BASE_TYPE_LONG: case BASE_TYPE_ULONG: { return "BigInt('" + value.constant + "')"; @@ -432,6 +447,7 @@ class TsGenerator : public BaseGenerator { return "null"; } + case BASE_TYPE_ARRAY: case BASE_TYPE_VECTOR: return "[]"; case BASE_TYPE_LONG: @@ -464,6 +480,22 @@ class TsGenerator : public BaseGenerator { case BASE_TYPE_BOOL: return allowNull ? "boolean|null" : "boolean"; case BASE_TYPE_LONG: case BASE_TYPE_ULONG: return allowNull ? "bigint|null" : "bigint"; + case BASE_TYPE_ARRAY: { + if (type.element == BASE_TYPE_LONG || type.element == BASE_TYPE_ULONG) { + return allowNull ? "bigint[]|null" : "bigint[]"; + } + if (type.element != BASE_TYPE_STRUCT) { + return allowNull ? "number[]|null" : "number[]"; + } + + std::string name = "any"; + + if (parser_.opts.generate_object_based_api) { + name += "|" + GetTypeName(*type.struct_def, /*object_api =*/true); + } + + return allowNull ? " (" + name + ")[] | null" : name; + } default: if (IsScalar(type.base_type)) { if (type.enum_def) { @@ -537,11 +569,78 @@ class TsGenerator : public BaseGenerator { // don't clash, and to make it obvious these arguments are constructing // a nested struct, prefix the name with the field name. GenStructBody(*field.value.type.struct_def, body, - nameprefix + field.name + "_"); + nameprefix.length() ? nameprefix + "_" + field.name : field.name); } else { - *body += " builder.write" + GenWriteMethod(field.value.type) + "("; - if (field.value.type.base_type == BASE_TYPE_BOOL) { *body += "+"; } - *body += nameprefix + field.name + ");\n"; + auto element_type = field.value.type.element; + + if (field.value.type.base_type == BASE_TYPE_ARRAY) { + switch (field.value.type.element) { + case BASE_TYPE_STRUCT: { + std::string str_last_item_idx = + NumToString(field.value.type.fixed_length - 1); + *body += "\n for (let i = " + str_last_item_idx + "; i >= 0; --i" + ") {\n"; + + std::string fname = nameprefix.length() ? nameprefix + "_" + field.name : field.name; + + *body += " const item = " + fname + "?.[i];\n\n"; + + if (parser_.opts.generate_object_based_api) { + *body += " if (item instanceof " + GetTypeName(*field.value.type.struct_def, /*object_api =*/true) + ") {\n"; + *body += " item.pack(builder);\n"; + *body += " continue;\n"; + *body += " }\n\n"; + } + + std::string class_name = GetPrefixedName(*field.value.type.struct_def); + std::string pack_func_create_call = + class_name + ".create" + class_name + "(builder,\n"; + pack_func_create_call += + " " + GenStructMemberValueTS(*field.value.type.struct_def, + "item", ",\n ", false) + + "\n "; + *body += " " + pack_func_create_call; + *body += " );\n }\n\n"; + + break; + } + default: { + std::string str_last_item_idx = + NumToString(field.value.type.fixed_length - 1); + std::string fname = nameprefix.length() ? nameprefix + "_" + field.name : field.name; + + *body += "\n for (let i = " + str_last_item_idx + "; i >= 0; --i) {\n"; + *body += " builder.write"; + *body += + GenWriteMethod(static_cast(field.value.type.element)); + *body += "("; + *body += element_type == BASE_TYPE_BOOL ? "+" : ""; + + if (element_type == BASE_TYPE_LONG || + element_type == BASE_TYPE_ULONG) { + *body += "BigInt(" + fname + "?.[i] ?? 0));\n"; + } else { + *body += "(" + fname + "?.[i] ?? 0));\n\n"; + } + *body += " }\n\n"; + break; + } + } + } else { + std::string fname = nameprefix.length() ? nameprefix + "_" + field.name : field.name; + + *body += " builder.write" + + GenWriteMethod(field.value.type) + "("; + if (field.value.type.base_type == BASE_TYPE_BOOL) { + *body += "Number(Boolean(" + fname + ")));\n"; + continue; + } else if (field.value.type.base_type == BASE_TYPE_LONG || + field.value.type.base_type == BASE_TYPE_ULONG) { + *body += "BigInt(" + fname + " ?? 0));\n"; + continue; + } + + *body += fname + ");\n"; + } } } } @@ -916,7 +1015,7 @@ class TsGenerator : public BaseGenerator { const auto conversion_function = GenUnionListConvFuncName(enum_def); ret = "(() => {\n"; - ret += " const ret = [];\n"; + ret += " const ret: (" + GenObjApiUnionTypeTS(imports, *union_type.struct_def, parser_.opts, *union_type.enum_def) + ")[] = [];\n"; ret += " for(let targetEnumIndex = 0; targetEnumIndex < this." + namer_.Method(field_name, "TypeLength") + "()" + "; " @@ -973,6 +1072,11 @@ class TsGenerator : public BaseGenerator { std::string nullValue = "0"; if (field.value.type.base_type == BASE_TYPE_BOOL) { nullValue = "false"; + } else if (field.value.type.base_type == BASE_TYPE_LONG || + field.value.type.base_type == BASE_TYPE_ULONG) { + nullValue = "BigInt(0)"; + } else if (field.value.type.base_type == BASE_TYPE_ARRAY) { + nullValue = "[]"; } ret += "(" + curr_member_accessor + " ?? " + nullValue + ")"; } else { @@ -1091,6 +1195,95 @@ class TsGenerator : public BaseGenerator { break; } + case BASE_TYPE_ARRAY: { + auto vectortype = field.value.type.VectorType(); + auto vectortypename = + GenTypeName(imports, struct_def, vectortype, false); + is_vector = true; + + field_type = "("; + + switch (vectortype.base_type) { + case BASE_TYPE_STRUCT: { + const auto &sd = *field.value.type.struct_def; + const auto field_type_name = + GetTypeName(sd, /*object_api=*/true); + field_type += field_type_name; + field_type += ")[]"; + + field_val = GenBBAccess() + ".createObjList<" + vectortypename + + ", " + field_type_name + ">(" + + field_binded_method + ", " + + NumToString(field.value.type.fixed_length) + ")"; + + if (sd.fixed) { + field_offset_decl = + "builder.createStructOffsetList(this." + field_field + + ", " + AddImport(imports, struct_def, struct_def).name + + "." + namer_.Method("start", field, "Vector") + ")"; + } else { + field_offset_decl = + AddImport(imports, struct_def, struct_def).name + "." + + namer_.Method("create", field, "Vector") + + "(builder, builder.createObjectOffsetList(" + "this." + + field_field + "))"; + } + + break; + } + + case BASE_TYPE_STRING: { + field_type += "string)[]"; + field_val = GenBBAccess() + ".createScalarList(" + + field_binded_method + ", this." + + namer_.Field(field, "Length") + "())"; + field_offset_decl = + AddImport(imports, struct_def, struct_def).name + "." + + namer_.Method("create", field, "Vector") + + "(builder, builder.createObjectOffsetList(" + "this." + + namer_.Field(field) + "))"; + break; + } + + case BASE_TYPE_UNION: { + field_type += GenObjApiUnionTypeTS( + imports, struct_def, parser.opts, *(vectortype.enum_def)); + field_type += ")[]"; + field_val = GenUnionValTS(imports, struct_def, field_method, + vectortype, true); + + field_offset_decl = + AddImport(imports, struct_def, struct_def).name + "." + + namer_.Method("create", field, "Vector") + + "(builder, builder.createObjectOffsetList(" + "this." + + namer_.Field(field) + "))"; + + break; + } + default: { + if (vectortype.enum_def) { + field_type += GenTypeName(imports, struct_def, vectortype, + false, HasNullDefault(field)); + } else { + field_type += vectortypename; + } + field_type += ")[]"; + field_val = GenBBAccess() + ".createScalarList<" + + vectortypename + ">(" + field_binded_method + ", " + + NumToString(field.value.type.fixed_length) + ")"; + + field_offset_decl = + AddImport(imports, struct_def, struct_def).name + "." + + namer_.Method("create", field, "Vector") + + "(builder, this." + field_field + ")"; + + break; + } + } + + break; + } + case BASE_TYPE_VECTOR: { auto vectortype = field.value.type.VectorType(); auto vectortypename = @@ -1344,9 +1537,16 @@ class TsGenerator : public BaseGenerator { it != struct_def.fields.vec.end(); ++it) { auto &field = **it; if (field.deprecated) continue; - auto offset_prefix = - " const offset = " + GenBBAccess() + ".__offset(this.bb_pos, " + - NumToString(field.value.offset) + ");\n return offset ? "; + std::string offset_prefix = ""; + + if (field.value.type.base_type == BASE_TYPE_ARRAY) { + offset_prefix = " return "; + } else { + offset_prefix = " const offset = " + GenBBAccess() + + ".__offset(this.bb_pos, " + + NumToString(field.value.offset) + ");\n"; + offset_prefix += " return offset ? "; + } // Emit a scalar field const auto is_string = IsString(field.value.type); @@ -1386,9 +1586,11 @@ class TsGenerator : public BaseGenerator { } else { std::string index = "this.bb_pos + offset"; if (is_string) { index += ", optionalEncoding"; } - code += offset_prefix + - GenGetter(field.value.type, "(" + index + ")") + " : " + - GenDefaultValue(field, imports); + code += + offset_prefix + GenGetter(field.value.type, "(" + index + ")"); + if (field.value.type.base_type != BASE_TYPE_ARRAY) { + code += " : " + GenDefaultValue(field, imports); + } code += ";\n"; } } @@ -1421,6 +1623,95 @@ class TsGenerator : public BaseGenerator { break; } + case BASE_TYPE_ARRAY: { + auto vectortype = field.value.type.VectorType(); + auto vectortypename = + GenTypeName(imports, struct_def, vectortype, false); + auto inline_size = InlineSize(vectortype); + auto index = "this.bb_pos + " + NumToString(field.value.offset) + + " + index" + MaybeScale(inline_size); + std::string ret_type; + bool is_union = false; + switch (vectortype.base_type) { + case BASE_TYPE_STRUCT: ret_type = vectortypename; break; + case BASE_TYPE_STRING: ret_type = vectortypename; break; + case BASE_TYPE_UNION: + ret_type = "?flatbuffers.Table"; + is_union = true; + break; + default: ret_type = vectortypename; + } + GenDocComment(field.doc_comment, code_ptr); + std::string prefix = namer_.Method(field); + // TODO: make it work without any + // if (is_union) { prefix += ""; } + if (is_union) { prefix += ""; } + prefix += "(index: number"; + if (is_union) { + const auto union_type = + GenUnionGenericTypeTS(*(field.value.type.enum_def)); + + vectortypename = union_type; + code += prefix + ", obj:" + union_type; + } else if (vectortype.base_type == BASE_TYPE_STRUCT) { + code += prefix + ", obj?:" + vectortypename; + } else if (IsString(vectortype)) { + code += prefix + "):string\n"; + code += prefix + ",optionalEncoding:flatbuffers.Encoding" + + "):" + vectortypename + "\n"; + code += prefix + ",optionalEncoding?:any"; + } else { + code += prefix; + } + code += "):" + vectortypename + "|null {\n"; + + if (vectortype.base_type == BASE_TYPE_STRUCT) { + code += offset_prefix + "(obj || " + + GenerateNewExpression(vectortypename); + code += ").__init("; + code += vectortype.struct_def->fixed + ? index + : GenBBAccess() + ".__indirect(" + index + ")"; + code += ", " + GenBBAccess() + ")"; + } else { + if (is_union) { + index = "obj, " + index; + } else if (IsString(vectortype)) { + index += ", optionalEncoding"; + } + code += offset_prefix + GenGetter(vectortype, "(" + index + ")"); + } + + switch (field.value.type.base_type) { + case BASE_TYPE_ARRAY: { + break; + } + case BASE_TYPE_BOOL: { + code += " : false"; + break; + } + case BASE_TYPE_LONG: + case BASE_TYPE_ULONG: { + code += " : BigInt(0)"; + break; + } + default: { + if (IsScalar(field.value.type.element)) { + if (field.value.type.enum_def) { + code += field.value.constant; + } else { + code += " : 0"; + } + } else { + code += ": null"; + } + break; + } + } + code += ";\n"; + break; + } + case BASE_TYPE_VECTOR: { auto vectortype = field.value.type.VectorType(); auto vectortypename = diff --git a/src/idl_parser.cpp b/src/idl_parser.cpp index 2ed5c4189470..ef4a5bc969de 100644 --- a/src/idl_parser.cpp +++ b/src/idl_parser.cpp @@ -2581,7 +2581,7 @@ bool Parser::SupportsAdvancedArrayFeatures() const { return (opts.lang_to_generate & ~(IDLOptions::kCpp | IDLOptions::kPython | IDLOptions::kJava | IDLOptions::kCSharp | IDLOptions::kJsonSchema | IDLOptions::kJson | - IDLOptions::kBinary | IDLOptions::kRust)) == 0; + IDLOptions::kBinary | IDLOptions::kRust | IDLOptions::kTs)) == 0; } Namespace *Parser::UniqueNamespace(Namespace *ns) { diff --git a/tests/ts/JavaScriptComplexArraysTest.js b/tests/ts/JavaScriptComplexArraysTest.js new file mode 100644 index 000000000000..f8601edfbe8d --- /dev/null +++ b/tests/ts/JavaScriptComplexArraysTest.js @@ -0,0 +1,129 @@ +/* global BigInt */ + +import assert from 'assert'; +import { readFileSync, writeFileSync } from 'fs'; +import * as flatbuffers from 'flatbuffers'; +import { + ArrayStructT, + ArrayTable, + ArrayTableT, + InnerStructT, + NestedStructT, + OuterStructT, + TestEnum, +} from './arrays_test_complex/arrays_test_complex_generated.js'; +// eslint-disable-next-line @typescript-eslint/no-explicit-any +BigInt.prototype.toJSON = function () { + return this.toString(); +}; +function fbObjToObj(fbObj) { + const ret = {}; + for (const propName of Object.keys(fbObj)) { + const key = propName; + const prop = fbObj[key]; + if (prop.valueOf) { + ret[key] = prop.valueOf(); + } else if (typeof prop === 'object') { + ret[key] = fbObjToObj(prop); + } + } + return ret; +} +function testBuild(monFile, jsFile) { + const arrayTable = new ArrayTableT( + 'Complex Array Test', + new ArrayStructT( + 221.139008, + [-700, -600, -500, -400, -300, -200, -100, 0, 100, 200, 300, 400, 500, 600, 700], + 13, + [ + new NestedStructT( + [233, -123], + TestEnum.B, + [TestEnum.A, TestEnum.C], + [ + new OuterStructT( + false, + 123.456, + new InnerStructT( + 123456792.0, + [13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1], + 91, + BigInt('9007199254740999') + ), + [ + new InnerStructT( + -987654321.9876, + [255, 254, 253, 252, 251, 250, 249, 248, 247, 246, 245, 244, 243], + 123, + BigInt('9007199254741000') + ), + new InnerStructT( + 123000987.9876, + [101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113], + -123, + BigInt('9007199254741000') + ), + ], + new InnerStructT( + 987654321.9876, + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13], + 19, + BigInt('9007199254741000') + ), + [111000111.222, 222000222.111, 333000333.333, 444000444.444] + ), + ] + ), + ], + -123456789 + ) + ); + const builder = new flatbuffers.Builder(); + builder.finish(arrayTable.pack(builder)); + if (jsFile) { + const obj = fbObjToObj(arrayTable); + writeFileSync(jsFile, `export default ${JSON.stringify(obj, null, 2)}`); + } + if (monFile) { + writeFileSync(monFile, builder.asUint8Array()); + } + return builder.asUint8Array(); +} +function testParse(monFile, jsFile, buffer) { + if (!buffer) { + if (!monFile) { + console.log(`Please specify mon file read the buffer from.`); + process.exit(1); + } + buffer = readFileSync(monFile); + } + const byteBuffer = new flatbuffers.ByteBuffer(new Uint8Array(buffer)); + const arrayTable = ArrayTable.getRootAsArrayTable(byteBuffer).unpack(); + const json = JSON.stringify(arrayTable, null, 2); + if (jsFile) { + writeFileSync(jsFile, `export default ${json}`); + } + return arrayTable; +} +if (process.argv[2] === 'build') { + testBuild(process.argv[3], process.argv[4]); +} else if (process.argv[2] === 'parse') { + testParse(process.argv[3], process.argv[4], null); +} else { + const arr = testBuild(null, null); + const parsed = testParse(null, null, Buffer.from(arr)); + assert.strictEqual(parsed.a, 'Complex Array Test', 'String Test'); + assert.strictEqual(parsed?.cUnderscore?.aUnderscore, 221.13900756835938, 'Float Test'); + assert.deepEqual(parsed?.cUnderscore?.bUnderscore, [-700, -600, -500, -400, -300, -200, -100, 0, 100, 200, 300, 400, 500, 600, 700], 'Array of signed integers'); + assert.strictEqual(parsed?.cUnderscore.d?.[0].dOuter[0].d[1].a, 123000987.9876, 'Float in deep'); + assert.deepEqual(parsed?.cUnderscore?.d[0].dOuter?.[0]?.e, { + a: 987654321.9876, + b: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13], + c: 19, + dUnderscore: '9007199254741000', + }, 'Object in deep'); + assert.deepEqual(parsed?.cUnderscore.g, ['0', '0'], 'Last object'); + + console.log('Arrays test: completed successfully'); +} diff --git a/tests/ts/TypeScriptTest.py b/tests/ts/TypeScriptTest.py index 0b42bc638d56..5ab40600ee64 100755 --- a/tests/ts/TypeScriptTest.py +++ b/tests/ts/TypeScriptTest.py @@ -95,6 +95,18 @@ def flatc(options, schema, prefix=None, include=None, data=None, cwd=tests_path) include="../../", ) +flatc( + options=["-b", "--schema"], + schema="arrays_test_complex/arrays_test_complex.fbs", + prefix="arrays_test_complex" +) + +flatc( + options=["--ts", "--reflect-names", "--ts-flat-files", "--gen-name-strings", "--gen-object-api"], + schema="arrays_test_complex/arrays_test_complex.bfbs", + prefix="arrays_test_complex" +) + flatc( options=[ "--ts", @@ -121,4 +133,5 @@ def flatc(options, schema, prefix=None, include=None, data=None, cwd=tests_path) print("Running TypeScript Tests...") check_call(NODE_CMD + ["JavaScriptTest"]) check_call(NODE_CMD + ["JavaScriptUnionVectorTest"]) -check_call(NODE_CMD + ["JavaScriptFlexBuffersTest"]) \ No newline at end of file +check_call(NODE_CMD + ["JavaScriptFlexBuffersTest"]) +check_call(NODE_CMD + ["JavaScriptComplexArraysTest"]) \ No newline at end of file diff --git a/tests/ts/arrays_test_complex/arrays_test_complex.fbs b/tests/ts/arrays_test_complex/arrays_test_complex.fbs new file mode 100644 index 000000000000..10d684a24b58 --- /dev/null +++ b/tests/ts/arrays_test_complex/arrays_test_complex.fbs @@ -0,0 +1,52 @@ +namespace MyGame.Example; + +// it appears that the library has already a problem with Enums +// when generating ts file with '--ts-flat-files' from a fbs. +// bfbs is fine. +// workaround is to generate bfbs from fbs first, and then +// generate flat .ts from bfbs if you have enum(s) in your chema + +enum TestEnum : byte { A, B, C } + +struct InnerStruct { + a:float64; + b:[ubyte:13]; + c:int8; + d_underscore:int64; +} + +struct OuterStruct { + a:bool; + b:double; + c_underscore:InnerStruct; + d:[InnerStruct:3]; + e:InnerStruct; + f:[float64:4]; +} + +struct NestedStruct{ + a:[int:2]; + b:TestEnum; + c_underscore:[TestEnum:2]; + d_outer:[OuterStruct:5]; + e:[int64:2]; +} + +struct ArrayStruct{ + a_underscore:float; + b_underscore:[int:0xF]; + c:byte; + d:[NestedStruct:2]; + e:int32; + f:[OuterStruct:2]; + g:[int64:2]; +} + +table ArrayTable{ + a:string; + c_underscore:ArrayStruct; +} + +root_type ArrayTable; +file_identifier "RHUB"; +file_extension "mon"; \ No newline at end of file diff --git a/tests/ts/arrays_test_complex/arrays_test_complex_generated.js b/tests/ts/arrays_test_complex/arrays_test_complex_generated.js new file mode 100644 index 000000000000..6f2bf5e54824 --- /dev/null +++ b/tests/ts/arrays_test_complex/arrays_test_complex_generated.js @@ -0,0 +1,409 @@ +// automatically generated by the FlatBuffers compiler, do not modify +import * as flatbuffers from 'flatbuffers'; +export var TestEnum; +(function (TestEnum) { + TestEnum[TestEnum["A"] = 0] = "A"; + TestEnum[TestEnum["B"] = 1] = "B"; + TestEnum[TestEnum["C"] = 2] = "C"; +})(TestEnum || (TestEnum = {})); +export class ArrayStruct { + constructor() { + this.bb = null; + this.bb_pos = 0; + } + __init(i, bb) { + this.bb_pos = i; + this.bb = bb; + return this; + } + aUnderscore() { + return this.bb.readFloat32(this.bb_pos); + } + bUnderscore(index) { + return this.bb.readInt32(this.bb_pos + 4 + index * 4); + } + c() { + return this.bb.readInt8(this.bb_pos + 64); + } + d(index, obj) { + return (obj || new NestedStruct()).__init(this.bb_pos + 72 + index * 1072, this.bb); + } + e() { + return this.bb.readInt32(this.bb_pos + 2216); + } + f(index, obj) { + return (obj || new OuterStruct()).__init(this.bb_pos + 2224 + index * 208, this.bb); + } + g(index) { + return this.bb.readInt64(this.bb_pos + 2640 + index * 8); + } + static getFullyQualifiedName() { + return 'MyGame_Example_ArrayStruct'; + } + static sizeOf() { + return 2656; + } + static createArrayStruct(builder, a_underscore, b_underscore, c, d, e, f, g) { + var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _p, _q, _r, _s, _t, _u; + builder.prep(8, 2656); + for (let i = 1; i >= 0; --i) { + builder.writeInt64(BigInt((_a = g === null || g === void 0 ? void 0 : g[i]) !== null && _a !== void 0 ? _a : 0)); + } + for (let i = 1; i >= 0; --i) { + const item = f === null || f === void 0 ? void 0 : f[i]; + if (item instanceof OuterStructT) { + item.pack(builder); + continue; + } + OuterStruct.createOuterStruct(builder, item === null || item === void 0 ? void 0 : item.a, item === null || item === void 0 ? void 0 : item.b, ((_c = (_b = item === null || item === void 0 ? void 0 : item.cUnderscore) === null || _b === void 0 ? void 0 : _b.a) !== null && _c !== void 0 ? _c : 0), ((_e = (_d = item === null || item === void 0 ? void 0 : item.cUnderscore) === null || _d === void 0 ? void 0 : _d.b) !== null && _e !== void 0 ? _e : []), ((_g = (_f = item === null || item === void 0 ? void 0 : item.cUnderscore) === null || _f === void 0 ? void 0 : _f.c) !== null && _g !== void 0 ? _g : 0), ((_j = (_h = item === null || item === void 0 ? void 0 : item.cUnderscore) === null || _h === void 0 ? void 0 : _h.dUnderscore) !== null && _j !== void 0 ? _j : BigInt(0)), item === null || item === void 0 ? void 0 : item.d, ((_l = (_k = item === null || item === void 0 ? void 0 : item.e) === null || _k === void 0 ? void 0 : _k.a) !== null && _l !== void 0 ? _l : 0), ((_p = (_m = item === null || item === void 0 ? void 0 : item.e) === null || _m === void 0 ? void 0 : _m.b) !== null && _p !== void 0 ? _p : []), ((_r = (_q = item === null || item === void 0 ? void 0 : item.e) === null || _q === void 0 ? void 0 : _q.c) !== null && _r !== void 0 ? _r : 0), ((_t = (_s = item === null || item === void 0 ? void 0 : item.e) === null || _s === void 0 ? void 0 : _s.dUnderscore) !== null && _t !== void 0 ? _t : BigInt(0)), item === null || item === void 0 ? void 0 : item.f); + } + builder.pad(4); + builder.writeInt32(e); + for (let i = 1; i >= 0; --i) { + const item = d === null || d === void 0 ? void 0 : d[i]; + if (item instanceof NestedStructT) { + item.pack(builder); + continue; + } + NestedStruct.createNestedStruct(builder, item === null || item === void 0 ? void 0 : item.a, item === null || item === void 0 ? void 0 : item.b, item === null || item === void 0 ? void 0 : item.cUnderscore, item === null || item === void 0 ? void 0 : item.dOuter, item === null || item === void 0 ? void 0 : item.e); + } + builder.pad(7); + builder.writeInt8(c); + for (let i = 14; i >= 0; --i) { + builder.writeInt32(((_u = b_underscore === null || b_underscore === void 0 ? void 0 : b_underscore[i]) !== null && _u !== void 0 ? _u : 0)); + } + builder.writeFloat32(a_underscore); + return builder.offset(); + } + unpack() { + return new ArrayStructT(this.aUnderscore(), this.bb.createScalarList(this.bUnderscore.bind(this), 15), this.c(), this.bb.createObjList(this.d.bind(this), 2), this.e(), this.bb.createObjList(this.f.bind(this), 2), this.bb.createScalarList(this.g.bind(this), 2)); + } + unpackTo(_o) { + _o.aUnderscore = this.aUnderscore(); + _o.bUnderscore = this.bb.createScalarList(this.bUnderscore.bind(this), 15); + _o.c = this.c(); + _o.d = this.bb.createObjList(this.d.bind(this), 2); + _o.e = this.e(); + _o.f = this.bb.createObjList(this.f.bind(this), 2); + _o.g = this.bb.createScalarList(this.g.bind(this), 2); + } +} +export class ArrayStructT { + constructor(aUnderscore = 0.0, bUnderscore = [], c = 0, d = [], e = 0, f = [], g = []) { + this.aUnderscore = aUnderscore; + this.bUnderscore = bUnderscore; + this.c = c; + this.d = d; + this.e = e; + this.f = f; + this.g = g; + } + pack(builder) { + return ArrayStruct.createArrayStruct(builder, this.aUnderscore, this.bUnderscore, this.c, this.d, this.e, this.f, this.g); + } +} +export class ArrayTable { + constructor() { + this.bb = null; + this.bb_pos = 0; + } + __init(i, bb) { + this.bb_pos = i; + this.bb = bb; + return this; + } + static getRootAsArrayTable(bb, obj) { + return (obj || new ArrayTable()).__init(bb.readInt32(bb.position()) + bb.position(), bb); + } + static getSizePrefixedRootAsArrayTable(bb, obj) { + bb.setPosition(bb.position() + flatbuffers.SIZE_PREFIX_LENGTH); + return (obj || new ArrayTable()).__init(bb.readInt32(bb.position()) + bb.position(), bb); + } + static bufferHasIdentifier(bb) { + return bb.__has_identifier('RHUB'); + } + a(optionalEncoding) { + const offset = this.bb.__offset(this.bb_pos, 4); + return offset ? this.bb.__string(this.bb_pos + offset, optionalEncoding) : null; + } + cUnderscore(obj) { + const offset = this.bb.__offset(this.bb_pos, 6); + return offset ? (obj || new ArrayStruct()).__init(this.bb_pos + offset, this.bb) : null; + } + static getFullyQualifiedName() { + return 'MyGame_Example_ArrayTable'; + } + static startArrayTable(builder) { + builder.startObject(2); + } + static addA(builder, aOffset) { + builder.addFieldOffset(0, aOffset, 0); + } + static addCUnderscore(builder, cUnderscoreOffset) { + builder.addFieldStruct(1, cUnderscoreOffset, 0); + } + static endArrayTable(builder) { + const offset = builder.endObject(); + return offset; + } + static finishArrayTableBuffer(builder, offset) { + builder.finish(offset, 'RHUB'); + } + static finishSizePrefixedArrayTableBuffer(builder, offset) { + builder.finish(offset, 'RHUB', true); + } + unpack() { + return new ArrayTableT(this.a(), (this.cUnderscore() !== null ? this.cUnderscore().unpack() : null)); + } + unpackTo(_o) { + _o.a = this.a(); + _o.cUnderscore = (this.cUnderscore() !== null ? this.cUnderscore().unpack() : null); + } +} +export class ArrayTableT { + constructor(a = null, cUnderscore = null) { + this.a = a; + this.cUnderscore = cUnderscore; + } + pack(builder) { + const a = (this.a !== null ? builder.createString(this.a) : 0); + ArrayTable.startArrayTable(builder); + ArrayTable.addA(builder, a); + ArrayTable.addCUnderscore(builder, (this.cUnderscore !== null ? this.cUnderscore.pack(builder) : 0)); + return ArrayTable.endArrayTable(builder); + } +} +export class InnerStruct { + constructor() { + this.bb = null; + this.bb_pos = 0; + } + __init(i, bb) { + this.bb_pos = i; + this.bb = bb; + return this; + } + a() { + return this.bb.readFloat64(this.bb_pos); + } + b(index) { + return this.bb.readUint8(this.bb_pos + 8 + index); + } + c() { + return this.bb.readInt8(this.bb_pos + 21); + } + dUnderscore() { + return this.bb.readInt64(this.bb_pos + 24); + } + static getFullyQualifiedName() { + return 'MyGame_Example_InnerStruct'; + } + static sizeOf() { + return 32; + } + static createInnerStruct(builder, a, b, c, d_underscore) { + var _a; + builder.prep(8, 32); + builder.writeInt64(BigInt(d_underscore !== null && d_underscore !== void 0 ? d_underscore : 0)); + builder.pad(2); + builder.writeInt8(c); + for (let i = 12; i >= 0; --i) { + builder.writeInt8(((_a = b === null || b === void 0 ? void 0 : b[i]) !== null && _a !== void 0 ? _a : 0)); + } + builder.writeFloat64(a); + return builder.offset(); + } + unpack() { + return new InnerStructT(this.a(), this.bb.createScalarList(this.b.bind(this), 13), this.c(), this.dUnderscore()); + } + unpackTo(_o) { + _o.a = this.a(); + _o.b = this.bb.createScalarList(this.b.bind(this), 13); + _o.c = this.c(); + _o.dUnderscore = this.dUnderscore(); + } +} +export class InnerStructT { + constructor(a = 0.0, b = [], c = 0, dUnderscore = BigInt('0')) { + this.a = a; + this.b = b; + this.c = c; + this.dUnderscore = dUnderscore; + } + pack(builder) { + return InnerStruct.createInnerStruct(builder, this.a, this.b, this.c, this.dUnderscore); + } +} +export class NestedStruct { + constructor() { + this.bb = null; + this.bb_pos = 0; + } + __init(i, bb) { + this.bb_pos = i; + this.bb = bb; + return this; + } + a(index) { + return this.bb.readInt32(this.bb_pos + 0 + index * 4); + } + b() { + return this.bb.readInt8(this.bb_pos + 8); + } + cUnderscore(index) { + return this.bb.readInt8(this.bb_pos + 9 + index); + } + dOuter(index, obj) { + return (obj || new OuterStruct()).__init(this.bb_pos + 16 + index * 208, this.bb); + } + e(index) { + return this.bb.readInt64(this.bb_pos + 1056 + index * 8); + } + static getFullyQualifiedName() { + return 'MyGame_Example_NestedStruct'; + } + static sizeOf() { + return 1072; + } + static createNestedStruct(builder, a, b, c_underscore, d_outer, e) { + var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _p, _q, _r, _s, _t, _u, _v; + builder.prep(8, 1072); + for (let i = 1; i >= 0; --i) { + builder.writeInt64(BigInt((_a = e === null || e === void 0 ? void 0 : e[i]) !== null && _a !== void 0 ? _a : 0)); + } + for (let i = 4; i >= 0; --i) { + const item = d_outer === null || d_outer === void 0 ? void 0 : d_outer[i]; + if (item instanceof OuterStructT) { + item.pack(builder); + continue; + } + OuterStruct.createOuterStruct(builder, item === null || item === void 0 ? void 0 : item.a, item === null || item === void 0 ? void 0 : item.b, ((_c = (_b = item === null || item === void 0 ? void 0 : item.cUnderscore) === null || _b === void 0 ? void 0 : _b.a) !== null && _c !== void 0 ? _c : 0), ((_e = (_d = item === null || item === void 0 ? void 0 : item.cUnderscore) === null || _d === void 0 ? void 0 : _d.b) !== null && _e !== void 0 ? _e : []), ((_g = (_f = item === null || item === void 0 ? void 0 : item.cUnderscore) === null || _f === void 0 ? void 0 : _f.c) !== null && _g !== void 0 ? _g : 0), ((_j = (_h = item === null || item === void 0 ? void 0 : item.cUnderscore) === null || _h === void 0 ? void 0 : _h.dUnderscore) !== null && _j !== void 0 ? _j : BigInt(0)), item === null || item === void 0 ? void 0 : item.d, ((_l = (_k = item === null || item === void 0 ? void 0 : item.e) === null || _k === void 0 ? void 0 : _k.a) !== null && _l !== void 0 ? _l : 0), ((_p = (_m = item === null || item === void 0 ? void 0 : item.e) === null || _m === void 0 ? void 0 : _m.b) !== null && _p !== void 0 ? _p : []), ((_r = (_q = item === null || item === void 0 ? void 0 : item.e) === null || _q === void 0 ? void 0 : _q.c) !== null && _r !== void 0 ? _r : 0), ((_t = (_s = item === null || item === void 0 ? void 0 : item.e) === null || _s === void 0 ? void 0 : _s.dUnderscore) !== null && _t !== void 0 ? _t : BigInt(0)), item === null || item === void 0 ? void 0 : item.f); + } + builder.pad(5); + for (let i = 1; i >= 0; --i) { + builder.writeInt8(((_u = c_underscore === null || c_underscore === void 0 ? void 0 : c_underscore[i]) !== null && _u !== void 0 ? _u : 0)); + } + builder.writeInt8(b); + for (let i = 1; i >= 0; --i) { + builder.writeInt32(((_v = a === null || a === void 0 ? void 0 : a[i]) !== null && _v !== void 0 ? _v : 0)); + } + return builder.offset(); + } + unpack() { + return new NestedStructT(this.bb.createScalarList(this.a.bind(this), 2), this.b(), this.bb.createScalarList(this.cUnderscore.bind(this), 2), this.bb.createObjList(this.dOuter.bind(this), 5), this.bb.createScalarList(this.e.bind(this), 2)); + } + unpackTo(_o) { + _o.a = this.bb.createScalarList(this.a.bind(this), 2); + _o.b = this.b(); + _o.cUnderscore = this.bb.createScalarList(this.cUnderscore.bind(this), 2); + _o.dOuter = this.bb.createObjList(this.dOuter.bind(this), 5); + _o.e = this.bb.createScalarList(this.e.bind(this), 2); + } +} +export class NestedStructT { + constructor(a = [], b = TestEnum.A, cUnderscore = [TestEnum.A, TestEnum.A], dOuter = [], e = []) { + this.a = a; + this.b = b; + this.cUnderscore = cUnderscore; + this.dOuter = dOuter; + this.e = e; + } + pack(builder) { + return NestedStruct.createNestedStruct(builder, this.a, this.b, this.cUnderscore, this.dOuter, this.e); + } +} +export class OuterStruct { + constructor() { + this.bb = null; + this.bb_pos = 0; + } + __init(i, bb) { + this.bb_pos = i; + this.bb = bb; + return this; + } + a() { + return !!this.bb.readInt8(this.bb_pos); + } + b() { + return this.bb.readFloat64(this.bb_pos + 8); + } + cUnderscore(obj) { + return (obj || new InnerStruct()).__init(this.bb_pos + 16, this.bb); + } + d(index, obj) { + return (obj || new InnerStruct()).__init(this.bb_pos + 48 + index * 32, this.bb); + } + e(obj) { + return (obj || new InnerStruct()).__init(this.bb_pos + 144, this.bb); + } + f(index) { + return this.bb.readFloat64(this.bb_pos + 176 + index * 8); + } + static getFullyQualifiedName() { + return 'MyGame_Example_OuterStruct'; + } + static sizeOf() { + return 208; + } + static createOuterStruct(builder, a, b, c_underscore_a, c_underscore_b, c_underscore_c, c_underscore_d_underscore, d, e_a, e_b, e_c, e_d_underscore, f) { + var _a, _b, _c; + builder.prep(8, 208); + for (let i = 3; i >= 0; --i) { + builder.writeFloat64(((_a = f === null || f === void 0 ? void 0 : f[i]) !== null && _a !== void 0 ? _a : 0)); + } + builder.prep(8, 32); + builder.writeInt64(BigInt(e_d_underscore !== null && e_d_underscore !== void 0 ? e_d_underscore : 0)); + builder.pad(2); + builder.writeInt8(e_c); + for (let i = 12; i >= 0; --i) { + builder.writeInt8(((_b = e_b === null || e_b === void 0 ? void 0 : e_b[i]) !== null && _b !== void 0 ? _b : 0)); + } + builder.writeFloat64(e_a); + for (let i = 2; i >= 0; --i) { + const item = d === null || d === void 0 ? void 0 : d[i]; + if (item instanceof InnerStructT) { + item.pack(builder); + continue; + } + InnerStruct.createInnerStruct(builder, item === null || item === void 0 ? void 0 : item.a, item === null || item === void 0 ? void 0 : item.b, item === null || item === void 0 ? void 0 : item.c, item === null || item === void 0 ? void 0 : item.dUnderscore); + } + builder.prep(8, 32); + builder.writeInt64(BigInt(c_underscore_d_underscore !== null && c_underscore_d_underscore !== void 0 ? c_underscore_d_underscore : 0)); + builder.pad(2); + builder.writeInt8(c_underscore_c); + for (let i = 12; i >= 0; --i) { + builder.writeInt8(((_c = c_underscore_b === null || c_underscore_b === void 0 ? void 0 : c_underscore_b[i]) !== null && _c !== void 0 ? _c : 0)); + } + builder.writeFloat64(c_underscore_a); + builder.writeFloat64(b); + builder.pad(7); + builder.writeInt8(Number(Boolean(a))); + return builder.offset(); + } + unpack() { + return new OuterStructT(this.a(), this.b(), (this.cUnderscore() !== null ? this.cUnderscore().unpack() : null), this.bb.createObjList(this.d.bind(this), 3), (this.e() !== null ? this.e().unpack() : null), this.bb.createScalarList(this.f.bind(this), 4)); + } + unpackTo(_o) { + _o.a = this.a(); + _o.b = this.b(); + _o.cUnderscore = (this.cUnderscore() !== null ? this.cUnderscore().unpack() : null); + _o.d = this.bb.createObjList(this.d.bind(this), 3); + _o.e = (this.e() !== null ? this.e().unpack() : null); + _o.f = this.bb.createScalarList(this.f.bind(this), 4); + } +} +export class OuterStructT { + constructor(a = false, b = 0.0, cUnderscore = null, d = [], e = null, f = []) { + this.a = a; + this.b = b; + this.cUnderscore = cUnderscore; + this.d = d; + this.e = e; + this.f = f; + } + pack(builder) { + var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _p, _q, _r, _s; + return OuterStruct.createOuterStruct(builder, this.a, this.b, ((_b = (_a = this.cUnderscore) === null || _a === void 0 ? void 0 : _a.a) !== null && _b !== void 0 ? _b : 0), ((_d = (_c = this.cUnderscore) === null || _c === void 0 ? void 0 : _c.b) !== null && _d !== void 0 ? _d : []), ((_f = (_e = this.cUnderscore) === null || _e === void 0 ? void 0 : _e.c) !== null && _f !== void 0 ? _f : 0), ((_h = (_g = this.cUnderscore) === null || _g === void 0 ? void 0 : _g.dUnderscore) !== null && _h !== void 0 ? _h : BigInt(0)), this.d, ((_k = (_j = this.e) === null || _j === void 0 ? void 0 : _j.a) !== null && _k !== void 0 ? _k : 0), ((_m = (_l = this.e) === null || _l === void 0 ? void 0 : _l.b) !== null && _m !== void 0 ? _m : []), ((_q = (_p = this.e) === null || _p === void 0 ? void 0 : _p.c) !== null && _q !== void 0 ? _q : 0), ((_s = (_r = this.e) === null || _r === void 0 ? void 0 : _r.dUnderscore) !== null && _s !== void 0 ? _s : BigInt(0)), this.f); + } +} diff --git a/tests/ts/arrays_test_complex/arrays_test_complex_generated.ts b/tests/ts/arrays_test_complex/arrays_test_complex_generated.ts new file mode 100644 index 000000000000..8fde21dd5bc7 --- /dev/null +++ b/tests/ts/arrays_test_complex/arrays_test_complex_generated.ts @@ -0,0 +1,626 @@ +// automatically generated by the FlatBuffers compiler, do not modify + +import * as flatbuffers from 'flatbuffers'; + + +export enum TestEnum { + A = 0, + B = 1, + C = 2 +} + +export class ArrayStruct implements flatbuffers.IUnpackableObject { + bb: flatbuffers.ByteBuffer|null = null; + bb_pos = 0; + __init(i:number, bb:flatbuffers.ByteBuffer):ArrayStruct { + this.bb_pos = i; + this.bb = bb; + return this; +} + +aUnderscore():number { + return this.bb!.readFloat32(this.bb_pos); +} + +bUnderscore(index: number):number|null { + return this.bb!.readInt32(this.bb_pos + 4 + index * 4); +} + +c():number { + return this.bb!.readInt8(this.bb_pos + 64); +} + +d(index: number, obj?:NestedStruct):NestedStruct|null { + return (obj || new NestedStruct()).__init(this.bb_pos + 72 + index * 1072, this.bb!); +} + +e():number { + return this.bb!.readInt32(this.bb_pos + 2216); +} + +f(index: number, obj?:OuterStruct):OuterStruct|null { + return (obj || new OuterStruct()).__init(this.bb_pos + 2224 + index * 208, this.bb!); +} + +g(index: number):bigint|null { + return this.bb!.readInt64(this.bb_pos + 2640 + index * 8); +} + +static getFullyQualifiedName():string { + return 'MyGame_Example_ArrayStruct'; +} + +static sizeOf():number { + return 2656; +} + +static createArrayStruct(builder:flatbuffers.Builder, a_underscore: number, b_underscore: number[]|null, c: number, d: (any|NestedStructT)[] | null, e: number, f: (any|OuterStructT)[] | null, g: bigint[]|null):flatbuffers.Offset { + builder.prep(8, 2656); + + for (let i = 1; i >= 0; --i) { + builder.writeInt64(BigInt(g?.[i] ?? 0)); + } + + + for (let i = 1; i >= 0; --i) { + const item = f?.[i]; + + if (item instanceof OuterStructT) { + item.pack(builder); + continue; + } + + OuterStruct.createOuterStruct(builder, + item?.a, + item?.b, + (item?.cUnderscore?.a ?? 0), + (item?.cUnderscore?.b ?? []), + (item?.cUnderscore?.c ?? 0), + (item?.cUnderscore?.dUnderscore ?? BigInt(0)), + item?.d, + (item?.e?.a ?? 0), + (item?.e?.b ?? []), + (item?.e?.c ?? 0), + (item?.e?.dUnderscore ?? BigInt(0)), + item?.f + ); + } + + builder.pad(4); + builder.writeInt32(e); + + for (let i = 1; i >= 0; --i) { + const item = d?.[i]; + + if (item instanceof NestedStructT) { + item.pack(builder); + continue; + } + + NestedStruct.createNestedStruct(builder, + item?.a, + item?.b, + item?.cUnderscore, + item?.dOuter, + item?.e + ); + } + + builder.pad(7); + builder.writeInt8(c); + + for (let i = 14; i >= 0; --i) { + builder.writeInt32((b_underscore?.[i] ?? 0)); + + } + + builder.writeFloat32(a_underscore); + return builder.offset(); +} + + +unpack(): ArrayStructT { + return new ArrayStructT( + this.aUnderscore(), + this.bb!.createScalarList(this.bUnderscore.bind(this), 15), + this.c(), + this.bb!.createObjList(this.d.bind(this), 2), + this.e(), + this.bb!.createObjList(this.f.bind(this), 2), + this.bb!.createScalarList(this.g.bind(this), 2) + ); +} + + +unpackTo(_o: ArrayStructT): void { + _o.aUnderscore = this.aUnderscore(); + _o.bUnderscore = this.bb!.createScalarList(this.bUnderscore.bind(this), 15); + _o.c = this.c(); + _o.d = this.bb!.createObjList(this.d.bind(this), 2); + _o.e = this.e(); + _o.f = this.bb!.createObjList(this.f.bind(this), 2); + _o.g = this.bb!.createScalarList(this.g.bind(this), 2); +} +} + +export class ArrayStructT implements flatbuffers.IGeneratedObject { +constructor( + public aUnderscore: number = 0.0, + public bUnderscore: (number)[] = [], + public c: number = 0, + public d: (NestedStructT)[] = [], + public e: number = 0, + public f: (OuterStructT)[] = [], + public g: (bigint)[] = [] +){} + + +pack(builder:flatbuffers.Builder): flatbuffers.Offset { + return ArrayStruct.createArrayStruct(builder, + this.aUnderscore, + this.bUnderscore, + this.c, + this.d, + this.e, + this.f, + this.g + ); +} +} + +export class ArrayTable implements flatbuffers.IUnpackableObject { + bb: flatbuffers.ByteBuffer|null = null; + bb_pos = 0; + __init(i:number, bb:flatbuffers.ByteBuffer):ArrayTable { + this.bb_pos = i; + this.bb = bb; + return this; +} + +static getRootAsArrayTable(bb:flatbuffers.ByteBuffer, obj?:ArrayTable):ArrayTable { + return (obj || new ArrayTable()).__init(bb.readInt32(bb.position()) + bb.position(), bb); +} + +static getSizePrefixedRootAsArrayTable(bb:flatbuffers.ByteBuffer, obj?:ArrayTable):ArrayTable { + bb.setPosition(bb.position() + flatbuffers.SIZE_PREFIX_LENGTH); + return (obj || new ArrayTable()).__init(bb.readInt32(bb.position()) + bb.position(), bb); +} + +static bufferHasIdentifier(bb:flatbuffers.ByteBuffer):boolean { + return bb.__has_identifier('RHUB'); +} + +a():string|null +a(optionalEncoding:flatbuffers.Encoding):string|Uint8Array|null +a(optionalEncoding?:any):string|Uint8Array|null { + const offset = this.bb!.__offset(this.bb_pos, 4); + return offset ? this.bb!.__string(this.bb_pos + offset, optionalEncoding) : null; +} + +cUnderscore(obj?:ArrayStruct):ArrayStruct|null { + const offset = this.bb!.__offset(this.bb_pos, 6); + return offset ? (obj || new ArrayStruct()).__init(this.bb_pos + offset, this.bb!) : null; +} + +static getFullyQualifiedName():string { + return 'MyGame_Example_ArrayTable'; +} + +static startArrayTable(builder:flatbuffers.Builder) { + builder.startObject(2); +} + +static addA(builder:flatbuffers.Builder, aOffset:flatbuffers.Offset) { + builder.addFieldOffset(0, aOffset, 0); +} + +static addCUnderscore(builder:flatbuffers.Builder, cUnderscoreOffset:flatbuffers.Offset) { + builder.addFieldStruct(1, cUnderscoreOffset, 0); +} + +static endArrayTable(builder:flatbuffers.Builder):flatbuffers.Offset { + const offset = builder.endObject(); + return offset; +} + +static finishArrayTableBuffer(builder:flatbuffers.Builder, offset:flatbuffers.Offset) { + builder.finish(offset, 'RHUB'); +} + +static finishSizePrefixedArrayTableBuffer(builder:flatbuffers.Builder, offset:flatbuffers.Offset) { + builder.finish(offset, 'RHUB', true); +} + + +unpack(): ArrayTableT { + return new ArrayTableT( + this.a(), + (this.cUnderscore() !== null ? this.cUnderscore()!.unpack() : null) + ); +} + + +unpackTo(_o: ArrayTableT): void { + _o.a = this.a(); + _o.cUnderscore = (this.cUnderscore() !== null ? this.cUnderscore()!.unpack() : null); +} +} + +export class ArrayTableT implements flatbuffers.IGeneratedObject { +constructor( + public a: string|Uint8Array|null = null, + public cUnderscore: ArrayStructT|null = null +){} + + +pack(builder:flatbuffers.Builder): flatbuffers.Offset { + const a = (this.a !== null ? builder.createString(this.a!) : 0); + + ArrayTable.startArrayTable(builder); + ArrayTable.addA(builder, a); + ArrayTable.addCUnderscore(builder, (this.cUnderscore !== null ? this.cUnderscore!.pack(builder) : 0)); + + return ArrayTable.endArrayTable(builder); +} +} + +export class InnerStruct implements flatbuffers.IUnpackableObject { + bb: flatbuffers.ByteBuffer|null = null; + bb_pos = 0; + __init(i:number, bb:flatbuffers.ByteBuffer):InnerStruct { + this.bb_pos = i; + this.bb = bb; + return this; +} + +a():number { + return this.bb!.readFloat64(this.bb_pos); +} + +b(index: number):number|null { + return this.bb!.readUint8(this.bb_pos + 8 + index); +} + +c():number { + return this.bb!.readInt8(this.bb_pos + 21); +} + +dUnderscore():bigint { + return this.bb!.readInt64(this.bb_pos + 24); +} + +static getFullyQualifiedName():string { + return 'MyGame_Example_InnerStruct'; +} + +static sizeOf():number { + return 32; +} + +static createInnerStruct(builder:flatbuffers.Builder, a: number, b: number[]|null, c: number, d_underscore: bigint):flatbuffers.Offset { + builder.prep(8, 32); + builder.writeInt64(BigInt(d_underscore ?? 0)); + builder.pad(2); + builder.writeInt8(c); + + for (let i = 12; i >= 0; --i) { + builder.writeInt8((b?.[i] ?? 0)); + + } + + builder.writeFloat64(a); + return builder.offset(); +} + + +unpack(): InnerStructT { + return new InnerStructT( + this.a(), + this.bb!.createScalarList(this.b.bind(this), 13), + this.c(), + this.dUnderscore() + ); +} + + +unpackTo(_o: InnerStructT): void { + _o.a = this.a(); + _o.b = this.bb!.createScalarList(this.b.bind(this), 13); + _o.c = this.c(); + _o.dUnderscore = this.dUnderscore(); +} +} + +export class InnerStructT implements flatbuffers.IGeneratedObject { +constructor( + public a: number = 0.0, + public b: (number)[] = [], + public c: number = 0, + public dUnderscore: bigint = BigInt('0') +){} + + +pack(builder:flatbuffers.Builder): flatbuffers.Offset { + return InnerStruct.createInnerStruct(builder, + this.a, + this.b, + this.c, + this.dUnderscore + ); +} +} + +export class NestedStruct implements flatbuffers.IUnpackableObject { + bb: flatbuffers.ByteBuffer|null = null; + bb_pos = 0; + __init(i:number, bb:flatbuffers.ByteBuffer):NestedStruct { + this.bb_pos = i; + this.bb = bb; + return this; +} + +a(index: number):number|null { + return this.bb!.readInt32(this.bb_pos + 0 + index * 4); +} + +b():TestEnum { + return this.bb!.readInt8(this.bb_pos + 8); +} + +cUnderscore(index: number):TestEnum|null { + return this.bb!.readInt8(this.bb_pos + 9 + index); +} + +dOuter(index: number, obj?:OuterStruct):OuterStruct|null { + return (obj || new OuterStruct()).__init(this.bb_pos + 16 + index * 208, this.bb!); +} + +e(index: number):bigint|null { + return this.bb!.readInt64(this.bb_pos + 1056 + index * 8); +} + +static getFullyQualifiedName():string { + return 'MyGame_Example_NestedStruct'; +} + +static sizeOf():number { + return 1072; +} + +static createNestedStruct(builder:flatbuffers.Builder, a: number[]|null, b: TestEnum, c_underscore: number[]|null, d_outer: (any|OuterStructT)[] | null, e: bigint[]|null):flatbuffers.Offset { + builder.prep(8, 1072); + + for (let i = 1; i >= 0; --i) { + builder.writeInt64(BigInt(e?.[i] ?? 0)); + } + + + for (let i = 4; i >= 0; --i) { + const item = d_outer?.[i]; + + if (item instanceof OuterStructT) { + item.pack(builder); + continue; + } + + OuterStruct.createOuterStruct(builder, + item?.a, + item?.b, + (item?.cUnderscore?.a ?? 0), + (item?.cUnderscore?.b ?? []), + (item?.cUnderscore?.c ?? 0), + (item?.cUnderscore?.dUnderscore ?? BigInt(0)), + item?.d, + (item?.e?.a ?? 0), + (item?.e?.b ?? []), + (item?.e?.c ?? 0), + (item?.e?.dUnderscore ?? BigInt(0)), + item?.f + ); + } + + builder.pad(5); + + for (let i = 1; i >= 0; --i) { + builder.writeInt8((c_underscore?.[i] ?? 0)); + + } + + builder.writeInt8(b); + + for (let i = 1; i >= 0; --i) { + builder.writeInt32((a?.[i] ?? 0)); + + } + + return builder.offset(); +} + + +unpack(): NestedStructT { + return new NestedStructT( + this.bb!.createScalarList(this.a.bind(this), 2), + this.b(), + this.bb!.createScalarList(this.cUnderscore.bind(this), 2), + this.bb!.createObjList(this.dOuter.bind(this), 5), + this.bb!.createScalarList(this.e.bind(this), 2) + ); +} + + +unpackTo(_o: NestedStructT): void { + _o.a = this.bb!.createScalarList(this.a.bind(this), 2); + _o.b = this.b(); + _o.cUnderscore = this.bb!.createScalarList(this.cUnderscore.bind(this), 2); + _o.dOuter = this.bb!.createObjList(this.dOuter.bind(this), 5); + _o.e = this.bb!.createScalarList(this.e.bind(this), 2); +} +} + +export class NestedStructT implements flatbuffers.IGeneratedObject { +constructor( + public a: (number)[] = [], + public b: TestEnum = TestEnum.A, + public cUnderscore: (TestEnum)[] = [TestEnum.A, TestEnum.A], + public dOuter: (OuterStructT)[] = [], + public e: (bigint)[] = [] +){} + + +pack(builder:flatbuffers.Builder): flatbuffers.Offset { + return NestedStruct.createNestedStruct(builder, + this.a, + this.b, + this.cUnderscore, + this.dOuter, + this.e + ); +} +} + +export class OuterStruct implements flatbuffers.IUnpackableObject { + bb: flatbuffers.ByteBuffer|null = null; + bb_pos = 0; + __init(i:number, bb:flatbuffers.ByteBuffer):OuterStruct { + this.bb_pos = i; + this.bb = bb; + return this; +} + +a():boolean { + return !!this.bb!.readInt8(this.bb_pos); +} + +b():number { + return this.bb!.readFloat64(this.bb_pos + 8); +} + +cUnderscore(obj?:InnerStruct):InnerStruct|null { + return (obj || new InnerStruct()).__init(this.bb_pos + 16, this.bb!); +} + +d(index: number, obj?:InnerStruct):InnerStruct|null { + return (obj || new InnerStruct()).__init(this.bb_pos + 48 + index * 32, this.bb!); +} + +e(obj?:InnerStruct):InnerStruct|null { + return (obj || new InnerStruct()).__init(this.bb_pos + 144, this.bb!); +} + +f(index: number):number|null { + return this.bb!.readFloat64(this.bb_pos + 176 + index * 8); +} + +static getFullyQualifiedName():string { + return 'MyGame_Example_OuterStruct'; +} + +static sizeOf():number { + return 208; +} + +static createOuterStruct(builder:flatbuffers.Builder, a: boolean, b: number, c_underscore_a: number, c_underscore_b: number[]|null, c_underscore_c: number, c_underscore_d_underscore: bigint, d: (any|InnerStructT)[] | null, e_a: number, e_b: number[]|null, e_c: number, e_d_underscore: bigint, f: number[]|null):flatbuffers.Offset { + builder.prep(8, 208); + + for (let i = 3; i >= 0; --i) { + builder.writeFloat64((f?.[i] ?? 0)); + + } + + builder.prep(8, 32); + builder.writeInt64(BigInt(e_d_underscore ?? 0)); + builder.pad(2); + builder.writeInt8(e_c); + + for (let i = 12; i >= 0; --i) { + builder.writeInt8((e_b?.[i] ?? 0)); + + } + + builder.writeFloat64(e_a); + + for (let i = 2; i >= 0; --i) { + const item = d?.[i]; + + if (item instanceof InnerStructT) { + item.pack(builder); + continue; + } + + InnerStruct.createInnerStruct(builder, + item?.a, + item?.b, + item?.c, + item?.dUnderscore + ); + } + + builder.prep(8, 32); + builder.writeInt64(BigInt(c_underscore_d_underscore ?? 0)); + builder.pad(2); + builder.writeInt8(c_underscore_c); + + for (let i = 12; i >= 0; --i) { + builder.writeInt8((c_underscore_b?.[i] ?? 0)); + + } + + builder.writeFloat64(c_underscore_a); + builder.writeFloat64(b); + builder.pad(7); + builder.writeInt8(Number(Boolean(a))); + return builder.offset(); +} + + +unpack(): OuterStructT { + return new OuterStructT( + this.a(), + this.b(), + (this.cUnderscore() !== null ? this.cUnderscore()!.unpack() : null), + this.bb!.createObjList(this.d.bind(this), 3), + (this.e() !== null ? this.e()!.unpack() : null), + this.bb!.createScalarList(this.f.bind(this), 4) + ); +} + + +unpackTo(_o: OuterStructT): void { + _o.a = this.a(); + _o.b = this.b(); + _o.cUnderscore = (this.cUnderscore() !== null ? this.cUnderscore()!.unpack() : null); + _o.d = this.bb!.createObjList(this.d.bind(this), 3); + _o.e = (this.e() !== null ? this.e()!.unpack() : null); + _o.f = this.bb!.createScalarList(this.f.bind(this), 4); +} +} + +export class OuterStructT implements flatbuffers.IGeneratedObject { +constructor( + public a: boolean = false, + public b: number = 0.0, + public cUnderscore: InnerStructT|null = null, + public d: (InnerStructT)[] = [], + public e: InnerStructT|null = null, + public f: (number)[] = [] +){} + + +pack(builder:flatbuffers.Builder): flatbuffers.Offset { + return OuterStruct.createOuterStruct(builder, + this.a, + this.b, + (this.cUnderscore?.a ?? 0), + (this.cUnderscore?.b ?? []), + (this.cUnderscore?.c ?? 0), + (this.cUnderscore?.dUnderscore ?? BigInt(0)), + this.d, + (this.e?.a ?? 0), + (this.e?.b ?? []), + (this.e?.c ?? 0), + (this.e?.dUnderscore ?? BigInt(0)), + this.f + ); +} +} + diff --git a/tests/ts/reflection/enum-val.js b/tests/ts/reflection/enum-val.js index f2ce03d1f11d..93926a61ae76 100644 --- a/tests/ts/reflection/enum-val.js +++ b/tests/ts/reflection/enum-val.js @@ -1,5 +1,6 @@ // automatically generated by the FlatBuffers compiler, do not modify import * as flatbuffers from 'flatbuffers'; +import { KeyValue } from '../reflection/key-value.js'; import { Type } from '../reflection/type.js'; export class EnumVal { constructor() { @@ -46,11 +47,19 @@ export class EnumVal { const offset = this.bb.__offset(this.bb_pos, 12); return offset ? this.bb.__vector_len(this.bb_pos + offset) : 0; } + attributes(index, obj) { + const offset = this.bb.__offset(this.bb_pos, 14); + return offset ? (obj || new KeyValue()).__init(this.bb.__indirect(this.bb.__vector(this.bb_pos + offset) + index * 4), this.bb) : null; + } + attributesLength() { + const offset = this.bb.__offset(this.bb_pos, 14); + return offset ? this.bb.__vector_len(this.bb_pos + offset) : 0; + } static getFullyQualifiedName() { return 'reflection_EnumVal'; } static startEnumVal(builder) { - builder.startObject(5); + builder.startObject(6); } static addName(builder, nameOffset) { builder.addFieldOffset(0, nameOffset, 0); @@ -74,37 +83,54 @@ export class EnumVal { static startDocumentationVector(builder, numElems) { builder.startVector(4, numElems, 4); } + static addAttributes(builder, attributesOffset) { + builder.addFieldOffset(5, attributesOffset, 0); + } + static createAttributesVector(builder, data) { + builder.startVector(4, data.length, 4); + for (let i = data.length - 1; i >= 0; i--) { + builder.addOffset(data[i]); + } + return builder.endVector(); + } + static startAttributesVector(builder, numElems) { + builder.startVector(4, numElems, 4); + } static endEnumVal(builder) { const offset = builder.endObject(); builder.requiredField(offset, 4); // name return offset; } unpack() { - return new EnumValT(this.name(), this.value(), (this.unionType() !== null ? this.unionType().unpack() : null), this.bb.createScalarList(this.documentation.bind(this), this.documentationLength())); + return new EnumValT(this.name(), this.value(), (this.unionType() !== null ? this.unionType().unpack() : null), this.bb.createScalarList(this.documentation.bind(this), this.documentationLength()), this.bb.createObjList(this.attributes.bind(this), this.attributesLength())); } unpackTo(_o) { _o.name = this.name(); _o.value = this.value(); _o.unionType = (this.unionType() !== null ? this.unionType().unpack() : null); _o.documentation = this.bb.createScalarList(this.documentation.bind(this), this.documentationLength()); + _o.attributes = this.bb.createObjList(this.attributes.bind(this), this.attributesLength()); } } export class EnumValT { - constructor(name = null, value = BigInt('0'), unionType = null, documentation = []) { + constructor(name = null, value = BigInt('0'), unionType = null, documentation = [], attributes = []) { this.name = name; this.value = value; this.unionType = unionType; this.documentation = documentation; + this.attributes = attributes; } pack(builder) { const name = (this.name !== null ? builder.createString(this.name) : 0); const unionType = (this.unionType !== null ? this.unionType.pack(builder) : 0); const documentation = EnumVal.createDocumentationVector(builder, builder.createObjectOffsetList(this.documentation)); + const attributes = EnumVal.createAttributesVector(builder, builder.createObjectOffsetList(this.attributes)); EnumVal.startEnumVal(builder); EnumVal.addName(builder, name); EnumVal.addValue(builder, this.value); EnumVal.addUnionType(builder, unionType); EnumVal.addDocumentation(builder, documentation); + EnumVal.addAttributes(builder, attributes); return EnumVal.endEnumVal(builder); } } diff --git a/tests/ts/reflection/enum-val.ts b/tests/ts/reflection/enum-val.ts index 2364dab2fa84..3119a2063862 100644 --- a/tests/ts/reflection/enum-val.ts +++ b/tests/ts/reflection/enum-val.ts @@ -2,6 +2,7 @@ import * as flatbuffers from 'flatbuffers'; +import { KeyValue, KeyValueT } from '../reflection/key-value.js'; import { Type, TypeT } from '../reflection/type.js'; @@ -63,12 +64,22 @@ documentationLength():number { return offset ? this.bb!.__vector_len(this.bb_pos + offset) : 0; } +attributes(index: number, obj?:KeyValue):KeyValue|null { + const offset = this.bb!.__offset(this.bb_pos, 14); + return offset ? (obj || new KeyValue()).__init(this.bb!.__indirect(this.bb!.__vector(this.bb_pos + offset) + index * 4), this.bb!) : null; +} + +attributesLength():number { + const offset = this.bb!.__offset(this.bb_pos, 14); + return offset ? this.bb!.__vector_len(this.bb_pos + offset) : 0; +} + static getFullyQualifiedName():string { return 'reflection_EnumVal'; } static startEnumVal(builder:flatbuffers.Builder) { - builder.startObject(5); + builder.startObject(6); } static addName(builder:flatbuffers.Builder, nameOffset:flatbuffers.Offset) { @@ -99,6 +110,22 @@ static startDocumentationVector(builder:flatbuffers.Builder, numElems:number) { builder.startVector(4, numElems, 4); } +static addAttributes(builder:flatbuffers.Builder, attributesOffset:flatbuffers.Offset) { + builder.addFieldOffset(5, attributesOffset, 0); +} + +static createAttributesVector(builder:flatbuffers.Builder, data:flatbuffers.Offset[]):flatbuffers.Offset { + builder.startVector(4, data.length, 4); + for (let i = data.length - 1; i >= 0; i--) { + builder.addOffset(data[i]!); + } + return builder.endVector(); +} + +static startAttributesVector(builder:flatbuffers.Builder, numElems:number) { + builder.startVector(4, numElems, 4); +} + static endEnumVal(builder:flatbuffers.Builder):flatbuffers.Offset { const offset = builder.endObject(); builder.requiredField(offset, 4) // name @@ -111,7 +138,8 @@ unpack(): EnumValT { this.name(), this.value(), (this.unionType() !== null ? this.unionType()!.unpack() : null), - this.bb!.createScalarList(this.documentation.bind(this), this.documentationLength()) + this.bb!.createScalarList(this.documentation.bind(this), this.documentationLength()), + this.bb!.createObjList(this.attributes.bind(this), this.attributesLength()) ); } @@ -121,6 +149,7 @@ unpackTo(_o: EnumValT): void { _o.value = this.value(); _o.unionType = (this.unionType() !== null ? this.unionType()!.unpack() : null); _o.documentation = this.bb!.createScalarList(this.documentation.bind(this), this.documentationLength()); + _o.attributes = this.bb!.createObjList(this.attributes.bind(this), this.attributesLength()); } } @@ -129,7 +158,8 @@ constructor( public name: string|Uint8Array|null = null, public value: bigint = BigInt('0'), public unionType: TypeT|null = null, - public documentation: (string)[] = [] + public documentation: (string)[] = [], + public attributes: (KeyValueT)[] = [] ){} @@ -137,12 +167,14 @@ pack(builder:flatbuffers.Builder): flatbuffers.Offset { const name = (this.name !== null ? builder.createString(this.name!) : 0); const unionType = (this.unionType !== null ? this.unionType!.pack(builder) : 0); const documentation = EnumVal.createDocumentationVector(builder, builder.createObjectOffsetList(this.documentation)); + const attributes = EnumVal.createAttributesVector(builder, builder.createObjectOffsetList(this.attributes)); EnumVal.startEnumVal(builder); EnumVal.addName(builder, name); EnumVal.addValue(builder, this.value); EnumVal.addUnionType(builder, unionType); EnumVal.addDocumentation(builder, documentation); + EnumVal.addAttributes(builder, attributes); return EnumVal.endEnumVal(builder); } diff --git a/tests/ts/reflection_generated.js b/tests/ts/reflection_generated.js index dd686cc9d67e..e0ed0076a82e 100644 --- a/tests/ts/reflection_generated.js +++ b/tests/ts/reflection_generated.js @@ -302,11 +302,19 @@ export class EnumVal { const offset = this.bb.__offset(this.bb_pos, 12); return offset ? this.bb.__vector_len(this.bb_pos + offset) : 0; } + attributes(index, obj) { + const offset = this.bb.__offset(this.bb_pos, 14); + return offset ? (obj || new KeyValue()).__init(this.bb.__indirect(this.bb.__vector(this.bb_pos + offset) + index * 4), this.bb) : null; + } + attributesLength() { + const offset = this.bb.__offset(this.bb_pos, 14); + return offset ? this.bb.__vector_len(this.bb_pos + offset) : 0; + } static getFullyQualifiedName() { return 'reflection_EnumVal'; } static startEnumVal(builder) { - builder.startObject(5); + builder.startObject(6); } static addName(builder, nameOffset) { builder.addFieldOffset(0, nameOffset, 0); @@ -330,37 +338,54 @@ export class EnumVal { static startDocumentationVector(builder, numElems) { builder.startVector(4, numElems, 4); } + static addAttributes(builder, attributesOffset) { + builder.addFieldOffset(5, attributesOffset, 0); + } + static createAttributesVector(builder, data) { + builder.startVector(4, data.length, 4); + for (let i = data.length - 1; i >= 0; i--) { + builder.addOffset(data[i]); + } + return builder.endVector(); + } + static startAttributesVector(builder, numElems) { + builder.startVector(4, numElems, 4); + } static endEnumVal(builder) { const offset = builder.endObject(); builder.requiredField(offset, 4); // name return offset; } unpack() { - return new EnumValT(this.name(), this.value(), (this.unionType() !== null ? this.unionType().unpack() : null), this.bb.createScalarList(this.documentation.bind(this), this.documentationLength())); + return new EnumValT(this.name(), this.value(), (this.unionType() !== null ? this.unionType().unpack() : null), this.bb.createScalarList(this.documentation.bind(this), this.documentationLength()), this.bb.createObjList(this.attributes.bind(this), this.attributesLength())); } unpackTo(_o) { _o.name = this.name(); _o.value = this.value(); _o.unionType = (this.unionType() !== null ? this.unionType().unpack() : null); _o.documentation = this.bb.createScalarList(this.documentation.bind(this), this.documentationLength()); + _o.attributes = this.bb.createObjList(this.attributes.bind(this), this.attributesLength()); } } export class EnumValT { - constructor(name = null, value = BigInt('0'), unionType = null, documentation = []) { + constructor(name = null, value = BigInt('0'), unionType = null, documentation = [], attributes = []) { this.name = name; this.value = value; this.unionType = unionType; this.documentation = documentation; + this.attributes = attributes; } pack(builder) { const name = (this.name !== null ? builder.createString(this.name) : 0); const unionType = (this.unionType !== null ? this.unionType.pack(builder) : 0); const documentation = EnumVal.createDocumentationVector(builder, builder.createObjectOffsetList(this.documentation)); + const attributes = EnumVal.createAttributesVector(builder, builder.createObjectOffsetList(this.attributes)); EnumVal.startEnumVal(builder); EnumVal.addName(builder, name); EnumVal.addValue(builder, this.value); EnumVal.addUnionType(builder, unionType); EnumVal.addDocumentation(builder, documentation); + EnumVal.addAttributes(builder, attributes); return EnumVal.endEnumVal(builder); } } diff --git a/tests/ts/reflection_generated.ts b/tests/ts/reflection_generated.ts index ec17d64b016c..6b553b1a064b 100644 --- a/tests/ts/reflection_generated.ts +++ b/tests/ts/reflection_generated.ts @@ -399,12 +399,22 @@ documentationLength():number { return offset ? this.bb!.__vector_len(this.bb_pos + offset) : 0; } +attributes(index: number, obj?:KeyValue):KeyValue|null { + const offset = this.bb!.__offset(this.bb_pos, 14); + return offset ? (obj || new KeyValue()).__init(this.bb!.__indirect(this.bb!.__vector(this.bb_pos + offset) + index * 4), this.bb!) : null; +} + +attributesLength():number { + const offset = this.bb!.__offset(this.bb_pos, 14); + return offset ? this.bb!.__vector_len(this.bb_pos + offset) : 0; +} + static getFullyQualifiedName():string { return 'reflection_EnumVal'; } static startEnumVal(builder:flatbuffers.Builder) { - builder.startObject(5); + builder.startObject(6); } static addName(builder:flatbuffers.Builder, nameOffset:flatbuffers.Offset) { @@ -435,6 +445,22 @@ static startDocumentationVector(builder:flatbuffers.Builder, numElems:number) { builder.startVector(4, numElems, 4); } +static addAttributes(builder:flatbuffers.Builder, attributesOffset:flatbuffers.Offset) { + builder.addFieldOffset(5, attributesOffset, 0); +} + +static createAttributesVector(builder:flatbuffers.Builder, data:flatbuffers.Offset[]):flatbuffers.Offset { + builder.startVector(4, data.length, 4); + for (let i = data.length - 1; i >= 0; i--) { + builder.addOffset(data[i]!); + } + return builder.endVector(); +} + +static startAttributesVector(builder:flatbuffers.Builder, numElems:number) { + builder.startVector(4, numElems, 4); +} + static endEnumVal(builder:flatbuffers.Builder):flatbuffers.Offset { const offset = builder.endObject(); builder.requiredField(offset, 4) // name @@ -447,7 +473,8 @@ unpack(): EnumValT { this.name(), this.value(), (this.unionType() !== null ? this.unionType()!.unpack() : null), - this.bb!.createScalarList(this.documentation.bind(this), this.documentationLength()) + this.bb!.createScalarList(this.documentation.bind(this), this.documentationLength()), + this.bb!.createObjList(this.attributes.bind(this), this.attributesLength()) ); } @@ -457,6 +484,7 @@ unpackTo(_o: EnumValT): void { _o.value = this.value(); _o.unionType = (this.unionType() !== null ? this.unionType()!.unpack() : null); _o.documentation = this.bb!.createScalarList(this.documentation.bind(this), this.documentationLength()); + _o.attributes = this.bb!.createObjList(this.attributes.bind(this), this.attributesLength()); } } @@ -465,7 +493,8 @@ constructor( public name: string|Uint8Array|null = null, public value: bigint = BigInt('0'), public unionType: TypeT|null = null, - public documentation: (string)[] = [] + public documentation: (string)[] = [], + public attributes: (KeyValueT)[] = [] ){} @@ -473,12 +502,14 @@ pack(builder:flatbuffers.Builder): flatbuffers.Offset { const name = (this.name !== null ? builder.createString(this.name!) : 0); const unionType = (this.unionType !== null ? this.unionType!.pack(builder) : 0); const documentation = EnumVal.createDocumentationVector(builder, builder.createObjectOffsetList(this.documentation)); + const attributes = EnumVal.createAttributesVector(builder, builder.createObjectOffsetList(this.attributes)); EnumVal.startEnumVal(builder); EnumVal.addName(builder, name); EnumVal.addValue(builder, this.value); EnumVal.addUnionType(builder, unionType); EnumVal.addDocumentation(builder, documentation); + EnumVal.addAttributes(builder, attributes); return EnumVal.endEnumVal(builder); } diff --git a/tests/ts/tsconfig.json b/tests/ts/tsconfig.json index 27196b356194..4678c63e6cd7 100644 --- a/tests/ts/tsconfig.json +++ b/tests/ts/tsconfig.json @@ -20,6 +20,7 @@ "typescript/**/*.ts", "optional_scalars/**/*.ts", "namespace_test/**/*.ts", - "union_vector/**/*.ts" + "union_vector/**/*.ts", + "arrays_test_complex/**/*.ts" ] } diff --git a/tests/ts/union_vector/movie.ts b/tests/ts/union_vector/movie.ts index 1f6ca378998f..a9f7553a43e7 100644 --- a/tests/ts/union_vector/movie.ts +++ b/tests/ts/union_vector/movie.ts @@ -146,7 +146,7 @@ unpack(): MovieT { })(), this.bb!.createScalarList(this.charactersType.bind(this), this.charactersTypeLength()), (() => { - const ret = []; + const ret: (AttackerT|BookReaderT|RapunzelT|string)[] = []; for(let targetEnumIndex = 0; targetEnumIndex < this.charactersTypeLength(); ++targetEnumIndex) { const targetEnum = this.charactersType(targetEnumIndex); if(targetEnum === null || Character[targetEnum!] === 'NONE') { continue; } @@ -172,7 +172,7 @@ unpackTo(_o: MovieT): void { })(); _o.charactersType = this.bb!.createScalarList(this.charactersType.bind(this), this.charactersTypeLength()); _o.characters = (() => { - const ret = []; + const ret: (AttackerT|BookReaderT|RapunzelT|string)[] = []; for(let targetEnumIndex = 0; targetEnumIndex < this.charactersTypeLength(); ++targetEnumIndex) { const targetEnum = this.charactersType(targetEnumIndex); if(targetEnum === null || Character[targetEnum!] === 'NONE') { continue; }