Commit 9cadf05d authored by Max Burke's avatar Max Burke Committed by Wouter van Oortmerssen

[typescript] Size-prefixed root accessors not taking into account size prefix (#5717)

* [typescript/javascript] Size-prefixed root accessor needs to account for the size prefix.

* [typescript] Add parentheses after "new" expression.

* Update generated test files
parent 6da1cf79
Pipeline #333 failed with stages
......@@ -600,6 +600,10 @@ class JsTsGenerator : public BaseGenerator {
}
}
std::string GenerateNewExpression(const std::string &object_name) {
return "new " + object_name + (lang_.language == IDLOptions::kTs ? "()" : "");
}
void GenerateRootAccessor(StructDef &struct_def, std::string *code_ptr,
std::string &code, std::string &object_name,
bool size_prefixed) {
......@@ -619,7 +623,10 @@ class JsTsGenerator : public BaseGenerator {
"Root" + Verbose(struct_def, "As");
code += " = function(bb, obj) {\n";
}
code += " return (obj || new " + object_name;
if (size_prefixed) {
code += " bb.setPosition(bb.position() + flatbuffers.SIZE_PREFIX_LENGTH);\n";
}
code += " return (obj || " + GenerateNewExpression(object_name);
code += ").__init(bb.readInt32(bb.position()) + bb.position(), bb);\n";
code += "};\n\n";
}
......@@ -858,12 +865,12 @@ class JsTsGenerator : public BaseGenerator {
}
if (struct_def.fixed) {
code += " return (obj || new " + type;
code += " return (obj || " + GenerateNewExpression(type);
code += ").__init(this.bb_pos";
code +=
MaybeAdd(field.value.offset) + ", " + GenBBAccess() + ");\n";
} else {
code += offset_prefix + "(obj || new " + type + ").__init(";
code += offset_prefix + "(obj || " + GenerateNewExpression(type) + ").__init(";
code += field.value.type.struct_def->fixed
? "this.bb_pos + offset"
: GenBBAccess() + ".__indirect(this.bb_pos + offset)";
......@@ -945,7 +952,7 @@ class JsTsGenerator : public BaseGenerator {
}
if (vectortype.base_type == BASE_TYPE_STRUCT) {
code += offset_prefix + "(obj || new " + vectortypename;
code += offset_prefix + "(obj || " + GenerateNewExpression(vectortypename);
code += ").__init(";
code += vectortype.struct_def->fixed
? index
......
......@@ -185,6 +185,7 @@ MyGame.InParentNamespace.getRootAsInParentNamespace = function(bb, obj) {
* @returns {MyGame.InParentNamespace}
*/
MyGame.InParentNamespace.getSizePrefixedRootAsInParentNamespace = function(bb, obj) {
bb.setPosition(bb.position() + flatbuffers.SIZE_PREFIX_LENGTH);
return (obj || new MyGame.InParentNamespace).__init(bb.readInt32(bb.position()) + bb.position(), bb);
};
......@@ -254,6 +255,7 @@ MyGame.Example2.Monster.getRootAsMonster = function(bb, obj) {
* @returns {MyGame.Example2.Monster}
*/
MyGame.Example2.Monster.getSizePrefixedRootAsMonster = function(bb, obj) {
bb.setPosition(bb.position() + flatbuffers.SIZE_PREFIX_LENGTH);
return (obj || new MyGame.Example2.Monster).__init(bb.readInt32(bb.position()) + bb.position(), bb);
};
......@@ -407,6 +409,7 @@ MyGame.Example.TestSimpleTableWithEnum.getRootAsTestSimpleTableWithEnum = functi
* @returns {MyGame.Example.TestSimpleTableWithEnum}
*/
MyGame.Example.TestSimpleTableWithEnum.getSizePrefixedRootAsTestSimpleTableWithEnum = function(bb, obj) {
bb.setPosition(bb.position() + flatbuffers.SIZE_PREFIX_LENGTH);
return (obj || new MyGame.Example.TestSimpleTableWithEnum).__init(bb.readInt32(bb.position()) + bb.position(), bb);
};
......@@ -764,6 +767,7 @@ MyGame.Example.Stat.getRootAsStat = function(bb, obj) {
* @returns {MyGame.Example.Stat}
*/
MyGame.Example.Stat.getSizePrefixedRootAsStat = function(bb, obj) {
bb.setPosition(bb.position() + flatbuffers.SIZE_PREFIX_LENGTH);
return (obj || new MyGame.Example.Stat).__init(bb.readInt32(bb.position()) + bb.position(), bb);
};
......@@ -918,6 +922,7 @@ MyGame.Example.Referrable.getRootAsReferrable = function(bb, obj) {
* @returns {MyGame.Example.Referrable}
*/
MyGame.Example.Referrable.getSizePrefixedRootAsReferrable = function(bb, obj) {
bb.setPosition(bb.position() + flatbuffers.SIZE_PREFIX_LENGTH);
return (obj || new MyGame.Example.Referrable).__init(bb.readInt32(bb.position()) + bb.position(), bb);
};
......@@ -1022,6 +1027,7 @@ MyGame.Example.Monster.getRootAsMonster = function(bb, obj) {
* @returns {MyGame.Example.Monster}
*/
MyGame.Example.Monster.getSizePrefixedRootAsMonster = function(bb, obj) {
bb.setPosition(bb.position() + flatbuffers.SIZE_PREFIX_LENGTH);
return (obj || new MyGame.Example.Monster).__init(bb.readInt32(bb.position()) + bb.position(), bb);
};
......@@ -2833,6 +2839,7 @@ MyGame.Example.TypeAliases.getRootAsTypeAliases = function(bb, obj) {
* @returns {MyGame.Example.TypeAliases}
*/
MyGame.Example.TypeAliases.getSizePrefixedRootAsTypeAliases = function(bb, obj) {
bb.setPosition(bb.position() + flatbuffers.SIZE_PREFIX_LENGTH);
return (obj || new MyGame.Example.TypeAliases).__init(bb.readInt32(bb.position()) + bb.position(), bb);
};
......
This diff is collapsed.
......@@ -71,6 +71,7 @@ NamespaceA.NamespaceB.TableInNestedNS.getRootAsTableInNestedNS = function(bb, ob
* @returns {NamespaceA.NamespaceB.TableInNestedNS}
*/
NamespaceA.NamespaceB.TableInNestedNS.getSizePrefixedRootAsTableInNestedNS = function(bb, obj) {
bb.setPosition(bb.position() + flatbuffers.SIZE_PREFIX_LENGTH);
return (obj || new NamespaceA.NamespaceB.TableInNestedNS).__init(bb.readInt32(bb.position()) + bb.position(), bb);
};
......
......@@ -35,7 +35,7 @@ __init(i:number, bb:flatbuffers.ByteBuffer):TableInNestedNS {
* @returns TableInNestedNS
*/
static getRootAsTableInNestedNS(bb:flatbuffers.ByteBuffer, obj?:TableInNestedNS):TableInNestedNS {
return (obj || new TableInNestedNS).__init(bb.readInt32(bb.position()) + bb.position(), bb);
return (obj || new TableInNestedNS()).__init(bb.readInt32(bb.position()) + bb.position(), bb);
};
/**
......@@ -44,7 +44,8 @@ static getRootAsTableInNestedNS(bb:flatbuffers.ByteBuffer, obj?:TableInNestedNS)
* @returns TableInNestedNS
*/
static getSizePrefixedRootAsTableInNestedNS(bb:flatbuffers.ByteBuffer, obj?:TableInNestedNS):TableInNestedNS {
return (obj || new TableInNestedNS).__init(bb.readInt32(bb.position()) + bb.position(), bb);
bb.setPosition(bb.position() + flatbuffers.SIZE_PREFIX_LENGTH);
return (obj || new TableInNestedNS()).__init(bb.readInt32(bb.position()) + bb.position(), bb);
};
/**
......
......@@ -59,6 +59,7 @@ NamespaceA.TableInFirstNS.getRootAsTableInFirstNS = function(bb, obj) {
* @returns {NamespaceA.TableInFirstNS}
*/
NamespaceA.TableInFirstNS.getSizePrefixedRootAsTableInFirstNS = function(bb, obj) {
bb.setPosition(bb.position() + flatbuffers.SIZE_PREFIX_LENGTH);
return (obj || new NamespaceA.TableInFirstNS).__init(bb.readInt32(bb.position()) + bb.position(), bb);
};
......@@ -199,6 +200,7 @@ NamespaceC.TableInC.getRootAsTableInC = function(bb, obj) {
* @returns {NamespaceC.TableInC}
*/
NamespaceC.TableInC.getSizePrefixedRootAsTableInC = function(bb, obj) {
bb.setPosition(bb.position() + flatbuffers.SIZE_PREFIX_LENGTH);
return (obj || new NamespaceC.TableInC).__init(bb.readInt32(bb.position()) + bb.position(), bb);
};
......@@ -306,6 +308,7 @@ NamespaceA.SecondTableInA.getRootAsSecondTableInA = function(bb, obj) {
* @returns {NamespaceA.SecondTableInA}
*/
NamespaceA.SecondTableInA.getSizePrefixedRootAsSecondTableInA = function(bb, obj) {
bb.setPosition(bb.position() + flatbuffers.SIZE_PREFIX_LENGTH);
return (obj || new NamespaceA.SecondTableInA).__init(bb.readInt32(bb.position()) + bb.position(), bb);
};
......
......@@ -26,7 +26,7 @@ __init(i:number, bb:flatbuffers.ByteBuffer):TableInFirstNS {
* @returns TableInFirstNS
*/
static getRootAsTableInFirstNS(bb:flatbuffers.ByteBuffer, obj?:TableInFirstNS):TableInFirstNS {
return (obj || new TableInFirstNS).__init(bb.readInt32(bb.position()) + bb.position(), bb);
return (obj || new TableInFirstNS()).__init(bb.readInt32(bb.position()) + bb.position(), bb);
};
/**
......@@ -35,7 +35,8 @@ static getRootAsTableInFirstNS(bb:flatbuffers.ByteBuffer, obj?:TableInFirstNS):T
* @returns TableInFirstNS
*/
static getSizePrefixedRootAsTableInFirstNS(bb:flatbuffers.ByteBuffer, obj?:TableInFirstNS):TableInFirstNS {
return (obj || new TableInFirstNS).__init(bb.readInt32(bb.position()) + bb.position(), bb);
bb.setPosition(bb.position() + flatbuffers.SIZE_PREFIX_LENGTH);
return (obj || new TableInFirstNS()).__init(bb.readInt32(bb.position()) + bb.position(), bb);
};
/**
......@@ -44,7 +45,7 @@ static getSizePrefixedRootAsTableInFirstNS(bb:flatbuffers.ByteBuffer, obj?:Table
*/
fooTable(obj?:NS8755221360535654258.NamespaceA.NamespaceB.TableInNestedNS):NS8755221360535654258.NamespaceA.NamespaceB.TableInNestedNS|null {
var offset = this.bb!.__offset(this.bb_pos, 4);
return offset ? (obj || new NS8755221360535654258.NamespaceA.NamespaceB.TableInNestedNS).__init(this.bb!.__indirect(this.bb_pos + offset), this.bb!) : null;
return offset ? (obj || new NS8755221360535654258.NamespaceA.NamespaceB.TableInNestedNS()).__init(this.bb!.__indirect(this.bb_pos + offset), this.bb!) : null;
};
/**
......@@ -76,7 +77,7 @@ mutate_foo_enum(value:NS8755221360535654258.NamespaceA.NamespaceB.EnumInNestedNS
*/
fooStruct(obj?:NS8755221360535654258.NamespaceA.NamespaceB.StructInNestedNS):NS8755221360535654258.NamespaceA.NamespaceB.StructInNestedNS|null {
var offset = this.bb!.__offset(this.bb_pos, 8);
return offset ? (obj || new NS8755221360535654258.NamespaceA.NamespaceB.StructInNestedNS).__init(this.bb_pos + offset, this.bb!) : null;
return offset ? (obj || new NS8755221360535654258.NamespaceA.NamespaceB.StructInNestedNS()).__init(this.bb_pos + offset, this.bb!) : null;
};
/**
......@@ -153,7 +154,7 @@ __init(i:number, bb:flatbuffers.ByteBuffer):TableInC {
* @returns TableInC
*/
static getRootAsTableInC(bb:flatbuffers.ByteBuffer, obj?:TableInC):TableInC {
return (obj || new TableInC).__init(bb.readInt32(bb.position()) + bb.position(), bb);
return (obj || new TableInC()).__init(bb.readInt32(bb.position()) + bb.position(), bb);
};
/**
......@@ -162,7 +163,8 @@ static getRootAsTableInC(bb:flatbuffers.ByteBuffer, obj?:TableInC):TableInC {
* @returns TableInC
*/
static getSizePrefixedRootAsTableInC(bb:flatbuffers.ByteBuffer, obj?:TableInC):TableInC {
return (obj || new TableInC).__init(bb.readInt32(bb.position()) + bb.position(), bb);
bb.setPosition(bb.position() + flatbuffers.SIZE_PREFIX_LENGTH);
return (obj || new TableInC()).__init(bb.readInt32(bb.position()) + bb.position(), bb);
};
/**
......@@ -171,7 +173,7 @@ static getSizePrefixedRootAsTableInC(bb:flatbuffers.ByteBuffer, obj?:TableInC):T
*/
referToA1(obj?:NamespaceA.TableInFirstNS):NamespaceA.TableInFirstNS|null {
var offset = this.bb!.__offset(this.bb_pos, 4);
return offset ? (obj || new NamespaceA.TableInFirstNS).__init(this.bb!.__indirect(this.bb_pos + offset), this.bb!) : null;
return offset ? (obj || new NamespaceA.TableInFirstNS()).__init(this.bb!.__indirect(this.bb_pos + offset), this.bb!) : null;
};
/**
......@@ -180,7 +182,7 @@ referToA1(obj?:NamespaceA.TableInFirstNS):NamespaceA.TableInFirstNS|null {
*/
referToA2(obj?:NamespaceA.SecondTableInA):NamespaceA.SecondTableInA|null {
var offset = this.bb!.__offset(this.bb_pos, 6);
return offset ? (obj || new NamespaceA.SecondTableInA).__init(this.bb!.__indirect(this.bb_pos + offset), this.bb!) : null;
return offset ? (obj || new NamespaceA.SecondTableInA()).__init(this.bb!.__indirect(this.bb_pos + offset), this.bb!) : null;
};
/**
......@@ -248,7 +250,7 @@ __init(i:number, bb:flatbuffers.ByteBuffer):SecondTableInA {
* @returns SecondTableInA
*/
static getRootAsSecondTableInA(bb:flatbuffers.ByteBuffer, obj?:SecondTableInA):SecondTableInA {
return (obj || new SecondTableInA).__init(bb.readInt32(bb.position()) + bb.position(), bb);
return (obj || new SecondTableInA()).__init(bb.readInt32(bb.position()) + bb.position(), bb);
};
/**
......@@ -257,7 +259,8 @@ static getRootAsSecondTableInA(bb:flatbuffers.ByteBuffer, obj?:SecondTableInA):S
* @returns SecondTableInA
*/
static getSizePrefixedRootAsSecondTableInA(bb:flatbuffers.ByteBuffer, obj?:SecondTableInA):SecondTableInA {
return (obj || new SecondTableInA).__init(bb.readInt32(bb.position()) + bb.position(), bb);
bb.setPosition(bb.position() + flatbuffers.SIZE_PREFIX_LENGTH);
return (obj || new SecondTableInA()).__init(bb.readInt32(bb.position()) + bb.position(), bb);
};
/**
......@@ -266,7 +269,7 @@ static getSizePrefixedRootAsSecondTableInA(bb:flatbuffers.ByteBuffer, obj?:Secon
*/
referToC(obj?:NamespaceC.TableInC):NamespaceC.TableInC|null {
var offset = this.bb!.__offset(this.bb_pos, 4);
return offset ? (obj || new NamespaceC.TableInC).__init(this.bb!.__indirect(this.bb_pos + offset), this.bb!) : null;
return offset ? (obj || new NamespaceC.TableInC()).__init(this.bb!.__indirect(this.bb_pos + offset), this.bb!) : null;
};
/**
......
......@@ -67,6 +67,7 @@ Attacker.getRootAsAttacker = function(bb, obj) {
* @returns {Attacker}
*/
Attacker.getSizePrefixedRootAsAttacker = function(bb, obj) {
bb.setPosition(bb.position() + flatbuffers.SIZE_PREFIX_LENGTH);
return (obj || new Attacker).__init(bb.readInt32(bb.position()) + bb.position(), bb);
};
......@@ -287,6 +288,7 @@ Movie.getRootAsMovie = function(bb, obj) {
* @returns {Movie}
*/
Movie.getSizePrefixedRootAsMovie = function(bb, obj) {
bb.setPosition(bb.position() + flatbuffers.SIZE_PREFIX_LENGTH);
return (obj || new Movie).__init(bb.readInt32(bb.position()) + bb.position(), bb);
};
......
......@@ -37,7 +37,7 @@ __init(i:number, bb:flatbuffers.ByteBuffer):Attacker {
* @returns Attacker
*/
static getRootAsAttacker(bb:flatbuffers.ByteBuffer, obj?:Attacker):Attacker {
return (obj || new Attacker).__init(bb.readInt32(bb.position()) + bb.position(), bb);
return (obj || new Attacker()).__init(bb.readInt32(bb.position()) + bb.position(), bb);
};
/**
......@@ -46,7 +46,8 @@ static getRootAsAttacker(bb:flatbuffers.ByteBuffer, obj?:Attacker):Attacker {
* @returns Attacker
*/
static getSizePrefixedRootAsAttacker(bb:flatbuffers.ByteBuffer, obj?:Attacker):Attacker {
return (obj || new Attacker).__init(bb.readInt32(bb.position()) + bb.position(), bb);
bb.setPosition(bb.position() + flatbuffers.SIZE_PREFIX_LENGTH);
return (obj || new Attacker()).__init(bb.readInt32(bb.position()) + bb.position(), bb);
};
/**
......@@ -230,7 +231,7 @@ __init(i:number, bb:flatbuffers.ByteBuffer):Movie {
* @returns Movie
*/
static getRootAsMovie(bb:flatbuffers.ByteBuffer, obj?:Movie):Movie {
return (obj || new Movie).__init(bb.readInt32(bb.position()) + bb.position(), bb);
return (obj || new Movie()).__init(bb.readInt32(bb.position()) + bb.position(), bb);
};
/**
......@@ -239,7 +240,8 @@ static getRootAsMovie(bb:flatbuffers.ByteBuffer, obj?:Movie):Movie {
* @returns Movie
*/
static getSizePrefixedRootAsMovie(bb:flatbuffers.ByteBuffer, obj?:Movie):Movie {
return (obj || new Movie).__init(bb.readInt32(bb.position()) + bb.position(), bb);
bb.setPosition(bb.position() + flatbuffers.SIZE_PREFIX_LENGTH);
return (obj || new Movie()).__init(bb.readInt32(bb.position()) + bb.position(), bb);
};
/**
......
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