Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion goldens/py/flatbuffers/goldens/Universe.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def UniverseCreateGalaxiesVector(builder, data):
return builder.CreateVectorOfTables(data)

def CreateGalaxiesVector(builder, data):
UniverseCreateGalaxiesVector(builder, data)
return UniverseCreateGalaxiesVector(builder, data)

def UniverseEnd(builder):
return builder.EndObject()
Expand Down
32 changes: 14 additions & 18 deletions goldens/swift/basic_generated.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,13 @@ public struct flatbuffers_goldens_Galaxy: FlatBufferTable, FlatbuffersVectorInit
private init(_ t: Table) { _accessor = t }
public init(_ bb: ByteBuffer, o: Int32) { _accessor = Table(bb: bb, position: o) }

private enum VTOFFSET: VOffset {
case numStars = 4
var v: Int32 { Int32(self.rawValue) }
var p: VOffset { self.rawValue }
private struct VT {
static let numStars: VOffset = 4
}

public var numStars: Int64 { let o = _accessor.offset(VTOFFSET.numStars.v); return o == 0 ? 0 : _accessor.readBuffer(of: Int64.self, at: o) }
public var numStars: Int64 { let o = _accessor.offset(VT.numStars); return o == 0 ? 0 : _accessor.readBuffer(of: Int64.self, at: o) }
public static func startGalaxy(_ fbb: inout FlatBufferBuilder) -> UOffset { fbb.startTable(with: 1) }
public static func add(numStars: Int64, _ fbb: inout FlatBufferBuilder) { fbb.add(element: numStars, def: 0, at: VTOFFSET.numStars.p) }
public static func add(numStars: Int64, _ fbb: inout FlatBufferBuilder) { fbb.add(element: numStars, def: 0, at: VT.numStars) }
public static func endGalaxy(_ fbb: inout FlatBufferBuilder, start: UOffset) -> Offset { let end = Offset(offset: fbb.endTable(at: start)); return end }
public static func createGalaxy(
_ fbb: inout FlatBufferBuilder,
Expand All @@ -38,7 +36,7 @@ public struct flatbuffers_goldens_Galaxy: FlatBufferTable, FlatbuffersVectorInit

public static func verify<T>(_ verifier: inout Verifier, at position: Int, of type: T.Type) throws where T: Verifiable {
var _v = try verifier.visitTable(at: position)
try _v.visit(field: VTOFFSET.numStars.p, fieldName: "numStars", required: false, type: Int64.self)
try _v.visit(field: VT.numStars, fieldName: "numStars", required: false, type: Int64.self)
_v.finish()
}
}
Expand All @@ -52,18 +50,16 @@ public struct flatbuffers_goldens_Universe: FlatBufferTable, FlatbuffersVectorIn
private init(_ t: Table) { _accessor = t }
public init(_ bb: ByteBuffer, o: Int32) { _accessor = Table(bb: bb, position: o) }

private enum VTOFFSET: VOffset {
case age = 4
case galaxies = 6
var v: Int32 { Int32(self.rawValue) }
var p: VOffset { self.rawValue }
private struct VT {
static let age: VOffset = 4
static let galaxies: VOffset = 6
}

public var age: Double { let o = _accessor.offset(VTOFFSET.age.v); return o == 0 ? 0.0 : _accessor.readBuffer(of: Double.self, at: o) }
public var galaxies: FlatbufferVector<flatbuffers_goldens_Galaxy> { return _accessor.vector(at: VTOFFSET.galaxies.v, byteSize: 4) }
public var age: Double { let o = _accessor.offset(VT.age); return o == 0 ? 0.0 : _accessor.readBuffer(of: Double.self, at: o) }
public var galaxies: FlatbufferVector<flatbuffers_goldens_Galaxy> { return _accessor.vector(at: VT.galaxies, byteSize: 4) }
public static func startUniverse(_ fbb: inout FlatBufferBuilder) -> UOffset { fbb.startTable(with: 2) }
public static func add(age: Double, _ fbb: inout FlatBufferBuilder) { fbb.add(element: age, def: 0.0, at: VTOFFSET.age.p) }
public static func addVectorOf(galaxies: Offset, _ fbb: inout FlatBufferBuilder) { fbb.add(offset: galaxies, at: VTOFFSET.galaxies.p) }
public static func add(age: Double, _ fbb: inout FlatBufferBuilder) { fbb.add(element: age, def: 0.0, at: VT.age) }
public static func addVectorOf(galaxies: Offset, _ fbb: inout FlatBufferBuilder) { fbb.add(offset: galaxies, at: VT.galaxies) }
public static func endUniverse(_ fbb: inout FlatBufferBuilder, start: UOffset) -> Offset { let end = Offset(offset: fbb.endTable(at: start)); return end }
public static func createUniverse(
_ fbb: inout FlatBufferBuilder,
Expand All @@ -78,8 +74,8 @@ public struct flatbuffers_goldens_Universe: FlatBufferTable, FlatbuffersVectorIn

public static func verify<T>(_ verifier: inout Verifier, at position: Int, of type: T.Type) throws where T: Verifiable {
var _v = try verifier.visitTable(at: position)
try _v.visit(field: VTOFFSET.age.p, fieldName: "age", required: false, type: Double.self)
try _v.visit(field: VTOFFSET.galaxies.p, fieldName: "galaxies", required: false, type: ForwardOffset<Vector<ForwardOffset<flatbuffers_goldens_Galaxy>, flatbuffers_goldens_Galaxy>>.self)
try _v.visit(field: VT.age, fieldName: "age", required: false, type: Double.self)
try _v.visit(field: VT.galaxies, fieldName: "galaxies", required: false, type: ForwardOffset<Vector<ForwardOffset<flatbuffers_goldens_Galaxy>, flatbuffers_goldens_Galaxy>>.self)
_v.finish()
}
}
Expand Down
64 changes: 51 additions & 13 deletions src/idl_gen_python.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1298,7 +1298,7 @@ class PythonGenerator : public BaseGenerator {
code += "):\n";
}

// Recursively generate struct construction statements and instert manual
// Recursively generate struct construction statements and insert manual
// padding.
void StructBuilderBody(const StructDef& struct_def, const char* nameprefix,
std::string* code_ptr, size_t index = 0,
Expand Down Expand Up @@ -1326,7 +1326,7 @@ class PythonGenerator : public BaseGenerator {
if (IsArray(field_type)) {
code += indent + " for " + index_var + " in range(";
code += NumToString(field_type.fixed_length);
code += " , 0, -1):\n";
code += ", 0, -1):\n";
in_array = true;
}
if (IsStruct(type)) {
Expand Down Expand Up @@ -2303,21 +2303,59 @@ class PythonGenerator : public BaseGenerator {
code_base += "\n";
}

void GenPackForStructBody(const StructDef &struct_def, const char *nameprefix,
std::string *code_ptr, const size_t depth = 0) const {
auto &code = *code_ptr;
const std::string indent((depth + 2) * 4, ' ');
code +=
indent + "builder.Prep(" + NumToString(struct_def.minalign) + ", ";
code += NumToString(struct_def.bytesize) + ")\n";
for (auto it = struct_def.fields.vec.rbegin();
it != struct_def.fields.vec.rend(); ++it) {
auto& field = **it;
const auto& field_type = field.value.type;
if (field.padding) {
code +=
indent + "builder.Pad(" + NumToString(field.padding) + ")\n";
}
if (IsStruct(field_type)) {
GenPackForStructBody(
*field_type.struct_def,
(nameprefix + (namer_.Field(field) + ".")).c_str(),
code_ptr, depth);
} else {
std::string namesuffix;
if (IsArray(field_type)) {
const auto index_var = "_idx" + NumToString(depth);
code += indent + "for " + index_var + " in range(";
code += NumToString(field_type.fixed_length);
code += ", 0, -1):\n";
namesuffix = "[" + index_var + "-1]";
}
const auto& type =
IsArray(field_type) ? field_type.VectorType() : field_type;
if (IsStruct(type)) {
GenPackForStructBody(
*field_type.struct_def,
(nameprefix + namer_.Field(field) + namesuffix + ".").c_str(),
code_ptr, depth + 1);
} else {
code += IsArray(field_type) ? " " : "";
code += indent + "builder.Prepend" + GenMethod(field) + "(";
code += nameprefix + namer_.Variable(field) + namesuffix;
code += ")\n";
}
}
}
}

void GenPackForStruct(const StructDef& struct_def,
std::string* code_ptr) const {
auto& code = *code_ptr;
const auto struct_fn = namer_.Function(struct_def);

GenReceiverForObjectAPI(struct_def, code_ptr);
code += "Pack(self, builder):";
code += GenIndents(2) + "return Create" + struct_fn + "(builder";

StructBuilderArgs(struct_def,
/* nameprefix = */ "self.",
/* namesuffix = */ "",
/* has_field_name = */ true,
/* fieldname_suffix = */ ".", code_ptr);
code += ")\n";
code += "Pack(self, builder):\n";
GenPackForStructBody(struct_def, "self.", code_ptr);
code += " return builder.Offset()\n";
}

void GenPackForStructVectorField(const StructDef& struct_def,
Expand Down
5 changes: 4 additions & 1 deletion tests/MyGame/Example/Ability.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,7 @@ def _UnPack(self, ability):

# AbilityT
def Pack(self, builder):
return CreateAbility(builder, self.id, self.distance)
builder.Prep(4, 8)
builder.PrependUint32(self.distance)
builder.PrependUint32(self.id)
return builder.Offset()
34 changes: 27 additions & 7 deletions tests/MyGame/Example/ArrayStruct.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,23 +84,23 @@ def FIsNone(self) -> bool:

def CreateArrayStruct(builder, a, b, c, d_a, d_b, d_c, d_d, e, f):
builder.Prep(8, 160)
for _idx0 in range(2 , 0, -1):
for _idx0 in range(2, 0, -1):
builder.PrependInt64(f[_idx0-1])
builder.Pad(4)
builder.PrependInt32(e)
for _idx0 in range(2 , 0, -1):
for _idx0 in range(2, 0, -1):
builder.Prep(8, 32)
for _idx1 in range(2 , 0, -1):
for _idx1 in range(2, 0, -1):
builder.PrependInt64(d_d[_idx0-1][_idx1-1])
builder.Pad(5)
for _idx1 in range(2 , 0, -1):
for _idx1 in range(2, 0, -1):
builder.PrependInt8(d_c[_idx0-1][_idx1-1])
builder.PrependInt8(d_b[_idx0-1])
for _idx1 in range(2 , 0, -1):
for _idx1 in range(2, 0, -1):
builder.PrependInt32(d_a[_idx0-1][_idx1-1])
builder.Pad(7)
builder.PrependInt8(c)
for _idx0 in range(15 , 0, -1):
for _idx0 in range(15, 0, -1):
builder.PrependInt32(b[_idx0-1])
builder.PrependFloat32(a)
return builder.Offset()
Expand Down Expand Up @@ -179,4 +179,24 @@ def _UnPack(self, arrayStruct):

# ArrayStructT
def Pack(self, builder):
return CreateArrayStruct(builder, self.a, self.b, self.c, self.d.a, self.d.b, self.d.c, self.d.d, self.e, self.f)
builder.Prep(8, 160)
for _idx0 in range(2, 0, -1):
builder.PrependInt64(self.f[_idx0-1])
builder.Pad(4)
builder.PrependInt32(self.e)
for _idx0 in range(2, 0, -1):
builder.Prep(8, 32)
for _idx1 in range(2, 0, -1):
builder.PrependInt64(self.d[_idx0-1].d[_idx1-1])
builder.Pad(5)
for _idx1 in range(2, 0, -1):
builder.PrependInt8(self.d[_idx0-1].c[_idx1-1])
builder.PrependInt8(self.d[_idx0-1].b)
for _idx1 in range(2, 0, -1):
builder.PrependInt32(self.d[_idx0-1].a[_idx1-1])
builder.Pad(7)
builder.PrependInt8(self.c)
for _idx0 in range(15, 0, -1):
builder.PrependInt32(self.b[_idx0-1])
builder.PrependFloat32(self.a)
return builder.Offset()
37 changes: 28 additions & 9 deletions tests/MyGame/Example/LargeArrayStruct.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,23 +120,23 @@ def HIsNone(self) -> bool:

def CreateLargeArrayStruct(builder, d, e, f, g_a, g_b, g_c, g_d, h):
builder.Prep(8, 2496)
for _idx0 in range(64 , 0, -1):
for _idx0 in range(64, 0, -1):
builder.PrependInt8(h[_idx0-1])
for _idx0 in range(64 , 0, -1):
for _idx0 in range(64, 0, -1):
builder.Prep(8, 32)
for _idx1 in range(2 , 0, -1):
for _idx1 in range(2, 0, -1):
builder.PrependInt64(g_d[_idx0-1][_idx1-1])
builder.Pad(5)
for _idx1 in range(2 , 0, -1):
for _idx1 in range(2, 0, -1):
builder.PrependInt8(g_c[_idx0-1][_idx1-1])
builder.PrependInt8(g_b[_idx0-1])
for _idx1 in range(2 , 0, -1):
for _idx1 in range(2, 0, -1):
builder.PrependInt32(g_a[_idx0-1][_idx1-1])
for _idx0 in range(64 , 0, -1):
for _idx0 in range(64, 0, -1):
builder.PrependBool(f[_idx0-1])
for _idx0 in range(64 , 0, -1):
for _idx0 in range(64, 0, -1):
builder.PrependFloat32(e[_idx0-1])
for _idx0 in range(64 , 0, -1):
for _idx0 in range(64, 0, -1):
builder.PrependUint8(d[_idx0-1])
return builder.Offset()

Expand Down Expand Up @@ -223,4 +223,23 @@ def _UnPack(self, largeArrayStruct):

# LargeArrayStructT
def Pack(self, builder):
return CreateLargeArrayStruct(builder, self.d, self.e, self.f, self.g.a, self.g.b, self.g.c, self.g.d, self.h)
builder.Prep(8, 2496)
for _idx0 in range(64, 0, -1):
builder.PrependInt8(self.h[_idx0-1])
for _idx0 in range(64, 0, -1):
builder.Prep(8, 32)
for _idx1 in range(2, 0, -1):
builder.PrependInt64(self.g[_idx0-1].d[_idx1-1])
builder.Pad(5)
for _idx1 in range(2, 0, -1):
builder.PrependInt8(self.g[_idx0-1].c[_idx1-1])
builder.PrependInt8(self.g[_idx0-1].b)
for _idx1 in range(2, 0, -1):
builder.PrependInt32(self.g[_idx0-1].a[_idx1-1])
for _idx0 in range(64, 0, -1):
builder.PrependBool(self.f[_idx0-1])
for _idx0 in range(64, 0, -1):
builder.PrependFloat32(self.e[_idx0-1])
for _idx0 in range(64, 0, -1):
builder.PrependUint8(self.d[_idx0-1])
return builder.Offset()
17 changes: 13 additions & 4 deletions tests/MyGame/Example/NestedStruct.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,13 @@ def DIsNone(self) -> bool:

def CreateNestedStruct(builder, a, b, c, d):
builder.Prep(8, 32)
for _idx0 in range(2 , 0, -1):
for _idx0 in range(2, 0, -1):
builder.PrependInt64(d[_idx0-1])
builder.Pad(5)
for _idx0 in range(2 , 0, -1):
for _idx0 in range(2, 0, -1):
builder.PrependInt8(c[_idx0-1])
builder.PrependInt8(b)
for _idx0 in range(2 , 0, -1):
for _idx0 in range(2, 0, -1):
builder.PrependInt32(a[_idx0-1])
return builder.Offset()

Expand Down Expand Up @@ -162,4 +162,13 @@ def _UnPack(self, nestedStruct):

# NestedStructT
def Pack(self, builder):
return CreateNestedStruct(builder, self.a, self.b, self.c, self.d)
builder.Prep(8, 32)
for _idx0 in range(2, 0, -1):
builder.PrependInt64(self.d[_idx0-1])
builder.Pad(5)
for _idx0 in range(2, 0, -1):
builder.PrependInt8(self.c[_idx0-1])
builder.PrependInt8(self.b)
for _idx0 in range(2, 0, -1):
builder.PrependInt32(self.a[_idx0-1])
return builder.Offset()
6 changes: 5 additions & 1 deletion tests/MyGame/Example/NestedUnion/Test.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,8 @@ def _UnPack(self, test):

# TestT
def Pack(self, builder):
return CreateTest(builder, self.a, self.b)
builder.Prep(2, 4)
builder.Pad(1)
builder.PrependInt8(self.b)
builder.PrependInt16(self.a)
return builder.Offset()
13 changes: 12 additions & 1 deletion tests/MyGame/Example/StructOfStructs.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,15 @@ def _UnPack(self, structOfStructs):

# StructOfStructsT
def Pack(self, builder):
return CreateStructOfStructs(builder, self.a.id, self.a.distance, self.b.a, self.b.b, self.c.id, self.c.distance)
builder.Prep(4, 20)
builder.Prep(4, 8)
builder.PrependUint32(self.c.distance)
builder.PrependUint32(self.c.id)
builder.Prep(2, 4)
builder.Pad(1)
builder.PrependInt8(self.b.b)
builder.PrependInt16(self.b.a)
builder.Prep(4, 8)
builder.PrependUint32(self.a.distance)
builder.PrependUint32(self.a.id)
return builder.Offset()
14 changes: 13 additions & 1 deletion tests/MyGame/Example/StructOfStructsOfStructs.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,16 @@ def _UnPack(self, structOfStructsOfStructs):

# StructOfStructsOfStructsT
def Pack(self, builder):
return CreateStructOfStructsOfStructs(builder, self.a.a.id, self.a.a.distance, self.a.b.a, self.a.b.b, self.a.c.id, self.a.c.distance)
builder.Prep(4, 20)
builder.Prep(4, 20)
builder.Prep(4, 8)
builder.PrependUint32(self.a.c.distance)
builder.PrependUint32(self.a.c.id)
builder.Prep(2, 4)
builder.Pad(1)
builder.PrependInt8(self.a.b.b)
builder.PrependInt16(self.a.b.a)
builder.Prep(4, 8)
builder.PrependUint32(self.a.a.distance)
builder.PrependUint32(self.a.a.id)
return builder.Offset()
6 changes: 5 additions & 1 deletion tests/MyGame/Example/Test.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,8 @@ def _UnPack(self, test):

# TestT
def Pack(self, builder):
return CreateTest(builder, self.a, self.b)
builder.Prep(2, 4)
builder.Pad(1)
builder.PrependInt8(self.b)
builder.PrependInt16(self.a)
return builder.Offset()
15 changes: 14 additions & 1 deletion tests/MyGame/Example/Vec3.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,17 @@ def _UnPack(self, vec3):

# Vec3T
def Pack(self, builder):
return CreateVec3(builder, self.x, self.y, self.z, self.test1, self.test2, self.test3.a, self.test3.b)
builder.Prep(8, 32)
builder.Pad(2)
builder.Prep(2, 4)
builder.Pad(1)
builder.PrependInt8(self.test3.b)
builder.PrependInt16(self.test3.a)
builder.Pad(1)
builder.PrependUint8(self.test2)
builder.PrependFloat64(self.test1)
builder.Pad(4)
builder.PrependFloat32(self.z)
builder.PrependFloat32(self.y)
builder.PrependFloat32(self.x)
return builder.Offset()
Loading