xref: /aosp_15_r20/external/flatbuffers/src/idl_gen_ts.cpp (revision 890232f25432b36107d06881e0a25aaa6b473652)
1*890232f2SAndroid Build Coastguard Worker /*
2*890232f2SAndroid Build Coastguard Worker  * Copyright 2014 Google Inc. All rights reserved.
3*890232f2SAndroid Build Coastguard Worker  *
4*890232f2SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*890232f2SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*890232f2SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*890232f2SAndroid Build Coastguard Worker  *
8*890232f2SAndroid Build Coastguard Worker  *     http://www.apache.org/licenses/LICENSE-2.0
9*890232f2SAndroid Build Coastguard Worker  *
10*890232f2SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*890232f2SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*890232f2SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*890232f2SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*890232f2SAndroid Build Coastguard Worker  * limitations under the License.
15*890232f2SAndroid Build Coastguard Worker  */
16*890232f2SAndroid Build Coastguard Worker 
17*890232f2SAndroid Build Coastguard Worker #include <algorithm>
18*890232f2SAndroid Build Coastguard Worker #include <cassert>
19*890232f2SAndroid Build Coastguard Worker #include <unordered_map>
20*890232f2SAndroid Build Coastguard Worker #include <unordered_set>
21*890232f2SAndroid Build Coastguard Worker 
22*890232f2SAndroid Build Coastguard Worker #include "flatbuffers/code_generators.h"
23*890232f2SAndroid Build Coastguard Worker #include "flatbuffers/flatbuffers.h"
24*890232f2SAndroid Build Coastguard Worker #include "flatbuffers/idl.h"
25*890232f2SAndroid Build Coastguard Worker #include "flatbuffers/util.h"
26*890232f2SAndroid Build Coastguard Worker 
27*890232f2SAndroid Build Coastguard Worker namespace flatbuffers {
28*890232f2SAndroid Build Coastguard Worker namespace {
29*890232f2SAndroid Build Coastguard Worker struct ImportDefinition {
30*890232f2SAndroid Build Coastguard Worker   std::string name;
31*890232f2SAndroid Build Coastguard Worker   std::string import_statement;
32*890232f2SAndroid Build Coastguard Worker   std::string export_statement;
33*890232f2SAndroid Build Coastguard Worker   std::string bare_file_path;
34*890232f2SAndroid Build Coastguard Worker   std::string rel_file_path;
35*890232f2SAndroid Build Coastguard Worker   std::string object_name;
36*890232f2SAndroid Build Coastguard Worker   const Definition *dependent = nullptr;
37*890232f2SAndroid Build Coastguard Worker   const Definition *dependency = nullptr;
38*890232f2SAndroid Build Coastguard Worker };
39*890232f2SAndroid Build Coastguard Worker 
40*890232f2SAndroid Build Coastguard Worker enum AnnotationType { kParam = 0, kType = 1, kReturns = 2 };
41*890232f2SAndroid Build Coastguard Worker }  // namespace
42*890232f2SAndroid Build Coastguard Worker 
43*890232f2SAndroid Build Coastguard Worker namespace ts {
44*890232f2SAndroid Build Coastguard Worker // Iterate through all definitions we haven't generate code for (enums, structs,
45*890232f2SAndroid Build Coastguard Worker // and tables) and output them to a single file.
46*890232f2SAndroid Build Coastguard Worker class TsGenerator : public BaseGenerator {
47*890232f2SAndroid Build Coastguard Worker  public:
48*890232f2SAndroid Build Coastguard Worker   typedef std::map<std::string, ImportDefinition> import_set;
49*890232f2SAndroid Build Coastguard Worker 
TsGenerator(const Parser & parser,const std::string & path,const std::string & file_name)50*890232f2SAndroid Build Coastguard Worker   TsGenerator(const Parser &parser, const std::string &path,
51*890232f2SAndroid Build Coastguard Worker               const std::string &file_name)
52*890232f2SAndroid Build Coastguard Worker       : BaseGenerator(parser, path, file_name, "", "_", "ts") {
53*890232f2SAndroid Build Coastguard Worker     // clang-format off
54*890232f2SAndroid Build Coastguard Worker 
55*890232f2SAndroid Build Coastguard Worker     // List of keywords retrieved from here:
56*890232f2SAndroid Build Coastguard Worker     // https://github.com/microsoft/TypeScript/issues/2536
57*890232f2SAndroid Build Coastguard Worker     // One per line to ease comparisons to that list are easier
58*890232f2SAndroid Build Coastguard Worker     static const char *const keywords[] = {
59*890232f2SAndroid Build Coastguard Worker       "arguments",
60*890232f2SAndroid Build Coastguard Worker       "break",
61*890232f2SAndroid Build Coastguard Worker       "case",
62*890232f2SAndroid Build Coastguard Worker       "catch",
63*890232f2SAndroid Build Coastguard Worker       "class",
64*890232f2SAndroid Build Coastguard Worker       "const",
65*890232f2SAndroid Build Coastguard Worker       "continue",
66*890232f2SAndroid Build Coastguard Worker       "debugger",
67*890232f2SAndroid Build Coastguard Worker       "default",
68*890232f2SAndroid Build Coastguard Worker       "delete",
69*890232f2SAndroid Build Coastguard Worker       "do",
70*890232f2SAndroid Build Coastguard Worker       "else",
71*890232f2SAndroid Build Coastguard Worker       "enum",
72*890232f2SAndroid Build Coastguard Worker       "export",
73*890232f2SAndroid Build Coastguard Worker       "extends",
74*890232f2SAndroid Build Coastguard Worker       "false",
75*890232f2SAndroid Build Coastguard Worker       "finally",
76*890232f2SAndroid Build Coastguard Worker       "for",
77*890232f2SAndroid Build Coastguard Worker       "function",
78*890232f2SAndroid Build Coastguard Worker       "if",
79*890232f2SAndroid Build Coastguard Worker       "import",
80*890232f2SAndroid Build Coastguard Worker       "in",
81*890232f2SAndroid Build Coastguard Worker       "instanceof",
82*890232f2SAndroid Build Coastguard Worker       "new",
83*890232f2SAndroid Build Coastguard Worker       "null",
84*890232f2SAndroid Build Coastguard Worker       "Object",
85*890232f2SAndroid Build Coastguard Worker       "return",
86*890232f2SAndroid Build Coastguard Worker       "super",
87*890232f2SAndroid Build Coastguard Worker       "switch",
88*890232f2SAndroid Build Coastguard Worker       "this",
89*890232f2SAndroid Build Coastguard Worker       "throw",
90*890232f2SAndroid Build Coastguard Worker       "true",
91*890232f2SAndroid Build Coastguard Worker       "try",
92*890232f2SAndroid Build Coastguard Worker       "typeof",
93*890232f2SAndroid Build Coastguard Worker       "var",
94*890232f2SAndroid Build Coastguard Worker       "void",
95*890232f2SAndroid Build Coastguard Worker       "while",
96*890232f2SAndroid Build Coastguard Worker       "with",
97*890232f2SAndroid Build Coastguard Worker       "as",
98*890232f2SAndroid Build Coastguard Worker       "implements",
99*890232f2SAndroid Build Coastguard Worker       "interface",
100*890232f2SAndroid Build Coastguard Worker       "let",
101*890232f2SAndroid Build Coastguard Worker       "package",
102*890232f2SAndroid Build Coastguard Worker       "private",
103*890232f2SAndroid Build Coastguard Worker       "protected",
104*890232f2SAndroid Build Coastguard Worker       "public",
105*890232f2SAndroid Build Coastguard Worker       "static",
106*890232f2SAndroid Build Coastguard Worker       "yield",
107*890232f2SAndroid Build Coastguard Worker       nullptr,
108*890232f2SAndroid Build Coastguard Worker       // clang-format on
109*890232f2SAndroid Build Coastguard Worker     };
110*890232f2SAndroid Build Coastguard Worker 
111*890232f2SAndroid Build Coastguard Worker     for (auto kw = keywords; *kw; kw++) keywords_.insert(*kw);
112*890232f2SAndroid Build Coastguard Worker   }
generate()113*890232f2SAndroid Build Coastguard Worker   bool generate() {
114*890232f2SAndroid Build Coastguard Worker     generateEnums();
115*890232f2SAndroid Build Coastguard Worker     generateStructs();
116*890232f2SAndroid Build Coastguard Worker     generateEntry();
117*890232f2SAndroid Build Coastguard Worker     return true;
118*890232f2SAndroid Build Coastguard Worker   }
119*890232f2SAndroid Build Coastguard Worker 
IncludeNamespace() const120*890232f2SAndroid Build Coastguard Worker   bool IncludeNamespace() const {
121*890232f2SAndroid Build Coastguard Worker     // When generating a single flat file and all its includes, namespaces are
122*890232f2SAndroid Build Coastguard Worker     // important to avoid type name clashes.
123*890232f2SAndroid Build Coastguard Worker     return parser_.opts.ts_flat_file && parser_.opts.generate_all;
124*890232f2SAndroid Build Coastguard Worker   }
125*890232f2SAndroid Build Coastguard Worker 
GetTypeName(const EnumDef & def,const bool=false,const bool force_ns_wrap=false)126*890232f2SAndroid Build Coastguard Worker   std::string GetTypeName(const EnumDef &def, const bool = false,
127*890232f2SAndroid Build Coastguard Worker                           const bool force_ns_wrap = false) {
128*890232f2SAndroid Build Coastguard Worker     std::string base_name = def.name;
129*890232f2SAndroid Build Coastguard Worker 
130*890232f2SAndroid Build Coastguard Worker     if (IncludeNamespace() || force_ns_wrap) {
131*890232f2SAndroid Build Coastguard Worker       base_name = WrapInNameSpace(def.defined_namespace, base_name);
132*890232f2SAndroid Build Coastguard Worker     }
133*890232f2SAndroid Build Coastguard Worker 
134*890232f2SAndroid Build Coastguard Worker     return EscapeKeyword(base_name);
135*890232f2SAndroid Build Coastguard Worker   }
136*890232f2SAndroid Build Coastguard Worker 
GetTypeName(const StructDef & def,const bool object_api=false,const bool force_ns_wrap=false)137*890232f2SAndroid Build Coastguard Worker   std::string GetTypeName(const StructDef &def, const bool object_api = false,
138*890232f2SAndroid Build Coastguard Worker                           const bool force_ns_wrap = false) {
139*890232f2SAndroid Build Coastguard Worker     std::string base_name = def.name;
140*890232f2SAndroid Build Coastguard Worker 
141*890232f2SAndroid Build Coastguard Worker     if (object_api && parser_.opts.generate_object_based_api) {
142*890232f2SAndroid Build Coastguard Worker       base_name =
143*890232f2SAndroid Build Coastguard Worker           parser_.opts.object_prefix + base_name + parser_.opts.object_suffix;
144*890232f2SAndroid Build Coastguard Worker     }
145*890232f2SAndroid Build Coastguard Worker 
146*890232f2SAndroid Build Coastguard Worker     if (IncludeNamespace() || force_ns_wrap) {
147*890232f2SAndroid Build Coastguard Worker       base_name = WrapInNameSpace(def.defined_namespace, base_name);
148*890232f2SAndroid Build Coastguard Worker     }
149*890232f2SAndroid Build Coastguard Worker 
150*890232f2SAndroid Build Coastguard Worker     return EscapeKeyword(base_name);
151*890232f2SAndroid Build Coastguard Worker   }
152*890232f2SAndroid Build Coastguard Worker 
153*890232f2SAndroid Build Coastguard Worker   // Save out the generated code for a single class while adding
154*890232f2SAndroid Build Coastguard Worker   // declaration boilerplate.
SaveType(const Definition & definition,const std::string & class_code,import_set & imports,import_set & bare_imports)155*890232f2SAndroid Build Coastguard Worker   bool SaveType(const Definition &definition, const std::string &class_code,
156*890232f2SAndroid Build Coastguard Worker                 import_set &imports, import_set &bare_imports) {
157*890232f2SAndroid Build Coastguard Worker     if (!class_code.length()) return true;
158*890232f2SAndroid Build Coastguard Worker 
159*890232f2SAndroid Build Coastguard Worker     std::string code;
160*890232f2SAndroid Build Coastguard Worker 
161*890232f2SAndroid Build Coastguard Worker     if (!parser_.opts.ts_flat_file) {
162*890232f2SAndroid Build Coastguard Worker       code += "// " + std::string(FlatBuffersGeneratedWarning()) + "\n\n";
163*890232f2SAndroid Build Coastguard Worker 
164*890232f2SAndroid Build Coastguard Worker       for (auto it = bare_imports.begin(); it != bare_imports.end(); it++) {
165*890232f2SAndroid Build Coastguard Worker         code += it->second.import_statement + "\n";
166*890232f2SAndroid Build Coastguard Worker       }
167*890232f2SAndroid Build Coastguard Worker       if (!bare_imports.empty()) code += "\n";
168*890232f2SAndroid Build Coastguard Worker 
169*890232f2SAndroid Build Coastguard Worker       for (auto it = imports.begin(); it != imports.end(); it++) {
170*890232f2SAndroid Build Coastguard Worker         if (it->second.dependency != &definition) {
171*890232f2SAndroid Build Coastguard Worker           code += it->second.import_statement + "\n";
172*890232f2SAndroid Build Coastguard Worker         }
173*890232f2SAndroid Build Coastguard Worker       }
174*890232f2SAndroid Build Coastguard Worker       if (!imports.empty()) code += "\n\n";
175*890232f2SAndroid Build Coastguard Worker     }
176*890232f2SAndroid Build Coastguard Worker 
177*890232f2SAndroid Build Coastguard Worker     code += class_code;
178*890232f2SAndroid Build Coastguard Worker 
179*890232f2SAndroid Build Coastguard Worker     if (parser_.opts.ts_flat_file) {
180*890232f2SAndroid Build Coastguard Worker       flat_file_ += code;
181*890232f2SAndroid Build Coastguard Worker       flat_file_ += "\n";
182*890232f2SAndroid Build Coastguard Worker       flat_file_definitions_.insert(&definition);
183*890232f2SAndroid Build Coastguard Worker       return true;
184*890232f2SAndroid Build Coastguard Worker     } else {
185*890232f2SAndroid Build Coastguard Worker       auto basename =
186*890232f2SAndroid Build Coastguard Worker           NamespaceDir(*definition.defined_namespace, true) +
187*890232f2SAndroid Build Coastguard Worker           ConvertCase(definition.name, Case::kDasher, Case::kUpperCamel);
188*890232f2SAndroid Build Coastguard Worker 
189*890232f2SAndroid Build Coastguard Worker       return SaveFile((basename + ".ts").c_str(), code, false);
190*890232f2SAndroid Build Coastguard Worker     }
191*890232f2SAndroid Build Coastguard Worker   }
192*890232f2SAndroid Build Coastguard Worker 
193*890232f2SAndroid Build Coastguard Worker  private:
194*890232f2SAndroid Build Coastguard Worker   std::unordered_set<std::string> keywords_;
195*890232f2SAndroid Build Coastguard Worker 
EscapeKeyword(const std::string & name) const196*890232f2SAndroid Build Coastguard Worker   std::string EscapeKeyword(const std::string &name) const {
197*890232f2SAndroid Build Coastguard Worker     return keywords_.find(name) == keywords_.end() ? name : name + "_";
198*890232f2SAndroid Build Coastguard Worker   }
199*890232f2SAndroid Build Coastguard Worker 
200*890232f2SAndroid Build Coastguard Worker   import_set imports_all_;
201*890232f2SAndroid Build Coastguard Worker 
202*890232f2SAndroid Build Coastguard Worker   // The following three members are used when generating typescript code into a
203*890232f2SAndroid Build Coastguard Worker   // single file rather than creating separate files for each type.
204*890232f2SAndroid Build Coastguard Worker 
205*890232f2SAndroid Build Coastguard Worker   // flat_file_ contains the aggregated contents of the file prior to being
206*890232f2SAndroid Build Coastguard Worker   // written to disk.
207*890232f2SAndroid Build Coastguard Worker   std::string flat_file_;
208*890232f2SAndroid Build Coastguard Worker   // flat_file_definitions_ tracks which types have been written to flat_file_.
209*890232f2SAndroid Build Coastguard Worker   std::unordered_set<const Definition *> flat_file_definitions_;
210*890232f2SAndroid Build Coastguard Worker   // This maps from import names to types to import.
211*890232f2SAndroid Build Coastguard Worker   std::map<std::string, std::map<std::string, std::string>>
212*890232f2SAndroid Build Coastguard Worker       flat_file_import_declarations_;
213*890232f2SAndroid Build Coastguard Worker   // For flat file codegen, tracks whether we need to import the flatbuffers
214*890232f2SAndroid Build Coastguard Worker   // library itself (not necessary for files that solely consist of enum
215*890232f2SAndroid Build Coastguard Worker   // definitions).
216*890232f2SAndroid Build Coastguard Worker   bool import_flatbuffers_lib_ = false;
217*890232f2SAndroid Build Coastguard Worker 
218*890232f2SAndroid Build Coastguard Worker   // Generate code for all enums.
generateEnums()219*890232f2SAndroid Build Coastguard Worker   void generateEnums() {
220*890232f2SAndroid Build Coastguard Worker     for (auto it = parser_.enums_.vec.begin(); it != parser_.enums_.vec.end();
221*890232f2SAndroid Build Coastguard Worker          ++it) {
222*890232f2SAndroid Build Coastguard Worker       import_set bare_imports;
223*890232f2SAndroid Build Coastguard Worker       import_set imports;
224*890232f2SAndroid Build Coastguard Worker       std::string enumcode;
225*890232f2SAndroid Build Coastguard Worker       auto &enum_def = **it;
226*890232f2SAndroid Build Coastguard Worker       GenEnum(enum_def, &enumcode, imports, false);
227*890232f2SAndroid Build Coastguard Worker       GenEnum(enum_def, &enumcode, imports, true);
228*890232f2SAndroid Build Coastguard Worker       SaveType(enum_def, enumcode, imports, bare_imports);
229*890232f2SAndroid Build Coastguard Worker       imports_all_.insert(imports.begin(), imports.end());
230*890232f2SAndroid Build Coastguard Worker     }
231*890232f2SAndroid Build Coastguard Worker   }
232*890232f2SAndroid Build Coastguard Worker 
233*890232f2SAndroid Build Coastguard Worker   // Generate code for all structs.
generateStructs()234*890232f2SAndroid Build Coastguard Worker   void generateStructs() {
235*890232f2SAndroid Build Coastguard Worker     for (auto it = parser_.structs_.vec.begin();
236*890232f2SAndroid Build Coastguard Worker          it != parser_.structs_.vec.end(); ++it) {
237*890232f2SAndroid Build Coastguard Worker       import_set bare_imports;
238*890232f2SAndroid Build Coastguard Worker       import_set imports;
239*890232f2SAndroid Build Coastguard Worker       AddImport(bare_imports, "* as flatbuffers", "flatbuffers");
240*890232f2SAndroid Build Coastguard Worker       import_flatbuffers_lib_ = true;
241*890232f2SAndroid Build Coastguard Worker       auto &struct_def = **it;
242*890232f2SAndroid Build Coastguard Worker       std::string declcode;
243*890232f2SAndroid Build Coastguard Worker       GenStruct(parser_, struct_def, &declcode, imports);
244*890232f2SAndroid Build Coastguard Worker       SaveType(struct_def, declcode, imports, bare_imports);
245*890232f2SAndroid Build Coastguard Worker       imports_all_.insert(imports.begin(), imports.end());
246*890232f2SAndroid Build Coastguard Worker     }
247*890232f2SAndroid Build Coastguard Worker   }
248*890232f2SAndroid Build Coastguard Worker 
249*890232f2SAndroid Build Coastguard Worker   // Generate code for a single entry point module.
generateEntry()250*890232f2SAndroid Build Coastguard Worker   void generateEntry() {
251*890232f2SAndroid Build Coastguard Worker     std::string code =
252*890232f2SAndroid Build Coastguard Worker         "// " + std::string(FlatBuffersGeneratedWarning()) + "\n\n";
253*890232f2SAndroid Build Coastguard Worker     if (parser_.opts.ts_flat_file) {
254*890232f2SAndroid Build Coastguard Worker       if (import_flatbuffers_lib_) {
255*890232f2SAndroid Build Coastguard Worker         code += "import * as flatbuffers from 'flatbuffers';\n";
256*890232f2SAndroid Build Coastguard Worker         code += "\n";
257*890232f2SAndroid Build Coastguard Worker       }
258*890232f2SAndroid Build Coastguard Worker       // Only include import statements when not generating all.
259*890232f2SAndroid Build Coastguard Worker       if (!parser_.opts.generate_all) {
260*890232f2SAndroid Build Coastguard Worker         for (const auto &it : flat_file_import_declarations_) {
261*890232f2SAndroid Build Coastguard Worker           // Note that we do end up generating an import for ourselves, which
262*890232f2SAndroid Build Coastguard Worker           // should generally be harmless.
263*890232f2SAndroid Build Coastguard Worker           // TODO: Make it so we don't generate a self-import; this will also
264*890232f2SAndroid Build Coastguard Worker           // require modifying AddImport to ensure that we don't use
265*890232f2SAndroid Build Coastguard Worker           // namespace-prefixed names anywhere...
266*890232f2SAndroid Build Coastguard Worker           std::string file = it.first;
267*890232f2SAndroid Build Coastguard Worker           if (file.empty()) { continue; }
268*890232f2SAndroid Build Coastguard Worker           std::string noext = flatbuffers::StripExtension(file);
269*890232f2SAndroid Build Coastguard Worker           std::string basename = flatbuffers::StripPath(noext);
270*890232f2SAndroid Build Coastguard Worker           std::string include_file = GeneratedFileName(
271*890232f2SAndroid Build Coastguard Worker               parser_.opts.include_prefix,
272*890232f2SAndroid Build Coastguard Worker               parser_.opts.keep_prefix ? noext : basename, parser_.opts);
273*890232f2SAndroid Build Coastguard Worker           // TODO: what is the right behavior when different include flags are
274*890232f2SAndroid Build Coastguard Worker           // specified here? Should we always be adding the "./" for a relative
275*890232f2SAndroid Build Coastguard Worker           // path or turn it off if --include-prefix is specified, or something
276*890232f2SAndroid Build Coastguard Worker           // else?
277*890232f2SAndroid Build Coastguard Worker           std::string include_name =
278*890232f2SAndroid Build Coastguard Worker               "./" + flatbuffers::StripExtension(include_file);
279*890232f2SAndroid Build Coastguard Worker           code += "import {";
280*890232f2SAndroid Build Coastguard Worker           for (const auto &pair : it.second) {
281*890232f2SAndroid Build Coastguard Worker             code += EscapeKeyword(pair.first) + " as " +
282*890232f2SAndroid Build Coastguard Worker                     EscapeKeyword(pair.second) + ", ";
283*890232f2SAndroid Build Coastguard Worker           }
284*890232f2SAndroid Build Coastguard Worker           code.resize(code.size() - 2);
285*890232f2SAndroid Build Coastguard Worker           code += "} from '" + include_name + "';\n";
286*890232f2SAndroid Build Coastguard Worker         }
287*890232f2SAndroid Build Coastguard Worker         code += "\n";
288*890232f2SAndroid Build Coastguard Worker       }
289*890232f2SAndroid Build Coastguard Worker 
290*890232f2SAndroid Build Coastguard Worker       code += flat_file_;
291*890232f2SAndroid Build Coastguard Worker       const std::string filename =
292*890232f2SAndroid Build Coastguard Worker           GeneratedFileName(path_, file_name_, parser_.opts);
293*890232f2SAndroid Build Coastguard Worker       SaveFile(filename.c_str(), code, false);
294*890232f2SAndroid Build Coastguard Worker     } else {
295*890232f2SAndroid Build Coastguard Worker       for (auto it = imports_all_.begin(); it != imports_all_.end(); it++) {
296*890232f2SAndroid Build Coastguard Worker         code += it->second.export_statement + "\n";
297*890232f2SAndroid Build Coastguard Worker       }
298*890232f2SAndroid Build Coastguard Worker       const std::string path =
299*890232f2SAndroid Build Coastguard Worker           GeneratedFileName(path_, file_name_, parser_.opts);
300*890232f2SAndroid Build Coastguard Worker       SaveFile(path.c_str(), code, false);
301*890232f2SAndroid Build Coastguard Worker     }
302*890232f2SAndroid Build Coastguard Worker   }
303*890232f2SAndroid Build Coastguard Worker 
304*890232f2SAndroid Build Coastguard Worker   // Generate a documentation comment, if available.
GenDocComment(const std::vector<std::string> & dc,std::string * code_ptr,const char * indent=nullptr)305*890232f2SAndroid Build Coastguard Worker   static void GenDocComment(const std::vector<std::string> &dc,
306*890232f2SAndroid Build Coastguard Worker                             std::string *code_ptr,
307*890232f2SAndroid Build Coastguard Worker                             const char *indent = nullptr) {
308*890232f2SAndroid Build Coastguard Worker     if (dc.empty()) {
309*890232f2SAndroid Build Coastguard Worker       // Don't output empty comment blocks with 0 lines of comment content.
310*890232f2SAndroid Build Coastguard Worker       return;
311*890232f2SAndroid Build Coastguard Worker     }
312*890232f2SAndroid Build Coastguard Worker 
313*890232f2SAndroid Build Coastguard Worker     std::string &code = *code_ptr;
314*890232f2SAndroid Build Coastguard Worker     if (indent) code += indent;
315*890232f2SAndroid Build Coastguard Worker     code += "/**\n";
316*890232f2SAndroid Build Coastguard Worker     for (auto it = dc.begin(); it != dc.end(); ++it) {
317*890232f2SAndroid Build Coastguard Worker       if (indent) code += indent;
318*890232f2SAndroid Build Coastguard Worker       code += " *" + *it + "\n";
319*890232f2SAndroid Build Coastguard Worker     }
320*890232f2SAndroid Build Coastguard Worker     if (indent) code += indent;
321*890232f2SAndroid Build Coastguard Worker     code += " */\n";
322*890232f2SAndroid Build Coastguard Worker   }
323*890232f2SAndroid Build Coastguard Worker 
GenDocComment(std::string * code_ptr)324*890232f2SAndroid Build Coastguard Worker   static void GenDocComment(std::string *code_ptr) {
325*890232f2SAndroid Build Coastguard Worker     GenDocComment(std::vector<std::string>(), code_ptr);
326*890232f2SAndroid Build Coastguard Worker   }
327*890232f2SAndroid Build Coastguard Worker 
328*890232f2SAndroid Build Coastguard Worker   // Generate an enum declaration and an enum string lookup table.
GenEnum(EnumDef & enum_def,std::string * code_ptr,import_set & imports,bool reverse)329*890232f2SAndroid Build Coastguard Worker   void GenEnum(EnumDef &enum_def, std::string *code_ptr, import_set &imports,
330*890232f2SAndroid Build Coastguard Worker                bool reverse) {
331*890232f2SAndroid Build Coastguard Worker     if (enum_def.generated) return;
332*890232f2SAndroid Build Coastguard Worker     if (reverse) return;  // FIXME.
333*890232f2SAndroid Build Coastguard Worker     std::string &code = *code_ptr;
334*890232f2SAndroid Build Coastguard Worker     GenDocComment(enum_def.doc_comment, code_ptr);
335*890232f2SAndroid Build Coastguard Worker     code += "export enum ";
336*890232f2SAndroid Build Coastguard Worker     code += GetTypeName(enum_def);
337*890232f2SAndroid Build Coastguard Worker     code += " {\n";
338*890232f2SAndroid Build Coastguard Worker     for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end(); ++it) {
339*890232f2SAndroid Build Coastguard Worker       auto &ev = **it;
340*890232f2SAndroid Build Coastguard Worker       if (!ev.doc_comment.empty()) {
341*890232f2SAndroid Build Coastguard Worker         if (it != enum_def.Vals().begin()) { code += '\n'; }
342*890232f2SAndroid Build Coastguard Worker         GenDocComment(ev.doc_comment, code_ptr, "  ");
343*890232f2SAndroid Build Coastguard Worker       }
344*890232f2SAndroid Build Coastguard Worker 
345*890232f2SAndroid Build Coastguard Worker       const std::string escaped_name = EscapeKeyword(ev.name);
346*890232f2SAndroid Build Coastguard Worker 
347*890232f2SAndroid Build Coastguard Worker       // Generate mapping between EnumName: EnumValue(int)
348*890232f2SAndroid Build Coastguard Worker       if (reverse) {
349*890232f2SAndroid Build Coastguard Worker         code += "  '" + enum_def.ToString(ev) + "'";
350*890232f2SAndroid Build Coastguard Worker         code += " = ";
351*890232f2SAndroid Build Coastguard Worker         code += "'" + escaped_name + "'";
352*890232f2SAndroid Build Coastguard Worker       } else {
353*890232f2SAndroid Build Coastguard Worker         code += "  " + escaped_name;
354*890232f2SAndroid Build Coastguard Worker         code += " = ";
355*890232f2SAndroid Build Coastguard Worker         // Unfortunately, because typescript does not support bigint enums,
356*890232f2SAndroid Build Coastguard Worker         // for 64-bit enums, we instead map the enum names to strings.
357*890232f2SAndroid Build Coastguard Worker         switch (enum_def.underlying_type.base_type) {
358*890232f2SAndroid Build Coastguard Worker           case BASE_TYPE_LONG:
359*890232f2SAndroid Build Coastguard Worker           case BASE_TYPE_ULONG: {
360*890232f2SAndroid Build Coastguard Worker             code += "'" + enum_def.ToString(ev) + "'";
361*890232f2SAndroid Build Coastguard Worker             break;
362*890232f2SAndroid Build Coastguard Worker           }
363*890232f2SAndroid Build Coastguard Worker           default: code += enum_def.ToString(ev);
364*890232f2SAndroid Build Coastguard Worker         }
365*890232f2SAndroid Build Coastguard Worker       }
366*890232f2SAndroid Build Coastguard Worker 
367*890232f2SAndroid Build Coastguard Worker       code += (it + 1) != enum_def.Vals().end() ? ",\n" : "\n";
368*890232f2SAndroid Build Coastguard Worker     }
369*890232f2SAndroid Build Coastguard Worker     code += "}";
370*890232f2SAndroid Build Coastguard Worker 
371*890232f2SAndroid Build Coastguard Worker     if (enum_def.is_union) {
372*890232f2SAndroid Build Coastguard Worker       code += GenUnionConvFunc(enum_def.underlying_type, imports);
373*890232f2SAndroid Build Coastguard Worker     }
374*890232f2SAndroid Build Coastguard Worker 
375*890232f2SAndroid Build Coastguard Worker     code += "\n";
376*890232f2SAndroid Build Coastguard Worker   }
377*890232f2SAndroid Build Coastguard Worker 
GenType(const Type & type)378*890232f2SAndroid Build Coastguard Worker   static std::string GenType(const Type &type) {
379*890232f2SAndroid Build Coastguard Worker     switch (type.base_type) {
380*890232f2SAndroid Build Coastguard Worker       case BASE_TYPE_BOOL:
381*890232f2SAndroid Build Coastguard Worker       case BASE_TYPE_CHAR: return "Int8";
382*890232f2SAndroid Build Coastguard Worker       case BASE_TYPE_UTYPE:
383*890232f2SAndroid Build Coastguard Worker       case BASE_TYPE_UCHAR: return "Uint8";
384*890232f2SAndroid Build Coastguard Worker       case BASE_TYPE_SHORT: return "Int16";
385*890232f2SAndroid Build Coastguard Worker       case BASE_TYPE_USHORT: return "Uint16";
386*890232f2SAndroid Build Coastguard Worker       case BASE_TYPE_INT: return "Int32";
387*890232f2SAndroid Build Coastguard Worker       case BASE_TYPE_UINT: return "Uint32";
388*890232f2SAndroid Build Coastguard Worker       case BASE_TYPE_LONG: return "Int64";
389*890232f2SAndroid Build Coastguard Worker       case BASE_TYPE_ULONG: return "Uint64";
390*890232f2SAndroid Build Coastguard Worker       case BASE_TYPE_FLOAT: return "Float32";
391*890232f2SAndroid Build Coastguard Worker       case BASE_TYPE_DOUBLE: return "Float64";
392*890232f2SAndroid Build Coastguard Worker       case BASE_TYPE_STRING: return "String";
393*890232f2SAndroid Build Coastguard Worker       case BASE_TYPE_VECTOR: return GenType(type.VectorType());
394*890232f2SAndroid Build Coastguard Worker       case BASE_TYPE_STRUCT: return type.struct_def->name;
395*890232f2SAndroid Build Coastguard Worker       default: return "flatbuffers.Table";
396*890232f2SAndroid Build Coastguard Worker     }
397*890232f2SAndroid Build Coastguard Worker   }
398*890232f2SAndroid Build Coastguard Worker 
GenGetter(const Type & type,const std::string & arguments)399*890232f2SAndroid Build Coastguard Worker   std::string GenGetter(const Type &type, const std::string &arguments) {
400*890232f2SAndroid Build Coastguard Worker     switch (type.base_type) {
401*890232f2SAndroid Build Coastguard Worker       case BASE_TYPE_STRING: return GenBBAccess() + ".__string" + arguments;
402*890232f2SAndroid Build Coastguard Worker       case BASE_TYPE_STRUCT: return GenBBAccess() + ".__struct" + arguments;
403*890232f2SAndroid Build Coastguard Worker       case BASE_TYPE_UNION:
404*890232f2SAndroid Build Coastguard Worker         if (!UnionHasStringType(*type.enum_def)) {
405*890232f2SAndroid Build Coastguard Worker           return GenBBAccess() + ".__union" + arguments;
406*890232f2SAndroid Build Coastguard Worker         }
407*890232f2SAndroid Build Coastguard Worker         return GenBBAccess() + ".__union_with_string" + arguments;
408*890232f2SAndroid Build Coastguard Worker       case BASE_TYPE_VECTOR: return GenGetter(type.VectorType(), arguments);
409*890232f2SAndroid Build Coastguard Worker       default: {
410*890232f2SAndroid Build Coastguard Worker         auto getter = GenBBAccess() + ".read" +
411*890232f2SAndroid Build Coastguard Worker                       ConvertCase(GenType(type), Case::kUpperCamel) + arguments;
412*890232f2SAndroid Build Coastguard Worker         if (type.base_type == BASE_TYPE_BOOL) { getter = "!!" + getter; }
413*890232f2SAndroid Build Coastguard Worker         return getter;
414*890232f2SAndroid Build Coastguard Worker       }
415*890232f2SAndroid Build Coastguard Worker     }
416*890232f2SAndroid Build Coastguard Worker   }
417*890232f2SAndroid Build Coastguard Worker 
GenBBAccess() const418*890232f2SAndroid Build Coastguard Worker   std::string GenBBAccess() const { return "this.bb!"; }
419*890232f2SAndroid Build Coastguard Worker 
GenDefaultValue(const FieldDef & field,import_set & imports)420*890232f2SAndroid Build Coastguard Worker   std::string GenDefaultValue(const FieldDef &field, import_set &imports) {
421*890232f2SAndroid Build Coastguard Worker     if (field.IsScalarOptional()) { return "null"; }
422*890232f2SAndroid Build Coastguard Worker 
423*890232f2SAndroid Build Coastguard Worker     const auto &value = field.value;
424*890232f2SAndroid Build Coastguard Worker     if (value.type.enum_def && value.type.base_type != BASE_TYPE_UNION &&
425*890232f2SAndroid Build Coastguard Worker         value.type.base_type != BASE_TYPE_VECTOR) {
426*890232f2SAndroid Build Coastguard Worker       // If the value is an enum with a 64-bit base type, we have to just
427*890232f2SAndroid Build Coastguard Worker       // return the bigint value directly since typescript does not support
428*890232f2SAndroid Build Coastguard Worker       // enums with bigint backing types.
429*890232f2SAndroid Build Coastguard Worker       switch (value.type.base_type) {
430*890232f2SAndroid Build Coastguard Worker         case BASE_TYPE_LONG:
431*890232f2SAndroid Build Coastguard Worker         case BASE_TYPE_ULONG: {
432*890232f2SAndroid Build Coastguard Worker           return "BigInt('" + value.constant + "')";
433*890232f2SAndroid Build Coastguard Worker         }
434*890232f2SAndroid Build Coastguard Worker         default: {
435*890232f2SAndroid Build Coastguard Worker           if (auto val = value.type.enum_def->FindByValue(value.constant)) {
436*890232f2SAndroid Build Coastguard Worker             return AddImport(imports, *value.type.enum_def,
437*890232f2SAndroid Build Coastguard Worker                              *value.type.enum_def)
438*890232f2SAndroid Build Coastguard Worker                        .name +
439*890232f2SAndroid Build Coastguard Worker                    "." + EscapeKeyword(val->name);
440*890232f2SAndroid Build Coastguard Worker           } else {
441*890232f2SAndroid Build Coastguard Worker             return value.constant;
442*890232f2SAndroid Build Coastguard Worker           }
443*890232f2SAndroid Build Coastguard Worker         }
444*890232f2SAndroid Build Coastguard Worker       }
445*890232f2SAndroid Build Coastguard Worker     }
446*890232f2SAndroid Build Coastguard Worker 
447*890232f2SAndroid Build Coastguard Worker     switch (value.type.base_type) {
448*890232f2SAndroid Build Coastguard Worker       case BASE_TYPE_BOOL: return value.constant == "0" ? "false" : "true";
449*890232f2SAndroid Build Coastguard Worker 
450*890232f2SAndroid Build Coastguard Worker       case BASE_TYPE_STRING:
451*890232f2SAndroid Build Coastguard Worker       case BASE_TYPE_UNION:
452*890232f2SAndroid Build Coastguard Worker       case BASE_TYPE_STRUCT: {
453*890232f2SAndroid Build Coastguard Worker         return "null";
454*890232f2SAndroid Build Coastguard Worker       }
455*890232f2SAndroid Build Coastguard Worker 
456*890232f2SAndroid Build Coastguard Worker       case BASE_TYPE_VECTOR: return "[]";
457*890232f2SAndroid Build Coastguard Worker 
458*890232f2SAndroid Build Coastguard Worker       case BASE_TYPE_LONG:
459*890232f2SAndroid Build Coastguard Worker       case BASE_TYPE_ULONG: {
460*890232f2SAndroid Build Coastguard Worker         return "BigInt('" + value.constant + "')";
461*890232f2SAndroid Build Coastguard Worker       }
462*890232f2SAndroid Build Coastguard Worker 
463*890232f2SAndroid Build Coastguard Worker       default: return value.constant;
464*890232f2SAndroid Build Coastguard Worker     }
465*890232f2SAndroid Build Coastguard Worker   }
466*890232f2SAndroid Build Coastguard Worker 
GenTypeName(import_set & imports,const Definition & owner,const Type & type,bool input,bool allowNull=false)467*890232f2SAndroid Build Coastguard Worker   std::string GenTypeName(import_set &imports, const Definition &owner,
468*890232f2SAndroid Build Coastguard Worker                           const Type &type, bool input,
469*890232f2SAndroid Build Coastguard Worker                           bool allowNull = false) {
470*890232f2SAndroid Build Coastguard Worker     if (!input) {
471*890232f2SAndroid Build Coastguard Worker       if (IsString(type) || type.base_type == BASE_TYPE_STRUCT) {
472*890232f2SAndroid Build Coastguard Worker         std::string name;
473*890232f2SAndroid Build Coastguard Worker         if (IsString(type)) {
474*890232f2SAndroid Build Coastguard Worker           name = "string|Uint8Array";
475*890232f2SAndroid Build Coastguard Worker         } else {
476*890232f2SAndroid Build Coastguard Worker           name = AddImport(imports, owner, *type.struct_def).name;
477*890232f2SAndroid Build Coastguard Worker         }
478*890232f2SAndroid Build Coastguard Worker         return allowNull ? (name + "|null") : name;
479*890232f2SAndroid Build Coastguard Worker       }
480*890232f2SAndroid Build Coastguard Worker     }
481*890232f2SAndroid Build Coastguard Worker 
482*890232f2SAndroid Build Coastguard Worker     switch (type.base_type) {
483*890232f2SAndroid Build Coastguard Worker       case BASE_TYPE_BOOL: return allowNull ? "boolean|null" : "boolean";
484*890232f2SAndroid Build Coastguard Worker       case BASE_TYPE_LONG:
485*890232f2SAndroid Build Coastguard Worker       case BASE_TYPE_ULONG: return allowNull ? "bigint|null" : "bigint";
486*890232f2SAndroid Build Coastguard Worker       default:
487*890232f2SAndroid Build Coastguard Worker         if (IsScalar(type.base_type)) {
488*890232f2SAndroid Build Coastguard Worker           if (type.enum_def) {
489*890232f2SAndroid Build Coastguard Worker             const auto enum_name =
490*890232f2SAndroid Build Coastguard Worker                 AddImport(imports, owner, *type.enum_def).name;
491*890232f2SAndroid Build Coastguard Worker             return allowNull ? (enum_name + "|null") : enum_name;
492*890232f2SAndroid Build Coastguard Worker           }
493*890232f2SAndroid Build Coastguard Worker           return allowNull ? "number|null" : "number";
494*890232f2SAndroid Build Coastguard Worker         }
495*890232f2SAndroid Build Coastguard Worker         return "flatbuffers.Offset";
496*890232f2SAndroid Build Coastguard Worker     }
497*890232f2SAndroid Build Coastguard Worker   }
498*890232f2SAndroid Build Coastguard Worker 
499*890232f2SAndroid Build Coastguard Worker   // Returns the method name for use with add/put calls.
GenWriteMethod(const Type & type)500*890232f2SAndroid Build Coastguard Worker   static std::string GenWriteMethod(const Type &type) {
501*890232f2SAndroid Build Coastguard Worker     // Forward to signed versions since unsigned versions don't exist
502*890232f2SAndroid Build Coastguard Worker     switch (type.base_type) {
503*890232f2SAndroid Build Coastguard Worker       case BASE_TYPE_UTYPE:
504*890232f2SAndroid Build Coastguard Worker       case BASE_TYPE_UCHAR: return GenWriteMethod(Type(BASE_TYPE_CHAR));
505*890232f2SAndroid Build Coastguard Worker       case BASE_TYPE_USHORT: return GenWriteMethod(Type(BASE_TYPE_SHORT));
506*890232f2SAndroid Build Coastguard Worker       case BASE_TYPE_UINT: return GenWriteMethod(Type(BASE_TYPE_INT));
507*890232f2SAndroid Build Coastguard Worker       case BASE_TYPE_ULONG: return GenWriteMethod(Type(BASE_TYPE_LONG));
508*890232f2SAndroid Build Coastguard Worker       default: break;
509*890232f2SAndroid Build Coastguard Worker     }
510*890232f2SAndroid Build Coastguard Worker 
511*890232f2SAndroid Build Coastguard Worker     return IsScalar(type.base_type)
512*890232f2SAndroid Build Coastguard Worker                ? ConvertCase(GenType(type), Case::kUpperCamel)
513*890232f2SAndroid Build Coastguard Worker                : (IsStruct(type) ? "Struct" : "Offset");
514*890232f2SAndroid Build Coastguard Worker   }
515*890232f2SAndroid Build Coastguard Worker 
MaybeAdd(T value)516*890232f2SAndroid Build Coastguard Worker   template<typename T> static std::string MaybeAdd(T value) {
517*890232f2SAndroid Build Coastguard Worker     return value != 0 ? " + " + NumToString(value) : "";
518*890232f2SAndroid Build Coastguard Worker   }
519*890232f2SAndroid Build Coastguard Worker 
MaybeScale(T value)520*890232f2SAndroid Build Coastguard Worker   template<typename T> static std::string MaybeScale(T value) {
521*890232f2SAndroid Build Coastguard Worker     return value != 1 ? " * " + NumToString(value) : "";
522*890232f2SAndroid Build Coastguard Worker   }
523*890232f2SAndroid Build Coastguard Worker 
GenStructArgs(import_set & imports,const StructDef & struct_def,std::string * arguments,const std::string & nameprefix)524*890232f2SAndroid Build Coastguard Worker   void GenStructArgs(import_set &imports, const StructDef &struct_def,
525*890232f2SAndroid Build Coastguard Worker                      std::string *arguments, const std::string &nameprefix) {
526*890232f2SAndroid Build Coastguard Worker     for (auto it = struct_def.fields.vec.begin();
527*890232f2SAndroid Build Coastguard Worker          it != struct_def.fields.vec.end(); ++it) {
528*890232f2SAndroid Build Coastguard Worker       auto &field = **it;
529*890232f2SAndroid Build Coastguard Worker       if (IsStruct(field.value.type)) {
530*890232f2SAndroid Build Coastguard Worker         // Generate arguments for a struct inside a struct. To ensure names
531*890232f2SAndroid Build Coastguard Worker         // don't clash, and to make it obvious these arguments are constructing
532*890232f2SAndroid Build Coastguard Worker         // a nested struct, prefix the name with the field name.
533*890232f2SAndroid Build Coastguard Worker         GenStructArgs(imports, *field.value.type.struct_def, arguments,
534*890232f2SAndroid Build Coastguard Worker                       nameprefix + field.name + "_");
535*890232f2SAndroid Build Coastguard Worker       } else {
536*890232f2SAndroid Build Coastguard Worker         *arguments += ", " + nameprefix + field.name + ": " +
537*890232f2SAndroid Build Coastguard Worker                       GenTypeName(imports, field, field.value.type, true,
538*890232f2SAndroid Build Coastguard Worker                                   field.IsOptional());
539*890232f2SAndroid Build Coastguard Worker       }
540*890232f2SAndroid Build Coastguard Worker     }
541*890232f2SAndroid Build Coastguard Worker   }
542*890232f2SAndroid Build Coastguard Worker 
GenStructBody(const StructDef & struct_def,std::string * body,const std::string & nameprefix)543*890232f2SAndroid Build Coastguard Worker   static void GenStructBody(const StructDef &struct_def, std::string *body,
544*890232f2SAndroid Build Coastguard Worker                             const std::string &nameprefix) {
545*890232f2SAndroid Build Coastguard Worker     *body += "  builder.prep(";
546*890232f2SAndroid Build Coastguard Worker     *body += NumToString(struct_def.minalign) + ", ";
547*890232f2SAndroid Build Coastguard Worker     *body += NumToString(struct_def.bytesize) + ");\n";
548*890232f2SAndroid Build Coastguard Worker 
549*890232f2SAndroid Build Coastguard Worker     for (auto it = struct_def.fields.vec.rbegin();
550*890232f2SAndroid Build Coastguard Worker          it != struct_def.fields.vec.rend(); ++it) {
551*890232f2SAndroid Build Coastguard Worker       auto &field = **it;
552*890232f2SAndroid Build Coastguard Worker       if (field.padding) {
553*890232f2SAndroid Build Coastguard Worker         *body += "  builder.pad(" + NumToString(field.padding) + ");\n";
554*890232f2SAndroid Build Coastguard Worker       }
555*890232f2SAndroid Build Coastguard Worker       if (IsStruct(field.value.type)) {
556*890232f2SAndroid Build Coastguard Worker         // Generate arguments for a struct inside a struct. To ensure names
557*890232f2SAndroid Build Coastguard Worker         // don't clash, and to make it obvious these arguments are constructing
558*890232f2SAndroid Build Coastguard Worker         // a nested struct, prefix the name with the field name.
559*890232f2SAndroid Build Coastguard Worker         GenStructBody(*field.value.type.struct_def, body,
560*890232f2SAndroid Build Coastguard Worker                       nameprefix + field.name + "_");
561*890232f2SAndroid Build Coastguard Worker       } else {
562*890232f2SAndroid Build Coastguard Worker         *body += "  builder.write" + GenWriteMethod(field.value.type) + "(";
563*890232f2SAndroid Build Coastguard Worker         if (field.value.type.base_type == BASE_TYPE_BOOL) { *body += "+"; }
564*890232f2SAndroid Build Coastguard Worker         *body += nameprefix + field.name + ");\n";
565*890232f2SAndroid Build Coastguard Worker       }
566*890232f2SAndroid Build Coastguard Worker     }
567*890232f2SAndroid Build Coastguard Worker   }
568*890232f2SAndroid Build Coastguard Worker 
GenerateNewExpression(const std::string & object_name)569*890232f2SAndroid Build Coastguard Worker   std::string GenerateNewExpression(const std::string &object_name) {
570*890232f2SAndroid Build Coastguard Worker     return "new " + EscapeKeyword(object_name) + "()";
571*890232f2SAndroid Build Coastguard Worker   }
572*890232f2SAndroid Build Coastguard Worker 
GenerateRootAccessor(StructDef & struct_def,std::string * code_ptr,std::string & code,const std::string & object_name,bool size_prefixed)573*890232f2SAndroid Build Coastguard Worker   void GenerateRootAccessor(StructDef &struct_def, std::string *code_ptr,
574*890232f2SAndroid Build Coastguard Worker                             std::string &code, const std::string &object_name,
575*890232f2SAndroid Build Coastguard Worker                             bool size_prefixed) {
576*890232f2SAndroid Build Coastguard Worker     if (!struct_def.fixed) {
577*890232f2SAndroid Build Coastguard Worker       GenDocComment(code_ptr);
578*890232f2SAndroid Build Coastguard Worker       std::string sizePrefixed("SizePrefixed");
579*890232f2SAndroid Build Coastguard Worker       code += "static get" + (size_prefixed ? sizePrefixed : "") + "Root" +
580*890232f2SAndroid Build Coastguard Worker               GetPrefixedName(struct_def, "As");
581*890232f2SAndroid Build Coastguard Worker       code += "(bb:flatbuffers.ByteBuffer, obj?:" + object_name +
582*890232f2SAndroid Build Coastguard Worker               "):" + object_name + " {\n";
583*890232f2SAndroid Build Coastguard Worker       if (size_prefixed) {
584*890232f2SAndroid Build Coastguard Worker         code +=
585*890232f2SAndroid Build Coastguard Worker             "  bb.setPosition(bb.position() + "
586*890232f2SAndroid Build Coastguard Worker             "flatbuffers.SIZE_PREFIX_LENGTH);\n";
587*890232f2SAndroid Build Coastguard Worker       }
588*890232f2SAndroid Build Coastguard Worker       code += "  return (obj || " + GenerateNewExpression(object_name);
589*890232f2SAndroid Build Coastguard Worker       code += ").__init(bb.readInt32(bb.position()) + bb.position(), bb);\n";
590*890232f2SAndroid Build Coastguard Worker       code += "}\n\n";
591*890232f2SAndroid Build Coastguard Worker     }
592*890232f2SAndroid Build Coastguard Worker   }
593*890232f2SAndroid Build Coastguard Worker 
GenerateFinisher(StructDef & struct_def,std::string * code_ptr,std::string & code,bool size_prefixed)594*890232f2SAndroid Build Coastguard Worker   void GenerateFinisher(StructDef &struct_def, std::string *code_ptr,
595*890232f2SAndroid Build Coastguard Worker                         std::string &code, bool size_prefixed) {
596*890232f2SAndroid Build Coastguard Worker     if (parser_.root_struct_def_ == &struct_def) {
597*890232f2SAndroid Build Coastguard Worker       std::string sizePrefixed("SizePrefixed");
598*890232f2SAndroid Build Coastguard Worker       GenDocComment(code_ptr);
599*890232f2SAndroid Build Coastguard Worker 
600*890232f2SAndroid Build Coastguard Worker       code += "static finish" + (size_prefixed ? sizePrefixed : "") +
601*890232f2SAndroid Build Coastguard Worker               GetPrefixedName(struct_def) + "Buffer";
602*890232f2SAndroid Build Coastguard Worker       code += "(builder:flatbuffers.Builder, offset:flatbuffers.Offset) {\n";
603*890232f2SAndroid Build Coastguard Worker       code += "  builder.finish(offset";
604*890232f2SAndroid Build Coastguard Worker       if (!parser_.file_identifier_.empty()) {
605*890232f2SAndroid Build Coastguard Worker         code += ", '" + parser_.file_identifier_ + "'";
606*890232f2SAndroid Build Coastguard Worker       }
607*890232f2SAndroid Build Coastguard Worker       if (size_prefixed) {
608*890232f2SAndroid Build Coastguard Worker         if (parser_.file_identifier_.empty()) { code += ", undefined"; }
609*890232f2SAndroid Build Coastguard Worker         code += ", true";
610*890232f2SAndroid Build Coastguard Worker       }
611*890232f2SAndroid Build Coastguard Worker       code += ");\n";
612*890232f2SAndroid Build Coastguard Worker       code += "}\n\n";
613*890232f2SAndroid Build Coastguard Worker     }
614*890232f2SAndroid Build Coastguard Worker   }
615*890232f2SAndroid Build Coastguard Worker 
UnionHasStringType(const EnumDef & union_enum)616*890232f2SAndroid Build Coastguard Worker   bool UnionHasStringType(const EnumDef &union_enum) {
617*890232f2SAndroid Build Coastguard Worker     return std::any_of(union_enum.Vals().begin(), union_enum.Vals().end(),
618*890232f2SAndroid Build Coastguard Worker                        [](const EnumVal *ev) {
619*890232f2SAndroid Build Coastguard Worker                          return !ev->IsZero() && IsString(ev->union_type);
620*890232f2SAndroid Build Coastguard Worker                        });
621*890232f2SAndroid Build Coastguard Worker   }
622*890232f2SAndroid Build Coastguard Worker 
GenUnionGenericTypeTS(const EnumDef & union_enum)623*890232f2SAndroid Build Coastguard Worker   std::string GenUnionGenericTypeTS(const EnumDef &union_enum) {
624*890232f2SAndroid Build Coastguard Worker     // TODO: make it work without any
625*890232f2SAndroid Build Coastguard Worker     // return std::string("T") + (UnionHasStringType(union_enum) ? "|string" :
626*890232f2SAndroid Build Coastguard Worker     // "");
627*890232f2SAndroid Build Coastguard Worker     return std::string("any") +
628*890232f2SAndroid Build Coastguard Worker            (UnionHasStringType(union_enum) ? "|string" : "");
629*890232f2SAndroid Build Coastguard Worker   }
630*890232f2SAndroid Build Coastguard Worker 
GenUnionTypeTS(const EnumDef & union_enum,import_set & imports)631*890232f2SAndroid Build Coastguard Worker   std::string GenUnionTypeTS(const EnumDef &union_enum, import_set &imports) {
632*890232f2SAndroid Build Coastguard Worker     std::string ret;
633*890232f2SAndroid Build Coastguard Worker     std::set<std::string> type_list;
634*890232f2SAndroid Build Coastguard Worker 
635*890232f2SAndroid Build Coastguard Worker     for (auto it = union_enum.Vals().begin(); it != union_enum.Vals().end();
636*890232f2SAndroid Build Coastguard Worker          ++it) {
637*890232f2SAndroid Build Coastguard Worker       const auto &ev = **it;
638*890232f2SAndroid Build Coastguard Worker       if (ev.IsZero()) { continue; }
639*890232f2SAndroid Build Coastguard Worker 
640*890232f2SAndroid Build Coastguard Worker       std::string type = "";
641*890232f2SAndroid Build Coastguard Worker       if (IsString(ev.union_type)) {
642*890232f2SAndroid Build Coastguard Worker         type = "string";  // no need to wrap string type in namespace
643*890232f2SAndroid Build Coastguard Worker       } else if (ev.union_type.base_type == BASE_TYPE_STRUCT) {
644*890232f2SAndroid Build Coastguard Worker         type = AddImport(imports, union_enum, *ev.union_type.struct_def).name;
645*890232f2SAndroid Build Coastguard Worker       } else {
646*890232f2SAndroid Build Coastguard Worker         FLATBUFFERS_ASSERT(false);
647*890232f2SAndroid Build Coastguard Worker       }
648*890232f2SAndroid Build Coastguard Worker       type_list.insert(type);
649*890232f2SAndroid Build Coastguard Worker     }
650*890232f2SAndroid Build Coastguard Worker 
651*890232f2SAndroid Build Coastguard Worker     for (auto it = type_list.begin(); it != type_list.end(); ++it) {
652*890232f2SAndroid Build Coastguard Worker       ret += *it + ((std::next(it) == type_list.end()) ? "" : "|");
653*890232f2SAndroid Build Coastguard Worker     }
654*890232f2SAndroid Build Coastguard Worker 
655*890232f2SAndroid Build Coastguard Worker     return ret;
656*890232f2SAndroid Build Coastguard Worker   }
657*890232f2SAndroid Build Coastguard Worker 
CheckIfNameClashes(const import_set & imports,const std::string & name)658*890232f2SAndroid Build Coastguard Worker   static bool CheckIfNameClashes(const import_set &imports,
659*890232f2SAndroid Build Coastguard Worker                                  const std::string &name) {
660*890232f2SAndroid Build Coastguard Worker     // TODO: this would be better as a hashset.
661*890232f2SAndroid Build Coastguard Worker     for (auto it = imports.begin(); it != imports.end(); it++) {
662*890232f2SAndroid Build Coastguard Worker       if (it->second.name == name) { return true; }
663*890232f2SAndroid Build Coastguard Worker     }
664*890232f2SAndroid Build Coastguard Worker     return false;
665*890232f2SAndroid Build Coastguard Worker   }
666*890232f2SAndroid Build Coastguard Worker 
GenSymbolExpression(const StructDef & struct_def,const bool has_name_clash,const std::string & import_name,const std::string & name,const std::string & object_name)667*890232f2SAndroid Build Coastguard Worker   std::string GenSymbolExpression(const StructDef &struct_def,
668*890232f2SAndroid Build Coastguard Worker                                   const bool has_name_clash,
669*890232f2SAndroid Build Coastguard Worker                                   const std::string &import_name,
670*890232f2SAndroid Build Coastguard Worker                                   const std::string &name,
671*890232f2SAndroid Build Coastguard Worker                                   const std::string &object_name) {
672*890232f2SAndroid Build Coastguard Worker     std::string symbols_expression;
673*890232f2SAndroid Build Coastguard Worker 
674*890232f2SAndroid Build Coastguard Worker     if (has_name_clash) {
675*890232f2SAndroid Build Coastguard Worker       // We have a name clash
676*890232f2SAndroid Build Coastguard Worker       symbols_expression += import_name + " as " + name;
677*890232f2SAndroid Build Coastguard Worker 
678*890232f2SAndroid Build Coastguard Worker       if (parser_.opts.generate_object_based_api) {
679*890232f2SAndroid Build Coastguard Worker         symbols_expression += ", " +
680*890232f2SAndroid Build Coastguard Worker                               GetTypeName(struct_def, /*object_api =*/true) +
681*890232f2SAndroid Build Coastguard Worker                               " as " + object_name;
682*890232f2SAndroid Build Coastguard Worker       }
683*890232f2SAndroid Build Coastguard Worker     } else {
684*890232f2SAndroid Build Coastguard Worker       // No name clash, use the provided name
685*890232f2SAndroid Build Coastguard Worker       symbols_expression += name;
686*890232f2SAndroid Build Coastguard Worker 
687*890232f2SAndroid Build Coastguard Worker       if (parser_.opts.generate_object_based_api) {
688*890232f2SAndroid Build Coastguard Worker         symbols_expression += ", " + object_name;
689*890232f2SAndroid Build Coastguard Worker       }
690*890232f2SAndroid Build Coastguard Worker     }
691*890232f2SAndroid Build Coastguard Worker 
692*890232f2SAndroid Build Coastguard Worker     return symbols_expression;
693*890232f2SAndroid Build Coastguard Worker   }
694*890232f2SAndroid Build Coastguard Worker 
GenSymbolExpression(const EnumDef & enum_def,const bool has_name_clash,const std::string & import_name,const std::string & name,const std::string &)695*890232f2SAndroid Build Coastguard Worker   std::string GenSymbolExpression(const EnumDef &enum_def,
696*890232f2SAndroid Build Coastguard Worker                                   const bool has_name_clash,
697*890232f2SAndroid Build Coastguard Worker                                   const std::string &import_name,
698*890232f2SAndroid Build Coastguard Worker                                   const std::string &name,
699*890232f2SAndroid Build Coastguard Worker                                   const std::string &) {
700*890232f2SAndroid Build Coastguard Worker     std::string symbols_expression;
701*890232f2SAndroid Build Coastguard Worker     if (has_name_clash) {
702*890232f2SAndroid Build Coastguard Worker       symbols_expression += import_name + " as " + name;
703*890232f2SAndroid Build Coastguard Worker     } else {
704*890232f2SAndroid Build Coastguard Worker       symbols_expression += name;
705*890232f2SAndroid Build Coastguard Worker     }
706*890232f2SAndroid Build Coastguard Worker 
707*890232f2SAndroid Build Coastguard Worker     if (enum_def.is_union) {
708*890232f2SAndroid Build Coastguard Worker       symbols_expression += ", unionTo" + name;
709*890232f2SAndroid Build Coastguard Worker       symbols_expression += ", unionListTo" + name;
710*890232f2SAndroid Build Coastguard Worker     }
711*890232f2SAndroid Build Coastguard Worker 
712*890232f2SAndroid Build Coastguard Worker     return symbols_expression;
713*890232f2SAndroid Build Coastguard Worker   }
714*890232f2SAndroid Build Coastguard Worker 
715*890232f2SAndroid Build Coastguard Worker   template<typename DefintionT>
AddImport(import_set & imports,const Definition & dependent,const DefintionT & dependency)716*890232f2SAndroid Build Coastguard Worker   ImportDefinition AddImport(import_set &imports, const Definition &dependent,
717*890232f2SAndroid Build Coastguard Worker                              const DefintionT &dependency) {
718*890232f2SAndroid Build Coastguard Worker     // The unique name of the dependency, fully qualified in its namespace.
719*890232f2SAndroid Build Coastguard Worker     const std::string unique_name = GetTypeName(
720*890232f2SAndroid Build Coastguard Worker         dependency, /*object_api = */ false, /*force_ns_wrap=*/true);
721*890232f2SAndroid Build Coastguard Worker 
722*890232f2SAndroid Build Coastguard Worker     // Look if we have already added this import and return its name if found.
723*890232f2SAndroid Build Coastguard Worker     const auto import_pair = imports.find(unique_name);
724*890232f2SAndroid Build Coastguard Worker     if (import_pair != imports.end()) { return import_pair->second; }
725*890232f2SAndroid Build Coastguard Worker 
726*890232f2SAndroid Build Coastguard Worker     // Check if this name would have a name clash with another type. Just use
727*890232f2SAndroid Build Coastguard Worker     // the "base" name (properly escaped) without any namespacing applied.
728*890232f2SAndroid Build Coastguard Worker     const std::string import_name = GetTypeName(dependency);
729*890232f2SAndroid Build Coastguard Worker     const bool has_name_clash = CheckIfNameClashes(imports, import_name);
730*890232f2SAndroid Build Coastguard Worker 
731*890232f2SAndroid Build Coastguard Worker     // If we have a name clash, use the unique name, otherwise use simple name.
732*890232f2SAndroid Build Coastguard Worker     std::string name = has_name_clash ? unique_name : import_name;
733*890232f2SAndroid Build Coastguard Worker 
734*890232f2SAndroid Build Coastguard Worker     const std::string object_name =
735*890232f2SAndroid Build Coastguard Worker         GetTypeName(dependency, /*object_api=*/true, has_name_clash);
736*890232f2SAndroid Build Coastguard Worker 
737*890232f2SAndroid Build Coastguard Worker     if (parser_.opts.ts_flat_file) {
738*890232f2SAndroid Build Coastguard Worker       // In flat-file generation, do not attempt to import things from ourselves
739*890232f2SAndroid Build Coastguard Worker       // *and* do not wrap namespaces (note that this does override the logic
740*890232f2SAndroid Build Coastguard Worker       // above, but since we force all non-self-imports to use namespace-based
741*890232f2SAndroid Build Coastguard Worker       // names in flat file generation, it's fine).
742*890232f2SAndroid Build Coastguard Worker       if (dependent.file == dependency.file) {
743*890232f2SAndroid Build Coastguard Worker         name = import_name;
744*890232f2SAndroid Build Coastguard Worker       } else {
745*890232f2SAndroid Build Coastguard Worker         const std::string file =
746*890232f2SAndroid Build Coastguard Worker             RelativeToRootPath(StripFileName(AbsolutePath(dependent.file)),
747*890232f2SAndroid Build Coastguard Worker                                dependency.file)
748*890232f2SAndroid Build Coastguard Worker                 // Strip the leading //
749*890232f2SAndroid Build Coastguard Worker                 .substr(2);
750*890232f2SAndroid Build Coastguard Worker         flat_file_import_declarations_[file][import_name] = name;
751*890232f2SAndroid Build Coastguard Worker         if (parser_.opts.generate_object_based_api &&
752*890232f2SAndroid Build Coastguard Worker             typeid(dependency) == typeid(StructDef)) {
753*890232f2SAndroid Build Coastguard Worker           flat_file_import_declarations_[file][import_name + "T"] = object_name;
754*890232f2SAndroid Build Coastguard Worker         }
755*890232f2SAndroid Build Coastguard Worker       }
756*890232f2SAndroid Build Coastguard Worker     }
757*890232f2SAndroid Build Coastguard Worker 
758*890232f2SAndroid Build Coastguard Worker     const std::string symbols_expression = GenSymbolExpression(
759*890232f2SAndroid Build Coastguard Worker         dependency, has_name_clash, import_name, name, object_name);
760*890232f2SAndroid Build Coastguard Worker 
761*890232f2SAndroid Build Coastguard Worker     std::string bare_file_path;
762*890232f2SAndroid Build Coastguard Worker     std::string rel_file_path;
763*890232f2SAndroid Build Coastguard Worker     const auto &dep_comps = dependent.defined_namespace->components;
764*890232f2SAndroid Build Coastguard Worker     for (size_t i = 0; i < dep_comps.size(); i++) {
765*890232f2SAndroid Build Coastguard Worker       rel_file_path += i == 0 ? ".." : (kPathSeparator + std::string(".."));
766*890232f2SAndroid Build Coastguard Worker     }
767*890232f2SAndroid Build Coastguard Worker     if (dep_comps.size() == 0) { rel_file_path += "."; }
768*890232f2SAndroid Build Coastguard Worker 
769*890232f2SAndroid Build Coastguard Worker     const auto &depc_comps = dependency.defined_namespace->components;
770*890232f2SAndroid Build Coastguard Worker     for (auto it = depc_comps.begin(); it != depc_comps.end(); it++) {
771*890232f2SAndroid Build Coastguard Worker       bare_file_path +=
772*890232f2SAndroid Build Coastguard Worker           kPathSeparator + ConvertCase(*it, Case::kDasher, Case::kUpperCamel);
773*890232f2SAndroid Build Coastguard Worker     }
774*890232f2SAndroid Build Coastguard Worker     bare_file_path +=
775*890232f2SAndroid Build Coastguard Worker         kPathSeparator +
776*890232f2SAndroid Build Coastguard Worker         ConvertCase(dependency.name, Case::kDasher, Case::kUpperCamel);
777*890232f2SAndroid Build Coastguard Worker     rel_file_path += bare_file_path;
778*890232f2SAndroid Build Coastguard Worker 
779*890232f2SAndroid Build Coastguard Worker     ImportDefinition import;
780*890232f2SAndroid Build Coastguard Worker     import.name = name;
781*890232f2SAndroid Build Coastguard Worker     import.object_name = object_name;
782*890232f2SAndroid Build Coastguard Worker     import.bare_file_path = bare_file_path;
783*890232f2SAndroid Build Coastguard Worker     import.rel_file_path = rel_file_path;
784*890232f2SAndroid Build Coastguard Worker     import.import_statement =
785*890232f2SAndroid Build Coastguard Worker         "import { " + symbols_expression + " } from '" + rel_file_path + "';";
786*890232f2SAndroid Build Coastguard Worker     import.export_statement =
787*890232f2SAndroid Build Coastguard Worker         "export { " + symbols_expression + " } from '." + bare_file_path + "';";
788*890232f2SAndroid Build Coastguard Worker     import.dependency = &dependency;
789*890232f2SAndroid Build Coastguard Worker     import.dependent = &dependent;
790*890232f2SAndroid Build Coastguard Worker 
791*890232f2SAndroid Build Coastguard Worker     imports.insert(std::make_pair(unique_name, import));
792*890232f2SAndroid Build Coastguard Worker 
793*890232f2SAndroid Build Coastguard Worker     return import;
794*890232f2SAndroid Build Coastguard Worker   }
795*890232f2SAndroid Build Coastguard Worker 
AddImport(import_set & imports,std::string import_name,std::string fileName)796*890232f2SAndroid Build Coastguard Worker   void AddImport(import_set &imports, std::string import_name,
797*890232f2SAndroid Build Coastguard Worker                  std::string fileName) {
798*890232f2SAndroid Build Coastguard Worker     ImportDefinition import;
799*890232f2SAndroid Build Coastguard Worker     import.name = import_name;
800*890232f2SAndroid Build Coastguard Worker     import.import_statement =
801*890232f2SAndroid Build Coastguard Worker         "import " + import_name + " from '" + fileName + "';";
802*890232f2SAndroid Build Coastguard Worker     imports.insert(std::make_pair(import_name, import));
803*890232f2SAndroid Build Coastguard Worker   }
804*890232f2SAndroid Build Coastguard Worker 
805*890232f2SAndroid Build Coastguard Worker   // Generate a TS union type based on a union's enum
GenObjApiUnionTypeTS(import_set & imports,const StructDef & dependent,const IDLOptions &,const EnumDef & union_enum)806*890232f2SAndroid Build Coastguard Worker   std::string GenObjApiUnionTypeTS(import_set &imports,
807*890232f2SAndroid Build Coastguard Worker                                    const StructDef &dependent,
808*890232f2SAndroid Build Coastguard Worker                                    const IDLOptions &,
809*890232f2SAndroid Build Coastguard Worker                                    const EnumDef &union_enum) {
810*890232f2SAndroid Build Coastguard Worker     std::string ret = "";
811*890232f2SAndroid Build Coastguard Worker     std::set<std::string> type_list;
812*890232f2SAndroid Build Coastguard Worker 
813*890232f2SAndroid Build Coastguard Worker     for (auto it = union_enum.Vals().begin(); it != union_enum.Vals().end();
814*890232f2SAndroid Build Coastguard Worker          ++it) {
815*890232f2SAndroid Build Coastguard Worker       const auto &ev = **it;
816*890232f2SAndroid Build Coastguard Worker       if (ev.IsZero()) { continue; }
817*890232f2SAndroid Build Coastguard Worker 
818*890232f2SAndroid Build Coastguard Worker       std::string type = "";
819*890232f2SAndroid Build Coastguard Worker       if (IsString(ev.union_type)) {
820*890232f2SAndroid Build Coastguard Worker         type = "string";  // no need to wrap string type in namespace
821*890232f2SAndroid Build Coastguard Worker       } else if (ev.union_type.base_type == BASE_TYPE_STRUCT) {
822*890232f2SAndroid Build Coastguard Worker         type = AddImport(imports, dependent, *ev.union_type.struct_def)
823*890232f2SAndroid Build Coastguard Worker                    .object_name;
824*890232f2SAndroid Build Coastguard Worker       } else {
825*890232f2SAndroid Build Coastguard Worker         FLATBUFFERS_ASSERT(false);
826*890232f2SAndroid Build Coastguard Worker       }
827*890232f2SAndroid Build Coastguard Worker       type_list.insert(type);
828*890232f2SAndroid Build Coastguard Worker     }
829*890232f2SAndroid Build Coastguard Worker 
830*890232f2SAndroid Build Coastguard Worker     size_t totalPrinted = 0;
831*890232f2SAndroid Build Coastguard Worker     for (auto it = type_list.begin(); it != type_list.end(); ++it) {
832*890232f2SAndroid Build Coastguard Worker       ++totalPrinted;
833*890232f2SAndroid Build Coastguard Worker       ret += *it + ((totalPrinted == type_list.size()) ? "" : "|");
834*890232f2SAndroid Build Coastguard Worker     }
835*890232f2SAndroid Build Coastguard Worker 
836*890232f2SAndroid Build Coastguard Worker     return ret;
837*890232f2SAndroid Build Coastguard Worker   }
838*890232f2SAndroid Build Coastguard Worker 
GenUnionConvFuncName(const EnumDef & enum_def)839*890232f2SAndroid Build Coastguard Worker   std::string GenUnionConvFuncName(const EnumDef &enum_def) {
840*890232f2SAndroid Build Coastguard Worker     return "unionTo" + enum_def.name;
841*890232f2SAndroid Build Coastguard Worker   }
842*890232f2SAndroid Build Coastguard Worker 
GenUnionListConvFuncName(const EnumDef & enum_def)843*890232f2SAndroid Build Coastguard Worker   std::string GenUnionListConvFuncName(const EnumDef &enum_def) {
844*890232f2SAndroid Build Coastguard Worker     return "unionListTo" + enum_def.name;
845*890232f2SAndroid Build Coastguard Worker   }
846*890232f2SAndroid Build Coastguard Worker 
GenUnionConvFunc(const Type & union_type,import_set & imports)847*890232f2SAndroid Build Coastguard Worker   std::string GenUnionConvFunc(const Type &union_type, import_set &imports) {
848*890232f2SAndroid Build Coastguard Worker     if (union_type.enum_def) {
849*890232f2SAndroid Build Coastguard Worker       const auto &enum_def = *union_type.enum_def;
850*890232f2SAndroid Build Coastguard Worker 
851*890232f2SAndroid Build Coastguard Worker       const auto valid_union_type = GenUnionTypeTS(enum_def, imports);
852*890232f2SAndroid Build Coastguard Worker       const auto valid_union_type_with_null = valid_union_type + "|null";
853*890232f2SAndroid Build Coastguard Worker 
854*890232f2SAndroid Build Coastguard Worker       auto ret = "\n\nexport function " + GenUnionConvFuncName(enum_def) +
855*890232f2SAndroid Build Coastguard Worker                  "(\n  type: " + GetTypeName(enum_def) +
856*890232f2SAndroid Build Coastguard Worker                  ",\n  accessor: (obj:" + valid_union_type + ") => " +
857*890232f2SAndroid Build Coastguard Worker                  valid_union_type_with_null +
858*890232f2SAndroid Build Coastguard Worker                  "\n): " + valid_union_type_with_null + " {\n";
859*890232f2SAndroid Build Coastguard Worker 
860*890232f2SAndroid Build Coastguard Worker       const auto enum_type = AddImport(imports, enum_def, enum_def).name;
861*890232f2SAndroid Build Coastguard Worker 
862*890232f2SAndroid Build Coastguard Worker       const auto union_enum_loop = [&](const std::string &accessor_str) {
863*890232f2SAndroid Build Coastguard Worker         ret += "  switch(" + enum_type + "[type]) {\n";
864*890232f2SAndroid Build Coastguard Worker         ret += "    case 'NONE': return null; \n";
865*890232f2SAndroid Build Coastguard Worker 
866*890232f2SAndroid Build Coastguard Worker         for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end();
867*890232f2SAndroid Build Coastguard Worker              ++it) {
868*890232f2SAndroid Build Coastguard Worker           const auto &ev = **it;
869*890232f2SAndroid Build Coastguard Worker           if (ev.IsZero()) { continue; }
870*890232f2SAndroid Build Coastguard Worker 
871*890232f2SAndroid Build Coastguard Worker           ret += "    case '" + ev.name + "': ";
872*890232f2SAndroid Build Coastguard Worker 
873*890232f2SAndroid Build Coastguard Worker           if (IsString(ev.union_type)) {
874*890232f2SAndroid Build Coastguard Worker             ret += "return " + accessor_str + "'') as string;";
875*890232f2SAndroid Build Coastguard Worker           } else if (ev.union_type.base_type == BASE_TYPE_STRUCT) {
876*890232f2SAndroid Build Coastguard Worker             const auto type =
877*890232f2SAndroid Build Coastguard Worker                 AddImport(imports, enum_def, *ev.union_type.struct_def).name;
878*890232f2SAndroid Build Coastguard Worker             ret += "return " + accessor_str + "new " + type + "())! as " +
879*890232f2SAndroid Build Coastguard Worker                    type + ";";
880*890232f2SAndroid Build Coastguard Worker           } else {
881*890232f2SAndroid Build Coastguard Worker             FLATBUFFERS_ASSERT(false);
882*890232f2SAndroid Build Coastguard Worker           }
883*890232f2SAndroid Build Coastguard Worker           ret += "\n";
884*890232f2SAndroid Build Coastguard Worker         }
885*890232f2SAndroid Build Coastguard Worker 
886*890232f2SAndroid Build Coastguard Worker         ret += "    default: return null;\n";
887*890232f2SAndroid Build Coastguard Worker         ret += "  }\n";
888*890232f2SAndroid Build Coastguard Worker       };
889*890232f2SAndroid Build Coastguard Worker 
890*890232f2SAndroid Build Coastguard Worker       union_enum_loop("accessor(");
891*890232f2SAndroid Build Coastguard Worker       ret += "}";
892*890232f2SAndroid Build Coastguard Worker 
893*890232f2SAndroid Build Coastguard Worker       ret += "\n\nexport function " + GenUnionListConvFuncName(enum_def) +
894*890232f2SAndroid Build Coastguard Worker              "(\n  type: " + GetTypeName(enum_def) +
895*890232f2SAndroid Build Coastguard Worker              ", \n  accessor: (index: number, obj:" + valid_union_type +
896*890232f2SAndroid Build Coastguard Worker              ") => " + valid_union_type_with_null +
897*890232f2SAndroid Build Coastguard Worker              ", \n  index: number\n): " + valid_union_type_with_null + " {\n";
898*890232f2SAndroid Build Coastguard Worker       union_enum_loop("accessor(index, ");
899*890232f2SAndroid Build Coastguard Worker       ret += "}";
900*890232f2SAndroid Build Coastguard Worker 
901*890232f2SAndroid Build Coastguard Worker       return ret;
902*890232f2SAndroid Build Coastguard Worker     }
903*890232f2SAndroid Build Coastguard Worker     FLATBUFFERS_ASSERT(0);
904*890232f2SAndroid Build Coastguard Worker     return "";
905*890232f2SAndroid Build Coastguard Worker   }
906*890232f2SAndroid Build Coastguard Worker 
907*890232f2SAndroid Build Coastguard Worker   // Used for generating a short function that returns the correct class
908*890232f2SAndroid Build Coastguard Worker   // based on union enum type. Assume the context is inside the non object api
909*890232f2SAndroid Build Coastguard Worker   // type
GenUnionValTS(import_set & imports,const StructDef & dependent,const std::string & field_name,const Type & union_type,const bool is_array=false)910*890232f2SAndroid Build Coastguard Worker   std::string GenUnionValTS(import_set &imports, const StructDef &dependent,
911*890232f2SAndroid Build Coastguard Worker                             const std::string &field_name,
912*890232f2SAndroid Build Coastguard Worker                             const Type &union_type,
913*890232f2SAndroid Build Coastguard Worker                             const bool is_array = false) {
914*890232f2SAndroid Build Coastguard Worker     if (union_type.enum_def) {
915*890232f2SAndroid Build Coastguard Worker       const auto &enum_def = *union_type.enum_def;
916*890232f2SAndroid Build Coastguard Worker       const auto enum_type = AddImport(imports, dependent, enum_def).name;
917*890232f2SAndroid Build Coastguard Worker       const std::string union_accessor = "this." + field_name;
918*890232f2SAndroid Build Coastguard Worker 
919*890232f2SAndroid Build Coastguard Worker       const auto union_has_string = UnionHasStringType(enum_def);
920*890232f2SAndroid Build Coastguard Worker       const auto field_binded_method = "this." + field_name + ".bind(this)";
921*890232f2SAndroid Build Coastguard Worker 
922*890232f2SAndroid Build Coastguard Worker       std::string ret;
923*890232f2SAndroid Build Coastguard Worker 
924*890232f2SAndroid Build Coastguard Worker       if (!is_array) {
925*890232f2SAndroid Build Coastguard Worker         const auto conversion_function = GenUnionConvFuncName(enum_def);
926*890232f2SAndroid Build Coastguard Worker         const auto target_enum = "this." + field_name + "Type()";
927*890232f2SAndroid Build Coastguard Worker 
928*890232f2SAndroid Build Coastguard Worker         ret = "(() => {\n";
929*890232f2SAndroid Build Coastguard Worker         ret += "      let temp = " + conversion_function + "(" + target_enum +
930*890232f2SAndroid Build Coastguard Worker                ", " + field_binded_method + ");\n";
931*890232f2SAndroid Build Coastguard Worker         ret += "      if(temp === null) { return null; }\n";
932*890232f2SAndroid Build Coastguard Worker         ret += union_has_string
933*890232f2SAndroid Build Coastguard Worker                    ? "      if(typeof temp === 'string') { return temp; }\n"
934*890232f2SAndroid Build Coastguard Worker                    : "";
935*890232f2SAndroid Build Coastguard Worker         ret += "      return temp.unpack()\n";
936*890232f2SAndroid Build Coastguard Worker         ret += "  })()";
937*890232f2SAndroid Build Coastguard Worker       } else {
938*890232f2SAndroid Build Coastguard Worker         const auto conversion_function = GenUnionListConvFuncName(enum_def);
939*890232f2SAndroid Build Coastguard Worker         const auto target_enum_accesor = "this." + field_name + "Type";
940*890232f2SAndroid Build Coastguard Worker         const auto target_enum_length = target_enum_accesor + "Length()";
941*890232f2SAndroid Build Coastguard Worker 
942*890232f2SAndroid Build Coastguard Worker         ret = "(() => {\n";
943*890232f2SAndroid Build Coastguard Worker         ret += "    let ret = [];\n";
944*890232f2SAndroid Build Coastguard Worker         ret += "    for(let targetEnumIndex = 0; targetEnumIndex < " +
945*890232f2SAndroid Build Coastguard Worker                target_enum_length +
946*890232f2SAndroid Build Coastguard Worker                "; "
947*890232f2SAndroid Build Coastguard Worker                "++targetEnumIndex) {\n";
948*890232f2SAndroid Build Coastguard Worker         ret += "      let targetEnum = " + target_enum_accesor +
949*890232f2SAndroid Build Coastguard Worker                "(targetEnumIndex);\n";
950*890232f2SAndroid Build Coastguard Worker         ret += "      if(targetEnum === null || " + enum_type +
951*890232f2SAndroid Build Coastguard Worker                "[targetEnum!] === 'NONE') { "
952*890232f2SAndroid Build Coastguard Worker                "continue; }\n\n";
953*890232f2SAndroid Build Coastguard Worker         ret += "      let temp = " + conversion_function + "(targetEnum, " +
954*890232f2SAndroid Build Coastguard Worker                field_binded_method + ", targetEnumIndex);\n";
955*890232f2SAndroid Build Coastguard Worker         ret += "      if(temp === null) { continue; }\n";
956*890232f2SAndroid Build Coastguard Worker         ret += union_has_string ? "      if(typeof temp === 'string') { "
957*890232f2SAndroid Build Coastguard Worker                                   "ret.push(temp); continue; }\n"
958*890232f2SAndroid Build Coastguard Worker                                 : "";
959*890232f2SAndroid Build Coastguard Worker         ret += "      ret.push(temp.unpack());\n";
960*890232f2SAndroid Build Coastguard Worker         ret += "    }\n";
961*890232f2SAndroid Build Coastguard Worker         ret += "    return ret;\n";
962*890232f2SAndroid Build Coastguard Worker         ret += "  })()";
963*890232f2SAndroid Build Coastguard Worker       }
964*890232f2SAndroid Build Coastguard Worker 
965*890232f2SAndroid Build Coastguard Worker       return ret;
966*890232f2SAndroid Build Coastguard Worker     }
967*890232f2SAndroid Build Coastguard Worker 
968*890232f2SAndroid Build Coastguard Worker     FLATBUFFERS_ASSERT(0);
969*890232f2SAndroid Build Coastguard Worker     return "";
970*890232f2SAndroid Build Coastguard Worker   }
971*890232f2SAndroid Build Coastguard Worker 
GenNullCheckConditional(const std::string & nullCheckVar,const std::string & trueVal,const std::string & falseVal="null")972*890232f2SAndroid Build Coastguard Worker   static std::string GenNullCheckConditional(
973*890232f2SAndroid Build Coastguard Worker       const std::string &nullCheckVar, const std::string &trueVal,
974*890232f2SAndroid Build Coastguard Worker       const std::string &falseVal = "null") {
975*890232f2SAndroid Build Coastguard Worker     return "(" + nullCheckVar + " !== null ? " + trueVal + " : " + falseVal +
976*890232f2SAndroid Build Coastguard Worker            ")";
977*890232f2SAndroid Build Coastguard Worker   }
978*890232f2SAndroid Build Coastguard Worker 
GenStructMemberValueTS(const StructDef & struct_def,const std::string & prefix,const std::string & delimiter,const bool nullCheck=true)979*890232f2SAndroid Build Coastguard Worker   std::string GenStructMemberValueTS(const StructDef &struct_def,
980*890232f2SAndroid Build Coastguard Worker                                      const std::string &prefix,
981*890232f2SAndroid Build Coastguard Worker                                      const std::string &delimiter,
982*890232f2SAndroid Build Coastguard Worker                                      const bool nullCheck = true) {
983*890232f2SAndroid Build Coastguard Worker     std::string ret;
984*890232f2SAndroid Build Coastguard Worker     for (auto it = struct_def.fields.vec.begin();
985*890232f2SAndroid Build Coastguard Worker          it != struct_def.fields.vec.end(); ++it) {
986*890232f2SAndroid Build Coastguard Worker       auto &field = **it;
987*890232f2SAndroid Build Coastguard Worker 
988*890232f2SAndroid Build Coastguard Worker       auto curr_member_accessor =
989*890232f2SAndroid Build Coastguard Worker           prefix + "." + ConvertCase(field.name, Case::kLowerCamel);
990*890232f2SAndroid Build Coastguard Worker       if (prefix != "this") {
991*890232f2SAndroid Build Coastguard Worker         curr_member_accessor =
992*890232f2SAndroid Build Coastguard Worker             prefix + "?." + ConvertCase(field.name, Case::kLowerCamel);
993*890232f2SAndroid Build Coastguard Worker       }
994*890232f2SAndroid Build Coastguard Worker       if (IsStruct(field.value.type)) {
995*890232f2SAndroid Build Coastguard Worker         ret += GenStructMemberValueTS(*field.value.type.struct_def,
996*890232f2SAndroid Build Coastguard Worker                                       curr_member_accessor, delimiter);
997*890232f2SAndroid Build Coastguard Worker       } else {
998*890232f2SAndroid Build Coastguard Worker         if (nullCheck) {
999*890232f2SAndroid Build Coastguard Worker           std::string nullValue = "0";
1000*890232f2SAndroid Build Coastguard Worker           if (field.value.type.base_type == BASE_TYPE_BOOL) {
1001*890232f2SAndroid Build Coastguard Worker             nullValue = "false";
1002*890232f2SAndroid Build Coastguard Worker           }
1003*890232f2SAndroid Build Coastguard Worker           ret += "(" + curr_member_accessor + " ?? " + nullValue + ")";
1004*890232f2SAndroid Build Coastguard Worker         } else {
1005*890232f2SAndroid Build Coastguard Worker           ret += curr_member_accessor;
1006*890232f2SAndroid Build Coastguard Worker         }
1007*890232f2SAndroid Build Coastguard Worker       }
1008*890232f2SAndroid Build Coastguard Worker 
1009*890232f2SAndroid Build Coastguard Worker       if (std::next(it) != struct_def.fields.vec.end()) { ret += delimiter; }
1010*890232f2SAndroid Build Coastguard Worker     }
1011*890232f2SAndroid Build Coastguard Worker 
1012*890232f2SAndroid Build Coastguard Worker     return ret;
1013*890232f2SAndroid Build Coastguard Worker   }
1014*890232f2SAndroid Build Coastguard Worker 
GenObjApi(const Parser & parser,StructDef & struct_def,std::string & obj_api_unpack_func,std::string & obj_api_class,import_set & imports)1015*890232f2SAndroid Build Coastguard Worker   void GenObjApi(const Parser &parser, StructDef &struct_def,
1016*890232f2SAndroid Build Coastguard Worker                  std::string &obj_api_unpack_func, std::string &obj_api_class,
1017*890232f2SAndroid Build Coastguard Worker                  import_set &imports) {
1018*890232f2SAndroid Build Coastguard Worker     const auto class_name = GetTypeName(struct_def, /*object_api=*/true);
1019*890232f2SAndroid Build Coastguard Worker 
1020*890232f2SAndroid Build Coastguard Worker     std::string unpack_func = "\nunpack(): " + class_name +
1021*890232f2SAndroid Build Coastguard Worker                               " {\n  return new " + class_name + "(" +
1022*890232f2SAndroid Build Coastguard Worker                               (struct_def.fields.vec.empty() ? "" : "\n");
1023*890232f2SAndroid Build Coastguard Worker     std::string unpack_to_func = "\nunpackTo(_o: " + class_name + "): void {" +
1024*890232f2SAndroid Build Coastguard Worker                                  +(struct_def.fields.vec.empty() ? "" : "\n");
1025*890232f2SAndroid Build Coastguard Worker 
1026*890232f2SAndroid Build Coastguard Worker     std::string constructor_func = "constructor(";
1027*890232f2SAndroid Build Coastguard Worker     constructor_func += (struct_def.fields.vec.empty() ? "" : "\n");
1028*890232f2SAndroid Build Coastguard Worker 
1029*890232f2SAndroid Build Coastguard Worker     const auto has_create =
1030*890232f2SAndroid Build Coastguard Worker         struct_def.fixed || CanCreateFactoryMethod(struct_def);
1031*890232f2SAndroid Build Coastguard Worker 
1032*890232f2SAndroid Build Coastguard Worker     std::string pack_func_prototype =
1033*890232f2SAndroid Build Coastguard Worker         "\npack(builder:flatbuffers.Builder): flatbuffers.Offset {\n";
1034*890232f2SAndroid Build Coastguard Worker 
1035*890232f2SAndroid Build Coastguard Worker     std::string pack_func_offset_decl;
1036*890232f2SAndroid Build Coastguard Worker     std::string pack_func_create_call;
1037*890232f2SAndroid Build Coastguard Worker 
1038*890232f2SAndroid Build Coastguard Worker     const auto struct_name = AddImport(imports, struct_def, struct_def).name;
1039*890232f2SAndroid Build Coastguard Worker 
1040*890232f2SAndroid Build Coastguard Worker     if (has_create) {
1041*890232f2SAndroid Build Coastguard Worker       pack_func_create_call = "  return " + struct_name + ".create" +
1042*890232f2SAndroid Build Coastguard Worker                               GetPrefixedName(struct_def) + "(builder" +
1043*890232f2SAndroid Build Coastguard Worker                               (struct_def.fields.vec.empty() ? "" : ",\n    ");
1044*890232f2SAndroid Build Coastguard Worker     } else {
1045*890232f2SAndroid Build Coastguard Worker       pack_func_create_call = "  " + struct_name + ".start" +
1046*890232f2SAndroid Build Coastguard Worker                               GetPrefixedName(struct_def) + "(builder);\n";
1047*890232f2SAndroid Build Coastguard Worker     }
1048*890232f2SAndroid Build Coastguard Worker 
1049*890232f2SAndroid Build Coastguard Worker     if (struct_def.fixed) {
1050*890232f2SAndroid Build Coastguard Worker       // when packing struct, nested struct's members instead of the struct's
1051*890232f2SAndroid Build Coastguard Worker       // offset are used
1052*890232f2SAndroid Build Coastguard Worker       pack_func_create_call +=
1053*890232f2SAndroid Build Coastguard Worker           GenStructMemberValueTS(struct_def, "this", ",\n    ", false) + "\n  ";
1054*890232f2SAndroid Build Coastguard Worker     }
1055*890232f2SAndroid Build Coastguard Worker 
1056*890232f2SAndroid Build Coastguard Worker     for (auto it = struct_def.fields.vec.begin();
1057*890232f2SAndroid Build Coastguard Worker          it != struct_def.fields.vec.end(); ++it) {
1058*890232f2SAndroid Build Coastguard Worker       auto &field = **it;
1059*890232f2SAndroid Build Coastguard Worker       if (field.deprecated) continue;
1060*890232f2SAndroid Build Coastguard Worker 
1061*890232f2SAndroid Build Coastguard Worker       const auto field_name = ConvertCase(field.name, Case::kLowerCamel);
1062*890232f2SAndroid Build Coastguard Worker       const auto field_name_escaped = EscapeKeyword(field_name);
1063*890232f2SAndroid Build Coastguard Worker       const std::string field_binded_method =
1064*890232f2SAndroid Build Coastguard Worker           "this." + field_name + ".bind(this)";
1065*890232f2SAndroid Build Coastguard Worker 
1066*890232f2SAndroid Build Coastguard Worker       std::string field_val;
1067*890232f2SAndroid Build Coastguard Worker       std::string field_type;
1068*890232f2SAndroid Build Coastguard Worker       // a string that declares a variable containing the
1069*890232f2SAndroid Build Coastguard Worker       // offset for things that can't be generated inline
1070*890232f2SAndroid Build Coastguard Worker       // empty otw
1071*890232f2SAndroid Build Coastguard Worker       std::string field_offset_decl;
1072*890232f2SAndroid Build Coastguard Worker       // a string that contains values for things that can be created inline or
1073*890232f2SAndroid Build Coastguard Worker       // the variable name from field_offset_decl
1074*890232f2SAndroid Build Coastguard Worker       std::string field_offset_val;
1075*890232f2SAndroid Build Coastguard Worker       const auto field_default_val = GenDefaultValue(field, imports);
1076*890232f2SAndroid Build Coastguard Worker 
1077*890232f2SAndroid Build Coastguard Worker       // Emit a scalar field
1078*890232f2SAndroid Build Coastguard Worker       const auto is_string = IsString(field.value.type);
1079*890232f2SAndroid Build Coastguard Worker       if (IsScalar(field.value.type.base_type) || is_string) {
1080*890232f2SAndroid Build Coastguard Worker         const auto has_null_default = is_string || HasNullDefault(field);
1081*890232f2SAndroid Build Coastguard Worker 
1082*890232f2SAndroid Build Coastguard Worker         field_type += GenTypeName(imports, field, field.value.type, false,
1083*890232f2SAndroid Build Coastguard Worker                                   has_null_default);
1084*890232f2SAndroid Build Coastguard Worker         field_val = "this." + field_name + "()";
1085*890232f2SAndroid Build Coastguard Worker 
1086*890232f2SAndroid Build Coastguard Worker         if (field.value.type.base_type != BASE_TYPE_STRING) {
1087*890232f2SAndroid Build Coastguard Worker           field_offset_val = "this." + field_name_escaped;
1088*890232f2SAndroid Build Coastguard Worker         } else {
1089*890232f2SAndroid Build Coastguard Worker           field_offset_decl = GenNullCheckConditional(
1090*890232f2SAndroid Build Coastguard Worker               "this." + field_name_escaped,
1091*890232f2SAndroid Build Coastguard Worker               "builder.createString(this." + field_name_escaped + "!)", "0");
1092*890232f2SAndroid Build Coastguard Worker         }
1093*890232f2SAndroid Build Coastguard Worker       }
1094*890232f2SAndroid Build Coastguard Worker 
1095*890232f2SAndroid Build Coastguard Worker       // Emit an object field
1096*890232f2SAndroid Build Coastguard Worker       else {
1097*890232f2SAndroid Build Coastguard Worker         auto is_vector = false;
1098*890232f2SAndroid Build Coastguard Worker         switch (field.value.type.base_type) {
1099*890232f2SAndroid Build Coastguard Worker           case BASE_TYPE_STRUCT: {
1100*890232f2SAndroid Build Coastguard Worker             const auto &sd = *field.value.type.struct_def;
1101*890232f2SAndroid Build Coastguard Worker             field_type += AddImport(imports, struct_def, sd).object_name;
1102*890232f2SAndroid Build Coastguard Worker 
1103*890232f2SAndroid Build Coastguard Worker             const std::string field_accessor = "this." + field_name + "()";
1104*890232f2SAndroid Build Coastguard Worker             field_val = GenNullCheckConditional(field_accessor,
1105*890232f2SAndroid Build Coastguard Worker                                                 field_accessor + "!.unpack()");
1106*890232f2SAndroid Build Coastguard Worker             auto packing = GenNullCheckConditional(
1107*890232f2SAndroid Build Coastguard Worker                 "this." + field_name_escaped,
1108*890232f2SAndroid Build Coastguard Worker                 "this." + field_name_escaped + "!.pack(builder)", "0");
1109*890232f2SAndroid Build Coastguard Worker 
1110*890232f2SAndroid Build Coastguard Worker             if (sd.fixed) {
1111*890232f2SAndroid Build Coastguard Worker               field_offset_val = std::move(packing);
1112*890232f2SAndroid Build Coastguard Worker             } else {
1113*890232f2SAndroid Build Coastguard Worker               field_offset_decl = std::move(packing);
1114*890232f2SAndroid Build Coastguard Worker             }
1115*890232f2SAndroid Build Coastguard Worker 
1116*890232f2SAndroid Build Coastguard Worker             break;
1117*890232f2SAndroid Build Coastguard Worker           }
1118*890232f2SAndroid Build Coastguard Worker 
1119*890232f2SAndroid Build Coastguard Worker           case BASE_TYPE_VECTOR: {
1120*890232f2SAndroid Build Coastguard Worker             auto vectortype = field.value.type.VectorType();
1121*890232f2SAndroid Build Coastguard Worker             auto vectortypename =
1122*890232f2SAndroid Build Coastguard Worker                 GenTypeName(imports, struct_def, vectortype, false);
1123*890232f2SAndroid Build Coastguard Worker             is_vector = true;
1124*890232f2SAndroid Build Coastguard Worker 
1125*890232f2SAndroid Build Coastguard Worker             field_type = "(";
1126*890232f2SAndroid Build Coastguard Worker 
1127*890232f2SAndroid Build Coastguard Worker             switch (vectortype.base_type) {
1128*890232f2SAndroid Build Coastguard Worker               case BASE_TYPE_STRUCT: {
1129*890232f2SAndroid Build Coastguard Worker                 const auto &sd = *field.value.type.struct_def;
1130*890232f2SAndroid Build Coastguard Worker                 field_type += GetTypeName(sd, /*object_api=*/true);
1131*890232f2SAndroid Build Coastguard Worker                 ;
1132*890232f2SAndroid Build Coastguard Worker                 field_type += ")[]";
1133*890232f2SAndroid Build Coastguard Worker 
1134*890232f2SAndroid Build Coastguard Worker                 field_val = GenBBAccess() + ".createObjList(" +
1135*890232f2SAndroid Build Coastguard Worker                             field_binded_method + ", this." + field_name +
1136*890232f2SAndroid Build Coastguard Worker                             "Length())";
1137*890232f2SAndroid Build Coastguard Worker 
1138*890232f2SAndroid Build Coastguard Worker                 if (sd.fixed) {
1139*890232f2SAndroid Build Coastguard Worker                   field_offset_decl =
1140*890232f2SAndroid Build Coastguard Worker                       "builder.createStructOffsetList(this." +
1141*890232f2SAndroid Build Coastguard Worker                       field_name_escaped + ", " +
1142*890232f2SAndroid Build Coastguard Worker                       AddImport(imports, struct_def, struct_def).name +
1143*890232f2SAndroid Build Coastguard Worker                       ".start" + ConvertCase(field_name, Case::kUpperCamel) +
1144*890232f2SAndroid Build Coastguard Worker                       "Vector)";
1145*890232f2SAndroid Build Coastguard Worker                 } else {
1146*890232f2SAndroid Build Coastguard Worker                   field_offset_decl =
1147*890232f2SAndroid Build Coastguard Worker                       AddImport(imports, struct_def, struct_def).name +
1148*890232f2SAndroid Build Coastguard Worker                       ".create" + ConvertCase(field_name, Case::kUpperCamel) +
1149*890232f2SAndroid Build Coastguard Worker                       "Vector(builder, builder.createObjectOffsetList(" +
1150*890232f2SAndroid Build Coastguard Worker                       "this." + field_name_escaped + "))";
1151*890232f2SAndroid Build Coastguard Worker                 }
1152*890232f2SAndroid Build Coastguard Worker 
1153*890232f2SAndroid Build Coastguard Worker                 break;
1154*890232f2SAndroid Build Coastguard Worker               }
1155*890232f2SAndroid Build Coastguard Worker 
1156*890232f2SAndroid Build Coastguard Worker               case BASE_TYPE_STRING: {
1157*890232f2SAndroid Build Coastguard Worker                 field_type += "string)[]";
1158*890232f2SAndroid Build Coastguard Worker                 field_val = GenBBAccess() + ".createScalarList(" +
1159*890232f2SAndroid Build Coastguard Worker                             field_binded_method + ", this." + field_name +
1160*890232f2SAndroid Build Coastguard Worker                             "Length())";
1161*890232f2SAndroid Build Coastguard Worker                 field_offset_decl =
1162*890232f2SAndroid Build Coastguard Worker                     AddImport(imports, struct_def, struct_def).name +
1163*890232f2SAndroid Build Coastguard Worker                     ".create" + ConvertCase(field_name, Case::kUpperCamel) +
1164*890232f2SAndroid Build Coastguard Worker                     "Vector(builder, builder.createObjectOffsetList(" +
1165*890232f2SAndroid Build Coastguard Worker                     "this." + field_name_escaped + "))";
1166*890232f2SAndroid Build Coastguard Worker                 break;
1167*890232f2SAndroid Build Coastguard Worker               }
1168*890232f2SAndroid Build Coastguard Worker 
1169*890232f2SAndroid Build Coastguard Worker               case BASE_TYPE_UNION: {
1170*890232f2SAndroid Build Coastguard Worker                 field_type += GenObjApiUnionTypeTS(
1171*890232f2SAndroid Build Coastguard Worker                     imports, struct_def, parser.opts, *(vectortype.enum_def));
1172*890232f2SAndroid Build Coastguard Worker                 field_type += ")[]";
1173*890232f2SAndroid Build Coastguard Worker                 field_val = GenUnionValTS(imports, struct_def, field_name,
1174*890232f2SAndroid Build Coastguard Worker                                           vectortype, true);
1175*890232f2SAndroid Build Coastguard Worker 
1176*890232f2SAndroid Build Coastguard Worker                 field_offset_decl =
1177*890232f2SAndroid Build Coastguard Worker                     AddImport(imports, struct_def, struct_def).name +
1178*890232f2SAndroid Build Coastguard Worker                     ".create" + ConvertCase(field_name, Case::kUpperCamel) +
1179*890232f2SAndroid Build Coastguard Worker                     "Vector(builder, builder.createObjectOffsetList(" +
1180*890232f2SAndroid Build Coastguard Worker                     "this." + field_name_escaped + "))";
1181*890232f2SAndroid Build Coastguard Worker 
1182*890232f2SAndroid Build Coastguard Worker                 break;
1183*890232f2SAndroid Build Coastguard Worker               }
1184*890232f2SAndroid Build Coastguard Worker               default: {
1185*890232f2SAndroid Build Coastguard Worker                 if (vectortype.enum_def) {
1186*890232f2SAndroid Build Coastguard Worker                   field_type += GenTypeName(imports, struct_def, vectortype,
1187*890232f2SAndroid Build Coastguard Worker                                             false, HasNullDefault(field));
1188*890232f2SAndroid Build Coastguard Worker                 } else {
1189*890232f2SAndroid Build Coastguard Worker                   field_type += vectortypename;
1190*890232f2SAndroid Build Coastguard Worker                 }
1191*890232f2SAndroid Build Coastguard Worker                 field_type += ")[]";
1192*890232f2SAndroid Build Coastguard Worker                 field_val = GenBBAccess() + ".createScalarList(" +
1193*890232f2SAndroid Build Coastguard Worker                             field_binded_method + ", this." + field_name +
1194*890232f2SAndroid Build Coastguard Worker                             "Length())";
1195*890232f2SAndroid Build Coastguard Worker 
1196*890232f2SAndroid Build Coastguard Worker                 field_offset_decl =
1197*890232f2SAndroid Build Coastguard Worker                     AddImport(imports, struct_def, struct_def).name +
1198*890232f2SAndroid Build Coastguard Worker                     ".create" + ConvertCase(field_name, Case::kUpperCamel) +
1199*890232f2SAndroid Build Coastguard Worker                     "Vector(builder, this." + field_name_escaped + ")";
1200*890232f2SAndroid Build Coastguard Worker 
1201*890232f2SAndroid Build Coastguard Worker                 break;
1202*890232f2SAndroid Build Coastguard Worker               }
1203*890232f2SAndroid Build Coastguard Worker             }
1204*890232f2SAndroid Build Coastguard Worker 
1205*890232f2SAndroid Build Coastguard Worker             break;
1206*890232f2SAndroid Build Coastguard Worker           }
1207*890232f2SAndroid Build Coastguard Worker 
1208*890232f2SAndroid Build Coastguard Worker           case BASE_TYPE_UNION: {
1209*890232f2SAndroid Build Coastguard Worker             field_type += GenObjApiUnionTypeTS(imports, struct_def, parser.opts,
1210*890232f2SAndroid Build Coastguard Worker                                                *(field.value.type.enum_def));
1211*890232f2SAndroid Build Coastguard Worker 
1212*890232f2SAndroid Build Coastguard Worker             field_val = GenUnionValTS(imports, struct_def, field_name,
1213*890232f2SAndroid Build Coastguard Worker                                       field.value.type);
1214*890232f2SAndroid Build Coastguard Worker             field_offset_decl =
1215*890232f2SAndroid Build Coastguard Worker                 "builder.createObjectOffset(this." + field_name_escaped + ")";
1216*890232f2SAndroid Build Coastguard Worker             break;
1217*890232f2SAndroid Build Coastguard Worker           }
1218*890232f2SAndroid Build Coastguard Worker 
1219*890232f2SAndroid Build Coastguard Worker           default: FLATBUFFERS_ASSERT(0); break;
1220*890232f2SAndroid Build Coastguard Worker         }
1221*890232f2SAndroid Build Coastguard Worker 
1222*890232f2SAndroid Build Coastguard Worker         // length 0 vector is simply empty instead of null
1223*890232f2SAndroid Build Coastguard Worker         field_type += is_vector ? "" : "|null";
1224*890232f2SAndroid Build Coastguard Worker       }
1225*890232f2SAndroid Build Coastguard Worker 
1226*890232f2SAndroid Build Coastguard Worker       if (!field_offset_decl.empty()) {
1227*890232f2SAndroid Build Coastguard Worker         field_offset_decl =
1228*890232f2SAndroid Build Coastguard Worker             "  const " + field_name_escaped + " = " + field_offset_decl + ";";
1229*890232f2SAndroid Build Coastguard Worker       }
1230*890232f2SAndroid Build Coastguard Worker       if (field_offset_val.empty()) { field_offset_val = field_name_escaped; }
1231*890232f2SAndroid Build Coastguard Worker 
1232*890232f2SAndroid Build Coastguard Worker       unpack_func += "    " + field_val;
1233*890232f2SAndroid Build Coastguard Worker       unpack_to_func += "  _o." + field_name_escaped + " = " + field_val + ";";
1234*890232f2SAndroid Build Coastguard Worker 
1235*890232f2SAndroid Build Coastguard Worker       // FIXME: if field_type and field_name_escaped are identical, then
1236*890232f2SAndroid Build Coastguard Worker       // this generates invalid typescript.
1237*890232f2SAndroid Build Coastguard Worker       constructor_func += "  public " + field_name_escaped + ": " + field_type +
1238*890232f2SAndroid Build Coastguard Worker                           " = " + field_default_val;
1239*890232f2SAndroid Build Coastguard Worker 
1240*890232f2SAndroid Build Coastguard Worker       if (!struct_def.fixed) {
1241*890232f2SAndroid Build Coastguard Worker         if (!field_offset_decl.empty()) {
1242*890232f2SAndroid Build Coastguard Worker           pack_func_offset_decl += field_offset_decl + "\n";
1243*890232f2SAndroid Build Coastguard Worker         }
1244*890232f2SAndroid Build Coastguard Worker 
1245*890232f2SAndroid Build Coastguard Worker         if (has_create) {
1246*890232f2SAndroid Build Coastguard Worker           pack_func_create_call += field_offset_val;
1247*890232f2SAndroid Build Coastguard Worker         } else {
1248*890232f2SAndroid Build Coastguard Worker           if (field.IsScalarOptional()) {
1249*890232f2SAndroid Build Coastguard Worker             pack_func_create_call +=
1250*890232f2SAndroid Build Coastguard Worker                 "  if (" + field_offset_val + " !== null)\n  ";
1251*890232f2SAndroid Build Coastguard Worker           }
1252*890232f2SAndroid Build Coastguard Worker           pack_func_create_call += "  " + struct_name + ".add" +
1253*890232f2SAndroid Build Coastguard Worker                                    ConvertCase(field.name, Case::kUpperCamel) +
1254*890232f2SAndroid Build Coastguard Worker                                    "(builder, " + field_offset_val + ");\n";
1255*890232f2SAndroid Build Coastguard Worker         }
1256*890232f2SAndroid Build Coastguard Worker       }
1257*890232f2SAndroid Build Coastguard Worker 
1258*890232f2SAndroid Build Coastguard Worker       if (std::next(it) != struct_def.fields.vec.end()) {
1259*890232f2SAndroid Build Coastguard Worker         constructor_func += ",\n";
1260*890232f2SAndroid Build Coastguard Worker 
1261*890232f2SAndroid Build Coastguard Worker         if (!struct_def.fixed && has_create) {
1262*890232f2SAndroid Build Coastguard Worker           pack_func_create_call += ",\n    ";
1263*890232f2SAndroid Build Coastguard Worker         }
1264*890232f2SAndroid Build Coastguard Worker 
1265*890232f2SAndroid Build Coastguard Worker         unpack_func += ",\n";
1266*890232f2SAndroid Build Coastguard Worker         unpack_to_func += "\n";
1267*890232f2SAndroid Build Coastguard Worker       } else {
1268*890232f2SAndroid Build Coastguard Worker         constructor_func += "\n";
1269*890232f2SAndroid Build Coastguard Worker         if (!struct_def.fixed) {
1270*890232f2SAndroid Build Coastguard Worker           pack_func_offset_decl += (pack_func_offset_decl.empty() ? "" : "\n");
1271*890232f2SAndroid Build Coastguard Worker           pack_func_create_call += "\n  ";
1272*890232f2SAndroid Build Coastguard Worker         }
1273*890232f2SAndroid Build Coastguard Worker 
1274*890232f2SAndroid Build Coastguard Worker         unpack_func += "\n  ";
1275*890232f2SAndroid Build Coastguard Worker         unpack_to_func += "\n";
1276*890232f2SAndroid Build Coastguard Worker       }
1277*890232f2SAndroid Build Coastguard Worker     }
1278*890232f2SAndroid Build Coastguard Worker 
1279*890232f2SAndroid Build Coastguard Worker     constructor_func += "){}\n\n";
1280*890232f2SAndroid Build Coastguard Worker 
1281*890232f2SAndroid Build Coastguard Worker     if (has_create) {
1282*890232f2SAndroid Build Coastguard Worker       pack_func_create_call += ");";
1283*890232f2SAndroid Build Coastguard Worker     } else {
1284*890232f2SAndroid Build Coastguard Worker       pack_func_create_call += "return " + struct_name + ".end" +
1285*890232f2SAndroid Build Coastguard Worker                                GetPrefixedName(struct_def) + "(builder);";
1286*890232f2SAndroid Build Coastguard Worker     }
1287*890232f2SAndroid Build Coastguard Worker     obj_api_class = "\n";
1288*890232f2SAndroid Build Coastguard Worker     obj_api_class += "export class ";
1289*890232f2SAndroid Build Coastguard Worker     obj_api_class += GetTypeName(struct_def, /*object_api=*/true);
1290*890232f2SAndroid Build Coastguard Worker     obj_api_class += " {\n";
1291*890232f2SAndroid Build Coastguard Worker     obj_api_class += constructor_func;
1292*890232f2SAndroid Build Coastguard Worker     obj_api_class += pack_func_prototype + pack_func_offset_decl +
1293*890232f2SAndroid Build Coastguard Worker                      pack_func_create_call + "\n}";
1294*890232f2SAndroid Build Coastguard Worker 
1295*890232f2SAndroid Build Coastguard Worker     obj_api_class += "\n}\n";
1296*890232f2SAndroid Build Coastguard Worker 
1297*890232f2SAndroid Build Coastguard Worker     unpack_func += ");\n}";
1298*890232f2SAndroid Build Coastguard Worker     unpack_to_func += "}\n";
1299*890232f2SAndroid Build Coastguard Worker 
1300*890232f2SAndroid Build Coastguard Worker     obj_api_unpack_func = unpack_func + "\n\n" + unpack_to_func;
1301*890232f2SAndroid Build Coastguard Worker   }
1302*890232f2SAndroid Build Coastguard Worker 
CanCreateFactoryMethod(const StructDef & struct_def)1303*890232f2SAndroid Build Coastguard Worker   static bool CanCreateFactoryMethod(const StructDef &struct_def) {
1304*890232f2SAndroid Build Coastguard Worker     // to preserve backwards compatibility, we allow the first field to be a
1305*890232f2SAndroid Build Coastguard Worker     // struct
1306*890232f2SAndroid Build Coastguard Worker     return struct_def.fields.vec.size() < 2 ||
1307*890232f2SAndroid Build Coastguard Worker            std::all_of(std::begin(struct_def.fields.vec) + 1,
1308*890232f2SAndroid Build Coastguard Worker                        std::end(struct_def.fields.vec),
1309*890232f2SAndroid Build Coastguard Worker                        [](const FieldDef *f) -> bool {
1310*890232f2SAndroid Build Coastguard Worker                          FLATBUFFERS_ASSERT(f != nullptr);
1311*890232f2SAndroid Build Coastguard Worker                          return f->value.type.base_type != BASE_TYPE_STRUCT;
1312*890232f2SAndroid Build Coastguard Worker                        });
1313*890232f2SAndroid Build Coastguard Worker   }
1314*890232f2SAndroid Build Coastguard Worker 
1315*890232f2SAndroid Build Coastguard Worker   // Generate an accessor struct with constructor for a flatbuffers struct.
GenStruct(const Parser & parser,StructDef & struct_def,std::string * code_ptr,import_set & imports)1316*890232f2SAndroid Build Coastguard Worker   void GenStruct(const Parser &parser, StructDef &struct_def,
1317*890232f2SAndroid Build Coastguard Worker                  std::string *code_ptr, import_set &imports) {
1318*890232f2SAndroid Build Coastguard Worker     if (struct_def.generated) return;
1319*890232f2SAndroid Build Coastguard Worker     std::string &code = *code_ptr;
1320*890232f2SAndroid Build Coastguard Worker 
1321*890232f2SAndroid Build Coastguard Worker     // Special case for the root struct, since no one will necessarily reference
1322*890232f2SAndroid Build Coastguard Worker     // it, we have to explicitly add it to the import list.
1323*890232f2SAndroid Build Coastguard Worker     if (&struct_def == parser_.root_struct_def_) {
1324*890232f2SAndroid Build Coastguard Worker       AddImport(imports, struct_def, struct_def);
1325*890232f2SAndroid Build Coastguard Worker     }
1326*890232f2SAndroid Build Coastguard Worker 
1327*890232f2SAndroid Build Coastguard Worker     const std::string object_name = GetTypeName(struct_def);
1328*890232f2SAndroid Build Coastguard Worker 
1329*890232f2SAndroid Build Coastguard Worker     // Emit constructor
1330*890232f2SAndroid Build Coastguard Worker     GenDocComment(struct_def.doc_comment, code_ptr);
1331*890232f2SAndroid Build Coastguard Worker     code += "export class ";
1332*890232f2SAndroid Build Coastguard Worker     code += object_name;
1333*890232f2SAndroid Build Coastguard Worker     code += " {\n";
1334*890232f2SAndroid Build Coastguard Worker     code += "  bb: flatbuffers.ByteBuffer|null = null;\n";
1335*890232f2SAndroid Build Coastguard Worker     code += "  bb_pos = 0;\n";
1336*890232f2SAndroid Build Coastguard Worker 
1337*890232f2SAndroid Build Coastguard Worker     // Generate the __init method that sets the field in a pre-existing
1338*890232f2SAndroid Build Coastguard Worker     // accessor object. This is to allow object reuse.
1339*890232f2SAndroid Build Coastguard Worker     code +=
1340*890232f2SAndroid Build Coastguard Worker         "  __init(i:number, bb:flatbuffers.ByteBuffer):" + object_name + " {\n";
1341*890232f2SAndroid Build Coastguard Worker     code += "  this.bb_pos = i;\n";
1342*890232f2SAndroid Build Coastguard Worker     code += "  this.bb = bb;\n";
1343*890232f2SAndroid Build Coastguard Worker     code += "  return this;\n";
1344*890232f2SAndroid Build Coastguard Worker     code += "}\n\n";
1345*890232f2SAndroid Build Coastguard Worker 
1346*890232f2SAndroid Build Coastguard Worker     // Generate special accessors for the table that when used as the root of a
1347*890232f2SAndroid Build Coastguard Worker     // FlatBuffer
1348*890232f2SAndroid Build Coastguard Worker     GenerateRootAccessor(struct_def, code_ptr, code, object_name, false);
1349*890232f2SAndroid Build Coastguard Worker     GenerateRootAccessor(struct_def, code_ptr, code, object_name, true);
1350*890232f2SAndroid Build Coastguard Worker 
1351*890232f2SAndroid Build Coastguard Worker     // Generate the identifier check method
1352*890232f2SAndroid Build Coastguard Worker     if (!struct_def.fixed && parser_.root_struct_def_ == &struct_def &&
1353*890232f2SAndroid Build Coastguard Worker         !parser_.file_identifier_.empty()) {
1354*890232f2SAndroid Build Coastguard Worker       GenDocComment(code_ptr);
1355*890232f2SAndroid Build Coastguard Worker       code +=
1356*890232f2SAndroid Build Coastguard Worker           "static bufferHasIdentifier(bb:flatbuffers.ByteBuffer):boolean "
1357*890232f2SAndroid Build Coastguard Worker           "{\n";
1358*890232f2SAndroid Build Coastguard Worker       code += "  return bb.__has_identifier('" + parser_.file_identifier_;
1359*890232f2SAndroid Build Coastguard Worker       code += "');\n}\n\n";
1360*890232f2SAndroid Build Coastguard Worker     }
1361*890232f2SAndroid Build Coastguard Worker 
1362*890232f2SAndroid Build Coastguard Worker     // Emit field accessors
1363*890232f2SAndroid Build Coastguard Worker     for (auto it = struct_def.fields.vec.begin();
1364*890232f2SAndroid Build Coastguard Worker          it != struct_def.fields.vec.end(); ++it) {
1365*890232f2SAndroid Build Coastguard Worker       auto &field = **it;
1366*890232f2SAndroid Build Coastguard Worker       if (field.deprecated) continue;
1367*890232f2SAndroid Build Coastguard Worker       auto offset_prefix =
1368*890232f2SAndroid Build Coastguard Worker           "  const offset = " + GenBBAccess() + ".__offset(this.bb_pos, " +
1369*890232f2SAndroid Build Coastguard Worker           NumToString(field.value.offset) + ");\n  return offset ? ";
1370*890232f2SAndroid Build Coastguard Worker 
1371*890232f2SAndroid Build Coastguard Worker       // Emit a scalar field
1372*890232f2SAndroid Build Coastguard Worker       const auto is_string = IsString(field.value.type);
1373*890232f2SAndroid Build Coastguard Worker       if (IsScalar(field.value.type.base_type) || is_string) {
1374*890232f2SAndroid Build Coastguard Worker         const auto has_null_default = is_string || HasNullDefault(field);
1375*890232f2SAndroid Build Coastguard Worker 
1376*890232f2SAndroid Build Coastguard Worker         GenDocComment(field.doc_comment, code_ptr);
1377*890232f2SAndroid Build Coastguard Worker         std::string prefix = ConvertCase(field.name, Case::kLowerCamel) + "(";
1378*890232f2SAndroid Build Coastguard Worker         if (is_string) {
1379*890232f2SAndroid Build Coastguard Worker           code += prefix + "):string|null\n";
1380*890232f2SAndroid Build Coastguard Worker           code +=
1381*890232f2SAndroid Build Coastguard Worker               prefix + "optionalEncoding:flatbuffers.Encoding" + "):" +
1382*890232f2SAndroid Build Coastguard Worker               GenTypeName(imports, struct_def, field.value.type, false, true) +
1383*890232f2SAndroid Build Coastguard Worker               "\n";
1384*890232f2SAndroid Build Coastguard Worker           code += prefix + "optionalEncoding?:any";
1385*890232f2SAndroid Build Coastguard Worker         } else {
1386*890232f2SAndroid Build Coastguard Worker           code += prefix;
1387*890232f2SAndroid Build Coastguard Worker         }
1388*890232f2SAndroid Build Coastguard Worker         if (field.value.type.enum_def) {
1389*890232f2SAndroid Build Coastguard Worker           code += "):" +
1390*890232f2SAndroid Build Coastguard Worker                   GenTypeName(imports, struct_def, field.value.type, false,
1391*890232f2SAndroid Build Coastguard Worker                               field.IsOptional()) +
1392*890232f2SAndroid Build Coastguard Worker                   " {\n";
1393*890232f2SAndroid Build Coastguard Worker         } else {
1394*890232f2SAndroid Build Coastguard Worker           code += "):" +
1395*890232f2SAndroid Build Coastguard Worker                   GenTypeName(imports, struct_def, field.value.type, false,
1396*890232f2SAndroid Build Coastguard Worker                               has_null_default) +
1397*890232f2SAndroid Build Coastguard Worker                   " {\n";
1398*890232f2SAndroid Build Coastguard Worker         }
1399*890232f2SAndroid Build Coastguard Worker 
1400*890232f2SAndroid Build Coastguard Worker         if (struct_def.fixed) {
1401*890232f2SAndroid Build Coastguard Worker           code +=
1402*890232f2SAndroid Build Coastguard Worker               "  return " +
1403*890232f2SAndroid Build Coastguard Worker               GenGetter(field.value.type,
1404*890232f2SAndroid Build Coastguard Worker                         "(this.bb_pos" + MaybeAdd(field.value.offset) + ")") +
1405*890232f2SAndroid Build Coastguard Worker               ";\n";
1406*890232f2SAndroid Build Coastguard Worker         } else {
1407*890232f2SAndroid Build Coastguard Worker           std::string index = "this.bb_pos + offset";
1408*890232f2SAndroid Build Coastguard Worker           if (is_string) { index += ", optionalEncoding"; }
1409*890232f2SAndroid Build Coastguard Worker           code += offset_prefix +
1410*890232f2SAndroid Build Coastguard Worker                   GenGetter(field.value.type, "(" + index + ")") + " : " +
1411*890232f2SAndroid Build Coastguard Worker                   GenDefaultValue(field, imports);
1412*890232f2SAndroid Build Coastguard Worker           code += ";\n";
1413*890232f2SAndroid Build Coastguard Worker         }
1414*890232f2SAndroid Build Coastguard Worker       }
1415*890232f2SAndroid Build Coastguard Worker 
1416*890232f2SAndroid Build Coastguard Worker       // Emit an object field
1417*890232f2SAndroid Build Coastguard Worker       else {
1418*890232f2SAndroid Build Coastguard Worker         switch (field.value.type.base_type) {
1419*890232f2SAndroid Build Coastguard Worker           case BASE_TYPE_STRUCT: {
1420*890232f2SAndroid Build Coastguard Worker             const auto type =
1421*890232f2SAndroid Build Coastguard Worker                 AddImport(imports, struct_def, *field.value.type.struct_def)
1422*890232f2SAndroid Build Coastguard Worker                     .name;
1423*890232f2SAndroid Build Coastguard Worker             GenDocComment(field.doc_comment, code_ptr);
1424*890232f2SAndroid Build Coastguard Worker             code += ConvertCase(field.name, Case::kLowerCamel);
1425*890232f2SAndroid Build Coastguard Worker             code += "(obj?:" + type + "):" + type + "|null {\n";
1426*890232f2SAndroid Build Coastguard Worker 
1427*890232f2SAndroid Build Coastguard Worker             if (struct_def.fixed) {
1428*890232f2SAndroid Build Coastguard Worker               code += "  return (obj || " + GenerateNewExpression(type);
1429*890232f2SAndroid Build Coastguard Worker               code += ").__init(this.bb_pos";
1430*890232f2SAndroid Build Coastguard Worker               code +=
1431*890232f2SAndroid Build Coastguard Worker                   MaybeAdd(field.value.offset) + ", " + GenBBAccess() + ");\n";
1432*890232f2SAndroid Build Coastguard Worker             } else {
1433*890232f2SAndroid Build Coastguard Worker               code += offset_prefix + "(obj || " + GenerateNewExpression(type) +
1434*890232f2SAndroid Build Coastguard Worker                       ").__init(";
1435*890232f2SAndroid Build Coastguard Worker               code += field.value.type.struct_def->fixed
1436*890232f2SAndroid Build Coastguard Worker                           ? "this.bb_pos + offset"
1437*890232f2SAndroid Build Coastguard Worker                           : GenBBAccess() + ".__indirect(this.bb_pos + offset)";
1438*890232f2SAndroid Build Coastguard Worker               code += ", " + GenBBAccess() + ") : null;\n";
1439*890232f2SAndroid Build Coastguard Worker             }
1440*890232f2SAndroid Build Coastguard Worker 
1441*890232f2SAndroid Build Coastguard Worker             break;
1442*890232f2SAndroid Build Coastguard Worker           }
1443*890232f2SAndroid Build Coastguard Worker 
1444*890232f2SAndroid Build Coastguard Worker           case BASE_TYPE_VECTOR: {
1445*890232f2SAndroid Build Coastguard Worker             auto vectortype = field.value.type.VectorType();
1446*890232f2SAndroid Build Coastguard Worker             auto vectortypename =
1447*890232f2SAndroid Build Coastguard Worker                 GenTypeName(imports, struct_def, vectortype, false);
1448*890232f2SAndroid Build Coastguard Worker             auto inline_size = InlineSize(vectortype);
1449*890232f2SAndroid Build Coastguard Worker             auto index = GenBBAccess() +
1450*890232f2SAndroid Build Coastguard Worker                          ".__vector(this.bb_pos + offset) + index" +
1451*890232f2SAndroid Build Coastguard Worker                          MaybeScale(inline_size);
1452*890232f2SAndroid Build Coastguard Worker             std::string ret_type;
1453*890232f2SAndroid Build Coastguard Worker             bool is_union = false;
1454*890232f2SAndroid Build Coastguard Worker             switch (vectortype.base_type) {
1455*890232f2SAndroid Build Coastguard Worker               case BASE_TYPE_STRUCT: ret_type = vectortypename; break;
1456*890232f2SAndroid Build Coastguard Worker               case BASE_TYPE_STRING: ret_type = vectortypename; break;
1457*890232f2SAndroid Build Coastguard Worker               case BASE_TYPE_UNION:
1458*890232f2SAndroid Build Coastguard Worker                 ret_type = "?flatbuffers.Table";
1459*890232f2SAndroid Build Coastguard Worker                 is_union = true;
1460*890232f2SAndroid Build Coastguard Worker                 break;
1461*890232f2SAndroid Build Coastguard Worker               default: ret_type = vectortypename;
1462*890232f2SAndroid Build Coastguard Worker             }
1463*890232f2SAndroid Build Coastguard Worker             GenDocComment(field.doc_comment, code_ptr);
1464*890232f2SAndroid Build Coastguard Worker             std::string prefix = ConvertCase(field.name, Case::kLowerCamel);
1465*890232f2SAndroid Build Coastguard Worker             // TODO: make it work without any
1466*890232f2SAndroid Build Coastguard Worker             // if (is_union) { prefix += "<T extends flatbuffers.Table>"; }
1467*890232f2SAndroid Build Coastguard Worker             if (is_union) { prefix += ""; }
1468*890232f2SAndroid Build Coastguard Worker             prefix += "(index: number";
1469*890232f2SAndroid Build Coastguard Worker             if (is_union) {
1470*890232f2SAndroid Build Coastguard Worker               const auto union_type =
1471*890232f2SAndroid Build Coastguard Worker                   GenUnionGenericTypeTS(*(field.value.type.enum_def));
1472*890232f2SAndroid Build Coastguard Worker 
1473*890232f2SAndroid Build Coastguard Worker               vectortypename = union_type;
1474*890232f2SAndroid Build Coastguard Worker               code += prefix + ", obj:" + union_type;
1475*890232f2SAndroid Build Coastguard Worker             } else if (vectortype.base_type == BASE_TYPE_STRUCT) {
1476*890232f2SAndroid Build Coastguard Worker               code += prefix + ", obj?:" + vectortypename;
1477*890232f2SAndroid Build Coastguard Worker             } else if (IsString(vectortype)) {
1478*890232f2SAndroid Build Coastguard Worker               code += prefix + "):string\n";
1479*890232f2SAndroid Build Coastguard Worker               code += prefix + ",optionalEncoding:flatbuffers.Encoding" +
1480*890232f2SAndroid Build Coastguard Worker                       "):" + vectortypename + "\n";
1481*890232f2SAndroid Build Coastguard Worker               code += prefix + ",optionalEncoding?:any";
1482*890232f2SAndroid Build Coastguard Worker             } else {
1483*890232f2SAndroid Build Coastguard Worker               code += prefix;
1484*890232f2SAndroid Build Coastguard Worker             }
1485*890232f2SAndroid Build Coastguard Worker             code += "):" + vectortypename + "|null {\n";
1486*890232f2SAndroid Build Coastguard Worker 
1487*890232f2SAndroid Build Coastguard Worker             if (vectortype.base_type == BASE_TYPE_STRUCT) {
1488*890232f2SAndroid Build Coastguard Worker               code += offset_prefix + "(obj || " +
1489*890232f2SAndroid Build Coastguard Worker                       GenerateNewExpression(vectortypename);
1490*890232f2SAndroid Build Coastguard Worker               code += ").__init(";
1491*890232f2SAndroid Build Coastguard Worker               code += vectortype.struct_def->fixed
1492*890232f2SAndroid Build Coastguard Worker                           ? index
1493*890232f2SAndroid Build Coastguard Worker                           : GenBBAccess() + ".__indirect(" + index + ")";
1494*890232f2SAndroid Build Coastguard Worker               code += ", " + GenBBAccess() + ")";
1495*890232f2SAndroid Build Coastguard Worker             } else {
1496*890232f2SAndroid Build Coastguard Worker               if (is_union) {
1497*890232f2SAndroid Build Coastguard Worker                 index = "obj, " + index;
1498*890232f2SAndroid Build Coastguard Worker               } else if (IsString(vectortype)) {
1499*890232f2SAndroid Build Coastguard Worker                 index += ", optionalEncoding";
1500*890232f2SAndroid Build Coastguard Worker               }
1501*890232f2SAndroid Build Coastguard Worker               code += offset_prefix + GenGetter(vectortype, "(" + index + ")");
1502*890232f2SAndroid Build Coastguard Worker             }
1503*890232f2SAndroid Build Coastguard Worker             code += " : ";
1504*890232f2SAndroid Build Coastguard Worker             if (field.value.type.element == BASE_TYPE_BOOL) {
1505*890232f2SAndroid Build Coastguard Worker               code += "false";
1506*890232f2SAndroid Build Coastguard Worker             } else if (field.value.type.element == BASE_TYPE_LONG ||
1507*890232f2SAndroid Build Coastguard Worker                        field.value.type.element == BASE_TYPE_ULONG) {
1508*890232f2SAndroid Build Coastguard Worker               code += "BigInt(0)";
1509*890232f2SAndroid Build Coastguard Worker             } else if (IsScalar(field.value.type.element)) {
1510*890232f2SAndroid Build Coastguard Worker               if (field.value.type.enum_def) {
1511*890232f2SAndroid Build Coastguard Worker                 code += field.value.constant;
1512*890232f2SAndroid Build Coastguard Worker               } else {
1513*890232f2SAndroid Build Coastguard Worker                 code += "0";
1514*890232f2SAndroid Build Coastguard Worker               }
1515*890232f2SAndroid Build Coastguard Worker             } else {
1516*890232f2SAndroid Build Coastguard Worker               code += "null";
1517*890232f2SAndroid Build Coastguard Worker             }
1518*890232f2SAndroid Build Coastguard Worker             code += ";\n";
1519*890232f2SAndroid Build Coastguard Worker             break;
1520*890232f2SAndroid Build Coastguard Worker           }
1521*890232f2SAndroid Build Coastguard Worker 
1522*890232f2SAndroid Build Coastguard Worker           case BASE_TYPE_UNION: {
1523*890232f2SAndroid Build Coastguard Worker             GenDocComment(field.doc_comment, code_ptr);
1524*890232f2SAndroid Build Coastguard Worker             code += ConvertCase(field.name, Case::kLowerCamel);
1525*890232f2SAndroid Build Coastguard Worker 
1526*890232f2SAndroid Build Coastguard Worker             const auto &union_enum = *(field.value.type.enum_def);
1527*890232f2SAndroid Build Coastguard Worker             const auto union_type = GenUnionGenericTypeTS(union_enum);
1528*890232f2SAndroid Build Coastguard Worker             code += "<T extends flatbuffers.Table>(obj:" + union_type +
1529*890232f2SAndroid Build Coastguard Worker                     "):" + union_type +
1530*890232f2SAndroid Build Coastguard Worker                     "|null "
1531*890232f2SAndroid Build Coastguard Worker                     "{\n";
1532*890232f2SAndroid Build Coastguard Worker 
1533*890232f2SAndroid Build Coastguard Worker             code += offset_prefix +
1534*890232f2SAndroid Build Coastguard Worker                     GenGetter(field.value.type, "(obj, this.bb_pos + offset)") +
1535*890232f2SAndroid Build Coastguard Worker                     " : null;\n";
1536*890232f2SAndroid Build Coastguard Worker             break;
1537*890232f2SAndroid Build Coastguard Worker           }
1538*890232f2SAndroid Build Coastguard Worker           default: FLATBUFFERS_ASSERT(0);
1539*890232f2SAndroid Build Coastguard Worker         }
1540*890232f2SAndroid Build Coastguard Worker       }
1541*890232f2SAndroid Build Coastguard Worker       code += "}\n\n";
1542*890232f2SAndroid Build Coastguard Worker 
1543*890232f2SAndroid Build Coastguard Worker       // Adds the mutable scalar value to the output
1544*890232f2SAndroid Build Coastguard Worker       if (IsScalar(field.value.type.base_type) && parser.opts.mutable_buffer &&
1545*890232f2SAndroid Build Coastguard Worker           !IsUnion(field.value.type)) {
1546*890232f2SAndroid Build Coastguard Worker         std::string type =
1547*890232f2SAndroid Build Coastguard Worker             GenTypeName(imports, struct_def, field.value.type, true);
1548*890232f2SAndroid Build Coastguard Worker 
1549*890232f2SAndroid Build Coastguard Worker         code += "mutate_" + field.name + "(value:" + type + "):boolean {\n";
1550*890232f2SAndroid Build Coastguard Worker 
1551*890232f2SAndroid Build Coastguard Worker         if (struct_def.fixed) {
1552*890232f2SAndroid Build Coastguard Worker           code += "  " + GenBBAccess() + ".write" +
1553*890232f2SAndroid Build Coastguard Worker                   ConvertCase(GenType(field.value.type), Case::kUpperCamel) +
1554*890232f2SAndroid Build Coastguard Worker                   "(this.bb_pos + " + NumToString(field.value.offset) + ", ";
1555*890232f2SAndroid Build Coastguard Worker         } else {
1556*890232f2SAndroid Build Coastguard Worker           code += "  const offset = " + GenBBAccess() +
1557*890232f2SAndroid Build Coastguard Worker                   ".__offset(this.bb_pos, " + NumToString(field.value.offset) +
1558*890232f2SAndroid Build Coastguard Worker                   ");\n\n";
1559*890232f2SAndroid Build Coastguard Worker           code += "  if (offset === 0) {\n";
1560*890232f2SAndroid Build Coastguard Worker           code += "    return false;\n";
1561*890232f2SAndroid Build Coastguard Worker           code += "  }\n\n";
1562*890232f2SAndroid Build Coastguard Worker 
1563*890232f2SAndroid Build Coastguard Worker           // special case for bools, which are treated as uint8
1564*890232f2SAndroid Build Coastguard Worker           code += "  " + GenBBAccess() + ".write" +
1565*890232f2SAndroid Build Coastguard Worker                   ConvertCase(GenType(field.value.type), Case::kUpperCamel) +
1566*890232f2SAndroid Build Coastguard Worker                   "(this.bb_pos + offset, ";
1567*890232f2SAndroid Build Coastguard Worker           if (field.value.type.base_type == BASE_TYPE_BOOL) { code += "+"; }
1568*890232f2SAndroid Build Coastguard Worker         }
1569*890232f2SAndroid Build Coastguard Worker 
1570*890232f2SAndroid Build Coastguard Worker         code += "value);\n";
1571*890232f2SAndroid Build Coastguard Worker         code += "  return true;\n";
1572*890232f2SAndroid Build Coastguard Worker         code += "}\n\n";
1573*890232f2SAndroid Build Coastguard Worker       }
1574*890232f2SAndroid Build Coastguard Worker 
1575*890232f2SAndroid Build Coastguard Worker       // Emit vector helpers
1576*890232f2SAndroid Build Coastguard Worker       if (IsVector(field.value.type)) {
1577*890232f2SAndroid Build Coastguard Worker         // Emit a length helper
1578*890232f2SAndroid Build Coastguard Worker         GenDocComment(code_ptr);
1579*890232f2SAndroid Build Coastguard Worker         code += ConvertCase(field.name, Case::kLowerCamel);
1580*890232f2SAndroid Build Coastguard Worker         code += "Length():number {\n" + offset_prefix;
1581*890232f2SAndroid Build Coastguard Worker 
1582*890232f2SAndroid Build Coastguard Worker         code +=
1583*890232f2SAndroid Build Coastguard Worker             GenBBAccess() + ".__vector_len(this.bb_pos + offset) : 0;\n}\n\n";
1584*890232f2SAndroid Build Coastguard Worker 
1585*890232f2SAndroid Build Coastguard Worker         // For scalar types, emit a typed array helper
1586*890232f2SAndroid Build Coastguard Worker         auto vectorType = field.value.type.VectorType();
1587*890232f2SAndroid Build Coastguard Worker         if (IsScalar(vectorType.base_type) && !IsLong(vectorType.base_type)) {
1588*890232f2SAndroid Build Coastguard Worker           GenDocComment(code_ptr);
1589*890232f2SAndroid Build Coastguard Worker 
1590*890232f2SAndroid Build Coastguard Worker           code += ConvertCase(field.name, Case::kLowerCamel);
1591*890232f2SAndroid Build Coastguard Worker           code += "Array():" + GenType(vectorType) + "Array|null {\n" +
1592*890232f2SAndroid Build Coastguard Worker                   offset_prefix;
1593*890232f2SAndroid Build Coastguard Worker 
1594*890232f2SAndroid Build Coastguard Worker           code += "new " + GenType(vectorType) + "Array(" + GenBBAccess() +
1595*890232f2SAndroid Build Coastguard Worker                   ".bytes().buffer, " + GenBBAccess() +
1596*890232f2SAndroid Build Coastguard Worker                   ".bytes().byteOffset + " + GenBBAccess() +
1597*890232f2SAndroid Build Coastguard Worker                   ".__vector(this.bb_pos + offset), " + GenBBAccess() +
1598*890232f2SAndroid Build Coastguard Worker                   ".__vector_len(this.bb_pos + offset)) : null;\n}\n\n";
1599*890232f2SAndroid Build Coastguard Worker         }
1600*890232f2SAndroid Build Coastguard Worker       }
1601*890232f2SAndroid Build Coastguard Worker     }
1602*890232f2SAndroid Build Coastguard Worker 
1603*890232f2SAndroid Build Coastguard Worker     // Emit the fully qualified name
1604*890232f2SAndroid Build Coastguard Worker     if (parser_.opts.generate_name_strings) {
1605*890232f2SAndroid Build Coastguard Worker       GenDocComment(code_ptr);
1606*890232f2SAndroid Build Coastguard Worker       code += "static getFullyQualifiedName():string {\n";
1607*890232f2SAndroid Build Coastguard Worker       code += "  return '" + WrapInNameSpace(struct_def) + "';\n";
1608*890232f2SAndroid Build Coastguard Worker       code += "}\n\n";
1609*890232f2SAndroid Build Coastguard Worker     }
1610*890232f2SAndroid Build Coastguard Worker 
1611*890232f2SAndroid Build Coastguard Worker     // Emit the size of the struct.
1612*890232f2SAndroid Build Coastguard Worker     if (struct_def.fixed) {
1613*890232f2SAndroid Build Coastguard Worker       GenDocComment(code_ptr);
1614*890232f2SAndroid Build Coastguard Worker       code += "static sizeOf():number {\n";
1615*890232f2SAndroid Build Coastguard Worker       code += "  return " + NumToString(struct_def.bytesize) + ";\n";
1616*890232f2SAndroid Build Coastguard Worker       code += "}\n\n";
1617*890232f2SAndroid Build Coastguard Worker     }
1618*890232f2SAndroid Build Coastguard Worker 
1619*890232f2SAndroid Build Coastguard Worker     // Emit a factory constructor
1620*890232f2SAndroid Build Coastguard Worker     if (struct_def.fixed) {
1621*890232f2SAndroid Build Coastguard Worker       std::string arguments;
1622*890232f2SAndroid Build Coastguard Worker       GenStructArgs(imports, struct_def, &arguments, "");
1623*890232f2SAndroid Build Coastguard Worker       GenDocComment(code_ptr);
1624*890232f2SAndroid Build Coastguard Worker 
1625*890232f2SAndroid Build Coastguard Worker       code += "static create" + GetPrefixedName(struct_def) +
1626*890232f2SAndroid Build Coastguard Worker               "(builder:flatbuffers.Builder";
1627*890232f2SAndroid Build Coastguard Worker       code += arguments + "):flatbuffers.Offset {\n";
1628*890232f2SAndroid Build Coastguard Worker 
1629*890232f2SAndroid Build Coastguard Worker       GenStructBody(struct_def, &code, "");
1630*890232f2SAndroid Build Coastguard Worker       code += "  return builder.offset();\n}\n\n";
1631*890232f2SAndroid Build Coastguard Worker     } else {
1632*890232f2SAndroid Build Coastguard Worker       // Generate a method to start building a new object
1633*890232f2SAndroid Build Coastguard Worker       GenDocComment(code_ptr);
1634*890232f2SAndroid Build Coastguard Worker 
1635*890232f2SAndroid Build Coastguard Worker       code += "static start" + GetPrefixedName(struct_def) +
1636*890232f2SAndroid Build Coastguard Worker               "(builder:flatbuffers.Builder) {\n";
1637*890232f2SAndroid Build Coastguard Worker 
1638*890232f2SAndroid Build Coastguard Worker       code += "  builder.startObject(" +
1639*890232f2SAndroid Build Coastguard Worker               NumToString(struct_def.fields.vec.size()) + ");\n";
1640*890232f2SAndroid Build Coastguard Worker       code += "}\n\n";
1641*890232f2SAndroid Build Coastguard Worker 
1642*890232f2SAndroid Build Coastguard Worker       // Generate a set of static methods that allow table construction
1643*890232f2SAndroid Build Coastguard Worker       for (auto it = struct_def.fields.vec.begin();
1644*890232f2SAndroid Build Coastguard Worker            it != struct_def.fields.vec.end(); ++it) {
1645*890232f2SAndroid Build Coastguard Worker         auto &field = **it;
1646*890232f2SAndroid Build Coastguard Worker         if (field.deprecated) continue;
1647*890232f2SAndroid Build Coastguard Worker         const auto argname = GetArgName(field);
1648*890232f2SAndroid Build Coastguard Worker 
1649*890232f2SAndroid Build Coastguard Worker         // Generate the field insertion method
1650*890232f2SAndroid Build Coastguard Worker         GenDocComment(code_ptr);
1651*890232f2SAndroid Build Coastguard Worker         code += "static add" + ConvertCase(field.name, Case::kUpperCamel);
1652*890232f2SAndroid Build Coastguard Worker         code += "(builder:flatbuffers.Builder, " + argname + ":" +
1653*890232f2SAndroid Build Coastguard Worker                 GetArgType(imports, struct_def, field, false) + ") {\n";
1654*890232f2SAndroid Build Coastguard Worker         code += "  builder.addField" + GenWriteMethod(field.value.type) + "(";
1655*890232f2SAndroid Build Coastguard Worker         code += NumToString(it - struct_def.fields.vec.begin()) + ", ";
1656*890232f2SAndroid Build Coastguard Worker         if (field.value.type.base_type == BASE_TYPE_BOOL) { code += "+"; }
1657*890232f2SAndroid Build Coastguard Worker         code += argname + ", ";
1658*890232f2SAndroid Build Coastguard Worker         if (!IsScalar(field.value.type.base_type)) {
1659*890232f2SAndroid Build Coastguard Worker           code += "0";
1660*890232f2SAndroid Build Coastguard Worker         } else if (HasNullDefault(field)) {
1661*890232f2SAndroid Build Coastguard Worker           if (IsLong(field.value.type.base_type)) {
1662*890232f2SAndroid Build Coastguard Worker             code += "BigInt(0)";
1663*890232f2SAndroid Build Coastguard Worker           } else {
1664*890232f2SAndroid Build Coastguard Worker             code += "0";
1665*890232f2SAndroid Build Coastguard Worker           }
1666*890232f2SAndroid Build Coastguard Worker         } else {
1667*890232f2SAndroid Build Coastguard Worker           if (field.value.type.base_type == BASE_TYPE_BOOL) { code += "+"; }
1668*890232f2SAndroid Build Coastguard Worker           code += GenDefaultValue(field, imports);
1669*890232f2SAndroid Build Coastguard Worker         }
1670*890232f2SAndroid Build Coastguard Worker         code += ");\n}\n\n";
1671*890232f2SAndroid Build Coastguard Worker 
1672*890232f2SAndroid Build Coastguard Worker         if (IsVector(field.value.type)) {
1673*890232f2SAndroid Build Coastguard Worker           auto vector_type = field.value.type.VectorType();
1674*890232f2SAndroid Build Coastguard Worker           auto alignment = InlineAlignment(vector_type);
1675*890232f2SAndroid Build Coastguard Worker           auto elem_size = InlineSize(vector_type);
1676*890232f2SAndroid Build Coastguard Worker 
1677*890232f2SAndroid Build Coastguard Worker           // Generate a method to create a vector from a JavaScript array
1678*890232f2SAndroid Build Coastguard Worker           if (!IsStruct(vector_type)) {
1679*890232f2SAndroid Build Coastguard Worker             GenDocComment(code_ptr);
1680*890232f2SAndroid Build Coastguard Worker 
1681*890232f2SAndroid Build Coastguard Worker             const std::string sig_begin =
1682*890232f2SAndroid Build Coastguard Worker                 "static create" + ConvertCase(field.name, Case::kUpperCamel) +
1683*890232f2SAndroid Build Coastguard Worker                 "Vector(builder:flatbuffers.Builder, data:";
1684*890232f2SAndroid Build Coastguard Worker             const std::string sig_end = "):flatbuffers.Offset";
1685*890232f2SAndroid Build Coastguard Worker             std::string type =
1686*890232f2SAndroid Build Coastguard Worker                 GenTypeName(imports, struct_def, vector_type, true) + "[]";
1687*890232f2SAndroid Build Coastguard Worker             if (type == "number[]") {
1688*890232f2SAndroid Build Coastguard Worker               const auto &array_type = GenType(vector_type);
1689*890232f2SAndroid Build Coastguard Worker               // the old type should be deprecated in the future
1690*890232f2SAndroid Build Coastguard Worker               std::string type_old = "number[]|Uint8Array";
1691*890232f2SAndroid Build Coastguard Worker               std::string type_new = "number[]|" + array_type + "Array";
1692*890232f2SAndroid Build Coastguard Worker               if (type_old == type_new) {
1693*890232f2SAndroid Build Coastguard Worker                 type = type_new;
1694*890232f2SAndroid Build Coastguard Worker               } else {
1695*890232f2SAndroid Build Coastguard Worker                 // add function overloads
1696*890232f2SAndroid Build Coastguard Worker                 code += sig_begin + type_new + sig_end + ";\n";
1697*890232f2SAndroid Build Coastguard Worker                 code +=
1698*890232f2SAndroid Build Coastguard Worker                     "/**\n * @deprecated This Uint8Array overload will "
1699*890232f2SAndroid Build Coastguard Worker                     "be removed in the future.\n */\n";
1700*890232f2SAndroid Build Coastguard Worker                 code += sig_begin + type_old + sig_end + ";\n";
1701*890232f2SAndroid Build Coastguard Worker                 type = type_new + "|Uint8Array";
1702*890232f2SAndroid Build Coastguard Worker               }
1703*890232f2SAndroid Build Coastguard Worker             }
1704*890232f2SAndroid Build Coastguard Worker             code += sig_begin + type + sig_end + " {\n";
1705*890232f2SAndroid Build Coastguard Worker             code += "  builder.startVector(" + NumToString(elem_size);
1706*890232f2SAndroid Build Coastguard Worker             code += ", data.length, " + NumToString(alignment) + ");\n";
1707*890232f2SAndroid Build Coastguard Worker             code += "  for (let i = data.length - 1; i >= 0; i--) {\n";
1708*890232f2SAndroid Build Coastguard Worker             code += "    builder.add" + GenWriteMethod(vector_type) + "(";
1709*890232f2SAndroid Build Coastguard Worker             if (vector_type.base_type == BASE_TYPE_BOOL) { code += "+"; }
1710*890232f2SAndroid Build Coastguard Worker             code += "data[i]!);\n";
1711*890232f2SAndroid Build Coastguard Worker             code += "  }\n";
1712*890232f2SAndroid Build Coastguard Worker             code += "  return builder.endVector();\n";
1713*890232f2SAndroid Build Coastguard Worker             code += "}\n\n";
1714*890232f2SAndroid Build Coastguard Worker           }
1715*890232f2SAndroid Build Coastguard Worker 
1716*890232f2SAndroid Build Coastguard Worker           // Generate a method to start a vector, data to be added manually
1717*890232f2SAndroid Build Coastguard Worker           // after
1718*890232f2SAndroid Build Coastguard Worker           GenDocComment(code_ptr);
1719*890232f2SAndroid Build Coastguard Worker 
1720*890232f2SAndroid Build Coastguard Worker           code += "static start" + ConvertCase(field.name, Case::kUpperCamel);
1721*890232f2SAndroid Build Coastguard Worker           code += "Vector(builder:flatbuffers.Builder, numElems:number) {\n";
1722*890232f2SAndroid Build Coastguard Worker           code += "  builder.startVector(" + NumToString(elem_size);
1723*890232f2SAndroid Build Coastguard Worker           code += ", numElems, " + NumToString(alignment) + ");\n";
1724*890232f2SAndroid Build Coastguard Worker           code += "}\n\n";
1725*890232f2SAndroid Build Coastguard Worker         }
1726*890232f2SAndroid Build Coastguard Worker       }
1727*890232f2SAndroid Build Coastguard Worker 
1728*890232f2SAndroid Build Coastguard Worker       // Generate a method to stop building a new object
1729*890232f2SAndroid Build Coastguard Worker       GenDocComment(code_ptr);
1730*890232f2SAndroid Build Coastguard Worker 
1731*890232f2SAndroid Build Coastguard Worker       code += "static end" + GetPrefixedName(struct_def);
1732*890232f2SAndroid Build Coastguard Worker       code += "(builder:flatbuffers.Builder):flatbuffers.Offset {\n";
1733*890232f2SAndroid Build Coastguard Worker 
1734*890232f2SAndroid Build Coastguard Worker       code += "  const offset = builder.endObject();\n";
1735*890232f2SAndroid Build Coastguard Worker       for (auto it = struct_def.fields.vec.begin();
1736*890232f2SAndroid Build Coastguard Worker            it != struct_def.fields.vec.end(); ++it) {
1737*890232f2SAndroid Build Coastguard Worker         auto &field = **it;
1738*890232f2SAndroid Build Coastguard Worker         if (!field.deprecated && field.IsRequired()) {
1739*890232f2SAndroid Build Coastguard Worker           code += "  builder.requiredField(offset, ";
1740*890232f2SAndroid Build Coastguard Worker           code += NumToString(field.value.offset);
1741*890232f2SAndroid Build Coastguard Worker           code += ") // " + field.name + "\n";
1742*890232f2SAndroid Build Coastguard Worker         }
1743*890232f2SAndroid Build Coastguard Worker       }
1744*890232f2SAndroid Build Coastguard Worker       code += "  return offset;\n";
1745*890232f2SAndroid Build Coastguard Worker       code += "}\n\n";
1746*890232f2SAndroid Build Coastguard Worker 
1747*890232f2SAndroid Build Coastguard Worker       // Generate the methods to complete buffer construction
1748*890232f2SAndroid Build Coastguard Worker       GenerateFinisher(struct_def, code_ptr, code, false);
1749*890232f2SAndroid Build Coastguard Worker       GenerateFinisher(struct_def, code_ptr, code, true);
1750*890232f2SAndroid Build Coastguard Worker 
1751*890232f2SAndroid Build Coastguard Worker       // Generate a convenient CreateX function
1752*890232f2SAndroid Build Coastguard Worker       if (CanCreateFactoryMethod(struct_def)) {
1753*890232f2SAndroid Build Coastguard Worker         code += "static create" + GetPrefixedName(struct_def);
1754*890232f2SAndroid Build Coastguard Worker         code += "(builder:flatbuffers.Builder";
1755*890232f2SAndroid Build Coastguard Worker         for (auto it = struct_def.fields.vec.begin();
1756*890232f2SAndroid Build Coastguard Worker              it != struct_def.fields.vec.end(); ++it) {
1757*890232f2SAndroid Build Coastguard Worker           const auto &field = **it;
1758*890232f2SAndroid Build Coastguard Worker           if (field.deprecated) continue;
1759*890232f2SAndroid Build Coastguard Worker           code += ", " + GetArgName(field) + ":" +
1760*890232f2SAndroid Build Coastguard Worker                   GetArgType(imports, struct_def, field, true);
1761*890232f2SAndroid Build Coastguard Worker         }
1762*890232f2SAndroid Build Coastguard Worker 
1763*890232f2SAndroid Build Coastguard Worker         code += "):flatbuffers.Offset {\n";
1764*890232f2SAndroid Build Coastguard Worker         code += "  " + object_name + ".start" + GetPrefixedName(struct_def) +
1765*890232f2SAndroid Build Coastguard Worker                 "(builder);\n";
1766*890232f2SAndroid Build Coastguard Worker 
1767*890232f2SAndroid Build Coastguard Worker         std::string methodPrefix = object_name;
1768*890232f2SAndroid Build Coastguard Worker         for (auto it = struct_def.fields.vec.begin();
1769*890232f2SAndroid Build Coastguard Worker              it != struct_def.fields.vec.end(); ++it) {
1770*890232f2SAndroid Build Coastguard Worker           const auto &field = **it;
1771*890232f2SAndroid Build Coastguard Worker           if (field.deprecated) continue;
1772*890232f2SAndroid Build Coastguard Worker 
1773*890232f2SAndroid Build Coastguard Worker           const auto arg_name = GetArgName(field);
1774*890232f2SAndroid Build Coastguard Worker 
1775*890232f2SAndroid Build Coastguard Worker           if (field.IsScalarOptional()) {
1776*890232f2SAndroid Build Coastguard Worker             code += "  if (" + arg_name + " !== null)\n  ";
1777*890232f2SAndroid Build Coastguard Worker           }
1778*890232f2SAndroid Build Coastguard Worker 
1779*890232f2SAndroid Build Coastguard Worker           code += "  " + methodPrefix + ".add" +
1780*890232f2SAndroid Build Coastguard Worker                   ConvertCase(field.name, Case::kUpperCamel) + "(";
1781*890232f2SAndroid Build Coastguard Worker           code += "builder, " + arg_name + ");\n";
1782*890232f2SAndroid Build Coastguard Worker         }
1783*890232f2SAndroid Build Coastguard Worker 
1784*890232f2SAndroid Build Coastguard Worker         code += "  return " + methodPrefix + ".end" +
1785*890232f2SAndroid Build Coastguard Worker                 GetPrefixedName(struct_def) + "(builder);\n";
1786*890232f2SAndroid Build Coastguard Worker         code += "}\n";
1787*890232f2SAndroid Build Coastguard Worker       }
1788*890232f2SAndroid Build Coastguard Worker     }
1789*890232f2SAndroid Build Coastguard Worker 
1790*890232f2SAndroid Build Coastguard Worker     if (!struct_def.fixed && parser_.services_.vec.size() != 0) {
1791*890232f2SAndroid Build Coastguard Worker       auto name = GetPrefixedName(struct_def, "");
1792*890232f2SAndroid Build Coastguard Worker       code += "\n";
1793*890232f2SAndroid Build Coastguard Worker       code += "serialize():Uint8Array {\n";
1794*890232f2SAndroid Build Coastguard Worker       code += "  return this.bb!.bytes();\n";
1795*890232f2SAndroid Build Coastguard Worker       code += "}\n";
1796*890232f2SAndroid Build Coastguard Worker 
1797*890232f2SAndroid Build Coastguard Worker       code += "\n";
1798*890232f2SAndroid Build Coastguard Worker       code += "static deserialize(buffer: Uint8Array):" + EscapeKeyword(name) +
1799*890232f2SAndroid Build Coastguard Worker               " {\n";
1800*890232f2SAndroid Build Coastguard Worker       code += "  return " + AddImport(imports, struct_def, struct_def).name +
1801*890232f2SAndroid Build Coastguard Worker               ".getRootAs" + name + "(new flatbuffers.ByteBuffer(buffer))\n";
1802*890232f2SAndroid Build Coastguard Worker       code += "}\n";
1803*890232f2SAndroid Build Coastguard Worker     }
1804*890232f2SAndroid Build Coastguard Worker 
1805*890232f2SAndroid Build Coastguard Worker     if (parser_.opts.generate_object_based_api) {
1806*890232f2SAndroid Build Coastguard Worker       std::string obj_api_class;
1807*890232f2SAndroid Build Coastguard Worker       std::string obj_api_unpack_func;
1808*890232f2SAndroid Build Coastguard Worker       GenObjApi(parser_, struct_def, obj_api_unpack_func, obj_api_class,
1809*890232f2SAndroid Build Coastguard Worker                 imports);
1810*890232f2SAndroid Build Coastguard Worker 
1811*890232f2SAndroid Build Coastguard Worker       code += obj_api_unpack_func + "}\n" + obj_api_class;
1812*890232f2SAndroid Build Coastguard Worker     } else {
1813*890232f2SAndroid Build Coastguard Worker       code += "}\n";
1814*890232f2SAndroid Build Coastguard Worker     }
1815*890232f2SAndroid Build Coastguard Worker   }
1816*890232f2SAndroid Build Coastguard Worker 
HasNullDefault(const FieldDef & field)1817*890232f2SAndroid Build Coastguard Worker   static bool HasNullDefault(const FieldDef &field) {
1818*890232f2SAndroid Build Coastguard Worker     return field.IsOptional() && field.value.constant == "null";
1819*890232f2SAndroid Build Coastguard Worker   }
1820*890232f2SAndroid Build Coastguard Worker 
GetArgType(import_set & imports,const Definition & owner,const FieldDef & field,bool allowNull)1821*890232f2SAndroid Build Coastguard Worker   std::string GetArgType(import_set &imports, const Definition &owner,
1822*890232f2SAndroid Build Coastguard Worker                          const FieldDef &field, bool allowNull) {
1823*890232f2SAndroid Build Coastguard Worker     return GenTypeName(imports, owner, field.value.type, true,
1824*890232f2SAndroid Build Coastguard Worker                        allowNull && field.IsOptional());
1825*890232f2SAndroid Build Coastguard Worker   }
1826*890232f2SAndroid Build Coastguard Worker 
GetArgName(const FieldDef & field)1827*890232f2SAndroid Build Coastguard Worker   std::string GetArgName(const FieldDef &field) {
1828*890232f2SAndroid Build Coastguard Worker     auto argname = ConvertCase(field.name, Case::kLowerCamel);
1829*890232f2SAndroid Build Coastguard Worker     if (!IsScalar(field.value.type.base_type)) {
1830*890232f2SAndroid Build Coastguard Worker       argname += "Offset";
1831*890232f2SAndroid Build Coastguard Worker     } else {
1832*890232f2SAndroid Build Coastguard Worker       argname = EscapeKeyword(argname);
1833*890232f2SAndroid Build Coastguard Worker     }
1834*890232f2SAndroid Build Coastguard Worker     return argname;
1835*890232f2SAndroid Build Coastguard Worker   }
1836*890232f2SAndroid Build Coastguard Worker 
GetPrefixedName(const StructDef & struct_def,const char * prefix="")1837*890232f2SAndroid Build Coastguard Worker   std::string GetPrefixedName(const StructDef &struct_def,
1838*890232f2SAndroid Build Coastguard Worker                               const char *prefix = "") {
1839*890232f2SAndroid Build Coastguard Worker     return prefix + struct_def.name;
1840*890232f2SAndroid Build Coastguard Worker   }
1841*890232f2SAndroid Build Coastguard Worker };  // namespace ts
1842*890232f2SAndroid Build Coastguard Worker }  // namespace ts
1843*890232f2SAndroid Build Coastguard Worker 
GenerateTS(const Parser & parser,const std::string & path,const std::string & file_name)1844*890232f2SAndroid Build Coastguard Worker bool GenerateTS(const Parser &parser, const std::string &path,
1845*890232f2SAndroid Build Coastguard Worker                 const std::string &file_name) {
1846*890232f2SAndroid Build Coastguard Worker   ts::TsGenerator generator(parser, path, file_name);
1847*890232f2SAndroid Build Coastguard Worker   return generator.generate();
1848*890232f2SAndroid Build Coastguard Worker }
1849*890232f2SAndroid Build Coastguard Worker 
TSMakeRule(const Parser & parser,const std::string & path,const std::string & file_name)1850*890232f2SAndroid Build Coastguard Worker std::string TSMakeRule(const Parser &parser, const std::string &path,
1851*890232f2SAndroid Build Coastguard Worker                        const std::string &file_name) {
1852*890232f2SAndroid Build Coastguard Worker   std::string filebase =
1853*890232f2SAndroid Build Coastguard Worker       flatbuffers::StripPath(flatbuffers::StripExtension(file_name));
1854*890232f2SAndroid Build Coastguard Worker   ts::TsGenerator generator(parser, path, file_name);
1855*890232f2SAndroid Build Coastguard Worker   std::string make_rule =
1856*890232f2SAndroid Build Coastguard Worker       generator.GeneratedFileName(path, filebase, parser.opts) + ": ";
1857*890232f2SAndroid Build Coastguard Worker 
1858*890232f2SAndroid Build Coastguard Worker   auto included_files = parser.GetIncludedFilesRecursive(file_name);
1859*890232f2SAndroid Build Coastguard Worker   for (auto it = included_files.begin(); it != included_files.end(); ++it) {
1860*890232f2SAndroid Build Coastguard Worker     make_rule += " " + *it;
1861*890232f2SAndroid Build Coastguard Worker   }
1862*890232f2SAndroid Build Coastguard Worker   return make_rule;
1863*890232f2SAndroid Build Coastguard Worker }
1864*890232f2SAndroid Build Coastguard Worker 
1865*890232f2SAndroid Build Coastguard Worker }  // namespace flatbuffers
1866