diff --git a/src/idl_gen_js.cpp b/src/idl_gen_js.cpp index 7efc39571..995364a6f 100644 --- a/src/idl_gen_js.cpp +++ b/src/idl_gen_js.cpp @@ -511,12 +511,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"; + } } } diff --git a/tests/JavaScriptTest.js b/tests/JavaScriptTest.js index 0f4f23351..4661f8700 100644 --- a/tests/JavaScriptTest.js +++ b/tests/JavaScriptTest.js @@ -105,6 +105,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); diff --git a/tests/monster_test_generated.js b/tests/monster_test_generated.js index 9d0f48b09..bafc1eb80 100644 --- a/tests/monster_test_generated.js +++ b/tests/monster_test_generated.js @@ -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 */