Commit c9b9fd04 authored by Wouter van Oortmerssen's avatar Wouter van Oortmerssen

Merge remote-tracking branch 'mbp/mutable-js' into ghfix

parents 6897bb99 d268d11c
......@@ -79,7 +79,7 @@ class JsGenerator : public BaseGenerator {
for (auto it = parser_.structs_.vec.begin();
it != parser_.structs_.vec.end(); ++it) {
auto &struct_def = **it;
GenStruct(struct_def, decl_code_ptr, exports_code_ptr);
GenStruct(parser_, struct_def, decl_code_ptr, exports_code_ptr);
}
}
void GenNamespaces(std::string *code_ptr, std::string *exports_ptr) {
......@@ -361,7 +361,7 @@ static void GenStructBody(const StructDef &struct_def,
}
// Generate an accessor struct with constructor for a flatbuffers struct.
void GenStruct(StructDef &struct_def, std::string *code_ptr, std::string *exports_ptr) {
void GenStruct(const Parser &parser, StructDef &struct_def, std::string *code_ptr, std::string *exports_ptr) {
if (struct_def.generated) return;
std::string &code = *code_ptr;
std::string &exports = *exports_ptr;
......@@ -550,6 +550,22 @@ void GenStruct(StructDef &struct_def, std::string *code_ptr, std::string *export
}
code += "};\n\n";
// Adds the mutable scalar value to the output
if (IsScalar(field.value.type.base_type) && parser.opts.mutable_buffer) {
std::string annotations = "@param {" + GenTypeName(field.value.type, true) + "} value\n";
GenDocComment(code_ptr, annotations +
"@returns {boolean}");
code += object_name + ".prototype.mutate_" + field.name + " = function(value) {\n";
code += " var offset = this.bb.__offset(this.bb_pos, " + NumToString(field.value.offset) + ")\n\n";
code += " if (offset === 0) {\n";
code += " return false;\n";
code += " }\n\n";
code += " this.bb.write" + MakeCamel(GenType(field.value.type)) + "(this.bb_pos + offset, value);\n";
code += " return true;\n";
code += "}\n\n";
}
// Emit vector helpers
if (field.value.type.base_type == BASE_TYPE_VECTOR) {
// Emit a length helper
......
......@@ -64,7 +64,10 @@ function main() {
fs.writeFileSync('monsterdata_javascript_wire.mon', new Buffer(fbb.asUint8Array()));
// Test it:
// Tests mutation first. This will verify that we did not trample any other
// part of the byte buffer.
testMutation(fbb.dataBuffer());
testBuffer(fbb.dataBuffer());
test64bit();
......@@ -74,6 +77,21 @@ function main() {
console.log('FlatBuffers test: completed successfully');
}
function testMutation(bb) {
var monster = MyGame.Example.Monster.getRootAsMonster(bb);
monster.mutate_hp(120);
assert.strictEqual(monster.hp(), 120);
monster.mutate_hp(80);
assert.strictEqual(monster.hp(), 80);
var manaRes = monster.mutate_mana(10);
assert.strictEqual(manaRes, false); // Field was NOT present, because default value.
// TODO: There is not the availability to mutate structs or vectors.
}
function testBuffer(bb) {
assert.ok(MyGame.Example.Monster.bufferHasIdentifier(bb));
......
This diff is collapsed.
......@@ -64,6 +64,21 @@ NamespaceA.NamespaceB.TableInNestedNS.prototype.foo = function() {
return offset ? this.bb.readInt32(this.bb_pos + offset) : 0;
};
/**
* @param {number} value
* @returns {boolean}
*/
NamespaceA.NamespaceB.TableInNestedNS.prototype.mutate_foo = function(value) {
var offset = this.bb.__offset(this.bb_pos, 4)
if (offset === 0) {
return false;
}
this.bb.writeInt32(this.bb_pos + offset, value);
return true;
}
/**
* @param {flatbuffers.Builder} builder
*/
......@@ -121,6 +136,21 @@ NamespaceA.NamespaceB.StructInNestedNS.prototype.a = function() {
return this.bb.readInt32(this.bb_pos);
};
/**
* @param {number} value
* @returns {boolean}
*/
NamespaceA.NamespaceB.StructInNestedNS.prototype.mutate_a = function(value) {
var offset = this.bb.__offset(this.bb_pos, 0)
if (offset === 0) {
return false;
}
this.bb.writeInt32(this.bb_pos + offset, value);
return true;
}
/**
* @returns {number}
*/
......@@ -128,6 +158,21 @@ NamespaceA.NamespaceB.StructInNestedNS.prototype.b = function() {
return this.bb.readInt32(this.bb_pos + 4);
};
/**
* @param {number} value
* @returns {boolean}
*/
NamespaceA.NamespaceB.StructInNestedNS.prototype.mutate_b = function(value) {
var offset = this.bb.__offset(this.bb_pos, 4)
if (offset === 0) {
return false;
}
this.bb.writeInt32(this.bb_pos + offset, value);
return true;
}
/**
* @param {flatbuffers.Builder} builder
* @param {number} a
......
......@@ -70,6 +70,21 @@ NamespaceA.TableInFirstNS.prototype.fooEnum = function() {
return offset ? /** @type {NamespaceA.NamespaceB.EnumInNestedNS} */ (this.bb.readInt8(this.bb_pos + offset)) : NamespaceA.NamespaceB.EnumInNestedNS.A;
};
/**
* @param {NamespaceA.NamespaceB.EnumInNestedNS} value
* @returns {boolean}
*/
NamespaceA.TableInFirstNS.prototype.mutate_foo_enum = function(value) {
var offset = this.bb.__offset(this.bb_pos, 6)
if (offset === 0) {
return false;
}
this.bb.writeInt8(this.bb_pos + offset, value);
return true;
}
/**
* @param {NamespaceA.NamespaceB.StructInNestedNS=} obj
* @returns {NamespaceA.NamespaceB.StructInNestedNS}
......
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