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 084798598..16a5bff54 100644 Binary files a/tests/monster_test.bfbs and b/tests/monster_test.bfbs differ diff --git a/tests/monster_test.fbs b/tests/monster_test.fbs index 28a4d5a03..014f8fc72 100755 --- a/tests/monster_test.fbs +++ b/tests/monster_test.fbs @@ -60,6 +60,7 @@ table Monster { testhashs64_fnv1a:long (id:22, hash:"fnv1a_64"); testhashu64_fnv1a:ulong (id:23, hash:"fnv1a_64"); testf:float = 3.14159 (id:25); + testf2:float = 3 (id:26); } rpc_service MonsterStorage { diff --git a/tests/monster_test_generated.h b/tests/monster_test_generated.h index f66f4c5dd..9fe72f0cd 100644 --- a/tests/monster_test_generated.h +++ b/tests/monster_test_generated.h @@ -204,7 +204,8 @@ struct Monster FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { VT_TESTHASHS64_FNV1A = 48, VT_TESTHASHU64_FNV1A = 50, VT_TESTARRAYOFBOOLS = 52, - VT_TESTF = 54 + VT_TESTF = 54, + VT_TESTF2 = 56 }; const Vec3 *pos() const { return GetStruct(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 a574e3108..8cc54d795 100644 Binary files a/tests/monsterdata_python_wire.mon and b/tests/monsterdata_python_wire.mon differ diff --git a/tests/monsterdata_test.mon b/tests/monsterdata_test.mon index ef04a5395..4c032bcfe 100644 Binary files a/tests/monsterdata_test.mon and b/tests/monsterdata_test.mon differ