mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-03 20:31:23 +00:00
memory leak fix + php/js/python transition to class based generator
This commit is contained in:
@@ -19,6 +19,7 @@
|
||||
#include "flatbuffers/flatbuffers.h"
|
||||
#include "flatbuffers/idl.h"
|
||||
#include "flatbuffers/util.h"
|
||||
#include "flatbuffers/code_generators.h"
|
||||
|
||||
namespace flatbuffers {
|
||||
namespace js {
|
||||
@@ -661,60 +662,76 @@ static void GenStruct(const Parser &parser, StructDef &struct_def,
|
||||
|
||||
} // namespace js
|
||||
|
||||
// Iterate through all definitions we haven't generate code for (enums, structs,
|
||||
// and tables) and output them to a single file.
|
||||
std::string GenerateJS(const Parser &parser) {
|
||||
using namespace js;
|
||||
|
||||
// Generate code for all the enum declarations.
|
||||
std::string enum_code, exports_code;
|
||||
for (auto it = parser.enums_.vec.begin();
|
||||
it != parser.enums_.vec.end(); ++it) {
|
||||
GenEnum(**it, &enum_code, &exports_code);
|
||||
}
|
||||
|
||||
// Generate code for all structs, then all tables.
|
||||
std::string decl_code;
|
||||
for (auto it = parser.structs_.vec.begin();
|
||||
it != parser.structs_.vec.end(); ++it) {
|
||||
GenStruct(parser, **it, &decl_code, &exports_code);
|
||||
}
|
||||
|
||||
// Only output file-level code if there were any declarations.
|
||||
if (enum_code.length() || decl_code.length()) {
|
||||
std::string code;
|
||||
code = "// automatically generated by the FlatBuffers compiler,"
|
||||
" do not modify\n\n";
|
||||
|
||||
// Generate code for all the namespace declarations.
|
||||
GenNamespaces(parser, &code, &exports_code);
|
||||
|
||||
// Output the main declaration code from above.
|
||||
code += enum_code;
|
||||
code += decl_code;
|
||||
|
||||
if (!exports_code.empty() && !parser.opts.skip_js_exports) {
|
||||
code += "// Exports for Node.js and RequireJS\n";
|
||||
code += exports_code;
|
||||
}
|
||||
|
||||
return code;
|
||||
}
|
||||
|
||||
return std::string();
|
||||
}
|
||||
|
||||
static std::string GeneratedFileName(const std::string &path,
|
||||
const std::string &file_name) {
|
||||
return path + file_name + "_generated.js";
|
||||
}
|
||||
|
||||
bool GenerateJS(const Parser &parser,
|
||||
const std::string &path,
|
||||
const std::string &file_name) {
|
||||
auto code = GenerateJS(parser);
|
||||
namespace js {
|
||||
// Iterate through all definitions we haven't generate code for (enums, structs,
|
||||
// and tables) and output them to a single file.
|
||||
class JsGenerator : public BaseGenerator {
|
||||
public:
|
||||
JsGenerator(const Parser &parser, const std::string &path,
|
||||
const std::string &file_name)
|
||||
: BaseGenerator(parser, path, file_name){};
|
||||
// Iterate through all definitions we haven't generate code for (enums,
|
||||
// structs, and tables) and output them to a single file.
|
||||
bool generate() {
|
||||
std::string enum_code, struct_code, exports_code, code;
|
||||
generateEnums(&enum_code, &exports_code);
|
||||
generateStructs(&struct_code, &exports_code);
|
||||
|
||||
// Only output file-level code if there were any declarations.
|
||||
if (enum_code.length() || struct_code.length()) {
|
||||
code +=
|
||||
"// automatically generated by the FlatBuffers compiler, do not "
|
||||
"modify\n\n";
|
||||
|
||||
// Generate code for all the namespace declarations.
|
||||
GenNamespaces(parser_, &code, &exports_code);
|
||||
|
||||
// Output the main declaration code from above.
|
||||
code += enum_code;
|
||||
code += struct_code;
|
||||
|
||||
if (!exports_code.empty() && !parser_.opts.skip_js_exports) {
|
||||
code += "// Exports for Node.js and RequireJS\n";
|
||||
code += exports_code;
|
||||
}
|
||||
}
|
||||
|
||||
return !code.length() ||
|
||||
SaveFile(GeneratedFileName(path, file_name).c_str(), code, false);
|
||||
SaveFile(GeneratedFileName(path_, file_name_).c_str(), code, false);
|
||||
}
|
||||
|
||||
private:
|
||||
// Generate code for all enums.
|
||||
void generateEnums(std::string *enum_code_ptr,
|
||||
std::string *exports_code_ptr) {
|
||||
for (auto it = parser_.enums_.vec.begin(); it != parser_.enums_.vec.end();
|
||||
++it) {
|
||||
auto &enum_def = **it;
|
||||
GenEnum(enum_def, enum_code_ptr, exports_code_ptr);
|
||||
}
|
||||
}
|
||||
|
||||
// Generate code for all structs.
|
||||
void generateStructs(std::string *decl_code_ptr,
|
||||
std::string *exports_code_ptr) {
|
||||
for (auto it = parser_.structs_.vec.begin();
|
||||
it != parser_.structs_.vec.end(); ++it) {
|
||||
auto &struct_def = **it;
|
||||
GenStruct(parser_, struct_def, decl_code_ptr, exports_code_ptr);
|
||||
}
|
||||
}
|
||||
};
|
||||
} // namespace js
|
||||
|
||||
bool GenerateJS(const Parser &parser, const std::string &path,
|
||||
const std::string &file_name) {
|
||||
js::JsGenerator generator(parser, path, file_name);
|
||||
return generator.generate();
|
||||
}
|
||||
|
||||
std::string JSMakeRule(const Parser &parser,
|
||||
|
||||
Reference in New Issue
Block a user