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 // independent from idl_parser, since this code is not needed for most clients
18 #include <algorithm>
19 #include <cassert>
20 #include <unordered_map>
21 #include <unordered_set>
22 
23 #include "flatbuffers/code_generators.h"
24 #include "flatbuffers/flatbuffers.h"
25 #include "flatbuffers/idl.h"
26 #include "flatbuffers/util.h"
27 
28 namespace flatbuffers {
29 
30 struct ImportDefinition {
31   std::string name;
32   std::string statement;
33   const Definition *dependent;
34   const Definition *dependency;
35 };
36 
37 enum AnnotationType { kParam = 0, kType = 1, kReturns = 2 };
38 
39 namespace ts {
40 // Iterate through all definitions we haven't generate code for (enums, structs,
41 // and tables) and output them to a single file.
42 class TsGenerator : public BaseGenerator {
43  public:
44   typedef std::map<std::string, ImportDefinition> import_set;
45 
TsGenerator(const Parser & parser,const std::string & path,const std::string & file_name)46   TsGenerator(const Parser &parser, const std::string &path,
47               const std::string &file_name)
48       : BaseGenerator(parser, path, file_name, "", ".", "ts") {}
generate()49   bool generate() {
50     generateEnums();
51     generateStructs();
52     return true;
53   }
54 
55   // Save out the generated code for a single class while adding
56   // declaration boilerplate.
SaveType(const Definition & definition,const std::string & classcode,import_set & imports,import_set & bare_imports) const57   bool SaveType(const Definition &definition, const std::string &classcode,
58                 import_set &imports, import_set &bare_imports) const {
59     if (!classcode.length()) return true;
60 
61     std::string code =
62         "// " + std::string(FlatBuffersGeneratedWarning()) + "\n\n";
63 
64     for (auto it = bare_imports.begin(); it != bare_imports.end(); it++)
65       code += it->second.statement + "\n";
66     if (!bare_imports.empty()) code += "\n";
67 
68     for (auto it = imports.begin(); it != imports.end(); it++)
69       if (it->second.dependency != &definition)  // do not import itself
70         code += it->second.statement + "\n";
71     if (!imports.empty()) code += "\n\n";
72 
73     code += classcode;
74     auto filename = NamespaceDir(*definition.defined_namespace, true) +
75                     ToDasherizedCase(definition.name) + ".ts";
76     return SaveFile(filename.c_str(), code, false);
77   }
78 
79  private:
80   // Generate code for all enums.
generateEnums()81   void generateEnums() {
82     for (auto it = parser_.enums_.vec.begin(); it != parser_.enums_.vec.end();
83          ++it) {
84       import_set bare_imports;
85       import_set imports;
86       std::string enumcode;
87       auto &enum_def = **it;
88       GenEnum(enum_def, &enumcode, imports, false);
89       GenEnum(enum_def, &enumcode, imports, true);
90       SaveType(enum_def, enumcode, imports, bare_imports);
91     }
92   }
93 
94   // Generate code for all structs.
generateStructs()95   void generateStructs() {
96     for (auto it = parser_.structs_.vec.begin();
97          it != parser_.structs_.vec.end(); ++it) {
98       import_set bare_imports;
99       import_set imports;
100       AddImport(bare_imports, "* as flatbuffers", "flatbuffers");
101       auto &struct_def = **it;
102       std::string declcode;
103       GenStruct(parser_, struct_def, &declcode, imports);
104       SaveType(struct_def, declcode, imports, bare_imports);
105     }
106   }
107 
108   // Generate a documentation comment, if available.
GenDocComment(const std::vector<std::string> & dc,std::string * code_ptr,const char * indent=nullptr)109   static void GenDocComment(const std::vector<std::string> &dc,
110                             std::string *code_ptr,
111                             const char *indent = nullptr) {
112     if (dc.empty()) {
113       // Don't output empty comment blocks with 0 lines of comment content.
114       return;
115     }
116 
117     std::string &code = *code_ptr;
118     if (indent) code += indent;
119     code += "/**\n";
120     for (auto it = dc.begin(); it != dc.end(); ++it) {
121       if (indent) code += indent;
122       code += " *" + *it + "\n";
123     }
124     if (indent) code += indent;
125     code += " */\n";
126   }
127 
GenDocComment(std::string * code_ptr)128   static void GenDocComment(std::string *code_ptr) {
129     GenDocComment(std::vector<std::string>(), code_ptr);
130   }
131 
132   // Generate an enum declaration and an enum string lookup table.
GenEnum(EnumDef & enum_def,std::string * code_ptr,import_set & imports,bool reverse)133   void GenEnum(EnumDef &enum_def, std::string *code_ptr, import_set &imports,
134                bool reverse) {
135     if (enum_def.generated) return;
136     if (reverse) return;  // FIXME.
137     std::string &code = *code_ptr;
138     GenDocComment(enum_def.doc_comment, code_ptr);
139     std::string ns = GetNameSpace(enum_def);
140     std::string enum_def_name = enum_def.name + (reverse ? "Name" : "");
141     code += "export enum " + enum_def.name + "{\n";
142     for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end(); ++it) {
143       auto &ev = **it;
144       if (!ev.doc_comment.empty()) {
145         if (it != enum_def.Vals().begin()) { code += '\n'; }
146         GenDocComment(ev.doc_comment, code_ptr, "  ");
147       }
148 
149       // Generate mapping between EnumName: EnumValue(int)
150       if (reverse) {
151         code += "  '" + enum_def.ToString(ev) + "'";
152         code += " = ";
153         code += "'" + ev.name + "'";
154       } else {
155         code += "  " + ev.name;
156         code += " = ";
157         code += enum_def.ToString(ev);
158       }
159 
160       code += (it + 1) != enum_def.Vals().end() ? ",\n" : "\n";
161     }
162     code += "}";
163 
164     if (enum_def.is_union) {
165       code += GenUnionConvFunc(enum_def.underlying_type, imports);
166     }
167 
168     code += "\n\n";
169   }
170 
GenType(const Type & type)171   static std::string GenType(const Type &type) {
172     switch (type.base_type) {
173       case BASE_TYPE_BOOL:
174       case BASE_TYPE_CHAR: return "Int8";
175       case BASE_TYPE_UTYPE:
176       case BASE_TYPE_UCHAR: return "Uint8";
177       case BASE_TYPE_SHORT: return "Int16";
178       case BASE_TYPE_USHORT: return "Uint16";
179       case BASE_TYPE_INT: return "Int32";
180       case BASE_TYPE_UINT: return "Uint32";
181       case BASE_TYPE_LONG: return "Int64";
182       case BASE_TYPE_ULONG: return "Uint64";
183       case BASE_TYPE_FLOAT: return "Float32";
184       case BASE_TYPE_DOUBLE: return "Float64";
185       case BASE_TYPE_STRING: return "String";
186       case BASE_TYPE_VECTOR: return GenType(type.VectorType());
187       case BASE_TYPE_STRUCT: return type.struct_def->name;
188       default: return "flatbuffers.Table";
189     }
190   }
191 
GenGetter(const Type & type,const std::string & arguments)192   std::string GenGetter(const Type &type, const std::string &arguments) {
193     switch (type.base_type) {
194       case BASE_TYPE_STRING: return GenBBAccess() + ".__string" + arguments;
195       case BASE_TYPE_STRUCT: return GenBBAccess() + ".__struct" + arguments;
196       case BASE_TYPE_UNION:
197         if (!UnionHasStringType(*type.enum_def)) {
198           return GenBBAccess() + ".__union" + arguments;
199         }
200         return GenBBAccess() + ".__union_with_string" + arguments;
201       case BASE_TYPE_VECTOR: return GenGetter(type.VectorType(), arguments);
202       default: {
203         auto getter =
204             GenBBAccess() + ".read" + MakeCamel(GenType(type)) + arguments;
205         if (type.base_type == BASE_TYPE_BOOL) { getter = "!!" + getter; }
206         return getter;
207       }
208     }
209   }
210 
GenBBAccess() const211   std::string GenBBAccess() const { return "this.bb!"; }
212 
GenDefaultValue(const FieldDef & field,const std::string & context,import_set & imports)213   std::string GenDefaultValue(const FieldDef &field, const std::string &context,
214                               import_set &imports) {
215     if (field.IsScalarOptional()) { return "null"; }
216 
217     const auto &value = field.value;
218     if (value.type.enum_def && value.type.base_type != BASE_TYPE_UNION &&
219         value.type.base_type != BASE_TYPE_VECTOR) {
220       if (auto val = value.type.enum_def->FindByValue(value.constant)) {
221         return AddImport(imports, *value.type.enum_def, *value.type.enum_def) +
222                "." + val->name;
223       } else {
224         return value.constant;
225       }
226     }
227 
228     switch (value.type.base_type) {
229       case BASE_TYPE_BOOL: return value.constant == "0" ? "false" : "true";
230 
231       case BASE_TYPE_STRING:
232       case BASE_TYPE_UNION:
233       case BASE_TYPE_STRUCT: {
234         return "null";
235       }
236 
237       case BASE_TYPE_VECTOR: return "[]";
238 
239       case BASE_TYPE_LONG:
240       case BASE_TYPE_ULONG: {
241         int64_t constant = StringToInt(value.constant.c_str());
242         std::string createLong = context + ".createLong";
243         return createLong + "(" + NumToString(static_cast<int32_t>(constant)) +
244                ", " + NumToString(static_cast<int32_t>(constant >> 32)) + ")";
245       }
246 
247       default: return value.constant;
248     }
249   }
250 
GenTypeName(import_set & imports,const Definition & owner,const Type & type,bool input,bool allowNull=false)251   std::string GenTypeName(import_set &imports, const Definition &owner,
252                           const Type &type, bool input,
253                           bool allowNull = false) {
254     if (!input) {
255       if (IsString(type) || type.base_type == BASE_TYPE_STRUCT) {
256         std::string name;
257         if (IsString(type)) {
258           name = "string|Uint8Array";
259         } else {
260           name = AddImport(imports, owner, *type.struct_def);
261         }
262         return allowNull ? (name + "|null") : name;
263       }
264     }
265 
266     switch (type.base_type) {
267       case BASE_TYPE_BOOL: return allowNull ? "boolean|null" : "boolean";
268       case BASE_TYPE_LONG:
269       case BASE_TYPE_ULONG:
270         return allowNull ? "flatbuffers.Long|null" : "flatbuffers.Long";
271       default:
272         if (IsScalar(type.base_type)) {
273           if (type.enum_def) {
274             const auto enum_name = AddImport(imports, owner, *type.enum_def);
275             return allowNull ? (enum_name + "|null") : enum_name;
276           }
277           return allowNull ? "number|null" : "number";
278         }
279         return "flatbuffers.Offset";
280     }
281   }
282 
283   // Returns the method name for use with add/put calls.
GenWriteMethod(const Type & type)284   static std::string GenWriteMethod(const Type &type) {
285     // Forward to signed versions since unsigned versions don't exist
286     switch (type.base_type) {
287       case BASE_TYPE_UTYPE:
288       case BASE_TYPE_UCHAR: return GenWriteMethod(Type(BASE_TYPE_CHAR));
289       case BASE_TYPE_USHORT: return GenWriteMethod(Type(BASE_TYPE_SHORT));
290       case BASE_TYPE_UINT: return GenWriteMethod(Type(BASE_TYPE_INT));
291       case BASE_TYPE_ULONG: return GenWriteMethod(Type(BASE_TYPE_LONG));
292       default: break;
293     }
294 
295     return IsScalar(type.base_type) ? MakeCamel(GenType(type))
296                                     : (IsStruct(type) ? "Struct" : "Offset");
297   }
298 
MaybeAdd(T value)299   template<typename T> static std::string MaybeAdd(T value) {
300     return value != 0 ? " + " + NumToString(value) : "";
301   }
302 
MaybeScale(T value)303   template<typename T> static std::string MaybeScale(T value) {
304     return value != 1 ? " * " + NumToString(value) : "";
305   }
306 
GenStructArgs(import_set & imports,const StructDef & struct_def,std::string * arguments,const std::string & nameprefix)307   void GenStructArgs(import_set &imports, const StructDef &struct_def,
308                      std::string *arguments, const std::string &nameprefix) {
309     for (auto it = struct_def.fields.vec.begin();
310          it != struct_def.fields.vec.end(); ++it) {
311       auto &field = **it;
312       if (IsStruct(field.value.type)) {
313         // Generate arguments for a struct inside a struct. To ensure names
314         // don't clash, and to make it obvious these arguments are constructing
315         // a nested struct, prefix the name with the field name.
316         GenStructArgs(imports, *field.value.type.struct_def, arguments,
317                       nameprefix + field.name + "_");
318       } else {
319         *arguments +=
320             ", " + nameprefix + field.name + ": " +
321             GenTypeName(imports, field, field.value.type, true, field.IsOptional());
322       }
323     }
324   }
325 
GenStructBody(const StructDef & struct_def,std::string * body,const std::string & nameprefix)326   static void GenStructBody(const StructDef &struct_def, std::string *body,
327                             const std::string &nameprefix) {
328     *body += "  builder.prep(";
329     *body += NumToString(struct_def.minalign) + ", ";
330     *body += NumToString(struct_def.bytesize) + ");\n";
331 
332     for (auto it = struct_def.fields.vec.rbegin();
333          it != struct_def.fields.vec.rend(); ++it) {
334       auto &field = **it;
335       if (field.padding) {
336         *body += "  builder.pad(" + NumToString(field.padding) + ");\n";
337       }
338       if (IsStruct(field.value.type)) {
339         // Generate arguments for a struct inside a struct. To ensure names
340         // don't clash, and to make it obvious these arguments are constructing
341         // a nested struct, prefix the name with the field name.
342         GenStructBody(*field.value.type.struct_def, body,
343                       nameprefix + field.name + "_");
344       } else {
345         *body += "  builder.write" + GenWriteMethod(field.value.type) + "(";
346         if (field.value.type.base_type == BASE_TYPE_BOOL) { *body += "+"; }
347         *body += nameprefix + field.name + ");\n";
348       }
349     }
350   }
351 
GenerateNewExpression(const std::string & object_name)352   std::string GenerateNewExpression(const std::string &object_name) {
353     return "new " + object_name + "()";
354   }
355 
GenerateRootAccessor(StructDef & struct_def,std::string * code_ptr,std::string & code,const std::string & object_name,bool size_prefixed)356   void GenerateRootAccessor(StructDef &struct_def, std::string *code_ptr,
357                             std::string &code, const std::string &object_name,
358                             bool size_prefixed) {
359     if (!struct_def.fixed) {
360       GenDocComment(code_ptr);
361       std::string sizePrefixed("SizePrefixed");
362       code += "static get" + (size_prefixed ? sizePrefixed : "") + "Root" +
363               GetPrefixedName(struct_def, "As");
364       code += "(bb:flatbuffers.ByteBuffer, obj?:" + object_name +
365               "):" + object_name + " {\n";
366       if (size_prefixed) {
367         code +=
368             "  bb.setPosition(bb.position() + "
369             "flatbuffers.SIZE_PREFIX_LENGTH);\n";
370       }
371       code += "  return (obj || " + GenerateNewExpression(object_name);
372       code += ").__init(bb.readInt32(bb.position()) + bb.position(), bb);\n";
373       code += "}\n\n";
374     }
375   }
376 
GenerateFinisher(StructDef & struct_def,std::string * code_ptr,std::string & code,bool size_prefixed)377   void GenerateFinisher(StructDef &struct_def, std::string *code_ptr,
378                         std::string &code, bool size_prefixed) {
379     if (parser_.root_struct_def_ == &struct_def) {
380       std::string sizePrefixed("SizePrefixed");
381       GenDocComment(code_ptr);
382 
383       code += "static finish" + (size_prefixed ? sizePrefixed : "") +
384               GetPrefixedName(struct_def) + "Buffer";
385       code += "(builder:flatbuffers.Builder, offset:flatbuffers.Offset) {\n";
386       code += "  builder.finish(offset";
387       if (!parser_.file_identifier_.empty()) {
388         code += ", '" + parser_.file_identifier_ + "'";
389       }
390       if (size_prefixed) {
391         if (parser_.file_identifier_.empty()) { code += ", undefined"; }
392         code += ", true";
393       }
394       code += ");\n";
395       code += "}\n\n";
396     }
397   }
398 
GetObjApiClassName(const StructDef & sd,const IDLOptions & opts)399   static std::string GetObjApiClassName(const StructDef &sd,
400                                         const IDLOptions &opts) {
401     return GetObjApiClassName(sd.name, opts);
402   }
403 
GetObjApiClassName(const std::string & name,const IDLOptions & opts)404   static std::string GetObjApiClassName(const std::string &name,
405                                         const IDLOptions &opts) {
406     return opts.object_prefix + name + opts.object_suffix;
407   }
408 
UnionHasStringType(const EnumDef & union_enum)409   bool UnionHasStringType(const EnumDef &union_enum) {
410     return std::any_of(union_enum.Vals().begin(), union_enum.Vals().end(),
411                        [](const EnumVal *ev) {
412                          return !ev->IsZero() && IsString(ev->union_type);
413                        });
414   }
415 
GenUnionGenericTypeTS(const EnumDef & union_enum)416   std::string GenUnionGenericTypeTS(const EnumDef &union_enum) {
417     // TODO: make it work without any
418     // return std::string("T") + (UnionHasStringType(union_enum) ? "|string" :
419     // "");
420     return std::string("any") +
421            (UnionHasStringType(union_enum) ? "|string" : "");
422   }
423 
GenUnionTypeTS(const EnumDef & union_enum,import_set & imports)424   std::string GenUnionTypeTS(const EnumDef &union_enum, import_set &imports) {
425     std::string ret;
426     std::set<std::string> type_list;
427 
428     for (auto it = union_enum.Vals().begin(); it != union_enum.Vals().end();
429          ++it) {
430       const auto &ev = **it;
431       if (ev.IsZero()) { continue; }
432 
433       std::string type = "";
434       if (IsString(ev.union_type)) {
435         type = "string";  // no need to wrap string type in namespace
436       } else if (ev.union_type.base_type == BASE_TYPE_STRUCT) {
437         type = AddImport(imports, union_enum, *ev.union_type.struct_def);
438       } else {
439         FLATBUFFERS_ASSERT(false);
440       }
441       type_list.insert(type);
442     }
443 
444     for (auto it = type_list.begin(); it != type_list.end(); ++it) {
445       ret += *it + ((std::next(it) == type_list.end()) ? "" : "|");
446     }
447 
448     return ret;
449   }
450 
AddImport(import_set & imports,const Definition & dependent,const StructDef & dependency)451   std::string AddImport(import_set &imports, const Definition &dependent,
452                         const StructDef &dependency) {
453     std::string ns;
454     const auto &depc_comps = dependency.defined_namespace->components;
455     for (auto it = depc_comps.begin(); it != depc_comps.end(); it++) ns += *it;
456     std::string unique_name = ns + dependency.name;
457     std::string import_name = dependency.name;
458     std::string long_import_name;
459     if (imports.find(unique_name) != imports.end())
460       return imports.find(unique_name)->second.name;
461     for (auto it = imports.begin(); it != imports.end(); it++) {
462       if (it->second.name == import_name) {
463         long_import_name = ns + import_name;
464         break;
465       }
466     }
467     std::string import_statement;
468     import_statement += "import { ";
469     if (long_import_name.empty()) {
470       import_statement += import_name;
471       if (parser_.opts.generate_object_based_api)
472         import_statement += ", " + import_name + "T";
473     } else {
474       import_statement += dependency.name + " as " + long_import_name;
475       if (parser_.opts.generate_object_based_api)
476         import_statement +=
477             ", " + dependency.name + "T as " + long_import_name + "T";
478     }
479     import_statement += " } from '";
480     std::string file_name;
481     const auto &dep_comps = dependent.defined_namespace->components;
482     for (size_t i = 0; i < dep_comps.size(); i++)
483       file_name += i == 0 ? ".." : (kPathSeparator + std::string(".."));
484     if (dep_comps.size() == 0) file_name += ".";
485     for (auto it = depc_comps.begin(); it != depc_comps.end(); it++)
486       file_name += kPathSeparator + ToDasherizedCase(*it);
487     file_name += kPathSeparator + ToDasherizedCase(dependency.name);
488     import_statement += file_name + "';";
489     ImportDefinition import;
490     import.name = long_import_name.empty() ? import_name : long_import_name;
491     import.statement = import_statement;
492     import.dependency = &dependency;
493     import.dependent = &dependent;
494     imports.insert(std::make_pair(unique_name, import));
495     return import.name;
496   }
497 
498   // TODO: largely (but not identical) duplicated code from above couln't find a
499   // good way to refactor
AddImport(import_set & imports,const Definition & dependent,const EnumDef & dependency)500   std::string AddImport(import_set &imports, const Definition &dependent,
501                         const EnumDef &dependency) {
502     std::string ns;
503     const auto &depc_comps = dependency.defined_namespace->components;
504     for (auto it = depc_comps.begin(); it != depc_comps.end(); it++) ns += *it;
505     std::string unique_name = ns + dependency.name;
506     std::string import_name = dependency.name;
507     std::string long_import_name;
508     if (imports.find(unique_name) != imports.end())
509       return imports.find(unique_name)->second.name;
510     for (auto it = imports.begin(); it != imports.end(); it++) {
511       if (it->second.name == import_name) {
512         long_import_name = ns + import_name;
513         break;
514       }
515     }
516     std::string import_statement;
517     import_statement += "import { ";
518     if (long_import_name.empty())
519       import_statement += import_name;
520     else
521       import_statement += dependency.name + " as " + long_import_name;
522     if (dependency.is_union) {
523       import_statement += ", unionTo" + import_name;
524       import_statement += ", unionListTo" + import_name;
525     }
526     import_statement += " } from '";
527     std::string file_name;
528     const auto &dep_comps = dependent.defined_namespace->components;
529     for (size_t i = 0; i < dep_comps.size(); i++)
530       file_name += i == 0 ? ".." : (kPathSeparator + std::string(".."));
531     if (dep_comps.size() == 0) file_name += ".";
532     for (auto it = depc_comps.begin(); it != depc_comps.end(); it++)
533       file_name += kPathSeparator + ToDasherizedCase(*it);
534     file_name += kPathSeparator + ToDasherizedCase(dependency.name);
535     import_statement += file_name + "';";
536     ImportDefinition import;
537     import.name = long_import_name.empty() ? import_name : long_import_name;
538     import.statement = import_statement;
539     import.dependency = &dependency;
540     import.dependent = &dependent;
541     imports.insert(std::make_pair(unique_name, import));
542     return import.name;
543   }
544 
AddImport(import_set & imports,std::string import_name,std::string fileName)545   void AddImport(import_set &imports, std::string import_name,
546                  std::string fileName) {
547     ImportDefinition import;
548     import.name = import_name;
549     import.statement = "import " + import_name + " from '" + fileName + "';";
550     imports.insert(std::make_pair(import_name, import));
551   }
552 
553   // Generate a TS union type based on a union's enum
GenObjApiUnionTypeTS(import_set & imports,const IDLOptions & opts,const EnumDef & union_enum)554   std::string GenObjApiUnionTypeTS(import_set &imports, const IDLOptions &opts,
555                                    const EnumDef &union_enum) {
556     std::string ret = "";
557     std::set<std::string> type_list;
558 
559     for (auto it = union_enum.Vals().begin(); it != union_enum.Vals().end();
560          ++it) {
561       const auto &ev = **it;
562       if (ev.IsZero()) { continue; }
563 
564       std::string type = "";
565       if (IsString(ev.union_type)) {
566         type = "string";  // no need to wrap string type in namespace
567       } else if (ev.union_type.base_type == BASE_TYPE_STRUCT) {
568         type = GetObjApiClassName(
569             AddImport(imports, union_enum, *ev.union_type.struct_def), opts);
570       } else {
571         FLATBUFFERS_ASSERT(false);
572       }
573       type_list.insert(type);
574     }
575 
576     size_t totalPrinted = 0;
577     for (auto it = type_list.begin(); it != type_list.end(); ++it) {
578       ++totalPrinted;
579       ret += *it + ((totalPrinted == type_list.size()) ? "" : "|");
580     }
581 
582     return ret;
583   }
584 
GenUnionConvFuncName(const EnumDef & enum_def)585   std::string GenUnionConvFuncName(const EnumDef &enum_def) {
586     return "unionTo" + enum_def.name;
587   }
588 
GenUnionListConvFuncName(const EnumDef & enum_def)589   std::string GenUnionListConvFuncName(const EnumDef &enum_def) {
590     return "unionListTo" + enum_def.name;
591   }
592 
GenUnionConvFunc(const Type & union_type,import_set & imports)593   std::string GenUnionConvFunc(const Type &union_type, import_set &imports) {
594     if (union_type.enum_def) {
595       const auto &enum_def = *union_type.enum_def;
596 
597       const auto valid_union_type = GenUnionTypeTS(enum_def, imports);
598       const auto valid_union_type_with_null = valid_union_type + "|null";
599 
600       auto ret = "\n\nexport function " + GenUnionConvFuncName(enum_def) +
601                  "(\n  type: " + enum_def.name +
602                  ",\n  accessor: (obj:" + valid_union_type + ") => " +
603                  valid_union_type_with_null +
604                  "\n): " + valid_union_type_with_null + " {\n";
605 
606       const auto enum_type = AddImport(imports, enum_def, enum_def);
607 
608       const auto union_enum_loop = [&](const std::string &accessor_str) {
609         ret += "  switch(" + enum_type + "[type]) {\n";
610         ret += "    case 'NONE': return null; \n";
611 
612         for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end();
613              ++it) {
614           const auto &ev = **it;
615           if (ev.IsZero()) { continue; }
616 
617           ret += "    case '" + ev.name + "': ";
618 
619           if (IsString(ev.union_type)) {
620             ret += "return " + accessor_str + "'') as string;";
621           } else if (ev.union_type.base_type == BASE_TYPE_STRUCT) {
622             const auto type =
623                 AddImport(imports, enum_def, *ev.union_type.struct_def);
624             ret += "return " + accessor_str + "new " + type + "())! as " +
625                    type + ";";
626           } else {
627             FLATBUFFERS_ASSERT(false);
628           }
629           ret += "\n";
630         }
631 
632         ret += "    default: return null;\n";
633         ret += "  }\n";
634       };
635 
636       union_enum_loop("accessor(");
637       ret += "}";
638 
639       ret += "\n\nexport function " + GenUnionListConvFuncName(enum_def) +
640              "(\n  type: " + enum_def.name +
641              ", \n  accessor: (index: number, obj:" + valid_union_type +
642              ") => " + valid_union_type_with_null +
643              ", \n  index: number\n): " + valid_union_type_with_null + " {\n";
644       union_enum_loop("accessor(index, ");
645       ret += "}";
646 
647       return ret;
648     }
649     FLATBUFFERS_ASSERT(0);
650     return "";
651   }
652 
653   // Used for generating a short function that returns the correct class
654   // based on union enum type. Assume the context is inside the non object api
655   // type
GenUnionValTS(import_set & imports,const std::string & field_name,const Type & union_type,const bool is_array=false)656   std::string GenUnionValTS(import_set &imports, const std::string &field_name,
657                             const Type &union_type,
658                             const bool is_array = false) {
659     if (union_type.enum_def) {
660       const auto &enum_def = *union_type.enum_def;
661       const auto enum_type = AddImport(imports, enum_def, enum_def);
662       const std::string union_accessor = "this." + field_name;
663 
664       const auto union_has_string = UnionHasStringType(enum_def);
665       const auto field_binded_method = "this." + field_name + ".bind(this)";
666 
667       std::string ret;
668 
669       if (!is_array) {
670         const auto conversion_function = GenUnionConvFuncName(enum_def);
671         const auto target_enum = "this." + field_name + "Type()";
672 
673         ret = "(() => {\n";
674         ret += "      let temp = " + conversion_function + "(" + target_enum +
675                ", " + field_binded_method + ");\n";
676         ret += "      if(temp === null) { return null; }\n";
677         ret += union_has_string
678                    ? "      if(typeof temp === 'string') { return temp; }\n"
679                    : "";
680         ret += "      return temp.unpack()\n";
681         ret += "  })()";
682       } else {
683         const auto conversion_function = GenUnionListConvFuncName(enum_def);
684         const auto target_enum_accesor = "this." + field_name + "Type";
685         const auto target_enum_length = target_enum_accesor + "Length()";
686 
687         ret = "(() => {\n";
688         ret += "    let ret = [];\n";
689         ret += "    for(let targetEnumIndex = 0; targetEnumIndex < " +
690                target_enum_length +
691                "; "
692                "++targetEnumIndex) {\n";
693         ret += "      let targetEnum = " + target_enum_accesor +
694                "(targetEnumIndex);\n";
695         ret += "      if(targetEnum === null || " + enum_type +
696                "[targetEnum!] === 'NONE') { "
697                "continue; }\n\n";
698         ret += "      let temp = " + conversion_function + "(targetEnum, " +
699                field_binded_method + ", targetEnumIndex);\n";
700         ret += "      if(temp === null) { continue; }\n";
701         ret += union_has_string ? "      if(typeof temp === 'string') { "
702                                   "ret.push(temp); continue; }\n"
703                                 : "";
704         ret += "      ret.push(temp.unpack());\n";
705         ret += "    }\n";
706         ret += "    return ret;\n";
707         ret += "  })()";
708       }
709 
710       return ret;
711     }
712 
713     FLATBUFFERS_ASSERT(0);
714     return "";
715   }
716 
GenNullCheckConditional(const std::string & nullCheckVar,const std::string & trueVal,const std::string & falseVal="null")717   static std::string GenNullCheckConditional(
718       const std::string &nullCheckVar, const std::string &trueVal,
719       const std::string &falseVal = "null") {
720     return "(" + nullCheckVar + " !== null ? " + trueVal + " : " + falseVal +
721            ")";
722   }
723 
GenStructMemberValueTS(const StructDef & struct_def,const std::string & prefix,const std::string & delimiter,const bool nullCheck=true)724   std::string GenStructMemberValueTS(const StructDef &struct_def,
725                                      const std::string &prefix,
726                                      const std::string &delimiter,
727                                      const bool nullCheck = true) {
728     std::string ret;
729     for (auto it = struct_def.fields.vec.begin();
730          it != struct_def.fields.vec.end(); ++it) {
731       auto &field = **it;
732 
733       const auto curr_member_accessor =
734           prefix + "." + MakeCamel(field.name, false);
735       if (IsStruct(field.value.type)) {
736         ret += GenStructMemberValueTS(*field.value.type.struct_def,
737                                       curr_member_accessor, delimiter);
738       } else {
739         if (nullCheck) {
740           ret +=
741               "(" + prefix + " === null ? 0 : " + curr_member_accessor + "!)";
742         } else {
743           ret += curr_member_accessor;
744         }
745       }
746 
747       if (std::next(it) != struct_def.fields.vec.end()) { ret += delimiter; }
748     }
749 
750     return ret;
751   }
752 
GenObjApi(const Parser & parser,StructDef & struct_def,std::string & obj_api_unpack_func,std::string & obj_api_class,import_set & imports)753   void GenObjApi(const Parser &parser, StructDef &struct_def,
754                  std::string &obj_api_unpack_func, std::string &obj_api_class,
755                  import_set &imports) {
756     const auto class_name = GetObjApiClassName(struct_def, parser.opts);
757 
758     std::string unpack_func = "\nunpack(): " + class_name +
759                               " {\n  return new " + class_name + "(" +
760                               (struct_def.fields.vec.empty() ? "" : "\n");
761     std::string unpack_to_func = "\nunpackTo(_o: " + class_name + "): void {" +
762                                  +(struct_def.fields.vec.empty() ? "" : "\n");
763 
764     std::string constructor_func = "constructor(";
765     constructor_func += (struct_def.fields.vec.empty() ? "" : "\n");
766 
767     const auto has_create =
768         struct_def.fixed || CanCreateFactoryMethod(struct_def);
769 
770     std::string pack_func_prototype =
771         "\npack(builder:flatbuffers.Builder): flatbuffers.Offset {\n";
772 
773     std::string pack_func_offset_decl;
774     std::string pack_func_create_call;
775 
776     const auto struct_name = AddImport(imports, struct_def, struct_def);
777 
778     if (has_create) {
779       pack_func_create_call = "  return " + struct_name + ".create" +
780                               GetPrefixedName(struct_def) + "(builder" +
781                               (struct_def.fields.vec.empty() ? "" : ",\n    ");
782     } else {
783       pack_func_create_call = "  " + struct_name + ".start" +
784                               GetPrefixedName(struct_def) + "(builder);\n";
785     }
786 
787     if (struct_def.fixed) {
788       // when packing struct, nested struct's members instead of the struct's
789       // offset are used
790       pack_func_create_call +=
791           GenStructMemberValueTS(struct_def, "this", ",\n    ", false) + "\n  ";
792     }
793 
794     for (auto it = struct_def.fields.vec.begin();
795          it != struct_def.fields.vec.end(); ++it) {
796       auto &field = **it;
797       if (field.deprecated) continue;
798 
799       const auto field_name = MakeCamel(field.name, false);
800       const std::string field_binded_method =
801           "this." + field_name + ".bind(this)";
802 
803       std::string field_val;
804       std::string field_type;
805       // a string that declares a variable containing the
806       // offset for things that can't be generated inline
807       // empty otw
808       std::string field_offset_decl;
809       // a string that contains values for things that can be created inline or
810       // the variable name from field_offset_decl
811       std::string field_offset_val;
812       const auto field_default_val =
813           GenDefaultValue(field, "flatbuffers", imports);
814 
815       // Emit a scalar field
816       const auto is_string = IsString(field.value.type);
817       if (IsScalar(field.value.type.base_type) || is_string) {
818         const auto has_null_default = is_string || HasNullDefault(field);
819 
820         field_type += GenTypeName(imports, field, field.value.type, false,
821                                   has_null_default);
822         field_val = "this." + field_name + "()";
823 
824         if (field.value.type.base_type != BASE_TYPE_STRING) {
825           field_offset_val = "this." + field_name;
826         } else {
827           field_offset_decl = GenNullCheckConditional(
828               "this." + field_name,
829               "builder.createString(this." + field_name + "!)", "0");
830         }
831       }
832 
833       // Emit an object field
834       else {
835         auto is_vector = false;
836         switch (field.value.type.base_type) {
837           case BASE_TYPE_STRUCT: {
838             const auto &sd = *field.value.type.struct_def;
839             field_type += GetObjApiClassName(sd, parser.opts);
840 
841             const std::string field_accessor = "this." + field_name + "()";
842             field_val = GenNullCheckConditional(field_accessor,
843                                                 field_accessor + "!.unpack()");
844             auto packing = GenNullCheckConditional(
845                 "this." + field_name, "this." + field_name + "!.pack(builder)",
846                 "0");
847 
848             if (sd.fixed) {
849               field_offset_val = std::move(packing);
850             } else {
851               field_offset_decl = std::move(packing);
852             }
853 
854             break;
855           }
856 
857           case BASE_TYPE_VECTOR: {
858             auto vectortype = field.value.type.VectorType();
859             auto vectortypename =
860                 GenTypeName(imports, struct_def, vectortype, false);
861             is_vector = true;
862 
863             field_type = "(";
864 
865             switch (vectortype.base_type) {
866               case BASE_TYPE_STRUCT: {
867                 const auto &sd = *field.value.type.struct_def;
868                 field_type += GetObjApiClassName(sd, parser.opts);
869                 field_type += ")[]";
870 
871                 field_val = GenBBAccess() + ".createObjList(" +
872                             field_binded_method + ", this." + field_name +
873                             "Length())";
874 
875                 if (sd.fixed) {
876                   field_offset_decl =
877                       "builder.createStructOffsetList(this." + field_name +
878                       ", " + AddImport(imports, struct_def, struct_def) +
879                       ".start" + MakeCamel(field_name) + "Vector)";
880                 } else {
881                   field_offset_decl =
882                       AddImport(imports, struct_def, struct_def) + ".create" +
883                       MakeCamel(field_name) +
884                       "Vector(builder, builder.createObjectOffsetList(" +
885                       "this." + field_name + "))";
886                 }
887 
888                 break;
889               }
890 
891               case BASE_TYPE_STRING: {
892                 field_type += "string)[]";
893                 field_val = GenBBAccess() + ".createScalarList(" +
894                             field_binded_method + ", this." + field_name +
895                             "Length())";
896                 field_offset_decl =
897                     AddImport(imports, struct_def, struct_def) + ".create" +
898                     MakeCamel(field_name) +
899                     "Vector(builder, builder.createObjectOffsetList(" +
900                     "this." + field_name + "))";
901                 break;
902               }
903 
904               case BASE_TYPE_UNION: {
905                 field_type += GenObjApiUnionTypeTS(imports, parser.opts,
906                                                    *(vectortype.enum_def));
907                 field_type += ")[]";
908                 field_val =
909                     GenUnionValTS(imports, field_name, vectortype, true);
910 
911                 field_offset_decl =
912                     AddImport(imports, struct_def, struct_def) + ".create" +
913                     MakeCamel(field_name) +
914                     "Vector(builder, builder.createObjectOffsetList(" +
915                     "this." + field_name + "))";
916 
917                 break;
918               }
919               default: {
920                 if (vectortype.enum_def) {
921                   field_type += GenTypeName(imports, struct_def, vectortype,
922                                             false, HasNullDefault(field));
923                 } else {
924                   field_type += vectortypename;
925                 }
926                 field_type += ")[]";
927                 field_val = GenBBAccess() + ".createScalarList(" +
928                             field_binded_method + ", this." + field_name +
929                             "Length())";
930 
931                 field_offset_decl = AddImport(imports, struct_def, struct_def) +
932                                     ".create" + MakeCamel(field_name) +
933                                     "Vector(builder, this." + field_name + ")";
934 
935                 break;
936               }
937             }
938 
939             break;
940           }
941 
942           case BASE_TYPE_UNION: {
943             field_type += GenObjApiUnionTypeTS(imports, parser.opts,
944                                                *(field.value.type.enum_def));
945 
946             field_val = GenUnionValTS(imports, field_name, field.value.type);
947             field_offset_decl =
948                 "builder.createObjectOffset(this." + field_name + ")";
949             break;
950           }
951 
952           default: FLATBUFFERS_ASSERT(0); break;
953         }
954 
955         // length 0 vector is simply empty instead of null
956         field_type += is_vector ? "" : "|null";
957       }
958 
959       if (!field_offset_decl.empty()) {
960         field_offset_decl =
961             "  const " + field_name + " = " + field_offset_decl + ";";
962       }
963       if (field_offset_val.empty()) { field_offset_val = field_name; }
964 
965       unpack_func += "    " + field_val;
966       unpack_to_func += "  _o." + field_name + " = " + field_val + ";";
967 
968       constructor_func += "  public " + field_name + ": " + field_type + " = " +
969                           field_default_val;
970 
971       if (!struct_def.fixed) {
972         if (!field_offset_decl.empty()) {
973           pack_func_offset_decl += field_offset_decl + "\n";
974         }
975 
976         if (has_create) {
977           pack_func_create_call += field_offset_val;
978         } else {
979           pack_func_create_call += "  " + struct_name + ".add" +
980                                    MakeCamel(field.name) + "(builder, " +
981                                    field_offset_val + ");\n";
982         }
983       }
984 
985       if (std::next(it) != struct_def.fields.vec.end()) {
986         constructor_func += ",\n";
987 
988         if (!struct_def.fixed && has_create) {
989           pack_func_create_call += ",\n    ";
990         }
991 
992         unpack_func += ",\n";
993         unpack_to_func += "\n";
994       } else {
995         constructor_func += "\n";
996         if (!struct_def.fixed) {
997           pack_func_offset_decl += (pack_func_offset_decl.empty() ? "" : "\n");
998           pack_func_create_call += "\n  ";
999         }
1000 
1001         unpack_func += "\n  ";
1002         unpack_to_func += "\n";
1003       }
1004     }
1005 
1006     constructor_func += "){}\n\n";
1007 
1008     if (has_create) {
1009       pack_func_create_call += ");";
1010     } else {
1011       pack_func_create_call += "return " + struct_name + ".end" +
1012                                GetPrefixedName(struct_def) + "(builder);";
1013     }
1014 
1015     obj_api_class = "\nexport class " +
1016                     GetObjApiClassName(struct_def, parser.opts) + " {\n";
1017 
1018     obj_api_class += constructor_func;
1019     obj_api_class += pack_func_prototype + pack_func_offset_decl +
1020                      pack_func_create_call + "\n}";
1021 
1022     obj_api_class += "\n}\n";
1023 
1024     unpack_func += ");\n}";
1025     unpack_to_func += "}\n";
1026 
1027     obj_api_unpack_func = unpack_func + "\n\n" + unpack_to_func;
1028   }
1029 
CanCreateFactoryMethod(const StructDef & struct_def)1030   static bool CanCreateFactoryMethod(const StructDef &struct_def) {
1031     // to preserve backwards compatibility, we allow the first field to be a
1032     // struct
1033     return struct_def.fields.vec.size() < 2 ||
1034            std::all_of(std::begin(struct_def.fields.vec) + 1,
1035                        std::end(struct_def.fields.vec),
1036                        [](const FieldDef *f) -> bool {
1037                          FLATBUFFERS_ASSERT(f != nullptr);
1038                          return f->value.type.base_type != BASE_TYPE_STRUCT;
1039                        });
1040   }
1041 
1042   // Generate an accessor struct with constructor for a flatbuffers struct.
GenStruct(const Parser & parser,StructDef & struct_def,std::string * code_ptr,import_set & imports)1043   void GenStruct(const Parser &parser, StructDef &struct_def,
1044                  std::string *code_ptr, import_set &imports) {
1045     if (struct_def.generated) return;
1046     std::string &code = *code_ptr;
1047 
1048     std::string object_name;
1049     std::string object_namespace = GetNameSpace(struct_def);
1050 
1051     // Emit constructor
1052     object_name = struct_def.name;
1053     GenDocComment(struct_def.doc_comment, code_ptr);
1054     code += "export class " + struct_def.name;
1055     code += " {\n";
1056     code += "  bb: flatbuffers.ByteBuffer|null = null;\n";
1057     code += "  bb_pos = 0;\n";
1058 
1059     // Generate the __init method that sets the field in a pre-existing
1060     // accessor object. This is to allow object reuse.
1061     code +=
1062         "__init(i:number, bb:flatbuffers.ByteBuffer):" + object_name + " {\n";
1063     code += "  this.bb_pos = i;\n";
1064     code += "  this.bb = bb;\n";
1065     code += "  return this;\n";
1066     code += "}\n\n";
1067 
1068     // Generate special accessors for the table that when used as the root of a
1069     // FlatBuffer
1070     GenerateRootAccessor(struct_def, code_ptr, code, object_name, false);
1071     GenerateRootAccessor(struct_def, code_ptr, code, object_name, true);
1072 
1073     // Generate the identifier check method
1074     if (!struct_def.fixed && parser_.root_struct_def_ == &struct_def &&
1075         !parser_.file_identifier_.empty()) {
1076       GenDocComment(code_ptr);
1077       code +=
1078           "static bufferHasIdentifier(bb:flatbuffers.ByteBuffer):boolean "
1079           "{\n";
1080       code += "  return bb.__has_identifier('" + parser_.file_identifier_;
1081       code += "');\n}\n\n";
1082     }
1083 
1084     // Emit field accessors
1085     for (auto it = struct_def.fields.vec.begin();
1086          it != struct_def.fields.vec.end(); ++it) {
1087       auto &field = **it;
1088       if (field.deprecated) continue;
1089       auto offset_prefix =
1090           "  const offset = " + GenBBAccess() + ".__offset(this.bb_pos, " +
1091           NumToString(field.value.offset) + ");\n  return offset ? ";
1092 
1093       // Emit a scalar field
1094       const auto is_string = IsString(field.value.type);
1095       if (IsScalar(field.value.type.base_type) || is_string) {
1096         const auto has_null_default = is_string || HasNullDefault(field);
1097 
1098         GenDocComment(field.doc_comment, code_ptr);
1099         std::string prefix = MakeCamel(field.name, false) + "(";
1100         if (is_string) {
1101           code += prefix + "):string|null\n";
1102           code +=
1103               prefix + "optionalEncoding:flatbuffers.Encoding" + "):" +
1104               GenTypeName(imports, struct_def, field.value.type, false, true) +
1105               "\n";
1106           code += prefix + "optionalEncoding?:any";
1107         } else {
1108           code += prefix;
1109         }
1110         if (field.value.type.enum_def) {
1111           code += "):" +
1112                   GenTypeName(imports, struct_def, field.value.type, false,
1113                               field.IsOptional()) +
1114                   " {\n";
1115         } else {
1116           code += "):" +
1117                   GenTypeName(imports, struct_def, field.value.type, false,
1118                               has_null_default) +
1119                   " {\n";
1120         }
1121 
1122         if (struct_def.fixed) {
1123           code +=
1124               "  return " +
1125               GenGetter(field.value.type,
1126                         "(this.bb_pos" + MaybeAdd(field.value.offset) + ")") +
1127               ";\n";
1128         } else {
1129           std::string index = "this.bb_pos + offset";
1130           if (is_string) { index += ", optionalEncoding"; }
1131           code += offset_prefix +
1132                   GenGetter(field.value.type, "(" + index + ")") + " : " +
1133                   GenDefaultValue(field, GenBBAccess(), imports);
1134           code += ";\n";
1135         }
1136       }
1137 
1138       // Emit an object field
1139       else {
1140         switch (field.value.type.base_type) {
1141           case BASE_TYPE_STRUCT: {
1142             const auto type =
1143                 AddImport(imports, struct_def, *field.value.type.struct_def);
1144             GenDocComment(field.doc_comment, code_ptr);
1145             code += MakeCamel(field.name, false);
1146             code += "(obj?:" + type + "):" + type + "|null {\n";
1147 
1148             if (struct_def.fixed) {
1149               code += "  return (obj || " + GenerateNewExpression(type);
1150               code += ").__init(this.bb_pos";
1151               code +=
1152                   MaybeAdd(field.value.offset) + ", " + GenBBAccess() + ");\n";
1153             } else {
1154               code += offset_prefix + "(obj || " + GenerateNewExpression(type) +
1155                       ").__init(";
1156               code += field.value.type.struct_def->fixed
1157                           ? "this.bb_pos + offset"
1158                           : GenBBAccess() + ".__indirect(this.bb_pos + offset)";
1159               code += ", " + GenBBAccess() + ") : null;\n";
1160             }
1161 
1162             break;
1163           }
1164 
1165           case BASE_TYPE_VECTOR: {
1166             auto vectortype = field.value.type.VectorType();
1167             auto vectortypename =
1168                 GenTypeName(imports, struct_def, vectortype, false);
1169             auto inline_size = InlineSize(vectortype);
1170             auto index = GenBBAccess() +
1171                          ".__vector(this.bb_pos + offset) + index" +
1172                          MaybeScale(inline_size);
1173             std::string ret_type;
1174             bool is_union = false;
1175             switch (vectortype.base_type) {
1176               case BASE_TYPE_STRUCT: ret_type = vectortypename; break;
1177               case BASE_TYPE_STRING: ret_type = vectortypename; break;
1178               case BASE_TYPE_UNION:
1179                 ret_type = "?flatbuffers.Table";
1180                 is_union = true;
1181                 break;
1182               default: ret_type = vectortypename;
1183             }
1184             GenDocComment(field.doc_comment, code_ptr);
1185             std::string prefix = MakeCamel(field.name, false);
1186             // TODO: make it work without any
1187             // if (is_union) { prefix += "<T extends flatbuffers.Table>"; }
1188             if (is_union) { prefix += ""; }
1189             prefix += "(index: number";
1190             if (is_union) {
1191               const auto union_type =
1192                   GenUnionGenericTypeTS(*(field.value.type.enum_def));
1193 
1194               vectortypename = union_type;
1195               code += prefix + ", obj:" + union_type;
1196             } else if (vectortype.base_type == BASE_TYPE_STRUCT) {
1197               code += prefix + ", obj?:" + vectortypename;
1198             } else if (IsString(vectortype)) {
1199               code += prefix + "):string\n";
1200               code += prefix + ",optionalEncoding:flatbuffers.Encoding" +
1201                       "):" + vectortypename + "\n";
1202               code += prefix + ",optionalEncoding?:any";
1203             } else {
1204               code += prefix;
1205             }
1206             code += "):" + vectortypename + "|null {\n";
1207 
1208             if (vectortype.base_type == BASE_TYPE_STRUCT) {
1209               code += offset_prefix + "(obj || " +
1210                       GenerateNewExpression(vectortypename);
1211               code += ").__init(";
1212               code += vectortype.struct_def->fixed
1213                           ? index
1214                           : GenBBAccess() + ".__indirect(" + index + ")";
1215               code += ", " + GenBBAccess() + ")";
1216             } else {
1217               if (is_union) {
1218                 index = "obj, " + index;
1219               } else if (IsString(vectortype)) {
1220                 index += ", optionalEncoding";
1221               }
1222               code += offset_prefix + GenGetter(vectortype, "(" + index + ")");
1223             }
1224             code += " : ";
1225             if (field.value.type.element == BASE_TYPE_BOOL) {
1226               code += "false";
1227             } else if (field.value.type.element == BASE_TYPE_LONG ||
1228                        field.value.type.element == BASE_TYPE_ULONG) {
1229               code += GenBBAccess() + ".createLong(0, 0)";
1230             } else if (IsScalar(field.value.type.element)) {
1231               if (field.value.type.enum_def) {
1232                 code += field.value.constant;
1233               } else {
1234                 code += "0";
1235               }
1236             } else {
1237               code += "null";
1238             }
1239             code += ";\n";
1240             break;
1241           }
1242 
1243           case BASE_TYPE_UNION: {
1244             GenDocComment(field.doc_comment, code_ptr);
1245             code += MakeCamel(field.name, false);
1246 
1247             const auto &union_enum = *(field.value.type.enum_def);
1248             const auto union_type = GenUnionGenericTypeTS(union_enum);
1249             code += "<T extends flatbuffers.Table>(obj:" + union_type +
1250                     "):" + union_type +
1251                     "|null "
1252                     "{\n";
1253 
1254             code += offset_prefix +
1255                     GenGetter(field.value.type, "(obj, this.bb_pos + offset)") +
1256                     " : null;\n";
1257             break;
1258           }
1259           default: FLATBUFFERS_ASSERT(0);
1260         }
1261       }
1262       code += "}\n\n";
1263 
1264       // Adds the mutable scalar value to the output
1265       if (IsScalar(field.value.type.base_type) && parser.opts.mutable_buffer &&
1266           !IsUnion(field.value.type)) {
1267         std::string type =
1268             GenTypeName(imports, struct_def, field.value.type, true);
1269 
1270         code += "mutate_" + field.name + "(value:" + type + "):boolean {\n";
1271 
1272         if (struct_def.fixed) {
1273           code += "  " + GenBBAccess() + ".write" +
1274                   MakeCamel(GenType(field.value.type)) + "(this.bb_pos + " +
1275                   NumToString(field.value.offset) + ", ";
1276         } else {
1277           code += "  const offset = " + GenBBAccess() +
1278                   ".__offset(this.bb_pos, " + NumToString(field.value.offset) +
1279                   ");\n\n";
1280           code += "  if (offset === 0) {\n";
1281           code += "    return false;\n";
1282           code += "  }\n\n";
1283 
1284           // special case for bools, which are treated as uint8
1285           code += "  " + GenBBAccess() + ".write" +
1286                   MakeCamel(GenType(field.value.type)) +
1287                   "(this.bb_pos + offset, ";
1288           if (field.value.type.base_type == BASE_TYPE_BOOL) { code += "+"; }
1289         }
1290 
1291         code += "value);\n";
1292         code += "  return true;\n";
1293         code += "}\n\n";
1294       }
1295 
1296       // Emit vector helpers
1297       if (IsVector(field.value.type)) {
1298         // Emit a length helper
1299         GenDocComment(code_ptr);
1300         code += MakeCamel(field.name, false);
1301         code += "Length():number {\n" + offset_prefix;
1302 
1303         code +=
1304             GenBBAccess() + ".__vector_len(this.bb_pos + offset) : 0;\n}\n\n";
1305 
1306         // For scalar types, emit a typed array helper
1307         auto vectorType = field.value.type.VectorType();
1308         if (IsScalar(vectorType.base_type) && !IsLong(vectorType.base_type)) {
1309           GenDocComment(code_ptr);
1310 
1311           code += MakeCamel(field.name, false);
1312           code += "Array():" + GenType(vectorType) + "Array|null {\n" +
1313                   offset_prefix;
1314 
1315           code += "new " + GenType(vectorType) + "Array(" + GenBBAccess() +
1316                   ".bytes().buffer, " + GenBBAccess() +
1317                   ".bytes().byteOffset + " + GenBBAccess() +
1318                   ".__vector(this.bb_pos + offset), " + GenBBAccess() +
1319                   ".__vector_len(this.bb_pos + offset)) : null;\n}\n\n";
1320         }
1321       }
1322     }
1323 
1324     // Emit the fully qualified name
1325     if (parser_.opts.generate_name_strings) {
1326       GenDocComment(code_ptr);
1327       code += "static getFullyQualifiedName():string {\n";
1328       code += "  return '" + WrapInNameSpace(struct_def) + "';\n";
1329       code += "}\n\n";
1330     }
1331 
1332     // Emit the size of the struct.
1333     if (struct_def.fixed) {
1334       GenDocComment(code_ptr);
1335       code += "static sizeOf():number {\n";
1336       code += "  return " + NumToString(struct_def.bytesize) + ";\n";
1337       code += "}\n\n";
1338     }
1339 
1340     // Emit a factory constructor
1341     if (struct_def.fixed) {
1342       std::string arguments;
1343       GenStructArgs(imports, struct_def, &arguments, "");
1344       GenDocComment(code_ptr);
1345 
1346       code += "static create" + GetPrefixedName(struct_def) +
1347               "(builder:flatbuffers.Builder";
1348       code += arguments + "):flatbuffers.Offset {\n";
1349 
1350       GenStructBody(struct_def, &code, "");
1351       code += "  return builder.offset();\n}\n\n";
1352     } else {
1353       // Generate a method to start building a new object
1354       GenDocComment(code_ptr);
1355 
1356       code += "static start" + GetPrefixedName(struct_def) +
1357               "(builder:flatbuffers.Builder) {\n";
1358 
1359       code += "  builder.startObject(" +
1360               NumToString(struct_def.fields.vec.size()) + ");\n";
1361       code += "}\n\n";
1362 
1363       // Generate a set of static methods that allow table construction
1364       for (auto it = struct_def.fields.vec.begin();
1365            it != struct_def.fields.vec.end(); ++it) {
1366         auto &field = **it;
1367         if (field.deprecated) continue;
1368         const auto argname = GetArgName(field);
1369 
1370         // Generate the field insertion method
1371         GenDocComment(code_ptr);
1372         code += "static add" + MakeCamel(field.name);
1373         code += "(builder:flatbuffers.Builder, " + argname + ":" +
1374                 GetArgType(imports, struct_def, field, false) + ") {\n";
1375         code += "  builder.addField" + GenWriteMethod(field.value.type) + "(";
1376         code += NumToString(it - struct_def.fields.vec.begin()) + ", ";
1377         if (field.value.type.base_type == BASE_TYPE_BOOL) { code += "+"; }
1378         code += argname + ", ";
1379         if (!IsScalar(field.value.type.base_type)) {
1380           code += "0";
1381         } else if (HasNullDefault(field)) {
1382           if (IsLong(field.value.type.base_type)) {
1383             code += "builder.createLong(0, 0)";
1384           } else {
1385             code += "0";
1386           }
1387         } else {
1388           if (field.value.type.base_type == BASE_TYPE_BOOL) { code += "+"; }
1389           code += GenDefaultValue(field, "builder", imports);
1390         }
1391         code += ");\n}\n\n";
1392 
1393         if (IsVector(field.value.type)) {
1394           auto vector_type = field.value.type.VectorType();
1395           auto alignment = InlineAlignment(vector_type);
1396           auto elem_size = InlineSize(vector_type);
1397 
1398           // Generate a method to create a vector from a JavaScript array
1399           if (!IsStruct(vector_type)) {
1400             GenDocComment(code_ptr);
1401 
1402             const std::string sig_begin =
1403                 "static create" + MakeCamel(field.name) +
1404                 "Vector(builder:flatbuffers.Builder, data:";
1405             const std::string sig_end = "):flatbuffers.Offset";
1406             std::string type =
1407                 GenTypeName(imports, struct_def, vector_type, true) + "[]";
1408             if (type == "number[]") {
1409               const auto &array_type = GenType(vector_type);
1410               // the old type should be deprecated in the future
1411               std::string type_old = "number[]|Uint8Array";
1412               std::string type_new = "number[]|" + array_type + "Array";
1413               if (type_old == type_new) {
1414                 type = type_new;
1415               } else {
1416                 // add function overloads
1417                 code += sig_begin + type_new + sig_end + ";\n";
1418                 code +=
1419                     "/**\n * @deprecated This Uint8Array overload will "
1420                     "be removed in the future.\n */\n";
1421                 code += sig_begin + type_old + sig_end + ";\n";
1422                 type = type_new + "|Uint8Array";
1423               }
1424             }
1425             code += sig_begin + type + sig_end + " {\n";
1426             code += "  builder.startVector(" + NumToString(elem_size);
1427             code += ", data.length, " + NumToString(alignment) + ");\n";
1428             code += "  for (let i = data.length - 1; i >= 0; i--) {\n";
1429             code += "    builder.add" + GenWriteMethod(vector_type) + "(";
1430             if (vector_type.base_type == BASE_TYPE_BOOL) { code += "+"; }
1431             code += "data[i]!);\n";
1432             code += "  }\n";
1433             code += "  return builder.endVector();\n";
1434             code += "}\n\n";
1435           }
1436 
1437           // Generate a method to start a vector, data to be added manually
1438           // after
1439           GenDocComment(code_ptr);
1440 
1441           code += "static start" + MakeCamel(field.name);
1442           code += "Vector(builder:flatbuffers.Builder, numElems:number) {\n";
1443           code += "  builder.startVector(" + NumToString(elem_size);
1444           code += ", numElems, " + NumToString(alignment) + ");\n";
1445           code += "}\n\n";
1446         }
1447       }
1448 
1449       // Generate a method to stop building a new object
1450       GenDocComment(code_ptr);
1451 
1452       code += "static end" + GetPrefixedName(struct_def);
1453       code += "(builder:flatbuffers.Builder):flatbuffers.Offset {\n";
1454 
1455       code += "  const offset = builder.endObject();\n";
1456       for (auto it = struct_def.fields.vec.begin();
1457            it != struct_def.fields.vec.end(); ++it) {
1458         auto &field = **it;
1459         if (!field.deprecated && field.IsRequired()) {
1460           code += "  builder.requiredField(offset, ";
1461           code += NumToString(field.value.offset);
1462           code += ") // " + field.name + "\n";
1463         }
1464       }
1465       code += "  return offset;\n";
1466       code += "}\n\n";
1467 
1468       // Generate the methods to complete buffer construction
1469       GenerateFinisher(struct_def, code_ptr, code, false);
1470       GenerateFinisher(struct_def, code_ptr, code, true);
1471 
1472       // Generate a convenient CreateX function
1473       if (CanCreateFactoryMethod(struct_def)) {
1474         code += "static create" + GetPrefixedName(struct_def);
1475         code += "(builder:flatbuffers.Builder";
1476         for (auto it = struct_def.fields.vec.begin();
1477              it != struct_def.fields.vec.end(); ++it) {
1478           const auto &field = **it;
1479           if (field.deprecated) continue;
1480           code += ", " + GetArgName(field) + ":" +
1481                   GetArgType(imports, struct_def, field, true);
1482         }
1483 
1484         code += "):flatbuffers.Offset {\n";
1485         code += "  " + struct_def.name + ".start" +
1486                 GetPrefixedName(struct_def) + "(builder);\n";
1487 
1488         std::string methodPrefix = struct_def.name;
1489         for (auto it = struct_def.fields.vec.begin();
1490              it != struct_def.fields.vec.end(); ++it) {
1491           const auto &field = **it;
1492           if (field.deprecated) continue;
1493 
1494           const auto arg_name = GetArgName(field);
1495 
1496           if (field.IsScalarOptional()) {
1497             code += "  if (" + arg_name + " !== null)\n  ";
1498           }
1499 
1500           code += "  " + methodPrefix + ".add" + MakeCamel(field.name) + "(";
1501           code += "builder, " + arg_name + ");\n";
1502         }
1503 
1504         code += "  return " + methodPrefix + ".end" +
1505                 GetPrefixedName(struct_def) + "(builder);\n";
1506         code += "}\n";
1507       }
1508     }
1509 
1510     if (!struct_def.fixed && parser_.services_.vec.size() != 0) {
1511       auto name = GetPrefixedName(struct_def, "");
1512       code += "\n";
1513       code += "serialize():Uint8Array {\n";
1514       code += "  return this.bb!.bytes();\n";
1515       code += "}\n";
1516 
1517       code += "\n";
1518       code += "static deserialize(buffer: Uint8Array):" + name + " {\n";
1519       code += "  return " + AddImport(imports, struct_def, struct_def) +
1520               ".getRootAs" + name + "(new flatbuffers.ByteBuffer(buffer))\n";
1521       code += "}\n";
1522     }
1523 
1524     if (parser_.opts.generate_object_based_api) {
1525       std::string obj_api_class;
1526       std::string obj_api_unpack_func;
1527       GenObjApi(parser_, struct_def, obj_api_unpack_func, obj_api_class,
1528                 imports);
1529 
1530       code += obj_api_unpack_func + "}\n" + obj_api_class;
1531     } else {
1532       code += "}\n";
1533     }
1534   }
1535 
HasNullDefault(const FieldDef & field)1536   static bool HasNullDefault(const FieldDef &field) {
1537     return field.IsOptional() && field.value.constant == "null";
1538   }
1539 
GetArgType(import_set & imports,const Definition & owner,const FieldDef & field,bool allowNull)1540   std::string GetArgType(import_set &imports, const Definition &owner,
1541                          const FieldDef &field, bool allowNull) {
1542     return GenTypeName(imports, owner, field.value.type, true,
1543                        allowNull && field.IsOptional());
1544   }
1545 
GetArgName(const FieldDef & field)1546   static std::string GetArgName(const FieldDef &field) {
1547     auto argname = MakeCamel(field.name, false);
1548     if (!IsScalar(field.value.type.base_type)) { argname += "Offset"; }
1549 
1550     return argname;
1551   }
1552 
GetPrefixedName(const StructDef & struct_def,const char * prefix="")1553   std::string GetPrefixedName(const StructDef &struct_def,
1554                               const char *prefix = "") {
1555     return prefix + struct_def.name;
1556   }
1557 };  // namespace ts
1558 }  // namespace ts
1559 
GenerateTS(const Parser & parser,const std::string & path,const std::string & file_name)1560 bool GenerateTS(const Parser &parser, const std::string &path,
1561                 const std::string &file_name) {
1562   ts::TsGenerator generator(parser, path, file_name);
1563   return generator.generate();
1564 }
1565 
TSMakeRule(const Parser & parser,const std::string & path,const std::string & file_name)1566 std::string TSMakeRule(const Parser &parser, const std::string &path,
1567                        const std::string &file_name) {
1568   FLATBUFFERS_ASSERT(parser.opts.lang <= IDLOptions::kMAX);
1569 
1570   std::string filebase =
1571       flatbuffers::StripPath(flatbuffers::StripExtension(file_name));
1572   ts::TsGenerator generator(parser, path, file_name);
1573   std::string make_rule =
1574       generator.GeneratedFileName(path, filebase, parser.opts) + ": ";
1575 
1576   auto included_files = parser.GetIncludedFilesRecursive(file_name);
1577   for (auto it = included_files.begin(); it != included_files.end(); ++it) {
1578     make_rule += " " + *it;
1579   }
1580   return make_rule;
1581 }
1582 
1583 }  // namespace flatbuffers
1584