From 4d7890c2c912d926f5e7a11cc2106bb37b5f0158 Mon Sep 17 00:00:00 2001 From: Wouter van Oortmerssen Date: Mon, 18 Apr 2016 11:48:53 -0700 Subject: [PATCH] Fixed whole-number float default values missing a .0 suffix. A previous commit that added "f" for C++/Java/C# would break on gcc of constants like 3f, which are now output as 3.0f Tested: on Linux Change-Id: If9cabbe3c6d6948a5050b8b123bda9c06e181f52 --- src/idl_parser.cpp | 4 ++++ tests/MyGame/Example/Monster.cs | 5 ++++- tests/MyGame/Example/Monster.go | 11 ++++++++++- tests/MyGame/Example/Monster.java | 5 ++++- tests/MyGame/Example/Monster.php | 26 +++++++++++++++++++++++--- tests/MyGame/Example/Monster.py | 10 +++++++++- tests/monster_test.bfbs | Bin 2912 -> 2968 bytes tests/monster_test.fbs | 1 + tests/monster_test_generated.h | 13 ++++++++++--- tests/monster_test_generated.js | 18 +++++++++++++++++- tests/monsterdata_python_wire.mon | Bin 288 -> 288 bytes tests/monsterdata_test.mon | Bin 384 -> 384 bytes 12 files changed, 82 insertions(+), 11 deletions(-) diff --git a/src/idl_parser.cpp b/src/idl_parser.cpp index e0eaa5d6a..28fd6a377 100644 --- a/src/idl_parser.cpp +++ b/src/idl_parser.cpp @@ -543,6 +543,10 @@ CheckedError Parser::ParseField(StructDef &struct_def) { if (!IsScalar(type.base_type)) return Error("default values currently only supported for scalars"); ECHECK(ParseSingleValue(field->value)); + if (IsFloat(field->value.type.base_type)) { + if (!strpbrk(field->value.constant.c_str(), ".eE")) + field->value.constant += ".0"; + } } if (type.enum_def && diff --git a/tests/MyGame/Example/Monster.cs b/tests/MyGame/Example/Monster.cs index e53afcc64..cd39d9515 100644 --- a/tests/MyGame/Example/Monster.cs +++ b/tests/MyGame/Example/Monster.cs @@ -74,8 +74,10 @@ public sealed class Monster : Table { public bool MutateTestarrayofbools(int j, bool testarrayofbools) { int o = __offset(52); if (o != 0) { bb.Put(__vector(o) + j * 1, (byte)(testarrayofbools ? 1 : 0)); return true; } else { return false; } } public float Testf { get { int o = __offset(54); return o != 0 ? bb.GetFloat(o + bb_pos) : (float)3.14159f; } } public bool MutateTestf(float testf) { int o = __offset(54); if (o != 0) { bb.PutFloat(o + bb_pos, testf); return true; } else { return false; } } + public float Testf2 { get { int o = __offset(56); return o != 0 ? bb.GetFloat(o + bb_pos) : (float)3.0f; } } + public bool MutateTestf2(float testf2) { int o = __offset(56); if (o != 0) { bb.PutFloat(o + bb_pos, testf2); return true; } else { return false; } } - public static void StartMonster(FlatBufferBuilder builder) { builder.StartObject(26); } + public static void StartMonster(FlatBufferBuilder builder) { builder.StartObject(27); } public static void AddPos(FlatBufferBuilder builder, Offset posOffset) { builder.AddStruct(0, posOffset.Value, 0); } public static void AddMana(FlatBufferBuilder builder, short mana) { builder.AddShort(1, mana, 150); } public static void AddHp(FlatBufferBuilder builder, short hp) { builder.AddShort(2, hp, 100); } @@ -112,6 +114,7 @@ public sealed class Monster : Table { public static VectorOffset CreateTestarrayofboolsVector(FlatBufferBuilder builder, bool[] data) { builder.StartVector(1, data.Length, 1); for (int i = data.Length - 1; i >= 0; i--) builder.AddBool(data[i]); return builder.EndVector(); } public static void StartTestarrayofboolsVector(FlatBufferBuilder builder, int numElems) { builder.StartVector(1, numElems, 1); } public static void AddTestf(FlatBufferBuilder builder, float testf) { builder.AddFloat(25, testf, 3.14159f); } + public static void AddTestf2(FlatBufferBuilder builder, float testf2) { builder.AddFloat(26, testf2, 3.0f); } public static Offset EndMonster(FlatBufferBuilder builder) { int o = builder.EndObject(); builder.Required(o, 10); // name diff --git a/tests/MyGame/Example/Monster.go b/tests/MyGame/Example/Monster.go index afdfe42d7..4de8fdc48 100644 --- a/tests/MyGame/Example/Monster.go +++ b/tests/MyGame/Example/Monster.go @@ -321,7 +321,15 @@ func (rcv *Monster) Testf() float32 { return 3.14159 } -func MonsterStart(builder *flatbuffers.Builder) { builder.StartObject(26) } +func (rcv *Monster) Testf2() float32 { + o := flatbuffers.UOffsetT(rcv._tab.Offset(56)) + if o != 0 { + return rcv._tab.GetFloat32(o + rcv._tab.Pos) + } + return 3.0 +} + +func MonsterStart(builder *flatbuffers.Builder) { builder.StartObject(27) } func MonsterAddPos(builder *flatbuffers.Builder, pos flatbuffers.UOffsetT) { builder.PrependStructSlot(0, flatbuffers.UOffsetT(pos), 0) } func MonsterAddMana(builder *flatbuffers.Builder, mana int16) { builder.PrependInt16Slot(1, mana, 150) } func MonsterAddHp(builder *flatbuffers.Builder, hp int16) { builder.PrependInt16Slot(2, hp, 100) } @@ -359,4 +367,5 @@ func MonsterAddTestarrayofbools(builder *flatbuffers.Builder, testarrayofbools f func MonsterStartTestarrayofboolsVector(builder *flatbuffers.Builder, numElems int) flatbuffers.UOffsetT { return builder.StartVector(1, numElems, 1) } func MonsterAddTestf(builder *flatbuffers.Builder, testf float32) { builder.PrependFloat32Slot(25, testf, 3.14159) } +func MonsterAddTestf2(builder *flatbuffers.Builder, testf2 float32) { builder.PrependFloat32Slot(26, testf2, 3.0) } func MonsterEnd(builder *flatbuffers.Builder) flatbuffers.UOffsetT { return builder.EndObject() } diff --git a/tests/MyGame/Example/Monster.java b/tests/MyGame/Example/Monster.java index 0b9a29d1f..487eba6eb 100644 --- a/tests/MyGame/Example/Monster.java +++ b/tests/MyGame/Example/Monster.java @@ -80,8 +80,10 @@ public final class Monster extends Table { public boolean mutateTestarrayofbools(int j, boolean testarrayofbools) { int o = __offset(52); if (o != 0) { bb.put(__vector(o) + j * 1, (byte)(testarrayofbools ? 1 : 0)); return true; } else { return false; } } public float testf() { int o = __offset(54); return o != 0 ? bb.getFloat(o + bb_pos) : 3.14159f; } public boolean mutateTestf(float testf) { int o = __offset(54); if (o != 0) { bb.putFloat(o + bb_pos, testf); return true; } else { return false; } } + public float testf2() { int o = __offset(56); return o != 0 ? bb.getFloat(o + bb_pos) : 3.0f; } + public boolean mutateTestf2(float testf2) { int o = __offset(56); if (o != 0) { bb.putFloat(o + bb_pos, testf2); return true; } else { return false; } } - public static void startMonster(FlatBufferBuilder builder) { builder.startObject(26); } + public static void startMonster(FlatBufferBuilder builder) { builder.startObject(27); } public static void addPos(FlatBufferBuilder builder, int posOffset) { builder.addStruct(0, posOffset, 0); } public static void addMana(FlatBufferBuilder builder, short mana) { builder.addShort(1, mana, 150); } public static void addHp(FlatBufferBuilder builder, short hp) { builder.addShort(2, hp, 100); } @@ -118,6 +120,7 @@ public final class Monster extends Table { public static int createTestarrayofboolsVector(FlatBufferBuilder builder, boolean[] data) { builder.startVector(1, data.length, 1); for (int i = data.length - 1; i >= 0; i--) builder.addBoolean(data[i]); return builder.endVector(); } public static void startTestarrayofboolsVector(FlatBufferBuilder builder, int numElems) { builder.startVector(1, numElems, 1); } public static void addTestf(FlatBufferBuilder builder, float testf) { builder.addFloat(25, testf, 3.14159f); } + public static void addTestf2(FlatBufferBuilder builder, float testf2) { builder.addFloat(26, testf2, 3.0f); } public static int endMonster(FlatBufferBuilder builder) { int o = builder.endObject(); builder.required(o, 10); // name diff --git a/tests/MyGame/Example/Monster.php b/tests/MyGame/Example/Monster.php index 70f4d34e6..e02413879 100644 --- a/tests/MyGame/Example/Monster.php +++ b/tests/MyGame/Example/Monster.php @@ -342,22 +342,31 @@ class Monster extends Table return $o != 0 ? $this->bb->getFloat($o + $this->bb_pos) : 3.14159; } + /** + * @return float + */ + public function getTestf2() + { + $o = $this->__offset(56); + return $o != 0 ? $this->bb->getFloat($o + $this->bb_pos) : 3.0; + } + /** * @param FlatBufferBuilder $builder * @return void */ public static function startMonster(FlatBufferBuilder $builder) { - $builder->StartObject(26); + $builder->StartObject(27); } /** * @param FlatBufferBuilder $builder * @return Monster */ - public static function createMonster(FlatBufferBuilder $builder, $pos, $mana, $hp, $name, $inventory, $color, $test_type, $test, $test4, $testarrayofstring, $testarrayoftables, $enemy, $testnestedflatbuffer, $testempty, $testbool, $testhashs32_fnv1, $testhashu32_fnv1, $testhashs64_fnv1, $testhashu64_fnv1, $testhashs32_fnv1a, $testhashu32_fnv1a, $testhashs64_fnv1a, $testhashu64_fnv1a, $testarrayofbools, $testf) + public static function createMonster(FlatBufferBuilder $builder, $pos, $mana, $hp, $name, $inventory, $color, $test_type, $test, $test4, $testarrayofstring, $testarrayoftables, $enemy, $testnestedflatbuffer, $testempty, $testbool, $testhashs32_fnv1, $testhashu32_fnv1, $testhashs64_fnv1, $testhashu64_fnv1, $testhashs32_fnv1a, $testhashu32_fnv1a, $testhashs64_fnv1a, $testhashu64_fnv1a, $testarrayofbools, $testf, $testf2) { - $builder->startObject(26); + $builder->startObject(27); self::addPos($builder, $pos); self::addMana($builder, $mana); self::addHp($builder, $hp); @@ -383,6 +392,7 @@ class Monster extends Table self::addTesthashu64Fnv1a($builder, $testhashu64_fnv1a); self::addTestarrayofbools($builder, $testarrayofbools); self::addTestf($builder, $testf); + self::addTestf2($builder, $testf2); $o = $builder->endObject(); $builder->required($o, 10); // name return $o; @@ -777,6 +787,16 @@ class Monster extends Table $builder->addFloatX(25, $testf, 3.14159); } + /** + * @param FlatBufferBuilder $builder + * @param float + * @return void + */ + public static function addTestf2(FlatBufferBuilder $builder, $testf2) + { + $builder->addFloatX(26, $testf2, 3.0); + } + /** * @param FlatBufferBuilder $builder * @return int table offset diff --git a/tests/MyGame/Example/Monster.py b/tests/MyGame/Example/Monster.py index 8a19b24fb..2e7c000ed 100644 --- a/tests/MyGame/Example/Monster.py +++ b/tests/MyGame/Example/Monster.py @@ -269,7 +269,14 @@ class Monster(object): return self._tab.Get(flatbuffers.number_types.Float32Flags, o + self._tab.Pos) return 3.14159 -def MonsterStart(builder): builder.StartObject(26) + # Monster + def Testf2(self): + o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(56)) + if o != 0: + return self._tab.Get(flatbuffers.number_types.Float32Flags, o + self._tab.Pos) + return 3.0 + +def MonsterStart(builder): builder.StartObject(27) def MonsterAddPos(builder, pos): builder.PrependStructSlot(0, flatbuffers.number_types.UOffsetTFlags.py_type(pos), 0) def MonsterAddMana(builder, mana): builder.PrependInt16Slot(1, mana, 150) def MonsterAddHp(builder, hp): builder.PrependInt16Slot(2, hp, 100) @@ -301,4 +308,5 @@ def MonsterAddTesthashu64Fnv1a(builder, testhashu64Fnv1a): builder.PrependUint64 def MonsterAddTestarrayofbools(builder, testarrayofbools): builder.PrependUOffsetTRelativeSlot(24, flatbuffers.number_types.UOffsetTFlags.py_type(testarrayofbools), 0) def MonsterStartTestarrayofboolsVector(builder, numElems): return builder.StartVector(1, numElems, 1) def MonsterAddTestf(builder, testf): builder.PrependFloat32Slot(25, testf, 3.14159) +def MonsterAddTestf2(builder, testf2): builder.PrependFloat32Slot(26, testf2, 3.0) def MonsterEnd(builder): return builder.EndObject() diff --git a/tests/monster_test.bfbs b/tests/monster_test.bfbs index 084798598be104aec8b445d97c35e37acb01939d..16a5bff54486d2a74df2898b54792caf71a1676a 100644 GIT binary patch delta 364 zcmYk2F)su`6vyA&L)P6%w!ke%j!vV_S<{{TaZ&4LKr2JvKi2k8bTrl)ONeZnIjxQC5iLLf51-Z|1!a3R z2Rc4qltJ#$LFm}^Fu~vIC0agAl6|VdKVO-1HTQa|v03%1$5rx()**9VpiM5WqZwL; zwu4#=o}i6?1r1<}h9{76HU(qEO$NM$5AtU;gcPpHowsAH9hF&T$1gI8xaHT~?*1`N Tj?k*lxngG)CZFY|u^zJNL+ delta 303 zcmW-dAy30l6h^<>0viMpM8|-Y%pfWtL10Lh3^CjoOHfM?3qfM|0R$2%Cup8Q5EK#= z`~ZSO%p)cUnV6U)VQP<4#Odkl{o4Dbcjd2@+pX<5kme*s_@^K>7o^vDX@Yx2DP*<- zL+E9lV|&P+tCnQ_y4Bp0l}pp9_cIK0T1Tw|q*J$N)wM2nyRZzwulFc4F7NKbHrV;? zy?6H(mm*$fm00;7&O>q^c4KGmFsNLubDU6QNXK*bY|ctCKEa(VT_POS); } Vec3 *mutable_pos() { return GetStruct(VT_POS); } @@ -261,6 +262,8 @@ struct Monster FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { flatbuffers::Vector *mutable_testarrayofbools() { return GetPointer *>(VT_TESTARRAYOFBOOLS); } float testf() const { return GetField(VT_TESTF, 3.14159f); } bool mutate_testf(float _testf) { return SetField(VT_TESTF, _testf); } + float testf2() const { return GetField(VT_TESTF2, 3.0f); } + bool mutate_testf2(float _testf2) { return SetField(VT_TESTF2, _testf2); } bool Verify(flatbuffers::Verifier &verifier) const { return VerifyTableStart(verifier) && VerifyField(verifier, VT_POS) && @@ -300,6 +303,7 @@ struct Monster FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { VerifyField(verifier, VT_TESTARRAYOFBOOLS) && verifier.Verify(testarrayofbools()) && VerifyField(verifier, VT_TESTF) && + VerifyField(verifier, VT_TESTF2) && verifier.EndTable(); } }; @@ -332,10 +336,11 @@ struct MonsterBuilder { void add_testhashu64_fnv1a(uint64_t testhashu64_fnv1a) { fbb_.AddElement(Monster::VT_TESTHASHU64_FNV1A, testhashu64_fnv1a, 0); } void add_testarrayofbools(flatbuffers::Offset> testarrayofbools) { fbb_.AddOffset(Monster::VT_TESTARRAYOFBOOLS, testarrayofbools); } void add_testf(float testf) { fbb_.AddElement(Monster::VT_TESTF, testf, 3.14159f); } + void add_testf2(float testf2) { fbb_.AddElement(Monster::VT_TESTF2, testf2, 3.0f); } MonsterBuilder(flatbuffers::FlatBufferBuilder &_fbb) : fbb_(_fbb) { start_ = fbb_.StartTable(); } MonsterBuilder &operator=(const MonsterBuilder &); flatbuffers::Offset Finish() { - auto o = flatbuffers::Offset(fbb_.EndTable(start_, 26)); + auto o = flatbuffers::Offset(fbb_.EndTable(start_, 27)); fbb_.Required(o, Monster::VT_NAME); // name return o; } @@ -366,12 +371,14 @@ inline flatbuffers::Offset CreateMonster(flatbuffers::FlatBufferBuilder int64_t testhashs64_fnv1a = 0, uint64_t testhashu64_fnv1a = 0, flatbuffers::Offset> testarrayofbools = 0, - float testf = 3.14159f) { + float testf = 3.14159f, + float testf2 = 3.0f) { MonsterBuilder builder_(_fbb); builder_.add_testhashu64_fnv1a(testhashu64_fnv1a); builder_.add_testhashs64_fnv1a(testhashs64_fnv1a); builder_.add_testhashu64_fnv1(testhashu64_fnv1); builder_.add_testhashs64_fnv1(testhashs64_fnv1); + builder_.add_testf2(testf2); builder_.add_testf(testf); builder_.add_testarrayofbools(testarrayofbools); builder_.add_testhashu32_fnv1a(testhashu32_fnv1a); diff --git a/tests/monster_test_generated.js b/tests/monster_test_generated.js index c13246ed5..eed24b5ea 100644 --- a/tests/monster_test_generated.js +++ b/tests/monster_test_generated.js @@ -685,11 +685,19 @@ MyGame.Example.Monster.prototype.testf = function() { return offset ? this.bb.readFloat32(this.bb_pos + offset) : 3.14159; }; +/** + * @returns {number} + */ +MyGame.Example.Monster.prototype.testf2 = function() { + var offset = this.bb.__offset(this.bb_pos, 56); + return offset ? this.bb.readFloat32(this.bb_pos + offset) : 3.0; +}; + /** * @param {flatbuffers.Builder} builder */ MyGame.Example.Monster.startMonster = function(builder) { - builder.startObject(26); + builder.startObject(27); }; /** @@ -1005,6 +1013,14 @@ MyGame.Example.Monster.addTestf = function(builder, testf) { builder.addFieldFloat32(25, testf, 3.14159); }; +/** + * @param {flatbuffers.Builder} builder + * @param {number} testf2 + */ +MyGame.Example.Monster.addTestf2 = function(builder, testf2) { + builder.addFieldFloat32(26, testf2, 3.0); +}; + /** * @param {flatbuffers.Builder} builder * @returns {flatbuffers.Offset} diff --git a/tests/monsterdata_python_wire.mon b/tests/monsterdata_python_wire.mon index a574e3108e1e4c8d0a226480929925a6495efda6..8cc54d795aeba99ee343d106fe92309e423e52f1 100644 GIT binary patch delta 70 zcmZ3$w1A1zfdK@p7+fZDn{tB~3=9rH>@m^PTAB&Sr~%>}AO^AJ7&I8T7({?9tBDKE JnL*S|cK|@K3A+FQ delta 70 zcmZ3$w1A1*fdK+67(6C&nsQnIc`iWgGttvp79_(2#1%lC0cFcEXfSXwh%i`8oNvx( IG4ZB505#1Cvj6}9 diff --git a/tests/monsterdata_test.mon b/tests/monsterdata_test.mon index ef04a53951641b737132ba4f3bab4c5e0f93737e..4c032bcfeae89ef809a3cd19c47d8fd381b1481d 100644 GIT binary patch delta 79 zcmZo*ZeSMlU|?YI_4f+~Qb1tE&@fThlo`mF=xNW{02GM;Vuy(ng&9>QE>-3P$}lhj U;jW2Sok6N5OEQ{+=|Dz(04D|xJpcdz delta 79 zcmZo*ZeSMnU|?YI_4f+~Qea@g&@xfbl+j|Mr#)v2P$UM3T_#QxX4IItRGAl~gc*n# VfpE{ni_VM|lf@a$fn*?~J^&gv4nqI{