Commit 617fedf3 authored by Wouter van Oortmerssen's avatar Wouter van Oortmerssen

Merge pull request #3765 from Mischanix/js-typed-arrays

Javascript helper to get typed array views
parents 59caa536 9d92aeb1
......@@ -509,12 +509,24 @@ static void GenStruct(const Parser &parser, StructDef &struct_def,
}
code += "};\n\n";
// Emit a length helper
// Emit vector helpers
if (field.value.type.base_type == BASE_TYPE_VECTOR) {
// Emit a length helper
GenDocComment(code_ptr, "@returns {number}");
code += object_name + ".prototype." + MakeCamel(field.name, false);
code += "Length = function() {\n" + offset_prefix;
code += "this.bb.__vector_len(this.bb_pos + offset) : 0;\n};\n\n";
// For scalar types, emit a typed array helper
auto vectorType = field.value.type.VectorType();
if (IsScalar(vectorType.base_type)) {
GenDocComment(code_ptr, "@returns {" + GenType(vectorType) + "Array}");
code += object_name + ".prototype." + MakeCamel(field.name, false);
code += "Array = function() {\n" + offset_prefix;
code += "new " + GenType(vectorType) + "Array(this.bb.bytes().buffer, "
"this.bb.__vector(this.bb_pos + offset), "
"this.bb.__vector_len(this.bb_pos + offset)) : null;\n};\n\n";
}
}
}
......
......@@ -106,6 +106,13 @@ function testBuffer(bb) {
}
assert.strictEqual(invsum, 10);
var invsum2 = 0;
var invArr = monster.inventoryArray();
for (var i = 0; i < invArr.length; i++) {
invsum2 += invArr[i];
}
assert.strictEqual(invsum2, 10);
var test_0 = monster.test4(0);
var test_1 = monster.test4(1);
assert.strictEqual(monster.test4Length(), 2);
......@@ -169,7 +176,7 @@ function testUnicode() {
var json = JSON.parse(fs.readFileSync('unicode_test.json', 'utf8'));
// Test reading
var bb = new flatbuffers.ByteBuffer(new Uint8Array(correct));
function testReadingUnicode(bb) {
var monster = MyGame.Example.Monster.getRootAsMonster(bb);
assert.strictEqual(monster.name(), json.name);
assert.deepEqual(new Buffer(monster.name(flatbuffers.Encoding.UTF8_BYTES)), new Buffer(json.name));
......@@ -184,6 +191,8 @@ function testUnicode() {
assert.strictEqual(monster.testarrayofstring(i), string);
assert.deepEqual(new Buffer(monster.testarrayofstring(i, flatbuffers.Encoding.UTF8_BYTES)), new Buffer(string));
});
}
testReadingUnicode(new flatbuffers.ByteBuffer(new Uint8Array(correct)));
// Test writing
var fbb = new flatbuffers.Builder();
......@@ -203,7 +212,7 @@ function testUnicode() {
MyGame.Example.Monster.addTestarrayoftables(fbb, testarrayoftablesOffset);
MyGame.Example.Monster.addName(fbb, name);
MyGame.Example.Monster.finishMonsterBuffer(fbb, MyGame.Example.Monster.endMonster(fbb));
assert.deepEqual(new Buffer(fbb.asUint8Array()), correct);
testReadingUnicode(new flatbuffers.ByteBuffer(fbb.asUint8Array()));
}
var __imul = Math.imul ? Math.imul : function(a, b) {
......
......@@ -447,6 +447,14 @@ MyGame.Example.Monster.prototype.inventoryLength = function() {
return offset ? this.bb.__vector_len(this.bb_pos + offset) : 0;
};
/**
* @returns {Uint8Array}
*/
MyGame.Example.Monster.prototype.inventoryArray = function() {
var offset = this.bb.__offset(this.bb_pos, 14);
return offset ? new Uint8Array(this.bb.bytes().buffer, this.bb.__vector(this.bb_pos + offset), this.bb.__vector_len(this.bb_pos + offset)) : null;
};
/**
* @returns {MyGame.Example.Color}
*/
......@@ -555,6 +563,14 @@ MyGame.Example.Monster.prototype.testnestedflatbufferLength = function() {
return offset ? this.bb.__vector_len(this.bb_pos + offset) : 0;
};
/**
* @returns {Uint8Array}
*/
MyGame.Example.Monster.prototype.testnestedflatbufferArray = function() {
var offset = this.bb.__offset(this.bb_pos, 30);
return offset ? new Uint8Array(this.bb.bytes().buffer, this.bb.__vector(this.bb_pos + offset), this.bb.__vector_len(this.bb_pos + offset)) : null;
};
/**
* @param {MyGame.Example.Stat=} obj
* @returns {MyGame.Example.Stat}
......@@ -653,6 +669,14 @@ MyGame.Example.Monster.prototype.testarrayofboolsLength = function() {
return offset ? this.bb.__vector_len(this.bb_pos + offset) : 0;
};
/**
* @returns {Int8Array}
*/
MyGame.Example.Monster.prototype.testarrayofboolsArray = function() {
var offset = this.bb.__offset(this.bb_pos, 52);
return offset ? new Int8Array(this.bb.bytes().buffer, this.bb.__vector(this.bb_pos + offset), this.bb.__vector_len(this.bb_pos + offset)) : null;
};
/**
* @param {flatbuffers.Builder} builder
*/
......
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