mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-28 13:50:02 +00:00
Add helper for javascript typed arrays
For scalar vector fields, emit an Array helper that returns a typed array view of the underlying bytes buffer.
This commit is contained in:
@@ -511,12 +511,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";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -105,6 +105,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);
|
||||||
|
|||||||
@@ -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