mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-02 12:05:50 +00:00
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.
This commit is contained in:
116
tests/JavaScriptTest.js
Normal file
116
tests/JavaScriptTest.js
Normal file
@@ -0,0 +1,116 @@
|
||||
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();
|
||||
Reference in New Issue
Block a user