Skip to content

Commit 224e33e

Browse files
committed
Add support for JavaScript code generation
This adds a JavaScript language target. The generated JavaScript uses Google Closure Compiler type annotations and can be compiled using the advanced compilation mode, which performs type checking and optimizations such as inlining and dead code elimination. The generated JavaScript also exports all generated symbols for use with Node.js and RequireJS. This export behavior can be turned off with the --no-js-exports flag for use with Google Closure Compiler.
1 parent 94680f5 commit 224e33e

File tree

10 files changed

+2818
-2
lines changed

10 files changed

+2818
-2
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ snapshot.sh
4444
tests/go_gen
4545
tests/monsterdata_java_wire.mon
4646
tests/monsterdata_go_wire.mon
47+
tests/monsterdata_javascript_wire.mon
4748
CMakeLists.txt.user
4849
CMakeScripts/**
4950
CTestTestfile.cmake
@@ -55,4 +56,4 @@ java/.idea
5556
java/*.iml
5657
java/target
5758
**/*.pyc
58-
.idea
59+
.idea

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ set(FlatBuffers_Compiler_SRCS
3333
src/idl_gen_cpp.cpp
3434
src/idl_gen_general.cpp
3535
src/idl_gen_go.cpp
36+
src/idl_gen_js.cpp
3637
src/idl_gen_python.cpp
3738
src/idl_gen_fbs.cpp
3839
src/flatc.cpp

include/flatbuffers/idl.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,7 @@ extern void GenComment(const std::vector<std::string> &dc,
449449
// Container of options that may apply to any of the source/text generators.
450450
struct GeneratorOptions {
451451
bool strict_json;
452+
bool skip_js_exports;
452453
bool output_default_scalars_in_json;
453454
int indent_step;
454455
bool output_enum_identifiers;
@@ -464,6 +465,7 @@ struct GeneratorOptions {
464465
Language lang;
465466

466467
GeneratorOptions() : strict_json(false),
468+
skip_js_exports(false),
467469
output_default_scalars_in_json(false),
468470
indent_step(2),
469471
output_enum_identifiers(true), prefixed_enums(true), scoped_enums(false),
@@ -506,6 +508,15 @@ extern bool GenerateCPP(const Parser &parser,
506508
const std::string &file_name,
507509
const GeneratorOptions &opts);
508510

511+
// Generate JavaScript code from the definitions in the Parser object.
512+
// See idl_gen_js.
513+
extern std::string GenerateJS(const Parser &parser,
514+
const GeneratorOptions &opts);
515+
extern bool GenerateJS(const Parser &parser,
516+
const std::string &path,
517+
const std::string &file_name,
518+
const GeneratorOptions &opts);
519+
509520
// Generate Go files from the definitions in the Parser object.
510521
// See idl_gen_go.cpp.
511522
extern bool GenerateGo(const Parser &parser,
@@ -551,6 +562,13 @@ extern bool GenerateFBS(const Parser &parser,
551562
const std::string &file_name,
552563
const GeneratorOptions &opts);
553564

565+
// Generate a make rule for the generated JavaScript code.
566+
// See idl_gen_js.cpp.
567+
extern std::string JSMakeRule(const Parser &parser,
568+
const std::string &path,
569+
const std::string &file_name,
570+
const GeneratorOptions &opts);
571+
554572
// Generate a make rule for the generated C++ header.
555573
// See idl_gen_cpp.cpp.
556574
extern std::string CPPMakeRule(const Parser &parser,

0 commit comments

Comments
 (0)