1/*
2 * Copyright 2014 Google Inc. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 // independent from idl_parser, since this code is not needed for most clients
18
19#include <string>
20
21#include "flatbuffers/code_generators.h"
22#include "flatbuffers/flatbuffers.h"
23#include "flatbuffers/idl.h"
24#include "flatbuffers/util.h"
25
26#include <unordered_set>
27
28namespace flatbuffers {
29namespace lua {
30
31 // Hardcode spaces per indentation.
32 const char * Indent = " ";
33 const char * Comment = "-- ";
34 const char * End = "end\n";
35 const char * EndFunc = "end\n";
36 const char * SelfData = "self.view";
37 const char * SelfDataPos = "self.view.pos";
38 const char * SelfDataBytes = "self.view.bytes";
39
40 class LuaGenerator : public BaseGenerator {
41 public:
42 LuaGenerator(const Parser &parser, const std::string &path,
43 const std::string &file_name)
44 : BaseGenerator(parser, path, file_name, "" /* not used */,
45 "" /* not used */) {
46 static const char * const keywords[] = {
47 "and",
48 "break",
49 "do",
50 "else",
51 "elseif",
52 "end",
53 "false",
54 "for",
55 "function",
56 "goto",
57 "if",
58 "in",
59 "local",
60 "nil",
61 "not",
62 "or",
63 "repeat",
64 "return",
65 "then",
66 "true",
67 "until",
68 "while"
69 };
70 keywords_.insert(std::begin(keywords), std::end(keywords));
71 }
72
73 // Most field accessors need to retrieve and test the field offset first,
74 // this is the prefix code for that.
75 std::string OffsetPrefix(const FieldDef &field) {
76 return std::string(Indent) +
77 "local o = " + SelfData + ":Offset(" + NumToString(field.value.offset) + ")\n" +
78 Indent + "if o ~= 0 then\n";
79 }
80
81 // Begin a class declaration.
82 void BeginClass(const StructDef &struct_def, std::string *code_ptr) {
83 std::string &code = *code_ptr;
84 code += "local " + NormalizedName(struct_def) + " = {} -- the module\n";
85 code += "local " + NormalizedMetaName(struct_def) + " = {} -- the class metatable\n";
86 code += "\n";
87 }
88
89 // Begin enum code with a class declaration.
90 void BeginEnum(const std::string class_name, std::string *code_ptr) {
91 std::string &code = *code_ptr;
92 code += "local " + class_name + " = {\n";
93 }
94
95 std::string EscapeKeyword(const std::string &name) const {
96 return keywords_.find(name) == keywords_.end() ? name : "_" + name;
97 }
98
99 std::string NormalizedName(const Definition &definition) const {
100 return EscapeKeyword(definition.name);
101 }
102
103 std::string NormalizedName(const EnumVal &ev) const {
104 return EscapeKeyword(ev.name);
105 }
106
107 std::string NormalizedMetaName(const Definition &definition) const {
108 return EscapeKeyword(definition.name) + "_mt";
109 }
110
111 // A single enum member.
112 void EnumMember(const EnumDef &enum_def, const EnumVal &ev, std::string *code_ptr) {
113 std::string &code = *code_ptr;
114 code += std::string(Indent) + NormalizedName(ev) + " = " + NumToString(ev.value) + ",\n";
115 (void)enum_def;
116 }
117
118 // End enum code.
119 void EndEnum(std::string *code_ptr) {
120 std::string &code = *code_ptr;
121 code += "}\n";
122 }
123
124 void GenerateNewObjectPrototype(const StructDef &struct_def,
125 std::string *code_ptr) {
126 std::string &code = *code_ptr;
127
128 code += "function " + NormalizedName(struct_def) + ".New()\n";
129 code += std::string(Indent) + "local o = {}\n";
130 code += std::string(Indent) + "setmetatable(o, {__index = " + NormalizedMetaName(struct_def) + "})\n";
131 code += std::string(Indent) + "return o\n";
132 code += EndFunc;
133 }
134
135 // Initialize a new struct or table from existing data.
136 void NewRootTypeFromBuffer(const StructDef &struct_def,
137 std::string *code_ptr) {
138 std::string &code = *code_ptr;
139
140 code += "function " + NormalizedName(struct_def) + ".GetRootAs" + NormalizedName(struct_def) + "(buf, offset)\n";
141 code += std::string(Indent) + "local n = flatbuffers.N.UOffsetT:Unpack(buf, offset)\n";
142 code += std::string(Indent) + "local o = " + NormalizedName(struct_def) + ".New()\n";
143 code += std::string(Indent) + "o:Init(buf, n + offset)\n";
144 code += std::string(Indent) + "return o\n";
145 code += EndFunc;
146 }
147
148 // Initialize an existing object with other data, to avoid an allocation.
149 void InitializeExisting(const StructDef &struct_def,
150 std::string *code_ptr) {
151 std::string &code = *code_ptr;
152
153 GenReceiver(struct_def, code_ptr);
154 code += "Init(buf, pos)\n";
155 code += std::string(Indent) + SelfData + " = flatbuffers.view.New(buf, pos)\n";
156 code += EndFunc;
157 }
158
159 // Get the length of a vector.
160 void GetVectorLen(const StructDef &struct_def, const FieldDef &field,
161 std::string *code_ptr) {
162 std::string &code = *code_ptr;
163
164 GenReceiver(struct_def, code_ptr);
165 code += MakeCamel(NormalizedName(field)) + "Length()\n";
166 code += OffsetPrefix(field);
167 code += std::string(Indent) + Indent + "return " + SelfData + ":VectorLen(o)\n";
168 code += std::string(Indent) + End;
169 code += std::string(Indent) + "return 0\n";
170 code += EndFunc;
171 }
172
173 // Get the value of a struct's scalar.
174 void GetScalarFieldOfStruct(const StructDef &struct_def,
175 const FieldDef &field,
176 std::string *code_ptr) {
177 std::string &code = *code_ptr;
178 std::string getter = GenGetter(field.value.type);
179 GenReceiver(struct_def, code_ptr);
180 code += MakeCamel(NormalizedName(field));
181 code += "()\n";
182 code += std::string(Indent) + "return " + getter;
183 code += std::string(SelfDataPos) + " + " + NumToString(field.value.offset) + ")\n";
184 code += EndFunc;
185 }
186
187 // Get the value of a table's scalar.
188 void GetScalarFieldOfTable(const StructDef &struct_def,
189 const FieldDef &field,
190 std::string *code_ptr) {
191 std::string &code = *code_ptr;
192 std::string getter = GenGetter(field.value.type);
193 GenReceiver(struct_def, code_ptr);
194 code += MakeCamel(NormalizedName(field));
195 code += "()\n";
196 code += OffsetPrefix(field);
197 getter += std::string("o + ") + SelfDataPos + ")";
198 auto is_bool = field.value.type.base_type == BASE_TYPE_BOOL;
199 if (is_bool) {
200 getter = "(" + getter + " ~= 0)";
201 }
202 code += std::string(Indent) + Indent + "return " + getter + "\n";
203 code += std::string(Indent) + End;
204 std::string default_value;
205 if (is_bool) {
206 default_value = field.value.constant == "0" ? "false" : "true";
207 }
208 else {
209 default_value = field.value.constant;
210 }
211 code += std::string(Indent) + "return " + default_value + "\n";
212 code += EndFunc;
213 }
214
215 // Get a struct by initializing an existing struct.
216 // Specific to Struct.
217 void GetStructFieldOfStruct(const StructDef &struct_def,
218 const FieldDef &field,
219 std::string *code_ptr) {
220 std::string &code = *code_ptr;
221 GenReceiver(struct_def, code_ptr);
222 code += MakeCamel(NormalizedName(field));
223 code += "(obj)\n";
224 code += std::string(Indent) + "obj:Init(" + SelfDataBytes + ", " + SelfDataPos + " + ";
225 code += NumToString(field.value.offset) + ")\n";
226 code += std::string(Indent) + "return obj\n";
227 code += EndFunc;
228 }
229
230 // Get a struct by initializing an existing struct.
231 // Specific to Table.
232 void GetStructFieldOfTable(const StructDef &struct_def,
233 const FieldDef &field,
234 std::string *code_ptr) {
235 std::string &code = *code_ptr;
236 GenReceiver(struct_def, code_ptr);
237 code += MakeCamel(NormalizedName(field));
238 code += "()\n";
239 code += OffsetPrefix(field);
240 if (field.value.type.struct_def->fixed) {
241 code += std::string(Indent) + Indent + "local x = o + " + SelfDataPos + "\n";
242 }
243 else {
244 code += std::string(Indent) + Indent + "local x = " + SelfData + ":Indirect(o + " + SelfDataPos + ")\n";
245 }
246 code += std::string(Indent) + Indent + "local obj = require('" + TypeNameWithNamespace(field) + "').New()\n";
247 code += std::string(Indent) + Indent + "obj:Init(" + SelfDataBytes + ", x)\n";
248 code += std::string(Indent) + Indent + "return obj\n";
249 code += std::string(Indent) + End;
250 code += EndFunc;
251 }
252
253 // Get the value of a string.
254 void GetStringField(const StructDef &struct_def, const FieldDef &field,
255 std::string *code_ptr) {
256 std::string &code = *code_ptr;
257 GenReceiver(struct_def, code_ptr);
258 code += MakeCamel(NormalizedName(field));
259 code += "()\n";
260 code += OffsetPrefix(field);
261 code += std::string(Indent) + Indent + "return " + GenGetter(field.value.type);
262 code += std::string("o + ") + SelfDataPos + ")\n";
263 code += std::string(Indent) + End;
264 code += EndFunc;
265 }
266
267 // Get the value of a union from an object.
268 void GetUnionField(const StructDef &struct_def, const FieldDef &field,
269 std::string *code_ptr) {
270 std::string &code = *code_ptr;
271 GenReceiver(struct_def, code_ptr);
272 code += MakeCamel(NormalizedName(field)) + "()\n";
273 code += OffsetPrefix(field);
274
275 // TODO(rw): this works and is not the good way to it:
276 //bool is_native_table = TypeName(field) == "*flatbuffers.Table";
277 //if (is_native_table) {
278 // code += std::string(Indent) + Indent + "from flatbuffers.table import Table\n";
279 //} else {
280 // code += std::string(Indent) + Indent +
281 // code += "from ." + TypeName(field) + " import " + TypeName(field) + "\n";
282 //}
283 code += std::string(Indent) + Indent + "local obj = flatbuffers.view.New(require('flatbuffers.binaryarray').New(0), 0)\n";
284 code += std::string(Indent) + Indent + GenGetter(field.value.type) + "obj, o)\n";
285 code += std::string(Indent) + Indent + "return obj\n";
286 code += std::string(Indent) + End;
287 code += EndFunc;
288 }
289
290 // Get the value of a vector's struct member.
291 void GetMemberOfVectorOfStruct(const StructDef &struct_def,
292 const FieldDef &field,
293 std::string *code_ptr) {
294 std::string &code = *code_ptr;
295 auto vectortype = field.value.type.VectorType();
296
297 GenReceiver(struct_def, code_ptr);
298 code += MakeCamel(NormalizedName(field));
299 code += "(j)\n";
300 code += OffsetPrefix(field);
301 code += std::string(Indent) + Indent + "local x = " + SelfData + ":Vector(o)\n";
302 code += std::string(Indent) + Indent + "x = x + ((j-1) * ";
303 code += NumToString(InlineSize(vectortype)) + ")\n";
304 if (!(vectortype.struct_def->fixed)) {
305 code += std::string(Indent) + Indent + "x = " + SelfData + ":Indirect(x)\n";
306 }
307 code += std::string(Indent) + Indent + "local obj = require('" + TypeNameWithNamespace(field) + "').New()\n";
308 code += std::string(Indent) + Indent + "obj:Init(" + SelfDataBytes + ", x)\n";
309 code += std::string(Indent) + Indent + "return obj\n";
310 code += std::string(Indent) + End;
311 code += EndFunc;
312 }
313
314 // Get the value of a vector's non-struct member. Uses a named return
315 // argument to conveniently set the zero value for the result.
316 void GetMemberOfVectorOfNonStruct(const StructDef &struct_def,
317 const FieldDef &field,
318 std::string *code_ptr) {
319 std::string &code = *code_ptr;
320 auto vectortype = field.value.type.VectorType();
321
322 GenReceiver(struct_def, code_ptr);
323 code += MakeCamel(NormalizedName(field));
324 code += "(j)\n";
325 code += OffsetPrefix(field);
326 code += std::string(Indent) + Indent + "local a = " + SelfData + ":Vector(o)\n";
327 code += std::string(Indent) + Indent;
328 code += "return " + GenGetter(field.value.type);
329 code += "a + ((j-1) * ";
330 code += NumToString(InlineSize(vectortype)) + "))\n";
331 code += std::string(Indent) + End;
332 if (vectortype.base_type == BASE_TYPE_STRING) {
333 code += std::string(Indent) + "return ''\n";
334 }
335 else {
336 code += std::string(Indent) + "return 0\n";
337 }
338 code += EndFunc;
339 }
340
341 // Begin the creator function signature.
342 void BeginBuilderArgs(const StructDef &struct_def,
343 std::string *code_ptr) {
344 std::string &code = *code_ptr;
345
346 code += "function " + NormalizedName(struct_def) + ".Create" + NormalizedName(struct_def);
347 code += "(builder";
348 }
349
350 // Recursively generate arguments for a constructor, to deal with nested
351 // structs.
352 void StructBuilderArgs(const StructDef &struct_def,
353 const char *nameprefix, std::string *code_ptr) {
354 for (auto it = struct_def.fields.vec.begin();
355 it != struct_def.fields.vec.end(); ++it) {
356 auto &field = **it;
357 if (IsStruct(field.value.type)) {
358 // Generate arguments for a struct inside a struct. To ensure names
359 // don't clash, and to make it obvious these arguments are constructing
360 // a nested struct, prefix the name with the field name.
361 StructBuilderArgs(*field.value.type.struct_def,
362 (nameprefix + (NormalizedName(field) + "_")).c_str(), code_ptr);
363 }
364 else {
365 std::string &code = *code_ptr;
366 code += std::string(", ") + nameprefix;
367 code += MakeCamel(NormalizedName(field), false);
368 }
369 }
370 }
371
372 // End the creator function signature.
373 void EndBuilderArgs(std::string *code_ptr) {
374 std::string &code = *code_ptr;
375 code += ")\n";
376 }
377
378 // Recursively generate struct construction statements and instert manual
379 // padding.
380 void StructBuilderBody(const StructDef &struct_def,
381 const char *nameprefix, std::string *code_ptr) {
382 std::string &code = *code_ptr;
383 code += std::string(Indent) + "builder:Prep(" + NumToString(struct_def.minalign) + ", ";
384 code += NumToString(struct_def.bytesize) + ")\n";
385 for (auto it = struct_def.fields.vec.rbegin();
386 it != struct_def.fields.vec.rend(); ++it) {
387 auto &field = **it;
388 if (field.padding)
389 code += std::string(Indent) + "builder:Pad(" + NumToString(field.padding) + ")\n";
390 if (IsStruct(field.value.type)) {
391 StructBuilderBody(*field.value.type.struct_def,
392 (nameprefix + (NormalizedName(field) + "_")).c_str(), code_ptr);
393 }
394 else {
395 code += std::string(Indent) + "builder:Prepend" + GenMethod(field) + "(";
396 code += nameprefix + MakeCamel(NormalizedName(field), false) + ")\n";
397 }
398 }
399 }
400
401 void EndBuilderBody(std::string *code_ptr) {
402 std::string &code = *code_ptr;
403 code += std::string(Indent) + "return builder:Offset()\n";
404 code += EndFunc;
405 }
406
407 // Get the value of a table's starting offset.
408 void GetStartOfTable(const StructDef &struct_def,
409 std::string *code_ptr) {
410 std::string &code = *code_ptr;
411 code += "function " + NormalizedName(struct_def) + ".Start";
412 code += "(builder) ";
413 code += "builder:StartObject(";
414 code += NumToString(struct_def.fields.vec.size());
415 code += ") end\n";
416 }
417
418 // Set the value of a table's field.
419 void BuildFieldOfTable(const StructDef &struct_def,
420 const FieldDef &field, const size_t offset,
421 std::string *code_ptr) {
422 std::string &code = *code_ptr;
423 code += "function " + NormalizedName(struct_def) + ".Add" + MakeCamel(NormalizedName(field));
424 code += "(builder, ";
425 code += MakeCamel(NormalizedName(field), false);
426 code += ") ";
427 code += "builder:Prepend";
428 code += GenMethod(field) + "Slot(";
429 code += NumToString(offset) + ", ";
430 // todo: i don't need to cast in Lua, but am I missing something?
431 // if (!IsScalar(field.value.type.base_type) && (!struct_def.fixed)) {
432 // code += "flatbuffers.N.UOffsetTFlags.py_type";
433 // code += "(";
434 // code += MakeCamel(NormalizedName(field), false) + ")";
435 // } else {
436 code += MakeCamel(NormalizedName(field), false);
437 // }
438 code += ", " + field.value.constant;
439 code += ") end\n";
440 }
441
442 // Set the value of one of the members of a table's vector.
443 void BuildVectorOfTable(const StructDef &struct_def,
444 const FieldDef &field, std::string *code_ptr) {
445 std::string &code = *code_ptr;
446 code += "function " + NormalizedName(struct_def) + ".Start";
447 code += MakeCamel(NormalizedName(field));
448 code += "Vector(builder, numElems) return builder:StartVector(";
449 auto vector_type = field.value.type.VectorType();
450 auto alignment = InlineAlignment(vector_type);
451 auto elem_size = InlineSize(vector_type);
452 code += NumToString(elem_size);
453 code += ", numElems, " + NumToString(alignment);
454 code += ") end\n";
455 }
456
457 // Get the offset of the end of a table.
458 void GetEndOffsetOnTable(const StructDef &struct_def,
459 std::string *code_ptr) {
460 std::string &code = *code_ptr;
461 code += "function " + NormalizedName(struct_def) + ".End";
462 code += "(builder) ";
463 code += "return builder:EndObject() end\n";
464 }
465
466 // Generate the receiver for function signatures.
467 void GenReceiver(const StructDef &struct_def, std::string *code_ptr) {
468 std::string &code = *code_ptr;
469 code += "function " + NormalizedMetaName(struct_def) + ":";
470 }
471
472 // Generate a struct field, conditioned on its child type(s).
473 void GenStructAccessor(const StructDef &struct_def,
474 const FieldDef &field, std::string *code_ptr) {
475 GenComment(field.doc_comment, code_ptr, nullptr, Comment);
476 if (IsScalar(field.value.type.base_type)) {
477 if (struct_def.fixed) {
478 GetScalarFieldOfStruct(struct_def, field, code_ptr);
479 }
480 else {
481 GetScalarFieldOfTable(struct_def, field, code_ptr);
482 }
483 }
484 else {
485 switch (field.value.type.base_type) {
486 case BASE_TYPE_STRUCT:
487 if (struct_def.fixed) {
488 GetStructFieldOfStruct(struct_def, field, code_ptr);
489 }
490 else {
491 GetStructFieldOfTable(struct_def, field, code_ptr);
492 }
493 break;
494 case BASE_TYPE_STRING: GetStringField(struct_def, field, code_ptr); break;
495 case BASE_TYPE_VECTOR: {
496 auto vectortype = field.value.type.VectorType();
497 if (vectortype.base_type == BASE_TYPE_STRUCT) {
498 GetMemberOfVectorOfStruct(struct_def, field, code_ptr);
499 }
500 else {
501 GetMemberOfVectorOfNonStruct(struct_def, field, code_ptr);
502 }
503 break;
504 }
505 case BASE_TYPE_UNION: GetUnionField(struct_def, field, code_ptr); break;
506 default: FLATBUFFERS_ASSERT(0);
507 }
508 }
509 if (field.value.type.base_type == BASE_TYPE_VECTOR) {
510 GetVectorLen(struct_def, field, code_ptr);
511 }
512 }
513
514 // Generate table constructors, conditioned on its members' types.
515 void GenTableBuilders(const StructDef &struct_def,
516 std::string *code_ptr) {
517 GetStartOfTable(struct_def, code_ptr);
518
519 for (auto it = struct_def.fields.vec.begin();
520 it != struct_def.fields.vec.end(); ++it) {
521 auto &field = **it;
522 if (field.deprecated) continue;
523
524 auto offset = it - struct_def.fields.vec.begin();
525 BuildFieldOfTable(struct_def, field, offset, code_ptr);
526 if (field.value.type.base_type == BASE_TYPE_VECTOR) {
527 BuildVectorOfTable(struct_def, field, code_ptr);
528 }
529 }
530
531 GetEndOffsetOnTable(struct_def, code_ptr);
532 }
533
534 // Generate struct or table methods.
535 void GenStruct(const StructDef &struct_def, std::string *code_ptr) {
536 if (struct_def.generated) return;
537
538 GenComment(struct_def.doc_comment, code_ptr, nullptr, Comment);
539 BeginClass(struct_def, code_ptr);
540
541 GenerateNewObjectPrototype(struct_def, code_ptr);
542
543 if (!struct_def.fixed) {
544 // Generate a special accessor for the table that has been declared as
545 // the root type.
546 NewRootTypeFromBuffer(struct_def, code_ptr);
547 }
548
549 // Generate the Init method that sets the field in a pre-existing
550 // accessor object. This is to allow object reuse.
551 InitializeExisting(struct_def, code_ptr);
552 for (auto it = struct_def.fields.vec.begin();
553 it != struct_def.fields.vec.end(); ++it) {
554 auto &field = **it;
555 if (field.deprecated) continue;
556
557 GenStructAccessor(struct_def, field, code_ptr);
558 }
559
560 if (struct_def.fixed) {
561 // create a struct constructor function
562 GenStructBuilder(struct_def, code_ptr);
563 }
564 else {
565 // Create a set of functions that allow table construction.
566 GenTableBuilders(struct_def, code_ptr);
567 }
568 }
569
570 // Generate enum declarations.
571 void GenEnum(const EnumDef &enum_def, std::string *code_ptr) {
572 if (enum_def.generated) return;
573
574 GenComment(enum_def.doc_comment, code_ptr, nullptr, Comment);
575 BeginEnum(NormalizedName(enum_def), code_ptr);
576 for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end();
577 ++it) {
578 auto &ev = **it;
579 GenComment(ev.doc_comment, code_ptr, nullptr, Comment);
580 EnumMember(enum_def, ev, code_ptr);
581 }
582 EndEnum(code_ptr);
583 }
584
585 // Returns the function name that is able to read a value of the given type.
586 std::string GenGetter(const Type &type) {
587 switch (type.base_type) {
588 case BASE_TYPE_STRING: return std::string(SelfData) + ":String(";
589 case BASE_TYPE_UNION: return std::string(SelfData) + ":Union(";
590 case BASE_TYPE_VECTOR: return GenGetter(type.VectorType());
591 default:
592 return std::string(SelfData) + ":Get(flatbuffers.N." +
593 MakeCamel(GenTypeGet(type)) + ", ";
594 }
595 }
596
597 // Returns the method name for use with add/put calls.
598 std::string GenMethod(const FieldDef &field) {
599 return IsScalar(field.value.type.base_type)
600 ? MakeCamel(GenTypeBasic(field.value.type))
601 : (IsStruct(field.value.type) ? "Struct" : "UOffsetTRelative");
602 }
603
604 std::string GenTypeBasic(const Type &type) {
605 static const char *ctypename[] = {
606 // clang-format off
607 #define FLATBUFFERS_TD(ENUM, IDLTYPE, \
608 CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, RTYPE) \
609 #PTYPE,
610 FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
611 #undef FLATBUFFERS_TD
612 // clang-format on
613 };
614 return ctypename[type.base_type];
615 }
616
617 std::string GenTypePointer(const Type &type) {
618 switch (type.base_type) {
619 case BASE_TYPE_STRING: return "string";
620 case BASE_TYPE_VECTOR: return GenTypeGet(type.VectorType());
621 case BASE_TYPE_STRUCT: return type.struct_def->name;
622 case BASE_TYPE_UNION:
623 // fall through
624 default: return "*flatbuffers.Table";
625 }
626 }
627
628 std::string GenTypeGet(const Type &type) {
629 return IsScalar(type.base_type) ? GenTypeBasic(type) : GenTypePointer(type);
630 }
631
632 std::string GetNamespace(const Type &type) {
633 return type.struct_def->defined_namespace->GetFullyQualifiedName(type.struct_def->name);
634 }
635
636 std::string TypeName(const FieldDef &field) {
637 return GenTypeGet(field.value.type);
638 }
639
640 std::string TypeNameWithNamespace(const FieldDef &field) {
641 return GetNamespace(field.value.type);
642 }
643
644 // Create a struct with a builder and the struct's arguments.
645 void GenStructBuilder(const StructDef &struct_def,
646 std::string *code_ptr) {
647 BeginBuilderArgs(struct_def, code_ptr);
648 StructBuilderArgs(struct_def, "", code_ptr);
649 EndBuilderArgs(code_ptr);
650
651 StructBuilderBody(struct_def, "", code_ptr);
652 EndBuilderBody(code_ptr);
653 }
654
655 bool generate() {
656 if (!generateEnums()) return false;
657 if (!generateStructs()) return false;
658 return true;
659 }
660
661 private:
662 bool generateEnums() {
663 for (auto it = parser_.enums_.vec.begin(); it != parser_.enums_.vec.end();
664 ++it) {
665 auto &enum_def = **it;
666 std::string enumcode;
667 GenEnum(enum_def, &enumcode);
668 if (!SaveType(enum_def, enumcode, false)) return false;
669 }
670 return true;
671 }
672
673 bool generateStructs() {
674 for (auto it = parser_.structs_.vec.begin();
675 it != parser_.structs_.vec.end(); ++it) {
676 auto &struct_def = **it;
677 std::string declcode;
678 GenStruct(struct_def, &declcode);
679 if (!SaveType(struct_def, declcode, true)) return false;
680 }
681 return true;
682 }
683
684 // Begin by declaring namespace and imports.
685 void BeginFile(const std::string name_space_name, const bool needs_imports,
686 std::string *code_ptr) {
687 std::string &code = *code_ptr;
688 code += std::string(Comment) + FlatBuffersGeneratedWarning() + "\n\n";
689 code += std::string(Comment) + "namespace: " + name_space_name + "\n\n";
690 if (needs_imports) {
691 code += "local flatbuffers = require('flatbuffers')\n\n";
692 }
693 }
694
695 // Save out the generated code for a Lua Table type.
696 bool SaveType(const Definition &def, const std::string &classcode,
697 bool needs_imports) {
698 if (!classcode.length()) return true;
699
700 std::string namespace_dir = path_;
701 auto &namespaces = def.defined_namespace->components;
702 for (auto it = namespaces.begin(); it != namespaces.end(); ++it) {
703 if (it != namespaces.begin()) namespace_dir += kPathSeparator;
704 namespace_dir += *it;
705 //std::string init_py_filename = namespace_dir + "/__init__.py";
706 //SaveFile(init_py_filename.c_str(), "", false);
707 }
708
709 std::string code = "";
710 BeginFile(LastNamespacePart(*def.defined_namespace), needs_imports, &code);
711 code += classcode;
712 code += "\n";
713 code += "return " + NormalizedName(def) + " " + Comment + "return the module";
714 std::string filename =
715 NamespaceDir(*def.defined_namespace) + NormalizedName(def) + ".lua";
716 return SaveFile(filename.c_str(), code, false);
717 }
718 private:
719 std::unordered_set<std::string> keywords_;
720 };
721
722} // namespace lua
723
724bool GenerateLua(const Parser &parser, const std::string &path,
725 const std::string &file_name) {
726 lua::LuaGenerator generator(parser, path, file_name);
727 return generator.generate();
728}
729
730} // namespace flatbuffers
731