Commit 59caa536 authored by Wouter van Oortmerssen's avatar Wouter van Oortmerssen

Merge pull request #3784 from evanw/fix-for-3773

Fix #3773: Generated JS now avoids the flatbuffers object
parents 959866b8 31b30335
...@@ -88,6 +88,16 @@ flatbuffers.Long = function(low, high) { ...@@ -88,6 +88,16 @@ flatbuffers.Long = function(low, high) {
this.high = high | 0; this.high = high | 0;
}; };
/**
* @param {number} high
* @param {number} low
* @returns {flatbuffers.Long}
*/
flatbuffers.Long.create = function(low, high) {
// Special-case zero to avoid GC overhead for default values
return low == 0 && high == 0 ? flatbuffers.Long.ZERO : new flatbuffers.Long(low, high);
};
/** /**
* @returns {number} * @returns {number}
*/ */
...@@ -751,6 +761,17 @@ flatbuffers.Builder.prototype.createString = function(s) { ...@@ -751,6 +761,17 @@ flatbuffers.Builder.prototype.createString = function(s) {
} }
return this.endVector(); return this.endVector();
}; };
/**
* A helper function to avoid generated code depending on this file directly.
*
* @param {number} low
* @param {number} high
* @returns {flatbuffers.Long}
*/
flatbuffers.Builder.prototype.createLong = function(low, high) {
return flatbuffers.Long.create(low, high);
};
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
/// @cond FLATBUFFERS_INTERNAL /// @cond FLATBUFFERS_INTERNAL
/** /**
...@@ -1101,6 +1122,17 @@ flatbuffers.ByteBuffer.prototype.__has_identifier = function(ident) { ...@@ -1101,6 +1122,17 @@ flatbuffers.ByteBuffer.prototype.__has_identifier = function(ident) {
return true; return true;
}; };
/**
* A helper function to avoid generated code depending on this file directly.
*
* @param {number} low
* @param {number} high
* @returns {flatbuffers.Long}
*/
flatbuffers.ByteBuffer.prototype.createLong = function(low, high) {
return flatbuffers.Long.create(low, high);
};
// Exports for Node.js and RequireJS // Exports for Node.js and RequireJS
this.flatbuffers = flatbuffers; this.flatbuffers = flatbuffers;
......
...@@ -189,7 +189,7 @@ static std::string GenGetter(const Type &type, const std::string &arguments) { ...@@ -189,7 +189,7 @@ static std::string GenGetter(const Type &type, const std::string &arguments) {
} }
} }
static std::string GenDefaultValue(const Value &value) { static std::string GenDefaultValue(const Value &value, const std::string &context) {
if (value.type.enum_def) { if (value.type.enum_def) {
if (auto val = value.type.enum_def->ReverseLookup( if (auto val = value.type.enum_def->ReverseLookup(
atoi(value.constant.c_str()), false)) { atoi(value.constant.c_str()), false)) {
...@@ -205,13 +205,11 @@ static std::string GenDefaultValue(const Value &value) { ...@@ -205,13 +205,11 @@ static std::string GenDefaultValue(const Value &value) {
return "null"; return "null";
case BASE_TYPE_LONG: case BASE_TYPE_LONG:
case BASE_TYPE_ULONG: case BASE_TYPE_ULONG: {
if (value.constant != "0") { int64_t constant = StringToInt(value.constant.c_str());
int64_t constant = StringToInt(value.constant.c_str()); return context + ".createLong(" + NumToString((int32_t)constant) +
return "new flatbuffers.Long(" + NumToString((int32_t)constant) + ", " + NumToString((int32_t)(constant >> 32)) + ")";
", " + NumToString((int32_t)(constant >> 32)) + ")"; }
}
return "flatbuffers.Long.ZERO";
default: default:
return value.constant; return value.constant;
...@@ -417,7 +415,7 @@ static void GenStruct(const Parser &parser, StructDef &struct_def, ...@@ -417,7 +415,7 @@ static void GenStruct(const Parser &parser, StructDef &struct_def,
index += ", optionalEncoding"; index += ", optionalEncoding";
} }
code += offset_prefix + GenGetter(field.value.type, code += offset_prefix + GenGetter(field.value.type,
"(" + index + ")") + " : " + GenDefaultValue(field.value); "(" + index + ")") + " : " + GenDefaultValue(field.value, "this.bb");
code += ";\n"; code += ";\n";
} }
} }
...@@ -485,7 +483,7 @@ static void GenStruct(const Parser &parser, StructDef &struct_def, ...@@ -485,7 +483,7 @@ static void GenStruct(const Parser &parser, StructDef &struct_def,
code += "false"; code += "false";
} else if (field.value.type.element == BASE_TYPE_LONG || } else if (field.value.type.element == BASE_TYPE_LONG ||
field.value.type.element == BASE_TYPE_ULONG) { field.value.type.element == BASE_TYPE_ULONG) {
code += "flatbuffers.Long.ZERO"; code += "this.bb.createLong(0, 0)";
} else if (IsScalar(field.value.type.element)) { } else if (IsScalar(field.value.type.element)) {
code += "0"; code += "0";
} else { } else {
...@@ -570,7 +568,7 @@ static void GenStruct(const Parser &parser, StructDef &struct_def, ...@@ -570,7 +568,7 @@ static void GenStruct(const Parser &parser, StructDef &struct_def,
if (field.value.type.base_type == BASE_TYPE_BOOL) { if (field.value.type.base_type == BASE_TYPE_BOOL) {
code += "+"; code += "+";
} }
code += GenDefaultValue(field.value); code += GenDefaultValue(field.value, "builder");
} }
code += ");\n};\n\n"; code += ");\n};\n\n";
......
...@@ -67,6 +67,7 @@ function main() { ...@@ -67,6 +67,7 @@ function main() {
// Test it: // Test it:
testBuffer(fbb.dataBuffer()); testBuffer(fbb.dataBuffer());
test64bit();
testUnicode(); testUnicode();
fuzzTest1(); fuzzTest1();
...@@ -117,6 +118,52 @@ function testBuffer(bb) { ...@@ -117,6 +118,52 @@ function testBuffer(bb) {
assert.strictEqual(monster.testbool(), false); assert.strictEqual(monster.testbool(), false);
} }
function test64bit() {
var fbb = new flatbuffers.Builder();
var required = fbb.createString('required');
MyGame.Example.Stat.startStat(fbb);
var stat2 = MyGame.Example.Stat.endStat(fbb);
MyGame.Example.Monster.startMonster(fbb);
MyGame.Example.Monster.addName(fbb, required);
MyGame.Example.Monster.addTestempty(fbb, stat2);
var mon2 = MyGame.Example.Monster.endMonster(fbb);
MyGame.Example.Stat.startStat(fbb);
MyGame.Example.Stat.addVal(fbb, new flatbuffers.Long(0x12345678, 0x23456789));
var stat = MyGame.Example.Stat.endStat(fbb);
MyGame.Example.Monster.startMonster(fbb);
MyGame.Example.Monster.addName(fbb, required);
MyGame.Example.Monster.addEnemy(fbb, mon2);
MyGame.Example.Monster.addTestempty(fbb, stat);
var mon = MyGame.Example.Monster.endMonster(fbb);
MyGame.Example.Monster.finishMonsterBuffer(fbb, mon);
var bytes = fbb.asUint8Array();
////////////////////////////////////////////////////////////////
var bb = new flatbuffers.ByteBuffer(bytes);
assert.ok(MyGame.Example.Monster.bufferHasIdentifier(bb));
var mon = MyGame.Example.Monster.getRootAsMonster(bb);
var stat = mon.testempty();
assert.strictEqual(stat != null, true);
assert.strictEqual(stat.val() != null, true);
assert.strictEqual(stat.val().low, 0x12345678);
assert.strictEqual(stat.val().high, 0x23456789);
var mon2 = mon.enemy();
assert.strictEqual(mon2 != null, true);
stat = mon2.testempty();
assert.strictEqual(stat != null, true);
assert.strictEqual(stat.val() != null, true);
assert.strictEqual(stat.val().low, 0); // default value
assert.strictEqual(stat.val().high, 0);
}
function testUnicode() { function testUnicode() {
var correct = fs.readFileSync('unicode_test.mon'); var correct = fs.readFileSync('unicode_test.mon');
var json = JSON.parse(fs.readFileSync('unicode_test.json', 'utf8')); var json = JSON.parse(fs.readFileSync('unicode_test.json', 'utf8'));
......
...@@ -300,7 +300,7 @@ MyGame.Example.Stat.prototype.id = function(optionalEncoding) { ...@@ -300,7 +300,7 @@ MyGame.Example.Stat.prototype.id = function(optionalEncoding) {
*/ */
MyGame.Example.Stat.prototype.val = function() { MyGame.Example.Stat.prototype.val = function() {
var offset = this.bb.__offset(this.bb_pos, 6); var offset = this.bb.__offset(this.bb_pos, 6);
return offset ? this.bb.readInt64(this.bb_pos + offset) : flatbuffers.Long.ZERO; return offset ? this.bb.readInt64(this.bb_pos + offset) : this.bb.createLong(0, 0);
}; };
/** /**
...@@ -331,7 +331,7 @@ MyGame.Example.Stat.addId = function(builder, idOffset) { ...@@ -331,7 +331,7 @@ MyGame.Example.Stat.addId = function(builder, idOffset) {
* @param {flatbuffers.Long} val * @param {flatbuffers.Long} val
*/ */
MyGame.Example.Stat.addVal = function(builder, val) { MyGame.Example.Stat.addVal = function(builder, val) {
builder.addFieldInt64(1, val, flatbuffers.Long.ZERO); builder.addFieldInt64(1, val, builder.createLong(0, 0));
}; };
/** /**
...@@ -593,7 +593,7 @@ MyGame.Example.Monster.prototype.testhashu32Fnv1 = function() { ...@@ -593,7 +593,7 @@ MyGame.Example.Monster.prototype.testhashu32Fnv1 = function() {
*/ */
MyGame.Example.Monster.prototype.testhashs64Fnv1 = function() { MyGame.Example.Monster.prototype.testhashs64Fnv1 = function() {
var offset = this.bb.__offset(this.bb_pos, 40); var offset = this.bb.__offset(this.bb_pos, 40);
return offset ? this.bb.readInt64(this.bb_pos + offset) : flatbuffers.Long.ZERO; return offset ? this.bb.readInt64(this.bb_pos + offset) : this.bb.createLong(0, 0);
}; };
/** /**
...@@ -601,7 +601,7 @@ MyGame.Example.Monster.prototype.testhashs64Fnv1 = function() { ...@@ -601,7 +601,7 @@ MyGame.Example.Monster.prototype.testhashs64Fnv1 = function() {
*/ */
MyGame.Example.Monster.prototype.testhashu64Fnv1 = function() { MyGame.Example.Monster.prototype.testhashu64Fnv1 = function() {
var offset = this.bb.__offset(this.bb_pos, 42); var offset = this.bb.__offset(this.bb_pos, 42);
return offset ? this.bb.readUint64(this.bb_pos + offset) : flatbuffers.Long.ZERO; return offset ? this.bb.readUint64(this.bb_pos + offset) : this.bb.createLong(0, 0);
}; };
/** /**
...@@ -625,7 +625,7 @@ MyGame.Example.Monster.prototype.testhashu32Fnv1a = function() { ...@@ -625,7 +625,7 @@ MyGame.Example.Monster.prototype.testhashu32Fnv1a = function() {
*/ */
MyGame.Example.Monster.prototype.testhashs64Fnv1a = function() { MyGame.Example.Monster.prototype.testhashs64Fnv1a = function() {
var offset = this.bb.__offset(this.bb_pos, 48); var offset = this.bb.__offset(this.bb_pos, 48);
return offset ? this.bb.readInt64(this.bb_pos + offset) : flatbuffers.Long.ZERO; return offset ? this.bb.readInt64(this.bb_pos + offset) : this.bb.createLong(0, 0);
}; };
/** /**
...@@ -633,7 +633,7 @@ MyGame.Example.Monster.prototype.testhashs64Fnv1a = function() { ...@@ -633,7 +633,7 @@ MyGame.Example.Monster.prototype.testhashs64Fnv1a = function() {
*/ */
MyGame.Example.Monster.prototype.testhashu64Fnv1a = function() { MyGame.Example.Monster.prototype.testhashu64Fnv1a = function() {
var offset = this.bb.__offset(this.bb_pos, 50); var offset = this.bb.__offset(this.bb_pos, 50);
return offset ? this.bb.readUint64(this.bb_pos + offset) : flatbuffers.Long.ZERO; return offset ? this.bb.readUint64(this.bb_pos + offset) : this.bb.createLong(0, 0);
}; };
/** /**
...@@ -893,7 +893,7 @@ MyGame.Example.Monster.addTesthashu32Fnv1 = function(builder, testhashu32Fnv1) { ...@@ -893,7 +893,7 @@ MyGame.Example.Monster.addTesthashu32Fnv1 = function(builder, testhashu32Fnv1) {
* @param {flatbuffers.Long} testhashs64Fnv1 * @param {flatbuffers.Long} testhashs64Fnv1
*/ */
MyGame.Example.Monster.addTesthashs64Fnv1 = function(builder, testhashs64Fnv1) { MyGame.Example.Monster.addTesthashs64Fnv1 = function(builder, testhashs64Fnv1) {
builder.addFieldInt64(18, testhashs64Fnv1, flatbuffers.Long.ZERO); builder.addFieldInt64(18, testhashs64Fnv1, builder.createLong(0, 0));
}; };
/** /**
...@@ -901,7 +901,7 @@ MyGame.Example.Monster.addTesthashs64Fnv1 = function(builder, testhashs64Fnv1) { ...@@ -901,7 +901,7 @@ MyGame.Example.Monster.addTesthashs64Fnv1 = function(builder, testhashs64Fnv1) {
* @param {flatbuffers.Long} testhashu64Fnv1 * @param {flatbuffers.Long} testhashu64Fnv1
*/ */
MyGame.Example.Monster.addTesthashu64Fnv1 = function(builder, testhashu64Fnv1) { MyGame.Example.Monster.addTesthashu64Fnv1 = function(builder, testhashu64Fnv1) {
builder.addFieldInt64(19, testhashu64Fnv1, flatbuffers.Long.ZERO); builder.addFieldInt64(19, testhashu64Fnv1, builder.createLong(0, 0));
}; };
/** /**
...@@ -925,7 +925,7 @@ MyGame.Example.Monster.addTesthashu32Fnv1a = function(builder, testhashu32Fnv1a) ...@@ -925,7 +925,7 @@ MyGame.Example.Monster.addTesthashu32Fnv1a = function(builder, testhashu32Fnv1a)
* @param {flatbuffers.Long} testhashs64Fnv1a * @param {flatbuffers.Long} testhashs64Fnv1a
*/ */
MyGame.Example.Monster.addTesthashs64Fnv1a = function(builder, testhashs64Fnv1a) { MyGame.Example.Monster.addTesthashs64Fnv1a = function(builder, testhashs64Fnv1a) {
builder.addFieldInt64(22, testhashs64Fnv1a, flatbuffers.Long.ZERO); builder.addFieldInt64(22, testhashs64Fnv1a, builder.createLong(0, 0));
}; };
/** /**
...@@ -933,7 +933,7 @@ MyGame.Example.Monster.addTesthashs64Fnv1a = function(builder, testhashs64Fnv1a) ...@@ -933,7 +933,7 @@ MyGame.Example.Monster.addTesthashs64Fnv1a = function(builder, testhashs64Fnv1a)
* @param {flatbuffers.Long} testhashu64Fnv1a * @param {flatbuffers.Long} testhashu64Fnv1a
*/ */
MyGame.Example.Monster.addTesthashu64Fnv1a = function(builder, testhashu64Fnv1a) { MyGame.Example.Monster.addTesthashu64Fnv1a = function(builder, testhashu64Fnv1a) {
builder.addFieldInt64(23, testhashu64Fnv1a, flatbuffers.Long.ZERO); builder.addFieldInt64(23, testhashu64Fnv1a, builder.createLong(0, 0));
}; };
/** /**
......
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