1 /*
2 * Copyright 2014 Google Inc. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "idl_gen_ts.h"
18
19 #include <algorithm>
20 #include <cassert>
21 #include <cmath>
22 #include <iostream>
23 #include <unordered_map>
24 #include <unordered_set>
25
26 #include "flatbuffers/code_generators.h"
27 #include "flatbuffers/flatbuffers.h"
28 #include "flatbuffers/flatc.h"
29 #include "flatbuffers/idl.h"
30 #include "flatbuffers/util.h"
31 #include "idl_namer.h"
32
33 namespace flatbuffers {
34 namespace {
35 struct ImportDefinition {
36 std::string name;
37 std::string import_statement;
38 std::string export_statement;
39 std::string bare_file_path;
40 std::string rel_file_path;
41 std::string object_name;
42 const Definition *dependent = nullptr;
43 const Definition *dependency = nullptr;
44 };
45
46 struct NsDefinition {
47 std::string path;
48 std::string filepath;
49 std::string symbolic_name;
50 const Namespace *ns;
51 std::map<std::string, const Definition *> definitions;
52 };
53
TypeScriptDefaultConfig()54 Namer::Config TypeScriptDefaultConfig() {
55 return { /*types=*/Case::kKeep,
56 /*constants=*/Case::kUnknown,
57 /*methods=*/Case::kLowerCamel,
58 /*functions=*/Case::kLowerCamel,
59 /*fields=*/Case::kLowerCamel,
60 /*variables=*/Case::kLowerCamel,
61 /*variants=*/Case::kKeep,
62 /*enum_variant_seperator=*/"::",
63 /*escape_keywords=*/Namer::Config::Escape::AfterConvertingCase,
64 /*namespaces=*/Case::kKeep,
65 /*namespace_seperator=*/"_",
66 /*object_prefix=*/"",
67 /*object_suffix=*/"T",
68 /*keyword_prefix=*/"",
69 /*keyword_suffix=*/"_",
70 /*filenames=*/Case::kDasher,
71 /*directories=*/Case::kDasher,
72 /*output_path=*/"",
73 /*filename_suffix=*/"_generated",
74 /*filename_extension=*/".ts" };
75 }
76
TypescriptKeywords()77 std::set<std::string> TypescriptKeywords() {
78 // List of keywords retrieved from here:
79 // https://github.com/microsoft/TypeScript/issues/2536
80 return {
81 "arguments", "break", "case", "catch", "class", "const",
82 "continue", "debugger", "default", "delete", "do", "else",
83 "enum", "export", "extends", "false", "finally", "for",
84 "function", "if", "import", "in", "instanceof", "new",
85 "null", "Object", "return", "super", "switch", "this",
86 "throw", "true", "try", "typeof", "var", "void",
87 "while", "with", "as", "implements", "interface", "let",
88 "package", "private", "protected", "public", "static", "yield",
89 };
90 }
91
92 enum AnnotationType { kParam = 0, kType = 1, kReturns = 2 };
93
94 template<typename T> struct SupportsObjectAPI : std::false_type {};
95
96 template<> struct SupportsObjectAPI<StructDef> : std::true_type {};
97
98 } // namespace
99
100 namespace ts {
101 // Iterate through all definitions we haven't generate code for (enums, structs,
102 // and tables) and output them to a single file.
103 class TsGenerator : public BaseGenerator {
104 public:
105 typedef std::map<std::string, ImportDefinition> import_set;
106
TsGenerator(const Parser & parser,const std::string & path,const std::string & file_name)107 TsGenerator(const Parser &parser, const std::string &path,
108 const std::string &file_name)
109 : BaseGenerator(parser, path, file_name, "", "_", "ts"),
110 namer_(WithFlagOptions(TypeScriptDefaultConfig(), parser.opts, path),
111 TypescriptKeywords()) {}
112
generate()113 bool generate() {
114 generateEnums();
115 generateStructs();
116 if (!parser_.opts.ts_omit_entrypoint) { generateEntry(); }
117 if (!generateBundle()) return false;
118 return true;
119 }
120
GetTypeName(const EnumDef & def,const bool=false,const bool force_ns_wrap=false)121 std::string GetTypeName(const EnumDef &def, const bool = false,
122 const bool force_ns_wrap = false) {
123 if (force_ns_wrap) { return namer_.NamespacedType(def); }
124 return namer_.Type(def);
125 }
126
GetTypeName(const StructDef & def,const bool object_api=false,const bool force_ns_wrap=false)127 std::string GetTypeName(const StructDef &def, const bool object_api = false,
128 const bool force_ns_wrap = false) {
129 if (object_api && parser_.opts.generate_object_based_api) {
130 if (force_ns_wrap) {
131 return namer_.NamespacedObjectType(def);
132 } else {
133 return namer_.ObjectType(def);
134 }
135 } else {
136 if (force_ns_wrap) {
137 return namer_.NamespacedType(def);
138 } else {
139 return namer_.Type(def);
140 }
141 }
142 }
143
144 // Save out the generated code for a single class while adding
145 // declaration boilerplate.
SaveType(const Definition & definition,const std::string & class_code,import_set & imports,import_set & bare_imports)146 bool SaveType(const Definition &definition, const std::string &class_code,
147 import_set &imports, import_set &bare_imports) {
148 if (!class_code.length()) return true;
149
150 std::string code;
151
152 code += "// " + std::string(FlatBuffersGeneratedWarning()) + "\n\n" +
153 "/* eslint-disable @typescript-eslint/no-unused-vars, @typescript-eslint/no-explicit-any, @typescript-eslint/no-non-null-assertion */\n\n";
154
155 for (auto it = bare_imports.begin(); it != bare_imports.end(); it++) {
156 code += it->second.import_statement + "\n";
157 }
158 if (!bare_imports.empty()) code += "\n";
159
160 for (auto it = imports.begin(); it != imports.end(); it++) {
161 if (it->second.dependency != &definition) {
162 code += it->second.import_statement + "\n";
163 }
164 }
165 if (!imports.empty()) code += "\n\n";
166
167 code += class_code;
168
169 auto dirs = namer_.Directories(*definition.defined_namespace);
170 EnsureDirExists(dirs);
171 auto basename = dirs + namer_.File(definition, SkipFile::Suffix);
172
173 return SaveFile(basename.c_str(), code, false);
174 }
175
TrackNsDef(const Definition & definition,std::string type_name)176 void TrackNsDef(const Definition &definition, std::string type_name) {
177 std::string path;
178 std::string filepath;
179 std::string symbolic_name;
180 if (definition.defined_namespace->components.size() > 0) {
181 path = namer_.Directories(*definition.defined_namespace,
182 SkipDir::TrailingPathSeperator);
183 filepath = path + ".ts";
184 path = namer_.Directories(*definition.defined_namespace,
185 SkipDir::OutputPathAndTrailingPathSeparator);
186 symbolic_name = definition.defined_namespace->components.back();
187 } else {
188 auto def_mod_name = namer_.File(definition, SkipFile::SuffixAndExtension);
189 symbolic_name = file_name_;
190 filepath = path_ + symbolic_name + ".ts";
191 }
192 if (ns_defs_.count(path) == 0) {
193 NsDefinition nsDef;
194 nsDef.path = path;
195 nsDef.filepath = filepath;
196 nsDef.ns = definition.defined_namespace;
197 nsDef.definitions.insert(std::make_pair(type_name, &definition));
198 nsDef.symbolic_name = symbolic_name;
199 ns_defs_[path] = nsDef;
200 } else {
201 ns_defs_[path].definitions.insert(std::make_pair(type_name, &definition));
202 }
203 }
204
205 private:
206 IdlNamer namer_;
207
208 std::map<std::string, NsDefinition> ns_defs_;
209
210 // Generate code for all enums.
generateEnums()211 void generateEnums() {
212 for (auto it = parser_.enums_.vec.begin(); it != parser_.enums_.vec.end();
213 ++it) {
214 import_set bare_imports;
215 import_set imports;
216 std::string enumcode;
217 auto &enum_def = **it;
218 GenEnum(enum_def, &enumcode, imports, false);
219 GenEnum(enum_def, &enumcode, imports, true);
220 std::string type_name = GetTypeName(enum_def);
221 TrackNsDef(enum_def, type_name);
222 SaveType(enum_def, enumcode, imports, bare_imports);
223 }
224 }
225
226 // Generate code for all structs.
generateStructs()227 void generateStructs() {
228 for (auto it = parser_.structs_.vec.begin();
229 it != parser_.structs_.vec.end(); ++it) {
230 import_set bare_imports;
231 import_set imports;
232 AddImport(bare_imports, "* as flatbuffers", "flatbuffers");
233 auto &struct_def = **it;
234 std::string declcode;
235 GenStruct(parser_, struct_def, &declcode, imports);
236 std::string type_name = GetTypeName(struct_def);
237 TrackNsDef(struct_def, type_name);
238 SaveType(struct_def, declcode, imports, bare_imports);
239 }
240 }
241
242 // Generate code for a single entry point module.
generateEntry()243 void generateEntry() {
244 std::string code;
245
246 // add root namespace def if not already existing from defs tracking
247 std::string root;
248 if (ns_defs_.count(root) == 0) {
249 NsDefinition nsDef;
250 nsDef.path = root;
251 nsDef.symbolic_name = file_name_;
252 nsDef.filepath = path_ + file_name_ + ".ts";
253 nsDef.ns = new Namespace();
254 ns_defs_[nsDef.path] = nsDef;
255 }
256
257 for (const auto &it : ns_defs_) {
258 code = "// " + std::string(FlatBuffersGeneratedWarning()) + "\n\n" +
259 "/* eslint-disable @typescript-eslint/no-unused-vars, @typescript-eslint/no-explicit-any, @typescript-eslint/no-non-null-assertion */\n\n";
260
261 // export all definitions in ns entry point module
262 int export_counter = 0;
263 for (const auto &def : it.second.definitions) {
264 std::vector<std::string> rel_components;
265 // build path for root level vs child level
266 if (it.second.ns->components.size() > 1)
267 std::copy(it.second.ns->components.begin() + 1,
268 it.second.ns->components.end(),
269 std::back_inserter(rel_components));
270 else
271 std::copy(it.second.ns->components.begin(),
272 it.second.ns->components.end(),
273 std::back_inserter(rel_components));
274 auto base_file_name =
275 namer_.File(*(def.second), SkipFile::SuffixAndExtension);
276 auto base_name =
277 namer_.Directories(it.second.ns->components, SkipDir::OutputPath) +
278 base_file_name;
279 auto ts_file_path = base_name + ".ts";
280 auto base_name_rel = std::string("./");
281 base_name_rel +=
282 namer_.Directories(rel_components, SkipDir::OutputPath);
283 base_name_rel += base_file_name;
284 auto ts_file_path_rel = base_name_rel + ".ts";
285 auto type_name = def.first;
286 auto fully_qualified_type_name =
287 it.second.ns->GetFullyQualifiedName(type_name);
288 auto is_struct = parser_.structs_.Lookup(fully_qualified_type_name);
289 code += "export { " + type_name;
290 if (parser_.opts.generate_object_based_api && is_struct) {
291 code += ", " + type_name + parser_.opts.object_suffix;
292 }
293 code += " } from '";
294 std::string import_extension =
295 parser_.opts.ts_no_import_ext ? "" : ".js";
296 code += base_name_rel + import_extension + "';\n";
297 export_counter++;
298 }
299
300 // re-export child namespace(s) in parent
301 const auto child_ns_level = it.second.ns->components.size() + 1;
302 for (const auto &it2 : ns_defs_) {
303 if (it2.second.ns->components.size() != child_ns_level) continue;
304 auto ts_file_path = it2.second.path + ".ts";
305 code += "export * as " + it2.second.symbolic_name + " from './";
306 std::string rel_path = it2.second.path;
307 code += rel_path + ".js';\n";
308 export_counter++;
309 }
310
311 if (export_counter > 0) SaveFile(it.second.filepath.c_str(), code, false);
312 }
313 }
314
generateBundle()315 bool generateBundle() {
316 if (parser_.opts.ts_flat_files) {
317 std::string inputpath;
318 std::string symbolic_name = file_name_;
319 inputpath = path_ + file_name_ + ".ts";
320 std::string bundlepath =
321 GeneratedFileName(path_, file_name_, parser_.opts);
322 bundlepath = bundlepath.substr(0, bundlepath.size() - 3) + ".js";
323 std::string cmd = "esbuild";
324 cmd += " ";
325 cmd += inputpath;
326 // cmd += " --minify";
327 cmd += " --format=cjs --bundle --outfile=";
328 cmd += bundlepath;
329 cmd += " --external:flatbuffers";
330 std::cout << "Entry point " << inputpath << " generated." << std::endl;
331 std::cout << "A single file bundle can be created using fx. esbuild with:"
332 << std::endl;
333 std::cout << "> " << cmd << std::endl;
334 }
335 return true;
336 }
337
338 // Generate a documentation comment, if available.
GenDocComment(const std::vector<std::string> & dc,std::string * code_ptr,const char * indent=nullptr)339 static void GenDocComment(const std::vector<std::string> &dc,
340 std::string *code_ptr,
341 const char *indent = nullptr) {
342 if (dc.empty()) {
343 // Don't output empty comment blocks with 0 lines of comment content.
344 return;
345 }
346
347 std::string &code = *code_ptr;
348 if (indent) code += indent;
349 code += "/**\n";
350 for (auto it = dc.begin(); it != dc.end(); ++it) {
351 if (indent) code += indent;
352 code += " *" + *it + "\n";
353 }
354 if (indent) code += indent;
355 code += " */\n";
356 }
357
GenDocComment(std::string * code_ptr)358 static void GenDocComment(std::string *code_ptr) {
359 GenDocComment(std::vector<std::string>(), code_ptr);
360 }
361
362 // Generate an enum declaration and an enum string lookup table.
GenEnum(EnumDef & enum_def,std::string * code_ptr,import_set & imports,bool reverse)363 void GenEnum(EnumDef &enum_def, std::string *code_ptr, import_set &imports,
364 bool reverse) {
365 if (enum_def.generated) return;
366 if (reverse) return; // FIXME.
367 std::string &code = *code_ptr;
368 GenDocComment(enum_def.doc_comment, code_ptr);
369 code += "export enum ";
370 code += GetTypeName(enum_def);
371 code += " {\n";
372 for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end(); ++it) {
373 auto &ev = **it;
374 if (!ev.doc_comment.empty()) {
375 if (it != enum_def.Vals().begin()) { code += '\n'; }
376 GenDocComment(ev.doc_comment, code_ptr, " ");
377 }
378
379 // Generate mapping between EnumName: EnumValue(int)
380 if (reverse) {
381 code += " '" + enum_def.ToString(ev) + "'";
382 code += " = ";
383 code += "'" + namer_.Variant(ev) + "'";
384 } else {
385 code += " " + namer_.Variant(ev);
386 code += " = ";
387 // Unfortunately, because typescript does not support bigint enums,
388 // for 64-bit enums, we instead map the enum names to strings.
389 switch (enum_def.underlying_type.base_type) {
390 case BASE_TYPE_LONG:
391 case BASE_TYPE_ULONG: {
392 code += "'" + enum_def.ToString(ev) + "'";
393 break;
394 }
395 default: code += enum_def.ToString(ev);
396 }
397 }
398
399 code += (it + 1) != enum_def.Vals().end() ? ",\n" : "\n";
400 }
401 code += "}";
402
403 if (enum_def.is_union) {
404 code += GenUnionConvFunc(enum_def.underlying_type, imports);
405 }
406
407 code += "\n";
408 }
409
GenType(const Type & type)410 static std::string GenType(const Type &type) {
411 switch (type.base_type) {
412 case BASE_TYPE_BOOL:
413 case BASE_TYPE_CHAR: return "Int8";
414 case BASE_TYPE_UTYPE: return GenType(GetUnionUnderlyingType(type));
415 case BASE_TYPE_UCHAR: return "Uint8";
416 case BASE_TYPE_SHORT: return "Int16";
417 case BASE_TYPE_USHORT: return "Uint16";
418 case BASE_TYPE_INT: return "Int32";
419 case BASE_TYPE_UINT: return "Uint32";
420 case BASE_TYPE_LONG: return "Int64";
421 case BASE_TYPE_ULONG: return "Uint64";
422 case BASE_TYPE_FLOAT: return "Float32";
423 case BASE_TYPE_DOUBLE: return "Float64";
424 case BASE_TYPE_STRING: return "String";
425 case BASE_TYPE_VECTOR: return GenType(type.VectorType());
426 case BASE_TYPE_STRUCT: return type.struct_def->name;
427 default: return "flatbuffers.Table";
428 }
429 }
430
GenGetter(const Type & type,const std::string & arguments)431 std::string GenGetter(const Type &type, const std::string &arguments) {
432 switch (type.base_type) {
433 case BASE_TYPE_STRING: return GenBBAccess() + ".__string" + arguments;
434 case BASE_TYPE_STRUCT: return GenBBAccess() + ".__struct" + arguments;
435 case BASE_TYPE_UNION:
436 if (!UnionHasStringType(*type.enum_def)) {
437 return GenBBAccess() + ".__union" + arguments;
438 }
439 return GenBBAccess() + ".__union_with_string" + arguments;
440 case BASE_TYPE_VECTOR: return GenGetter(type.VectorType(), arguments);
441 default: {
442 auto getter = GenBBAccess() + "." + "read" + GenType(type) + arguments;
443 if (type.base_type == BASE_TYPE_BOOL) { getter = "!!" + getter; }
444 return getter;
445 }
446 }
447 }
448
GenBBAccess() const449 std::string GenBBAccess() const { return "this.bb!"; }
450
GenDefaultValue(const FieldDef & field,import_set & imports)451 std::string GenDefaultValue(const FieldDef &field, import_set &imports) {
452 if (field.IsScalarOptional()) { return "null"; }
453
454 const auto &value = field.value;
455 if (value.type.enum_def && value.type.base_type != BASE_TYPE_UNION &&
456 value.type.base_type != BASE_TYPE_VECTOR) {
457 switch (value.type.base_type) {
458 case BASE_TYPE_ARRAY: {
459 std::string ret = "[";
460 for (auto i = 0; i < value.type.fixed_length; ++i) {
461 std::string enum_name =
462 AddImport(imports, *value.type.enum_def, *value.type.enum_def)
463 .name;
464 std::string enum_value = namer_.Variant(
465 *value.type.enum_def->FindByValue(value.constant));
466 ret += enum_name + "." + enum_value +
467 (i < value.type.fixed_length - 1 ? ", " : "");
468 }
469 ret += "]";
470 return ret;
471 }
472 case BASE_TYPE_LONG:
473 case BASE_TYPE_ULONG: {
474 // If the value is an enum with a 64-bit base type, we have to just
475 // return the bigint value directly since typescript does not support
476 // enums with bigint backing types.
477 return "BigInt('" + value.constant + "')";
478 }
479 default: {
480 EnumVal *val = value.type.enum_def->FindByValue(value.constant);
481 if (val == nullptr)
482 val = const_cast<EnumVal *>(value.type.enum_def->MinValue());
483 return AddImport(imports, *value.type.enum_def, *value.type.enum_def)
484 .name +
485 "." + namer_.Variant(*val);
486 }
487 }
488 }
489
490 switch (value.type.base_type) {
491 case BASE_TYPE_BOOL: return value.constant == "0" ? "false" : "true";
492
493 case BASE_TYPE_STRING:
494 case BASE_TYPE_UNION:
495 case BASE_TYPE_STRUCT: {
496 return "null";
497 }
498
499 case BASE_TYPE_ARRAY:
500 case BASE_TYPE_VECTOR: return "[]";
501
502 case BASE_TYPE_LONG:
503 case BASE_TYPE_ULONG: {
504 return "BigInt('" + value.constant + "')";
505 }
506
507 default: {
508 if (StringIsFlatbufferNan(value.constant)) {
509 return "NaN";
510 } else if (StringIsFlatbufferPositiveInfinity(value.constant)) {
511 return "Infinity";
512 } else if (StringIsFlatbufferNegativeInfinity(value.constant)) {
513 return "-Infinity";
514 }
515 return value.constant;
516 }
517 }
518 }
519
GenTypeName(import_set & imports,const Definition & owner,const Type & type,bool input,bool allowNull=false)520 std::string GenTypeName(import_set &imports, const Definition &owner,
521 const Type &type, bool input,
522 bool allowNull = false) {
523 if (!input) {
524 if (IsString(type) || type.base_type == BASE_TYPE_STRUCT) {
525 std::string name;
526 if (IsString(type)) {
527 name = "string|Uint8Array";
528 } else {
529 name = AddImport(imports, owner, *type.struct_def).name;
530 }
531 return allowNull ? (name + "|null") : name;
532 }
533 }
534
535 switch (type.base_type) {
536 case BASE_TYPE_BOOL: return allowNull ? "boolean|null" : "boolean";
537 case BASE_TYPE_LONG:
538 case BASE_TYPE_ULONG: return allowNull ? "bigint|null" : "bigint";
539 case BASE_TYPE_ARRAY: {
540 std::string name;
541 if (type.element == BASE_TYPE_LONG || type.element == BASE_TYPE_ULONG) {
542 name = "bigint[]";
543 } else if (type.element != BASE_TYPE_STRUCT) {
544 name = "number[]";
545 } else {
546 name = "any[]";
547 if (parser_.opts.generate_object_based_api) {
548 name = "(any|" +
549 GetTypeName(*type.struct_def, /*object_api =*/true) + ")[]";
550 }
551 }
552
553 return name + (allowNull ? "|null" : "");
554 }
555 default:
556 if (IsScalar(type.base_type)) {
557 if (type.enum_def) {
558 const auto enum_name =
559 AddImport(imports, owner, *type.enum_def).name;
560 return allowNull ? (enum_name + "|null") : enum_name;
561 }
562 return allowNull ? "number|null" : "number";
563 }
564 return "flatbuffers.Offset";
565 }
566 }
567
GetUnionUnderlyingType(const Type & type)568 static Type GetUnionUnderlyingType(const Type &type)
569 {
570 if (type.enum_def != nullptr &&
571 type.enum_def->underlying_type.base_type != type.base_type) {
572 return type.enum_def->underlying_type;
573 } else {
574 return Type(BASE_TYPE_UCHAR);
575 }
576 }
577
GetUnderlyingVectorType(const Type & vector_type)578 static Type GetUnderlyingVectorType(const Type &vector_type)
579 {
580 return (vector_type.base_type == BASE_TYPE_UTYPE) ? GetUnionUnderlyingType(vector_type) : vector_type;
581 }
582
583 // Returns the method name for use with add/put calls.
GenWriteMethod(const Type & type)584 std::string GenWriteMethod(const Type &type) {
585 // Forward to signed versions since unsigned versions don't exist
586 switch (type.base_type) {
587 case BASE_TYPE_UTYPE: return GenWriteMethod(GetUnionUnderlyingType(type));
588 case BASE_TYPE_UCHAR: return GenWriteMethod(Type(BASE_TYPE_CHAR));
589 case BASE_TYPE_USHORT: return GenWriteMethod(Type(BASE_TYPE_SHORT));
590 case BASE_TYPE_UINT: return GenWriteMethod(Type(BASE_TYPE_INT));
591 case BASE_TYPE_ULONG: return GenWriteMethod(Type(BASE_TYPE_LONG));
592 default: break;
593 }
594
595 return IsScalar(type.base_type) ? namer_.Type(GenType(type))
596 : (IsStruct(type) ? "Struct" : "Offset");
597 }
598
MaybeAdd(T value)599 template<typename T> static std::string MaybeAdd(T value) {
600 return value != 0 ? " + " + NumToString(value) : "";
601 }
602
MaybeScale(T value)603 template<typename T> static std::string MaybeScale(T value) {
604 return value != 1 ? " * " + NumToString(value) : "";
605 }
606
GenStructArgs(import_set & imports,const StructDef & struct_def,std::string * arguments,const std::string & nameprefix)607 void GenStructArgs(import_set &imports, const StructDef &struct_def,
608 std::string *arguments, const std::string &nameprefix) {
609 for (auto it = struct_def.fields.vec.begin();
610 it != struct_def.fields.vec.end(); ++it) {
611 auto &field = **it;
612 if (IsStruct(field.value.type)) {
613 // Generate arguments for a struct inside a struct. To ensure names
614 // don't clash, and to make it obvious these arguments are constructing
615 // a nested struct, prefix the name with the field name.
616 GenStructArgs(imports, *field.value.type.struct_def, arguments,
617 nameprefix + field.name + "_");
618 } else {
619 *arguments += ", " + nameprefix + field.name + ": " +
620 GenTypeName(imports, field, field.value.type, true,
621 field.IsOptional());
622 }
623 }
624 }
625
GenStructBody(const StructDef & struct_def,std::string * body,const std::string & nameprefix)626 void GenStructBody(const StructDef &struct_def, std::string *body,
627 const std::string &nameprefix) {
628 *body += " builder.prep(";
629 *body += NumToString(struct_def.minalign) + ", ";
630 *body += NumToString(struct_def.bytesize) + ");\n";
631
632 for (auto it = struct_def.fields.vec.rbegin();
633 it != struct_def.fields.vec.rend(); ++it) {
634 auto &field = **it;
635 if (field.padding) {
636 *body += " builder.pad(" + NumToString(field.padding) + ");\n";
637 }
638 if (IsStruct(field.value.type)) {
639 // Generate arguments for a struct inside a struct. To ensure names
640 // don't clash, and to make it obvious these arguments are constructing
641 // a nested struct, prefix the name with the field name.
642 GenStructBody(
643 *field.value.type.struct_def, body,
644 nameprefix.length() ? nameprefix + "_" + field.name : field.name);
645 } else {
646 auto element_type = field.value.type.element;
647
648 if (field.value.type.base_type == BASE_TYPE_ARRAY) {
649 switch (field.value.type.element) {
650 case BASE_TYPE_STRUCT: {
651 std::string str_last_item_idx =
652 NumToString(field.value.type.fixed_length - 1);
653 *body += "\n for (let i = " + str_last_item_idx +
654 "; i >= 0; --i" + ") {\n";
655
656 std::string fname = nameprefix.length()
657 ? nameprefix + "_" + field.name
658 : field.name;
659
660 *body += " const item = " + fname + "?.[i];\n\n";
661
662 if (parser_.opts.generate_object_based_api) {
663 *body += " if (item instanceof " +
664 GetTypeName(*field.value.type.struct_def,
665 /*object_api =*/true) +
666 ") {\n";
667 *body += " item.pack(builder);\n";
668 *body += " continue;\n";
669 *body += " }\n\n";
670 }
671
672 std::string class_name =
673 GetPrefixedName(*field.value.type.struct_def);
674 std::string pack_func_create_call =
675 class_name + ".create" + class_name + "(builder,\n";
676 pack_func_create_call +=
677 " " +
678 GenStructMemberValueTS(*field.value.type.struct_def, "item",
679 ",\n ", false) +
680 "\n ";
681 *body += " " + pack_func_create_call;
682 *body += " );\n }\n\n";
683
684 break;
685 }
686 default: {
687 std::string str_last_item_idx =
688 NumToString(field.value.type.fixed_length - 1);
689 std::string fname = nameprefix.length()
690 ? nameprefix + "_" + field.name
691 : field.name;
692
693 *body += "\n for (let i = " + str_last_item_idx +
694 "; i >= 0; --i) {\n";
695 *body += " builder.write";
696 *body += GenWriteMethod(
697 static_cast<flatbuffers::Type>(field.value.type.element));
698 *body += "(";
699 *body += element_type == BASE_TYPE_BOOL ? "+" : "";
700
701 if (element_type == BASE_TYPE_LONG ||
702 element_type == BASE_TYPE_ULONG) {
703 *body += "BigInt(" + fname + "?.[i] ?? 0));\n";
704 } else {
705 *body += "(" + fname + "?.[i] ?? 0));\n\n";
706 }
707 *body += " }\n\n";
708 break;
709 }
710 }
711 } else {
712 std::string fname =
713 nameprefix.length() ? nameprefix + "_" + field.name : field.name;
714
715 *body += " builder.write" + GenWriteMethod(field.value.type) + "(";
716 if (field.value.type.base_type == BASE_TYPE_BOOL) {
717 *body += "Number(Boolean(" + fname + ")));\n";
718 continue;
719 } else if (field.value.type.base_type == BASE_TYPE_LONG ||
720 field.value.type.base_type == BASE_TYPE_ULONG) {
721 *body += "BigInt(" + fname + " ?? 0));\n";
722 continue;
723 }
724
725 *body += fname + ");\n";
726 }
727 }
728 }
729 }
730
GenerateNewExpression(const std::string & object_name)731 std::string GenerateNewExpression(const std::string &object_name) {
732 return "new " + namer_.Type(object_name) + "()";
733 }
734
GenerateRootAccessor(StructDef & struct_def,std::string * code_ptr,std::string & code,const std::string & object_name,bool size_prefixed)735 void GenerateRootAccessor(StructDef &struct_def, std::string *code_ptr,
736 std::string &code, const std::string &object_name,
737 bool size_prefixed) {
738 if (!struct_def.fixed) {
739 GenDocComment(code_ptr);
740 std::string sizePrefixed("SizePrefixed");
741 code += "static get" + (size_prefixed ? sizePrefixed : "") + "Root" +
742 GetPrefixedName(struct_def, "As");
743 code += "(bb:flatbuffers.ByteBuffer, obj?:" + object_name +
744 "):" + object_name + " {\n";
745 if (size_prefixed) {
746 code +=
747 " bb.setPosition(bb.position() + "
748 "flatbuffers.SIZE_PREFIX_LENGTH);\n";
749 }
750 code += " return (obj || " + GenerateNewExpression(object_name);
751 code += ").__init(bb.readInt32(bb.position()) + bb.position(), bb);\n";
752 code += "}\n\n";
753 }
754 }
755
GenerateFinisher(StructDef & struct_def,std::string * code_ptr,std::string & code,bool size_prefixed)756 void GenerateFinisher(StructDef &struct_def, std::string *code_ptr,
757 std::string &code, bool size_prefixed) {
758 if (parser_.root_struct_def_ == &struct_def) {
759 std::string sizePrefixed("SizePrefixed");
760 GenDocComment(code_ptr);
761
762 code += "static finish" + (size_prefixed ? sizePrefixed : "") +
763 GetPrefixedName(struct_def) + "Buffer";
764 code += "(builder:flatbuffers.Builder, offset:flatbuffers.Offset) {\n";
765 code += " builder.finish(offset";
766 if (!parser_.file_identifier_.empty()) {
767 code += ", '" + parser_.file_identifier_ + "'";
768 }
769 if (size_prefixed) {
770 if (parser_.file_identifier_.empty()) { code += ", undefined"; }
771 code += ", true";
772 }
773 code += ");\n";
774 code += "}\n\n";
775 }
776 }
777
UnionHasStringType(const EnumDef & union_enum)778 bool UnionHasStringType(const EnumDef &union_enum) {
779 return std::any_of(union_enum.Vals().begin(), union_enum.Vals().end(),
780 [](const EnumVal *ev) {
781 return !ev->IsZero() && IsString(ev->union_type);
782 });
783 }
784
GenUnionGenericTypeTS(const EnumDef & union_enum)785 std::string GenUnionGenericTypeTS(const EnumDef &union_enum) {
786 // TODO: make it work without any
787 // return std::string("T") + (UnionHasStringType(union_enum) ? "|string" :
788 // "");
789 return std::string("any") +
790 (UnionHasStringType(union_enum) ? "|string" : "");
791 }
792
GenUnionTypeTS(const EnumDef & union_enum,import_set & imports)793 std::string GenUnionTypeTS(const EnumDef &union_enum, import_set &imports) {
794 std::string ret;
795 std::set<std::string> type_list;
796
797 for (auto it = union_enum.Vals().begin(); it != union_enum.Vals().end();
798 ++it) {
799 const auto &ev = **it;
800 if (ev.IsZero()) { continue; }
801
802 std::string type = "";
803 if (IsString(ev.union_type)) {
804 type = "string"; // no need to wrap string type in namespace
805 } else if (ev.union_type.base_type == BASE_TYPE_STRUCT) {
806 type = AddImport(imports, union_enum, *ev.union_type.struct_def).name;
807 } else {
808 FLATBUFFERS_ASSERT(false);
809 }
810 type_list.insert(type);
811 }
812
813 for (auto it = type_list.begin(); it != type_list.end(); ++it) {
814 ret += *it + ((std::next(it) == type_list.end()) ? "" : "|");
815 }
816
817 return ret;
818 }
819
CheckIfNameClashes(const import_set & imports,const std::string & name)820 static bool CheckIfNameClashes(const import_set &imports,
821 const std::string &name) {
822 // TODO: this would be better as a hashset.
823 for (auto it = imports.begin(); it != imports.end(); it++) {
824 if (it->second.name == name) { return true; }
825 }
826 return false;
827 }
828
GenSymbolExpression(const StructDef & struct_def,const bool has_name_clash,const std::string & import_name,const std::string & name,const std::string & object_name)829 std::string GenSymbolExpression(const StructDef &struct_def,
830 const bool has_name_clash,
831 const std::string &import_name,
832 const std::string &name,
833 const std::string &object_name) {
834 std::string symbols_expression;
835
836 if (has_name_clash) {
837 // We have a name clash
838 symbols_expression += import_name + " as " + name;
839
840 if (parser_.opts.generate_object_based_api) {
841 symbols_expression += ", " +
842 GetTypeName(struct_def, /*object_api =*/true) +
843 " as " + object_name;
844 }
845 } else {
846 // No name clash, use the provided name
847 symbols_expression += name;
848
849 if (parser_.opts.generate_object_based_api) {
850 symbols_expression += ", " + object_name;
851 }
852 }
853
854 return symbols_expression;
855 }
856
GenSymbolExpression(const EnumDef & enum_def,const bool has_name_clash,const std::string & import_name,const std::string & name,const std::string &)857 std::string GenSymbolExpression(const EnumDef &enum_def,
858 const bool has_name_clash,
859 const std::string &import_name,
860 const std::string &name,
861 const std::string &) {
862 std::string symbols_expression;
863 if (has_name_clash) {
864 symbols_expression += import_name + " as " + name;
865 } else {
866 symbols_expression += name;
867 }
868
869 if (enum_def.is_union) {
870 symbols_expression += (", " + namer_.Function("unionTo" + name));
871 symbols_expression += (", " + namer_.Function("unionListTo" + name));
872 }
873
874 return symbols_expression;
875 }
876
877 template<typename DefinitionT>
AddImport(import_set & imports,const Definition & dependent,const DefinitionT & dependency)878 ImportDefinition AddImport(import_set &imports, const Definition &dependent,
879 const DefinitionT &dependency) {
880 // The unique name of the dependency, fully qualified in its namespace.
881 const std::string unique_name = GetTypeName(
882 dependency, /*object_api = */ false, /*force_ns_wrap=*/true);
883
884 // Look if we have already added this import and return its name if found.
885 const auto import_pair = imports.find(unique_name);
886 if (import_pair != imports.end()) { return import_pair->second; }
887
888 // Check if this name would have a name clash with another type. Just use
889 // the "base" name (properly escaped) without any namespacing applied.
890 const std::string import_name = GetTypeName(dependency);
891 const bool has_name_clash = CheckIfNameClashes(imports, import_name);
892
893 // If we have a name clash, use the unique name, otherwise use simple name.
894 std::string name = has_name_clash ? unique_name : import_name;
895
896 const std::string object_name =
897 GetTypeName(dependency, /*object_api=*/true, has_name_clash);
898
899 const std::string symbols_expression = GenSymbolExpression(
900 dependency, has_name_clash, import_name, name, object_name);
901
902 std::string bare_file_path;
903 std::string rel_file_path;
904 if (dependent.defined_namespace) {
905 const auto &dep_comps = dependent.defined_namespace->components;
906 for (size_t i = 0; i < dep_comps.size(); i++) {
907 rel_file_path += i == 0 ? ".." : (kPathSeparator + std::string(".."));
908 }
909 if (dep_comps.size() == 0) { rel_file_path += "."; }
910 } else {
911 rel_file_path += "..";
912 }
913
914 bare_file_path +=
915 kPathSeparator +
916 namer_.Directories(dependency.defined_namespace->components,
917 SkipDir::OutputPath) +
918 namer_.File(dependency, SkipFile::SuffixAndExtension);
919 rel_file_path += bare_file_path;
920
921 ImportDefinition import;
922 import.name = name;
923 import.object_name = object_name;
924 import.bare_file_path = bare_file_path;
925 import.rel_file_path = rel_file_path;
926 std::string import_extension = parser_.opts.ts_no_import_ext ? "" : ".js";
927 import.import_statement = "import { " + symbols_expression + " } from '" +
928 rel_file_path + import_extension + "';";
929 import.export_statement = "export { " + symbols_expression + " } from '." +
930 bare_file_path + import_extension + "';";
931 import.dependency = &dependency;
932 import.dependent = &dependent;
933
934 imports.insert(std::make_pair(unique_name, import));
935
936 return import;
937 }
938
AddImport(import_set & imports,std::string import_name,std::string fileName)939 void AddImport(import_set &imports, std::string import_name,
940 std::string fileName) {
941 ImportDefinition import;
942 import.name = import_name;
943 import.import_statement =
944 "import " + import_name + " from '" + fileName + "';";
945 imports.insert(std::make_pair(import_name, import));
946 }
947
948 // Generate a TS union type based on a union's enum
GenObjApiUnionTypeTS(import_set & imports,const StructDef & dependent,const IDLOptions &,const EnumDef & union_enum)949 std::string GenObjApiUnionTypeTS(import_set &imports,
950 const StructDef &dependent,
951 const IDLOptions &,
952 const EnumDef &union_enum) {
953 std::string ret = "";
954 std::set<std::string> type_list;
955
956 for (auto it = union_enum.Vals().begin(); it != union_enum.Vals().end();
957 ++it) {
958 const auto &ev = **it;
959 if (ev.IsZero()) { continue; }
960
961 std::string type = "";
962 if (IsString(ev.union_type)) {
963 type = "string"; // no need to wrap string type in namespace
964 } else if (ev.union_type.base_type == BASE_TYPE_STRUCT) {
965 type = AddImport(imports, dependent, *ev.union_type.struct_def)
966 .object_name;
967 } else {
968 FLATBUFFERS_ASSERT(false);
969 }
970 type_list.insert(type);
971 }
972
973 size_t totalPrinted = 0;
974 for (auto it = type_list.begin(); it != type_list.end(); ++it) {
975 ++totalPrinted;
976 ret += *it + ((totalPrinted == type_list.size()) ? "" : "|");
977 }
978
979 return ret;
980 }
981
GenUnionConvFuncName(const EnumDef & enum_def)982 std::string GenUnionConvFuncName(const EnumDef &enum_def) {
983 return namer_.Function("unionTo", enum_def);
984 }
985
GenUnionListConvFuncName(const EnumDef & enum_def)986 std::string GenUnionListConvFuncName(const EnumDef &enum_def) {
987 return namer_.Function("unionListTo", enum_def);
988 }
989
GenUnionConvFunc(const Type & union_type,import_set & imports)990 std::string GenUnionConvFunc(const Type &union_type, import_set &imports) {
991 if (union_type.enum_def) {
992 const auto &enum_def = *union_type.enum_def;
993
994 const auto valid_union_type = GenUnionTypeTS(enum_def, imports);
995 const auto valid_union_type_with_null = valid_union_type + "|null";
996
997 auto ret = "\n\nexport function " + GenUnionConvFuncName(enum_def) +
998 "(\n type: " + GetTypeName(enum_def) +
999 ",\n accessor: (obj:" + valid_union_type + ") => " +
1000 valid_union_type_with_null +
1001 "\n): " + valid_union_type_with_null + " {\n";
1002
1003 const auto enum_type = AddImport(imports, enum_def, enum_def).name;
1004
1005 const auto union_enum_loop = [&](const std::string &accessor_str) {
1006 ret += " switch(" + enum_type + "[type]) {\n";
1007 ret += " case 'NONE': return null; \n";
1008
1009 for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end();
1010 ++it) {
1011 const auto &ev = **it;
1012 if (ev.IsZero()) { continue; }
1013
1014 ret += " case '" + namer_.Variant(ev) + "': ";
1015
1016 if (IsString(ev.union_type)) {
1017 ret += "return " + accessor_str + "'') as string;";
1018 } else if (ev.union_type.base_type == BASE_TYPE_STRUCT) {
1019 const auto type =
1020 AddImport(imports, enum_def, *ev.union_type.struct_def).name;
1021 ret += "return " + accessor_str + "new " + type + "())! as " +
1022 type + ";";
1023 } else {
1024 FLATBUFFERS_ASSERT(false);
1025 }
1026 ret += "\n";
1027 }
1028
1029 ret += " default: return null;\n";
1030 ret += " }\n";
1031 };
1032
1033 union_enum_loop("accessor(");
1034 ret += "}";
1035
1036 ret += "\n\nexport function " + GenUnionListConvFuncName(enum_def) +
1037 "(\n type: " + GetTypeName(enum_def) +
1038 ", \n accessor: (index: number, obj:" + valid_union_type +
1039 ") => " + valid_union_type_with_null +
1040 ", \n index: number\n): " + valid_union_type_with_null + " {\n";
1041 union_enum_loop("accessor(index, ");
1042 ret += "}";
1043
1044 return ret;
1045 }
1046 FLATBUFFERS_ASSERT(0);
1047 return "";
1048 }
1049
1050 // Used for generating a short function that returns the correct class
1051 // based on union enum type. Assume the context is inside the non object api
1052 // type
GenUnionValTS(import_set & imports,const StructDef & dependent,const std::string & field_name,const Type & union_type,const bool is_array=false)1053 std::string GenUnionValTS(import_set &imports, const StructDef &dependent,
1054 const std::string &field_name,
1055 const Type &union_type,
1056 const bool is_array = false) {
1057 if (union_type.enum_def) {
1058 const auto &enum_def = *union_type.enum_def;
1059 const auto enum_type = AddImport(imports, dependent, enum_def).name;
1060 const std::string union_accessor = "this." + field_name;
1061
1062 const auto union_has_string = UnionHasStringType(enum_def);
1063 const auto field_binded_method = "this." + field_name + ".bind(this)";
1064
1065 std::string ret;
1066
1067 if (!is_array) {
1068 const auto conversion_function = GenUnionConvFuncName(enum_def);
1069
1070 ret = "(() => {\n";
1071 ret += " const temp = " + conversion_function + "(this." +
1072 namer_.Method(field_name, "Type") + "(), " +
1073 field_binded_method + ");\n";
1074 ret += " if(temp === null) { return null; }\n";
1075 ret += union_has_string
1076 ? " if(typeof temp === 'string') { return temp; }\n"
1077 : "";
1078 ret += " return temp.unpack()\n";
1079 ret += " })()";
1080 } else {
1081 const auto conversion_function = GenUnionListConvFuncName(enum_def);
1082
1083 ret = "(() => {\n";
1084 ret += " const ret: (" +
1085 GenObjApiUnionTypeTS(imports, *union_type.struct_def,
1086 parser_.opts, *union_type.enum_def) +
1087 ")[] = [];\n";
1088 ret += " for(let targetEnumIndex = 0; targetEnumIndex < this." +
1089 namer_.Method(field_name, "TypeLength") + "()" +
1090 "; "
1091 "++targetEnumIndex) {\n";
1092 ret += " const targetEnum = this." +
1093 namer_.Method(field_name, "Type") + "(targetEnumIndex);\n";
1094 ret += " if(targetEnum === null || " + enum_type +
1095 "[targetEnum!] === 'NONE') { "
1096 "continue; }\n\n";
1097 ret += " const temp = " + conversion_function + "(targetEnum, " +
1098 field_binded_method + ", targetEnumIndex);\n";
1099 ret += " if(temp === null) { continue; }\n";
1100 ret += union_has_string ? " if(typeof temp === 'string') { "
1101 "ret.push(temp); continue; }\n"
1102 : "";
1103 ret += " ret.push(temp.unpack());\n";
1104 ret += " }\n";
1105 ret += " return ret;\n";
1106 ret += " })()";
1107 }
1108
1109 return ret;
1110 }
1111
1112 FLATBUFFERS_ASSERT(0);
1113 return "";
1114 }
1115
GenNullCheckConditional(const std::string & nullCheckVar,const std::string & trueVal,const std::string & falseVal="null")1116 static std::string GenNullCheckConditional(
1117 const std::string &nullCheckVar, const std::string &trueVal,
1118 const std::string &falseVal = "null") {
1119 return "(" + nullCheckVar + " !== null ? " + trueVal + " : " + falseVal +
1120 ")";
1121 }
1122
GenStructMemberValueTS(const StructDef & struct_def,const std::string & prefix,const std::string & delimiter,const bool nullCheck=true)1123 std::string GenStructMemberValueTS(const StructDef &struct_def,
1124 const std::string &prefix,
1125 const std::string &delimiter,
1126 const bool nullCheck = true) {
1127 std::string ret;
1128 for (auto it = struct_def.fields.vec.begin();
1129 it != struct_def.fields.vec.end(); ++it) {
1130 auto &field = **it;
1131
1132 auto curr_member_accessor = prefix + "." + namer_.Method(field);
1133 if (prefix != "this") {
1134 curr_member_accessor = prefix + "?." + namer_.Method(field);
1135 }
1136 if (IsStruct(field.value.type)) {
1137 ret += GenStructMemberValueTS(*field.value.type.struct_def,
1138 curr_member_accessor, delimiter);
1139 } else {
1140 if (nullCheck) {
1141 std::string nullValue = "0";
1142 if (field.value.type.base_type == BASE_TYPE_BOOL) {
1143 nullValue = "false";
1144 } else if (field.value.type.base_type == BASE_TYPE_LONG ||
1145 field.value.type.base_type == BASE_TYPE_ULONG) {
1146 nullValue = "BigInt(0)";
1147 } else if (field.value.type.base_type == BASE_TYPE_ARRAY) {
1148 nullValue = "[]";
1149 }
1150 ret += "(" + curr_member_accessor + " ?? " + nullValue + ")";
1151 } else {
1152 ret += curr_member_accessor;
1153 }
1154 }
1155
1156 if (std::next(it) != struct_def.fields.vec.end()) { ret += delimiter; }
1157 }
1158
1159 return ret;
1160 }
1161
GenObjApi(const Parser & parser,StructDef & struct_def,std::string & obj_api_unpack_func,std::string & obj_api_class,import_set & imports)1162 void GenObjApi(const Parser &parser, StructDef &struct_def,
1163 std::string &obj_api_unpack_func, std::string &obj_api_class,
1164 import_set &imports) {
1165 const auto class_name = GetTypeName(struct_def, /*object_api=*/true);
1166
1167 std::string unpack_func = "\nunpack(): " + class_name +
1168 " {\n return new " + class_name + "(" +
1169 (struct_def.fields.vec.empty() ? "" : "\n");
1170 std::string unpack_to_func = "\nunpackTo(_o: " + class_name + "): void {" +
1171 +(struct_def.fields.vec.empty() ? "" : "\n");
1172
1173 std::string constructor_func = "constructor(";
1174 constructor_func += (struct_def.fields.vec.empty() ? "" : "\n");
1175
1176 const auto has_create =
1177 struct_def.fixed || CanCreateFactoryMethod(struct_def);
1178
1179 std::string pack_func_prototype =
1180 "\npack(builder:flatbuffers.Builder): flatbuffers.Offset {\n";
1181
1182 std::string pack_func_offset_decl;
1183 std::string pack_func_create_call;
1184
1185 const auto struct_name = AddImport(imports, struct_def, struct_def).name;
1186
1187 if (has_create) {
1188 pack_func_create_call = " return " + struct_name + ".create" +
1189 GetPrefixedName(struct_def) + "(builder" +
1190 (struct_def.fields.vec.empty() ? "" : ",\n ");
1191 } else {
1192 pack_func_create_call = " " + struct_name + ".start" +
1193 GetPrefixedName(struct_def) + "(builder);\n";
1194 }
1195
1196 if (struct_def.fixed) {
1197 // when packing struct, nested struct's members instead of the struct's
1198 // offset are used
1199 pack_func_create_call +=
1200 GenStructMemberValueTS(struct_def, "this", ",\n ", false) + "\n ";
1201 }
1202
1203 for (auto it = struct_def.fields.vec.begin();
1204 it != struct_def.fields.vec.end(); ++it) {
1205 auto &field = **it;
1206 if (field.deprecated) continue;
1207
1208 const auto field_method = namer_.Method(field);
1209 const auto field_field = namer_.Field(field);
1210 const std::string field_binded_method =
1211 "this." + field_method + ".bind(this)";
1212
1213 std::string field_val;
1214 std::string field_type;
1215 // a string that declares a variable containing the
1216 // offset for things that can't be generated inline
1217 // empty otw
1218 std::string field_offset_decl;
1219 // a string that contains values for things that can be created inline or
1220 // the variable name from field_offset_decl
1221 std::string field_offset_val;
1222 const auto field_default_val = GenDefaultValue(field, imports);
1223
1224 // Emit a scalar field
1225 const auto is_string = IsString(field.value.type);
1226 if (IsScalar(field.value.type.base_type) || is_string) {
1227 const auto has_null_default = is_string || HasNullDefault(field);
1228
1229 field_type += GenTypeName(imports, field, field.value.type, false,
1230 has_null_default);
1231 field_val = "this." + namer_.Method(field) + "()";
1232
1233 if (field.value.type.base_type != BASE_TYPE_STRING) {
1234 field_offset_val = "this." + namer_.Field(field);
1235 } else {
1236 field_offset_decl = GenNullCheckConditional(
1237 "this." + namer_.Field(field),
1238 "builder.createString(this." + field_field + "!)", "0");
1239 }
1240 }
1241
1242 // Emit an object field
1243 else {
1244 auto is_vector = false;
1245 switch (field.value.type.base_type) {
1246 case BASE_TYPE_STRUCT: {
1247 const auto &sd = *field.value.type.struct_def;
1248 field_type += AddImport(imports, struct_def, sd).object_name;
1249
1250 const std::string field_accessor =
1251 "this." + namer_.Method(field) + "()";
1252 field_val = GenNullCheckConditional(field_accessor,
1253 field_accessor + "!.unpack()");
1254 auto packing = GenNullCheckConditional(
1255 "this." + field_field,
1256 "this." + field_field + "!.pack(builder)", "0");
1257
1258 if (sd.fixed) {
1259 field_offset_val = std::move(packing);
1260 } else {
1261 field_offset_decl = std::move(packing);
1262 }
1263
1264 break;
1265 }
1266
1267 case BASE_TYPE_ARRAY: {
1268 auto vectortype = field.value.type.VectorType();
1269 auto vectortypename =
1270 GenTypeName(imports, struct_def, vectortype, false);
1271 is_vector = true;
1272
1273 field_type = "(";
1274
1275 switch (vectortype.base_type) {
1276 case BASE_TYPE_STRUCT: {
1277 const auto &sd = *field.value.type.struct_def;
1278 const auto field_type_name =
1279 GetTypeName(sd, /*object_api=*/true);
1280 field_type += field_type_name;
1281 field_type += ")[]";
1282
1283 field_val = GenBBAccess() + ".createObjList<" + vectortypename +
1284 ", " + field_type_name + ">(" +
1285 field_binded_method + ", " +
1286 NumToString(field.value.type.fixed_length) + ")";
1287
1288 if (sd.fixed) {
1289 field_offset_decl =
1290 "builder.createStructOffsetList(this." + field_field +
1291 ", " + AddImport(imports, struct_def, struct_def).name +
1292 "." + namer_.Method("start", field, "Vector") + ")";
1293 } else {
1294 field_offset_decl =
1295 AddImport(imports, struct_def, struct_def).name + "." +
1296 namer_.Method("create", field, "Vector") +
1297 "(builder, builder.createObjectOffsetList(" + "this." +
1298 field_field + "))";
1299 }
1300
1301 break;
1302 }
1303
1304 case BASE_TYPE_STRING: {
1305 field_type += "string)[]";
1306 field_val = GenBBAccess() + ".createScalarList<string>(" +
1307 field_binded_method + ", this." +
1308 namer_.Field(field, "Length") + "())";
1309 field_offset_decl =
1310 AddImport(imports, struct_def, struct_def).name + "." +
1311 namer_.Method("create", field, "Vector") +
1312 "(builder, builder.createObjectOffsetList(" + "this." +
1313 namer_.Field(field) + "))";
1314 break;
1315 }
1316
1317 case BASE_TYPE_UNION: {
1318 field_type += GenObjApiUnionTypeTS(
1319 imports, struct_def, parser.opts, *(vectortype.enum_def));
1320 field_type += ")[]";
1321 field_val = GenUnionValTS(imports, struct_def, field_method,
1322 vectortype, true);
1323
1324 field_offset_decl =
1325 AddImport(imports, struct_def, struct_def).name + "." +
1326 namer_.Method("create", field, "Vector") +
1327 "(builder, builder.createObjectOffsetList(" + "this." +
1328 namer_.Field(field) + "))";
1329
1330 break;
1331 }
1332 default: {
1333 if (vectortype.enum_def) {
1334 field_type += GenTypeName(imports, struct_def, vectortype,
1335 false, HasNullDefault(field));
1336 } else {
1337 field_type += vectortypename;
1338 }
1339 field_type += ")[]";
1340 field_val = GenBBAccess() + ".createScalarList<" +
1341 vectortypename + ">(" + field_binded_method + ", " +
1342 NumToString(field.value.type.fixed_length) + ")";
1343
1344 field_offset_decl =
1345 AddImport(imports, struct_def, struct_def).name + "." +
1346 namer_.Method("create", field, "Vector") +
1347 "(builder, this." + field_field + ")";
1348
1349 break;
1350 }
1351 }
1352
1353 break;
1354 }
1355
1356 case BASE_TYPE_VECTOR: {
1357 auto vectortype = field.value.type.VectorType();
1358 auto vectortypename =
1359 GenTypeName(imports, struct_def, vectortype, false);
1360 is_vector = true;
1361
1362 field_type = "(";
1363
1364 switch (vectortype.base_type) {
1365 case BASE_TYPE_STRUCT: {
1366 const auto &sd = *field.value.type.struct_def;
1367 const auto field_type_name =
1368 GetTypeName(sd, /*object_api=*/true);
1369 field_type += field_type_name;
1370 field_type += ")[]";
1371
1372 field_val = GenBBAccess() + ".createObjList<" + vectortypename +
1373 ", " + field_type_name + ">(" +
1374 field_binded_method + ", this." +
1375 namer_.Method(field, "Length") + "())";
1376
1377 if (sd.fixed) {
1378 field_offset_decl =
1379 "builder.createStructOffsetList(this." + field_field +
1380 ", " + AddImport(imports, struct_def, struct_def).name +
1381 "." + namer_.Method("start", field, "Vector") + ")";
1382 } else {
1383 field_offset_decl =
1384 AddImport(imports, struct_def, struct_def).name + "." +
1385 namer_.Method("create", field, "Vector") +
1386 "(builder, builder.createObjectOffsetList(" + "this." +
1387 field_field + "))";
1388 }
1389
1390 break;
1391 }
1392
1393 case BASE_TYPE_STRING: {
1394 field_type += "string)[]";
1395 field_val = GenBBAccess() + ".createScalarList<string>(" +
1396 field_binded_method + ", this." +
1397 namer_.Field(field, "Length") + "())";
1398 field_offset_decl =
1399 AddImport(imports, struct_def, struct_def).name + "." +
1400 namer_.Method("create", field, "Vector") +
1401 "(builder, builder.createObjectOffsetList(" + "this." +
1402 namer_.Field(field) + "))";
1403 break;
1404 }
1405
1406 case BASE_TYPE_UNION: {
1407 field_type += GenObjApiUnionTypeTS(
1408 imports, struct_def, parser.opts, *(vectortype.enum_def));
1409 field_type += ")[]";
1410 field_val = GenUnionValTS(imports, struct_def, field_method,
1411 vectortype, true);
1412
1413 field_offset_decl =
1414 AddImport(imports, struct_def, struct_def).name + "." +
1415 namer_.Method("create", field, "Vector") +
1416 "(builder, builder.createObjectOffsetList(" + "this." +
1417 namer_.Field(field) + "))";
1418
1419 break;
1420 }
1421 default: {
1422 if (vectortype.enum_def) {
1423 field_type += GenTypeName(imports, struct_def, vectortype,
1424 false, HasNullDefault(field));
1425 } else {
1426 field_type += vectortypename;
1427 }
1428 field_type += ")[]";
1429 field_val = GenBBAccess() + ".createScalarList<" +
1430 vectortypename + ">(" + field_binded_method +
1431 ", this." + namer_.Method(field, "Length") + "())";
1432
1433 field_offset_decl =
1434 AddImport(imports, struct_def, struct_def).name + "." +
1435 namer_.Method("create", field, "Vector") +
1436 "(builder, this." + field_field + ")";
1437
1438 break;
1439 }
1440 }
1441
1442 break;
1443 }
1444
1445 case BASE_TYPE_UNION: {
1446 field_type += GenObjApiUnionTypeTS(imports, struct_def, parser.opts,
1447 *(field.value.type.enum_def));
1448
1449 field_val = GenUnionValTS(imports, struct_def, field_method,
1450 field.value.type);
1451 field_offset_decl =
1452 "builder.createObjectOffset(this." + field_field + ")";
1453 break;
1454 }
1455
1456 default: FLATBUFFERS_ASSERT(0); break;
1457 }
1458
1459 // length 0 vector is simply empty instead of null
1460 field_type += is_vector ? "" : "|null";
1461 }
1462
1463 if (!field_offset_decl.empty()) {
1464 field_offset_decl =
1465 " const " + field_field + " = " + field_offset_decl + ";";
1466 }
1467 if (field_offset_val.empty()) { field_offset_val = field_field; }
1468
1469 unpack_func += " " + field_val;
1470 unpack_to_func += " _o." + field_field + " = " + field_val + ";";
1471
1472 // FIXME: if field_type and field_field are identical, then
1473 // this generates invalid typescript.
1474 constructor_func += " public " + field_field + ": " + field_type +
1475 " = " + field_default_val;
1476
1477 if (!struct_def.fixed) {
1478 if (!field_offset_decl.empty()) {
1479 pack_func_offset_decl += field_offset_decl + "\n";
1480 }
1481
1482 if (has_create) {
1483 pack_func_create_call += field_offset_val;
1484 } else {
1485 if (field.IsScalarOptional()) {
1486 pack_func_create_call +=
1487 " if (" + field_offset_val + " !== null)\n ";
1488 }
1489 pack_func_create_call += " " + struct_name + "." +
1490 namer_.Method("add", field) + "(builder, " +
1491 field_offset_val + ");\n";
1492 }
1493 }
1494
1495 if (std::next(it) != struct_def.fields.vec.end()) {
1496 constructor_func += ",\n";
1497
1498 if (!struct_def.fixed && has_create) {
1499 pack_func_create_call += ",\n ";
1500 }
1501
1502 unpack_func += ",\n";
1503 unpack_to_func += "\n";
1504 } else {
1505 constructor_func += "\n";
1506 if (!struct_def.fixed) {
1507 pack_func_offset_decl += (pack_func_offset_decl.empty() ? "" : "\n");
1508 pack_func_create_call += "\n ";
1509 }
1510
1511 unpack_func += "\n ";
1512 unpack_to_func += "\n";
1513 }
1514 }
1515
1516 constructor_func += "){}\n\n";
1517
1518 if (has_create) {
1519 pack_func_create_call += ");";
1520 } else {
1521 pack_func_create_call += "return " + struct_name + ".end" +
1522 GetPrefixedName(struct_def) + "(builder);";
1523 }
1524 obj_api_class = "\n";
1525 obj_api_class += "export class ";
1526 obj_api_class += GetTypeName(struct_def, /*object_api=*/true);
1527 obj_api_class += " implements flatbuffers.IGeneratedObject {\n";
1528 obj_api_class += constructor_func;
1529 obj_api_class += pack_func_prototype + pack_func_offset_decl +
1530 pack_func_create_call + "\n}";
1531
1532 obj_api_class += "\n}\n";
1533
1534 unpack_func += ");\n}";
1535 unpack_to_func += "}\n";
1536
1537 obj_api_unpack_func = unpack_func + "\n\n" + unpack_to_func;
1538 }
1539
CanCreateFactoryMethod(const StructDef & struct_def)1540 static bool CanCreateFactoryMethod(const StructDef &struct_def) {
1541 // to preserve backwards compatibility, we allow the first field to be a
1542 // struct
1543 return struct_def.fields.vec.size() < 2 ||
1544 std::all_of(std::begin(struct_def.fields.vec) + 1,
1545 std::end(struct_def.fields.vec),
1546 [](const FieldDef *f) -> bool {
1547 FLATBUFFERS_ASSERT(f != nullptr);
1548 return f->value.type.base_type != BASE_TYPE_STRUCT;
1549 });
1550 }
1551
1552 // Generate an accessor struct with constructor for a flatbuffers struct.
GenStruct(const Parser & parser,StructDef & struct_def,std::string * code_ptr,import_set & imports)1553 void GenStruct(const Parser &parser, StructDef &struct_def,
1554 std::string *code_ptr, import_set &imports) {
1555 if (struct_def.generated) return;
1556 std::string &code = *code_ptr;
1557
1558 // Special case for the root struct, since no one will necessarily reference
1559 // it, we have to explicitly add it to the import list.
1560 if (&struct_def == parser_.root_struct_def_) {
1561 AddImport(imports, struct_def, struct_def);
1562 }
1563
1564 const std::string object_name = GetTypeName(struct_def);
1565 const std::string object_api_name = GetTypeName(struct_def, true);
1566
1567 // Emit constructor
1568 GenDocComment(struct_def.doc_comment, code_ptr);
1569 code += "export class ";
1570 code += object_name;
1571 if (parser.opts.generate_object_based_api)
1572 code += " implements flatbuffers.IUnpackableObject<" + object_api_name +
1573 "> {\n";
1574 else
1575 code += " {\n";
1576 code += " bb: flatbuffers.ByteBuffer|null = null;\n";
1577 code += " bb_pos = 0;\n";
1578
1579 // Generate the __init method that sets the field in a pre-existing
1580 // accessor object. This is to allow object reuse.
1581 code +=
1582 " __init(i:number, bb:flatbuffers.ByteBuffer):" + object_name + " {\n";
1583 code += " this.bb_pos = i;\n";
1584 code += " this.bb = bb;\n";
1585 code += " return this;\n";
1586 code += "}\n\n";
1587
1588 // Generate special accessors for the table that when used as the root of a
1589 // FlatBuffer
1590 GenerateRootAccessor(struct_def, code_ptr, code, object_name, false);
1591 GenerateRootAccessor(struct_def, code_ptr, code, object_name, true);
1592
1593 // Generate the identifier check method
1594 if (!struct_def.fixed && parser_.root_struct_def_ == &struct_def &&
1595 !parser_.file_identifier_.empty()) {
1596 GenDocComment(code_ptr);
1597 code +=
1598 "static bufferHasIdentifier(bb:flatbuffers.ByteBuffer):boolean "
1599 "{\n";
1600 code += " return bb.__has_identifier('" + parser_.file_identifier_;
1601 code += "');\n}\n\n";
1602 }
1603
1604 // Emit field accessors
1605 for (auto it = struct_def.fields.vec.begin();
1606 it != struct_def.fields.vec.end(); ++it) {
1607 auto &field = **it;
1608 if (field.deprecated) continue;
1609 std::string offset_prefix = "";
1610
1611 if (field.value.type.base_type == BASE_TYPE_ARRAY) {
1612 offset_prefix = " return ";
1613 } else {
1614 offset_prefix = " const offset = " + GenBBAccess() +
1615 ".__offset(this.bb_pos, " +
1616 NumToString(field.value.offset) + ");\n";
1617 offset_prefix += " return offset ? ";
1618 }
1619
1620 // Emit a scalar field
1621 const auto is_string = IsString(field.value.type);
1622 if (IsScalar(field.value.type.base_type) || is_string) {
1623 const auto has_null_default = is_string || HasNullDefault(field);
1624
1625 GenDocComment(field.doc_comment, code_ptr);
1626 std::string prefix = namer_.Method(field) + "(";
1627 if (is_string) {
1628 code += prefix + "):string|null\n";
1629 code +=
1630 prefix + "optionalEncoding:flatbuffers.Encoding" + "):" +
1631 GenTypeName(imports, struct_def, field.value.type, false, true) +
1632 "\n";
1633 code += prefix + "optionalEncoding?:any";
1634 } else {
1635 code += prefix;
1636 }
1637 if (field.value.type.enum_def) {
1638 code += "):" +
1639 GenTypeName(imports, struct_def, field.value.type, false,
1640 field.IsOptional()) +
1641 " {\n";
1642 } else {
1643 code += "):" +
1644 GenTypeName(imports, struct_def, field.value.type, false,
1645 has_null_default) +
1646 " {\n";
1647 }
1648
1649 if (struct_def.fixed) {
1650 code +=
1651 " return " +
1652 GenGetter(field.value.type,
1653 "(this.bb_pos" + MaybeAdd(field.value.offset) + ")") +
1654 ";\n";
1655 } else {
1656 std::string index = "this.bb_pos + offset";
1657 if (is_string) { index += ", optionalEncoding"; }
1658 code +=
1659 offset_prefix + GenGetter(field.value.type, "(" + index + ")");
1660 if (field.value.type.base_type != BASE_TYPE_ARRAY) {
1661 code += " : " + GenDefaultValue(field, imports);
1662 }
1663 code += ";\n";
1664 }
1665 }
1666
1667 // Emit an object field
1668 else {
1669 switch (field.value.type.base_type) {
1670 case BASE_TYPE_STRUCT: {
1671 const auto type =
1672 AddImport(imports, struct_def, *field.value.type.struct_def)
1673 .name;
1674 GenDocComment(field.doc_comment, code_ptr);
1675 code += namer_.Method(field);
1676 code += "(obj?:" + type + "):" + type + "|null {\n";
1677
1678 if (struct_def.fixed) {
1679 code += " return (obj || " + GenerateNewExpression(type);
1680 code += ").__init(this.bb_pos";
1681 code +=
1682 MaybeAdd(field.value.offset) + ", " + GenBBAccess() + ");\n";
1683 } else {
1684 code += offset_prefix + "(obj || " + GenerateNewExpression(type) +
1685 ").__init(";
1686 code += field.value.type.struct_def->fixed
1687 ? "this.bb_pos + offset"
1688 : GenBBAccess() + ".__indirect(this.bb_pos + offset)";
1689 code += ", " + GenBBAccess() + ") : null;\n";
1690 }
1691
1692 break;
1693 }
1694
1695 case BASE_TYPE_ARRAY: {
1696 auto vectortype = field.value.type.VectorType();
1697 auto vectortypename =
1698 GenTypeName(imports, struct_def, vectortype, false);
1699 auto inline_size = InlineSize(vectortype);
1700 auto index = "this.bb_pos + " + NumToString(field.value.offset) +
1701 " + index" + MaybeScale(inline_size);
1702 std::string ret_type;
1703 bool is_union = false;
1704 switch (vectortype.base_type) {
1705 case BASE_TYPE_STRUCT: ret_type = vectortypename; break;
1706 case BASE_TYPE_STRING: ret_type = vectortypename; break;
1707 case BASE_TYPE_UNION:
1708 ret_type = "?flatbuffers.Table";
1709 is_union = true;
1710 break;
1711 default: ret_type = vectortypename;
1712 }
1713 GenDocComment(field.doc_comment, code_ptr);
1714 std::string prefix = namer_.Method(field);
1715 // TODO: make it work without any
1716 // if (is_union) { prefix += "<T extends flatbuffers.Table>"; }
1717 if (is_union) { prefix += ""; }
1718 prefix += "(index: number";
1719 if (is_union) {
1720 const auto union_type =
1721 GenUnionGenericTypeTS(*(field.value.type.enum_def));
1722
1723 vectortypename = union_type;
1724 code += prefix + ", obj:" + union_type;
1725 } else if (vectortype.base_type == BASE_TYPE_STRUCT) {
1726 code += prefix + ", obj?:" + vectortypename;
1727 } else if (IsString(vectortype)) {
1728 code += prefix + "):string\n";
1729 code += prefix + ",optionalEncoding:flatbuffers.Encoding" +
1730 "):" + vectortypename + "\n";
1731 code += prefix + ",optionalEncoding?:any";
1732 } else {
1733 code += prefix;
1734 }
1735 code += "):" + vectortypename + "|null {\n";
1736
1737 if (vectortype.base_type == BASE_TYPE_STRUCT) {
1738 code += offset_prefix + "(obj || " +
1739 GenerateNewExpression(vectortypename);
1740 code += ").__init(";
1741 code += vectortype.struct_def->fixed
1742 ? index
1743 : GenBBAccess() + ".__indirect(" + index + ")";
1744 code += ", " + GenBBAccess() + ")";
1745 } else {
1746 if (is_union) {
1747 index = "obj, " + index;
1748 } else if (IsString(vectortype)) {
1749 index += ", optionalEncoding";
1750 }
1751 code += offset_prefix + GenGetter(vectortype, "(" + index + ")");
1752 }
1753
1754 switch (field.value.type.base_type) {
1755 case BASE_TYPE_ARRAY: {
1756 break;
1757 }
1758 case BASE_TYPE_BOOL: {
1759 code += " : false";
1760 break;
1761 }
1762 case BASE_TYPE_LONG:
1763 case BASE_TYPE_ULONG: {
1764 code += " : BigInt(0)";
1765 break;
1766 }
1767 default: {
1768 if (IsScalar(field.value.type.element)) {
1769 if (field.value.type.enum_def) {
1770 code += field.value.constant;
1771 } else {
1772 code += " : 0";
1773 }
1774 } else {
1775 code += ": null";
1776 }
1777 break;
1778 }
1779 }
1780 code += ";\n";
1781 break;
1782 }
1783
1784 case BASE_TYPE_VECTOR: {
1785 auto vectortype = field.value.type.VectorType();
1786 auto vectortypename =
1787 GenTypeName(imports, struct_def, vectortype, false);
1788 auto type = GetUnderlyingVectorType(vectortype);
1789 auto inline_size = InlineSize(type);
1790 auto index = GenBBAccess() +
1791 ".__vector(this.bb_pos + offset) + index" +
1792 MaybeScale(inline_size);
1793 std::string ret_type;
1794 bool is_union = false;
1795 switch (vectortype.base_type) {
1796 case BASE_TYPE_STRUCT: ret_type = vectortypename; break;
1797 case BASE_TYPE_STRING: ret_type = vectortypename; break;
1798 case BASE_TYPE_UNION:
1799 ret_type = "?flatbuffers.Table";
1800 is_union = true;
1801 break;
1802 default: ret_type = vectortypename;
1803 }
1804 GenDocComment(field.doc_comment, code_ptr);
1805 std::string prefix = namer_.Method(field);
1806 // TODO: make it work without any
1807 // if (is_union) { prefix += "<T extends flatbuffers.Table>"; }
1808 if (is_union) { prefix += ""; }
1809 prefix += "(index: number";
1810 if (is_union) {
1811 const auto union_type =
1812 GenUnionGenericTypeTS(*(field.value.type.enum_def));
1813
1814 vectortypename = union_type;
1815 code += prefix + ", obj:" + union_type;
1816 } else if (vectortype.base_type == BASE_TYPE_STRUCT) {
1817 code += prefix + ", obj?:" + vectortypename;
1818 } else if (IsString(vectortype)) {
1819 code += prefix + "):string\n";
1820 code += prefix + ",optionalEncoding:flatbuffers.Encoding" +
1821 "):" + vectortypename + "\n";
1822 code += prefix + ",optionalEncoding?:any";
1823 } else {
1824 code += prefix;
1825 }
1826 code += "):" + vectortypename + "|null {\n";
1827
1828 if (vectortype.base_type == BASE_TYPE_STRUCT) {
1829 code += offset_prefix + "(obj || " +
1830 GenerateNewExpression(vectortypename);
1831 code += ").__init(";
1832 code += vectortype.struct_def->fixed
1833 ? index
1834 : GenBBAccess() + ".__indirect(" + index + ")";
1835 code += ", " + GenBBAccess() + ")";
1836 } else {
1837 if (is_union) {
1838 index = "obj, " + index;
1839 } else if (IsString(vectortype)) {
1840 index += ", optionalEncoding";
1841 }
1842 code += offset_prefix + GenGetter(vectortype, "(" + index + ")");
1843 }
1844 code += " : ";
1845 if (field.value.type.element == BASE_TYPE_BOOL) {
1846 code += "false";
1847 } else if (field.value.type.element == BASE_TYPE_LONG ||
1848 field.value.type.element == BASE_TYPE_ULONG) {
1849 code += "BigInt(0)";
1850 } else if (IsScalar(field.value.type.element)) {
1851 if (field.value.type.enum_def) {
1852 code += field.value.constant;
1853 } else {
1854 code += "0";
1855 }
1856 } else {
1857 code += "null";
1858 }
1859 code += ";\n";
1860 break;
1861 }
1862
1863 case BASE_TYPE_UNION: {
1864 GenDocComment(field.doc_comment, code_ptr);
1865 code += namer_.Method(field);
1866
1867 const auto &union_enum = *(field.value.type.enum_def);
1868 const auto union_type = GenUnionGenericTypeTS(union_enum);
1869 code += "<T extends flatbuffers.Table>(obj:" + union_type +
1870 "):" + union_type +
1871 "|null "
1872 "{\n";
1873
1874 code += offset_prefix +
1875 GenGetter(field.value.type, "(obj, this.bb_pos + offset)") +
1876 " : null;\n";
1877 break;
1878 }
1879 default: FLATBUFFERS_ASSERT(0);
1880 }
1881 }
1882 code += "}\n\n";
1883
1884 // Adds the mutable scalar value to the output
1885 if (IsScalar(field.value.type.base_type) && parser.opts.mutable_buffer &&
1886 !IsUnion(field.value.type)) {
1887 std::string type =
1888 GenTypeName(imports, struct_def, field.value.type, true);
1889
1890 code += namer_.LegacyTsMutateMethod(field) + "(value:" + type +
1891 "):boolean {\n";
1892
1893 const std::string write_method =
1894 "." + namer_.Method("write", GenType(field.value.type));
1895
1896 if (struct_def.fixed) {
1897 code += " " + GenBBAccess() + write_method + "(this.bb_pos + " +
1898 NumToString(field.value.offset) + ", ";
1899 } else {
1900 code += " const offset = " + GenBBAccess() +
1901 ".__offset(this.bb_pos, " + NumToString(field.value.offset) +
1902 ");\n\n";
1903 code += " if (offset === 0) {\n";
1904 code += " return false;\n";
1905 code += " }\n\n";
1906
1907 // special case for bools, which are treated as uint8
1908 code +=
1909 " " + GenBBAccess() + write_method + "(this.bb_pos + offset, ";
1910 if (field.value.type.base_type == BASE_TYPE_BOOL) { code += "+"; }
1911 }
1912
1913 code += "value);\n";
1914 code += " return true;\n";
1915 code += "}\n\n";
1916 }
1917
1918 // Emit vector helpers
1919 if (IsVector(field.value.type)) {
1920 // Emit a length helper
1921 GenDocComment(code_ptr);
1922 code += namer_.Method(field, "Length");
1923 code += "():number {\n" + offset_prefix;
1924
1925 code +=
1926 GenBBAccess() + ".__vector_len(this.bb_pos + offset) : 0;\n}\n\n";
1927
1928 // For scalar types, emit a typed array helper
1929 auto vectorType = field.value.type.VectorType();
1930 if (IsScalar(vectorType.base_type) && !IsLong(vectorType.base_type)) {
1931 GenDocComment(code_ptr);
1932
1933 code += namer_.Method(field, "Array");
1934 code +=
1935 "():" + GenType(vectorType) + "Array|null {\n" + offset_prefix;
1936
1937 code += "new " + GenType(vectorType) + "Array(" + GenBBAccess() +
1938 ".bytes().buffer, " + GenBBAccess() +
1939 ".bytes().byteOffset + " + GenBBAccess() +
1940 ".__vector(this.bb_pos + offset), " + GenBBAccess() +
1941 ".__vector_len(this.bb_pos + offset)) : null;\n}\n\n";
1942 }
1943 }
1944 }
1945
1946 // Emit the fully qualified name
1947 if (parser_.opts.generate_name_strings) {
1948 GenDocComment(code_ptr);
1949 code += "static getFullyQualifiedName():string {\n";
1950 code +=
1951 " return '" +
1952 struct_def.defined_namespace->GetFullyQualifiedName(struct_def.name) +
1953 "';\n";
1954 code += "}\n\n";
1955 }
1956
1957 // Emit the size of the struct.
1958 if (struct_def.fixed) {
1959 GenDocComment(code_ptr);
1960 code += "static sizeOf():number {\n";
1961 code += " return " + NumToString(struct_def.bytesize) + ";\n";
1962 code += "}\n\n";
1963 }
1964
1965 // Emit a factory constructor
1966 if (struct_def.fixed) {
1967 std::string arguments;
1968 GenStructArgs(imports, struct_def, &arguments, "");
1969 GenDocComment(code_ptr);
1970
1971 code += "static create" + GetPrefixedName(struct_def) +
1972 "(builder:flatbuffers.Builder";
1973 code += arguments + "):flatbuffers.Offset {\n";
1974
1975 GenStructBody(struct_def, &code, "");
1976 code += " return builder.offset();\n}\n\n";
1977 } else {
1978 // Generate a method to start building a new object
1979 GenDocComment(code_ptr);
1980
1981 code += "static start" + GetPrefixedName(struct_def) +
1982 "(builder:flatbuffers.Builder) {\n";
1983
1984 code += " builder.startObject(" +
1985 NumToString(struct_def.fields.vec.size()) + ");\n";
1986 code += "}\n\n";
1987
1988 // Generate a set of static methods that allow table construction
1989 for (auto it = struct_def.fields.vec.begin();
1990 it != struct_def.fields.vec.end(); ++it) {
1991 auto &field = **it;
1992 if (field.deprecated) continue;
1993 const auto argname = GetArgName(field);
1994
1995 // Generate the field insertion method
1996 GenDocComment(code_ptr);
1997 code += "static " + namer_.Method("add", field);
1998 code += "(builder:flatbuffers.Builder, " + argname + ":" +
1999 GetArgType(imports, struct_def, field, false) + ") {\n";
2000 code += " builder.addField" + GenWriteMethod(field.value.type) + "(";
2001 code += NumToString(it - struct_def.fields.vec.begin()) + ", ";
2002 if (field.value.type.base_type == BASE_TYPE_BOOL) { code += "+"; }
2003 code += argname + ", ";
2004 if (!IsScalar(field.value.type.base_type)) {
2005 code += "0";
2006 } else if (HasNullDefault(field)) {
2007 code += "null";
2008 } else {
2009 if (field.value.type.base_type == BASE_TYPE_BOOL) { code += "+"; }
2010 code += GenDefaultValue(field, imports);
2011 }
2012 code += ");\n}\n\n";
2013
2014 if (IsVector(field.value.type)) {
2015 auto vector_type = field.value.type.VectorType();
2016 auto type = GetUnderlyingVectorType(vector_type);
2017 auto alignment = InlineAlignment(type);
2018 auto elem_size = InlineSize(type);
2019
2020 // Generate a method to create a vector from a JavaScript array
2021 if (!IsStruct(vector_type)) {
2022 GenDocComment(code_ptr);
2023
2024 const std::string sig_begin =
2025 "static " + namer_.Method("create", field, "Vector") +
2026 "(builder:flatbuffers.Builder, data:";
2027 const std::string sig_end = "):flatbuffers.Offset";
2028 std::string type =
2029 GenTypeName(imports, struct_def, vector_type, true) + "[]";
2030 if (type == "number[]") {
2031 const auto &array_type = GenType(vector_type);
2032 // the old type should be deprecated in the future
2033 std::string type_old = "number[]|Uint8Array";
2034 std::string type_new = "number[]|" + array_type + "Array";
2035 if (type_old == type_new) {
2036 type = type_new;
2037 } else {
2038 // add function overloads
2039 code += sig_begin + type_new + sig_end + ";\n";
2040 code +=
2041 "/**\n * @deprecated This Uint8Array overload will "
2042 "be removed in the future.\n */\n";
2043 code += sig_begin + type_old + sig_end + ";\n";
2044 type = type_new + "|Uint8Array";
2045 }
2046 }
2047 code += sig_begin + type + sig_end + " {\n";
2048 code += " builder.startVector(" + NumToString(elem_size);
2049 code += ", data.length, " + NumToString(alignment) + ");\n";
2050 code += " for (let i = data.length - 1; i >= 0; i--) {\n";
2051 code += " builder.add" + GenWriteMethod(vector_type) + "(";
2052 if (vector_type.base_type == BASE_TYPE_BOOL) { code += "+"; }
2053 code += "data[i]!);\n";
2054 code += " }\n";
2055 code += " return builder.endVector();\n";
2056 code += "}\n\n";
2057 }
2058
2059 // Generate a method to start a vector, data to be added manually
2060 // after
2061 GenDocComment(code_ptr);
2062
2063 code += "static ";
2064 code += namer_.Method("start", field, "Vector");
2065 code += "(builder:flatbuffers.Builder, numElems:number) {\n";
2066 code += " builder.startVector(" + NumToString(elem_size);
2067 code += ", numElems, " + NumToString(alignment) + ");\n";
2068 code += "}\n\n";
2069 }
2070 }
2071
2072 // Generate a method to stop building a new object
2073 GenDocComment(code_ptr);
2074
2075 code += "static end" + GetPrefixedName(struct_def);
2076 code += "(builder:flatbuffers.Builder):flatbuffers.Offset {\n";
2077
2078 code += " const offset = builder.endObject();\n";
2079 for (auto it = struct_def.fields.vec.begin();
2080 it != struct_def.fields.vec.end(); ++it) {
2081 auto &field = **it;
2082 if (!field.deprecated && field.IsRequired()) {
2083 code += " builder.requiredField(offset, ";
2084 code += NumToString(field.value.offset);
2085 code += ") // " + field.name + "\n";
2086 }
2087 }
2088 code += " return offset;\n";
2089 code += "}\n\n";
2090
2091 // Generate the methods to complete buffer construction
2092 GenerateFinisher(struct_def, code_ptr, code, false);
2093 GenerateFinisher(struct_def, code_ptr, code, true);
2094
2095 // Generate a convenient CreateX function
2096 if (CanCreateFactoryMethod(struct_def)) {
2097 code += "static create" + GetPrefixedName(struct_def);
2098 code += "(builder:flatbuffers.Builder";
2099 for (auto it = struct_def.fields.vec.begin();
2100 it != struct_def.fields.vec.end(); ++it) {
2101 const auto &field = **it;
2102 if (field.deprecated) continue;
2103 code += ", " + GetArgName(field) + ":" +
2104 GetArgType(imports, struct_def, field, true);
2105 }
2106
2107 code += "):flatbuffers.Offset {\n";
2108 code += " " + object_name + ".start" + GetPrefixedName(struct_def) +
2109 "(builder);\n";
2110
2111 std::string methodPrefix = object_name;
2112 for (auto it = struct_def.fields.vec.begin();
2113 it != struct_def.fields.vec.end(); ++it) {
2114 const auto &field = **it;
2115 if (field.deprecated) continue;
2116
2117 const auto arg_name = GetArgName(field);
2118
2119 if (field.IsScalarOptional()) {
2120 code += " if (" + arg_name + " !== null)\n ";
2121 }
2122
2123 code += " " + methodPrefix + "." + namer_.Method("add", field) + "(";
2124 code += "builder, " + arg_name + ");\n";
2125 }
2126
2127 code += " return " + methodPrefix + ".end" +
2128 GetPrefixedName(struct_def) + "(builder);\n";
2129 code += "}\n";
2130 }
2131 }
2132
2133 if (!struct_def.fixed && parser_.services_.vec.size() != 0) {
2134 auto name = GetPrefixedName(struct_def, "");
2135 code += "\n";
2136 code += "serialize():Uint8Array {\n";
2137 code += " return this.bb!.bytes();\n";
2138 code += "}\n";
2139
2140 code += "\n";
2141 code += "static deserialize(buffer: Uint8Array):" +
2142 namer_.EscapeKeyword(name) + " {\n";
2143 code += " return " + AddImport(imports, struct_def, struct_def).name +
2144 ".getRootAs" + name + "(new flatbuffers.ByteBuffer(buffer))\n";
2145 code += "}\n";
2146 }
2147
2148 if (parser_.opts.generate_object_based_api) {
2149 std::string obj_api_class;
2150 std::string obj_api_unpack_func;
2151 GenObjApi(parser_, struct_def, obj_api_unpack_func, obj_api_class,
2152 imports);
2153
2154 code += obj_api_unpack_func + "}\n" + obj_api_class;
2155 } else {
2156 code += "}\n";
2157 }
2158 }
2159
HasNullDefault(const FieldDef & field)2160 static bool HasNullDefault(const FieldDef &field) {
2161 return field.IsOptional() && field.value.constant == "null";
2162 }
2163
GetArgType(import_set & imports,const Definition & owner,const FieldDef & field,bool allowNull)2164 std::string GetArgType(import_set &imports, const Definition &owner,
2165 const FieldDef &field, bool allowNull) {
2166 return GenTypeName(imports, owner, field.value.type, true,
2167 allowNull && field.IsOptional());
2168 }
2169
GetArgName(const FieldDef & field)2170 std::string GetArgName(const FieldDef &field) {
2171 auto argname = namer_.Variable(field);
2172 if (!IsScalar(field.value.type.base_type)) { argname += "Offset"; }
2173 return argname;
2174 }
2175
GetPrefixedName(const StructDef & struct_def,const char * prefix="")2176 std::string GetPrefixedName(const StructDef &struct_def,
2177 const char *prefix = "") {
2178 return prefix + struct_def.name;
2179 }
2180 }; // namespace ts
2181 } // namespace ts
2182
GenerateTS(const Parser & parser,const std::string & path,const std::string & file_name)2183 static bool GenerateTS(const Parser &parser, const std::string &path,
2184 const std::string &file_name) {
2185 ts::TsGenerator generator(parser, path, file_name);
2186 return generator.generate();
2187 }
2188
TSMakeRule(const Parser & parser,const std::string & path,const std::string & file_name)2189 static std::string TSMakeRule(const Parser &parser, const std::string &path,
2190 const std::string &file_name) {
2191 std::string filebase =
2192 flatbuffers::StripPath(flatbuffers::StripExtension(file_name));
2193 ts::TsGenerator generator(parser, path, file_name);
2194 std::string make_rule =
2195 generator.GeneratedFileName(path, filebase, parser.opts) + ": ";
2196
2197 auto included_files = parser.GetIncludedFilesRecursive(file_name);
2198 for (auto it = included_files.begin(); it != included_files.end(); ++it) {
2199 make_rule += " " + *it;
2200 }
2201 return make_rule;
2202 }
2203
2204 namespace {
2205
2206 class TsCodeGenerator : public CodeGenerator {
2207 public:
GenerateCode(const Parser & parser,const std::string & path,const std::string & filename)2208 Status GenerateCode(const Parser &parser, const std::string &path,
2209 const std::string &filename) override {
2210 if (!GenerateTS(parser, path, filename)) { return Status::ERROR; }
2211 return Status::OK;
2212 }
2213
GenerateCode(const uint8_t *,int64_t,const CodeGenOptions &)2214 Status GenerateCode(const uint8_t *, int64_t,
2215 const CodeGenOptions &) override {
2216 return Status::NOT_IMPLEMENTED;
2217 }
2218
GenerateMakeRule(const Parser & parser,const std::string & path,const std::string & filename,std::string & output)2219 Status GenerateMakeRule(const Parser &parser, const std::string &path,
2220 const std::string &filename,
2221 std::string &output) override {
2222 output = TSMakeRule(parser, path, filename);
2223 return Status::OK;
2224 }
2225
GenerateGrpcCode(const Parser & parser,const std::string & path,const std::string & filename)2226 Status GenerateGrpcCode(const Parser &parser, const std::string &path,
2227 const std::string &filename) override {
2228 if (!GenerateTSGRPC(parser, path, filename)) { return Status::ERROR; }
2229 return Status::OK;
2230 }
2231
GenerateRootFile(const Parser & parser,const std::string & path)2232 Status GenerateRootFile(const Parser &parser,
2233 const std::string &path) override {
2234 (void)parser;
2235 (void)path;
2236 return Status::NOT_IMPLEMENTED;
2237 }
IsSchemaOnly() const2238 bool IsSchemaOnly() const override { return true; }
2239
SupportsBfbsGeneration() const2240 bool SupportsBfbsGeneration() const override { return false; }
SupportsRootFileGeneration() const2241 bool SupportsRootFileGeneration() const override { return false; }
2242
Language() const2243 IDLOptions::Language Language() const override { return IDLOptions::kTs; }
2244
LanguageName() const2245 std::string LanguageName() const override { return "TS"; }
2246 };
2247 } // namespace
2248
NewTsCodeGenerator()2249 std::unique_ptr<CodeGenerator> NewTsCodeGenerator() {
2250 return std::unique_ptr<TsCodeGenerator>(new TsCodeGenerator());
2251 }
2252
2253 } // namespace flatbuffers
2254