Commit 224e33ed authored by Evan Wallace's avatar Evan Wallace

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.
parent 94680f54
......@@ -44,6 +44,7 @@ snapshot.sh
tests/go_gen
tests/monsterdata_java_wire.mon
tests/monsterdata_go_wire.mon
tests/monsterdata_javascript_wire.mon
CMakeLists.txt.user
CMakeScripts/**
CTestTestfile.cmake
......
......@@ -33,6 +33,7 @@ set(FlatBuffers_Compiler_SRCS
src/idl_gen_cpp.cpp
src/idl_gen_general.cpp
src/idl_gen_go.cpp
src/idl_gen_js.cpp
src/idl_gen_python.cpp
src/idl_gen_fbs.cpp
src/flatc.cpp
......
......@@ -449,6 +449,7 @@ extern void GenComment(const std::vector<std::string> &dc,
// Container of options that may apply to any of the source/text generators.
struct GeneratorOptions {
bool strict_json;
bool skip_js_exports;
bool output_default_scalars_in_json;
int indent_step;
bool output_enum_identifiers;
......@@ -464,6 +465,7 @@ struct GeneratorOptions {
Language lang;
GeneratorOptions() : strict_json(false),
skip_js_exports(false),
output_default_scalars_in_json(false),
indent_step(2),
output_enum_identifiers(true), prefixed_enums(true), scoped_enums(false),
......@@ -506,6 +508,15 @@ extern bool GenerateCPP(const Parser &parser,
const std::string &file_name,
const GeneratorOptions &opts);
// Generate JavaScript code from the definitions in the Parser object.
// See idl_gen_js.
extern std::string GenerateJS(const Parser &parser,
const GeneratorOptions &opts);
extern bool GenerateJS(const Parser &parser,
const std::string &path,
const std::string &file_name,
const GeneratorOptions &opts);
// Generate Go files from the definitions in the Parser object.
// See idl_gen_go.cpp.
extern bool GenerateGo(const Parser &parser,
......@@ -551,6 +562,13 @@ extern bool GenerateFBS(const Parser &parser,
const std::string &file_name,
const GeneratorOptions &opts);
// Generate a make rule for the generated JavaScript code.
// See idl_gen_js.cpp.
extern std::string JSMakeRule(const Parser &parser,
const std::string &path,
const std::string &file_name,
const GeneratorOptions &opts);
// Generate a make rule for the generated C++ header.
// See idl_gen_cpp.cpp.
extern std::string CPPMakeRule(const Parser &parser,
......
This diff is collapsed.
......@@ -60,6 +60,10 @@ const Generator generators[] = {
flatbuffers::GeneratorOptions::kJava,
"Generate Java classes for tables/structs",
flatbuffers::GeneralMakeRule },
{ flatbuffers::GenerateJS, "-s", "JavaScript",
flatbuffers::GeneratorOptions::kMAX,
"Generate JavaScript code for tables/structs",
flatbuffers::JSMakeRule },
{ flatbuffers::GenerateGeneral, "-n", "C#",
flatbuffers::GeneratorOptions::kCSharp,
"Generate C# classes for tables/structs",
......@@ -140,6 +144,8 @@ int main(int argc, const char *argv[]) {
include_directories.push_back(argv[argi]);
} else if(arg == "--strict-json") {
opts.strict_json = true;
} else if(arg == "--no-js-exports") {
opts.skip_js_exports = true;
} else if(arg == "--defaults-json") {
opts.output_default_scalars_in_json = true;
} else if(arg == "--no-prefix") {
......
This diff is collapsed.
var assert = require('assert');
var fs = require('fs');
var flatbuffers = require('../js/flatbuffers').flatbuffers;
var MyGame = require('./monster_test_generated').MyGame;
function main() {
// First, let's test reading a FlatBuffer generated by C++ code:
// This file was generated from monsterdata_test.json
var data = new Uint8Array(fs.readFileSync('monsterdata_test.mon'));
// Now test it:
var bb = new flatbuffers.ByteBuffer(data);
testBuffer(bb);
// Second, let's create a FlatBuffer from scratch in JavaScript, and test it also.
// We use an initial size of 1 to exercise the reallocation algorithm,
// normally a size larger than the typical FlatBuffer you generate would be
// better for performance.
var fbb = new flatbuffers.Builder(1);
// We set up the same values as monsterdata.json:
var str = fbb.createString('MyMonster');
var inv = MyGame.Example.Monster.createInventoryVector(fbb, [0, 1, 2, 3, 4]);
var fred = fbb.createString('Fred');
MyGame.Example.Monster.startMonster(fbb);
MyGame.Example.Monster.addName(fbb, fred);
var mon2 = MyGame.Example.Monster.endMonster(fbb);
MyGame.Example.Monster.startTest4Vector(fbb, 2);
MyGame.Example.Test.createTest(fbb, 10, 20);
MyGame.Example.Test.createTest(fbb, 30, 40);
var test4 = fbb.endVector();
var testArrayOfString = MyGame.Example.Monster.createTestarrayofstringVector(fbb, [
fbb.createString('test1'),
fbb.createString('test2')
]);
MyGame.Example.Monster.startMonster(fbb);
MyGame.Example.Monster.addPos(fbb, MyGame.Example.Vec3.createVec3(fbb, 1, 2, 3, 3, MyGame.Example.Color.Green, 5, 6));
MyGame.Example.Monster.addHp(fbb, 80);
MyGame.Example.Monster.addName(fbb, str);
MyGame.Example.Monster.addInventory(fbb, inv);
MyGame.Example.Monster.addTestType(fbb, MyGame.Example.Any.Monster);
MyGame.Example.Monster.addTest(fbb, mon2);
MyGame.Example.Monster.addTest4(fbb, test4);
MyGame.Example.Monster.addTestarrayofstring(fbb, testArrayOfString);
MyGame.Example.Monster.addTestbool(fbb, false);
var mon = MyGame.Example.Monster.endMonster(fbb);
MyGame.Example.Monster.finishMonsterBuffer(fbb, mon);
// Write the result to a file for debugging purposes:
// Note that the binaries are not necessarily identical, since the JSON
// parser may serialize in a slightly different order than the above
// JavaScript code. They are functionally equivalent though.
fs.writeFileSync('monsterdata_javascript_wire.mon', new Buffer(fbb.asUint8Array()));
// Test it:
testBuffer(fbb.dataBuffer());
console.log('FlatBuffers test: completed successfully');
}
function testBuffer(bb) {
assert.ok(MyGame.Example.Monster.bufferHasIdentifier(bb));
var monster = MyGame.Example.Monster.getRootAsMonster(bb);
assert.strictEqual(monster.hp(), 80);
assert.strictEqual(monster.mana(), 150); // default
assert.strictEqual(monster.name(), 'MyMonster');
var pos = monster.pos();
assert.strictEqual(pos.x(), 1);
assert.strictEqual(pos.y(), 2);
assert.strictEqual(pos.z(), 3);
assert.strictEqual(pos.test1(), 3);
assert.strictEqual(pos.test2(), MyGame.Example.Color.Green);
var t = pos.test3();
assert.strictEqual(t.a(), 5);
assert.strictEqual(t.b(), 6);
assert.strictEqual(monster.testType(), MyGame.Example.Any.Monster);
var monster2 = new MyGame.Example.Monster();
assert.strictEqual(monster.test(monster2) != null, true);
assert.strictEqual(monster2.name(), 'Fred');
assert.strictEqual(monster.inventoryLength(), 5);
var invsum = 0;
for (var i = 0; i < monster.inventoryLength(); i++) {
invsum += monster.inventory(i);
}
assert.strictEqual(invsum, 10);
var test_0 = monster.test4(0);
var test_1 = monster.test4(1);
assert.strictEqual(monster.test4Length(), 2);
assert.strictEqual(test_0.a() + test_0.b() + test_1.a() + test_1.b(), 100);
assert.strictEqual(monster.testarrayofstringLength(), 2);
assert.strictEqual(monster.testarrayofstring(0), 'test1');
assert.strictEqual(monster.testarrayofstring(1), 'test2');
assert.strictEqual(monster.testbool(), false);
}
main();
#!/bin/sh
pushd "$(dirname $0)" >/dev/null
test_dir="$(pwd)"
node ${test_dir}/JavaScriptTest
../flatc -c -j -n -g -b -p --gen-mutable --no-includes monster_test.fbs monsterdata_test.json
../flatc -c -j -n -g -b -p -s --gen-mutable --no-includes monster_test.fbs monsterdata_test.json
../flatc -b --schema monster_test.fbs
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment