mirror of
https://github.com/google/flatbuffers.git
synced 2026-07-04 05:14:13 +00:00
Merge pull request #3765 from Mischanix/js-typed-arrays
Javascript helper to get typed array views
This commit is contained in:
@@ -509,12 +509,24 @@ static void GenStruct(const Parser &parser, StructDef &struct_def,
|
|||||||
}
|
}
|
||||||
code += "};\n\n";
|
code += "};\n\n";
|
||||||
|
|
||||||
// Emit a length helper
|
// Emit vector helpers
|
||||||
if (field.value.type.base_type == BASE_TYPE_VECTOR) {
|
if (field.value.type.base_type == BASE_TYPE_VECTOR) {
|
||||||
|
// Emit a length helper
|
||||||
GenDocComment(code_ptr, "@returns {number}");
|
GenDocComment(code_ptr, "@returns {number}");
|
||||||
code += object_name + ".prototype." + MakeCamel(field.name, false);
|
code += object_name + ".prototype." + MakeCamel(field.name, false);
|
||||||
code += "Length = function() {\n" + offset_prefix;
|
code += "Length = function() {\n" + offset_prefix;
|
||||||
code += "this.bb.__vector_len(this.bb_pos + offset) : 0;\n};\n\n";
|
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);
|
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_0 = monster.test4(0);
|
||||||
var test_1 = monster.test4(1);
|
var test_1 = monster.test4(1);
|
||||||
assert.strictEqual(monster.test4Length(), 2);
|
assert.strictEqual(monster.test4Length(), 2);
|
||||||
@@ -169,21 +176,23 @@ function testUnicode() {
|
|||||||
var json = JSON.parse(fs.readFileSync('unicode_test.json', 'utf8'));
|
var json = JSON.parse(fs.readFileSync('unicode_test.json', 'utf8'));
|
||||||
|
|
||||||
// Test reading
|
// Test reading
|
||||||
var bb = new flatbuffers.ByteBuffer(new Uint8Array(correct));
|
function testReadingUnicode(bb) {
|
||||||
var monster = MyGame.Example.Monster.getRootAsMonster(bb);
|
var monster = MyGame.Example.Monster.getRootAsMonster(bb);
|
||||||
assert.strictEqual(monster.name(), json.name);
|
assert.strictEqual(monster.name(), json.name);
|
||||||
assert.deepEqual(new Buffer(monster.name(flatbuffers.Encoding.UTF8_BYTES)), new Buffer(json.name));
|
assert.deepEqual(new Buffer(monster.name(flatbuffers.Encoding.UTF8_BYTES)), new Buffer(json.name));
|
||||||
assert.strictEqual(monster.testarrayoftablesLength(), json.testarrayoftables.length);
|
assert.strictEqual(monster.testarrayoftablesLength(), json.testarrayoftables.length);
|
||||||
json.testarrayoftables.forEach(function(table, i) {
|
json.testarrayoftables.forEach(function(table, i) {
|
||||||
var value = monster.testarrayoftables(i);
|
var value = monster.testarrayoftables(i);
|
||||||
assert.strictEqual(value.name(), table.name);
|
assert.strictEqual(value.name(), table.name);
|
||||||
assert.deepEqual(new Buffer(value.name(flatbuffers.Encoding.UTF8_BYTES)), new Buffer(table.name));
|
assert.deepEqual(new Buffer(value.name(flatbuffers.Encoding.UTF8_BYTES)), new Buffer(table.name));
|
||||||
});
|
});
|
||||||
assert.strictEqual(monster.testarrayofstringLength(), json.testarrayofstring.length);
|
assert.strictEqual(monster.testarrayofstringLength(), json.testarrayofstring.length);
|
||||||
json.testarrayofstring.forEach(function(string, i) {
|
json.testarrayofstring.forEach(function(string, i) {
|
||||||
assert.strictEqual(monster.testarrayofstring(i), string);
|
assert.strictEqual(monster.testarrayofstring(i), string);
|
||||||
assert.deepEqual(new Buffer(monster.testarrayofstring(i, flatbuffers.Encoding.UTF8_BYTES)), new Buffer(string));
|
assert.deepEqual(new Buffer(monster.testarrayofstring(i, flatbuffers.Encoding.UTF8_BYTES)), new Buffer(string));
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
testReadingUnicode(new flatbuffers.ByteBuffer(new Uint8Array(correct)));
|
||||||
|
|
||||||
// Test writing
|
// Test writing
|
||||||
var fbb = new flatbuffers.Builder();
|
var fbb = new flatbuffers.Builder();
|
||||||
@@ -203,7 +212,7 @@ function testUnicode() {
|
|||||||
MyGame.Example.Monster.addTestarrayoftables(fbb, testarrayoftablesOffset);
|
MyGame.Example.Monster.addTestarrayoftables(fbb, testarrayoftablesOffset);
|
||||||
MyGame.Example.Monster.addName(fbb, name);
|
MyGame.Example.Monster.addName(fbb, name);
|
||||||
MyGame.Example.Monster.finishMonsterBuffer(fbb, MyGame.Example.Monster.endMonster(fbb));
|
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) {
|
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;
|
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}
|
* @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;
|
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
|
* @param {MyGame.Example.Stat=} obj
|
||||||
* @returns {MyGame.Example.Stat}
|
* @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;
|
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
|
* @param {flatbuffers.Builder} builder
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user