Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TS/Rust/Go/Dart] Fix handling of +/-inf defaults in codegen #7588

Merged
merged 9 commits into from
Nov 8, 2022
Merged
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
15 changes: 12 additions & 3 deletions dart/test/flat_buffers_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ class CheckOtherLangaugesData {
'testrequirednestedflatbuffer: null, scalarKeySortedTables: null, '
'nativeInline: null, '
'longEnumNonEnumDefault: LongEnum{value: 0}, '
'longEnumNormalDefault: LongEnum{value: 2}}, '
'longEnumNormalDefault: LongEnum{value: 2}, nanDefault: NaN, '
'infDefault: Infinity, positiveInfDefault: Infinity, infinityDefault: '
'Infinity, positiveInfinityDefault: Infinity, negativeInfDefault: '
'-Infinity, negativeInfinityDefault: -Infinity, doubleInfDefault: Infinity}, '
'test4: [Test{a: 10, b: 20}, Test{a: 30, b: 40}], '
'testarrayofstring: [test1, test2], testarrayoftables: null, '
'enemy: Monster{pos: null, mana: 150, hp: 100, name: Fred, '
Expand All @@ -110,7 +113,10 @@ class CheckOtherLangaugesData {
'testrequirednestedflatbuffer: null, scalarKeySortedTables: null, '
'nativeInline: null, '
'longEnumNonEnumDefault: LongEnum{value: 0}, '
'longEnumNormalDefault: LongEnum{value: 2}}, '
'longEnumNormalDefault: LongEnum{value: 2}, nanDefault: NaN, '
'infDefault: Infinity, positiveInfDefault: Infinity, infinityDefault: '
'Infinity, positiveInfinityDefault: Infinity, negativeInfDefault: '
'-Infinity, negativeInfinityDefault: -Infinity, doubleInfDefault: Infinity}, '
'testnestedflatbuffer: null, testempty: null, testbool: true, '
'testhashs32Fnv1: -579221183, testhashu32Fnv1: 3715746113, '
'testhashs64Fnv1: 7930699090847568257, '
Expand All @@ -137,7 +143,10 @@ class CheckOtherLangaugesData {
'miss, val: 0, count: 0}, Stat{id: hit, val: 10, count: 1}], '
'nativeInline: Test{a: 1, b: 2}, '
'longEnumNonEnumDefault: LongEnum{value: 0}, '
'longEnumNormalDefault: LongEnum{value: 2}}',
'longEnumNormalDefault: LongEnum{value: 2}, nanDefault: NaN, '
'infDefault: Infinity, positiveInfDefault: Infinity, infinityDefault: '
'Infinity, positiveInfinityDefault: Infinity, negativeInfDefault: '
'-Infinity, negativeInfinityDefault: -Infinity, doubleInfDefault: Infinity}'
);
}
}
Expand Down
12 changes: 12 additions & 0 deletions include/flatbuffers/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,18 @@ inline uint64_t StringToUInt(const char *s, int base = 10) {
return StringToIntegerImpl(&val, s, base) ? val : 0;
}

inline bool StringIsFlatbufferNan(const std::string &s) {
return s == "nan" || s == "+nan" || s == "-nan";
}

inline bool StringIsFlatbufferPositiveInfinity(const std::string &s) {
return s == "inf" || s == "+inf" || s == "infinity" || s == "+infinity";
}

inline bool StringIsFlatbufferNegativeInfinity(const std::string &s) {
return s == "-inf" || s == "-infinity";
}

typedef bool (*LoadFileFunction)(const char *filename, bool binary,
std::string *dest);
typedef bool (*FileExistsFunction)(const char *filename);
Expand Down
7 changes: 7 additions & 0 deletions src/bfbs_gen_nim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,13 @@ class NimBfbsGenerator : public BaseBfbsGenerator {
std::string DefaultValue(const r::Field *field) const {
const r::BaseType base_type = field->type()->base_type();
if (IsFloatingPoint(base_type)) {
if (field->default_real() != field->default_real()) {
return "NaN";
} else if (field->default_real() == std::numeric_limits<double>::infinity()) {
return "Inf";
} else if (field->default_real() == -std::numeric_limits<double>::infinity()) {
return "-Inf";
}
return NumToString(field->default_real());
}
if (IsBool(base_type)) {
Expand Down
20 changes: 11 additions & 9 deletions src/idl_gen_dart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

// independent from idl_parser, since this code is not needed for most clients
#include <cassert>
#include <cmath>

#include "flatbuffers/code_generators.h"
#include "flatbuffers/flatbuffers.h"
Expand Down Expand Up @@ -721,16 +722,17 @@ class DartGenerator : public BaseGenerator {
if (!value.constant.empty() && value.constant != "0") {
if (IsBool(value.type.base_type)) {
return "true";
} else if (value.constant == "nan" || value.constant == "+nan" ||
value.constant == "-nan") {
return "double.nan";
} else if (value.constant == "inf" || value.constant == "+inf") {
return "double.infinity";
} else if (value.constant == "-inf") {
return "double.negativeInfinity";
} else {
return value.constant;
}
if (IsScalar(value.type.base_type)) {
if (StringIsFlatbufferNan(value.constant)) {
return "double.nan";
} else if (StringIsFlatbufferPositiveInfinity(value.constant)) {
return "double.infinity";
} else if (StringIsFlatbufferNegativeInfinity(value.constant)) {
return "double.negativeInfinity";
}
}
return value.constant;
} else if (IsBool(value.type.base_type)) {
return "false";
} else if (IsScalar(value.type.base_type) && !IsUnion(value.type)) {
Expand Down
27 changes: 27 additions & 0 deletions src/idl_gen_go.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
// independent from idl_parser, since this code is not needed for most clients

#include <algorithm>
#include <cmath>
#include <sstream>
#include <string>

Expand Down Expand Up @@ -102,6 +103,7 @@ class GoGenerator : public BaseGenerator {
for (auto it = parser_.enums_.vec.begin(); it != parser_.enums_.vec.end();
++it) {
tracked_imported_namespaces_.clear();
needs_math_import_ = false;
needs_imports = false;
std::string enumcode;
GenEnum(**it, &enumcode);
Expand All @@ -121,6 +123,7 @@ class GoGenerator : public BaseGenerator {
for (auto it = parser_.structs_.vec.begin();
it != parser_.structs_.vec.end(); ++it) {
tracked_imported_namespaces_.clear();
needs_math_import_ = false;
std::string declcode;
GenStruct(**it, &declcode);
if (parser_.opts.one_file) {
Expand Down Expand Up @@ -154,6 +157,7 @@ class GoGenerator : public BaseGenerator {
}
};
std::set<const Namespace *, NamespacePtrLess> tracked_imported_namespaces_;
bool needs_math_import_ = false;

// Most field accessors need to retrieve and test the field offset first,
// this is the prefix code for that.
Expand Down Expand Up @@ -1277,6 +1281,23 @@ class GoGenerator : public BaseGenerator {
switch (field.value.type.base_type) {
case BASE_TYPE_BOOL:
return field.value.constant == "0" ? "false" : "true";
case BASE_TYPE_FLOAT:
case BASE_TYPE_DOUBLE: {
const std::string float_type =
field.value.type.base_type == BASE_TYPE_FLOAT ? "float32"
: "float64";
if (StringIsFlatbufferNan(field.value.constant)) {
needs_math_import_ = true;
return float_type + "(math.NaN())";
} else if (StringIsFlatbufferPositiveInfinity(field.value.constant)) {
needs_math_import_ = true;
return float_type + "(math.Inf(1))";
} else if (StringIsFlatbufferNegativeInfinity(field.value.constant)) {
needs_math_import_ = true;
return float_type + "(math.Inf(-1))";
}
return field.value.constant;
}
default: return field.value.constant;
}
}
Expand Down Expand Up @@ -1330,6 +1351,8 @@ class GoGenerator : public BaseGenerator {
if (needs_imports) {
code += "import (\n";
if (is_enum) { code += "\t\"strconv\"\n\n"; }
// math is needed to support non-finite scalar default values.
if (needs_math_import_) { code += "\t\"math\"\n\n"; }
if (!parser_.opts.go_import.empty()) {
code += "\tflatbuffers \"" + parser_.opts.go_import + "\"\n";
} else {
Expand All @@ -1346,6 +1369,10 @@ class GoGenerator : public BaseGenerator {
code += ")\n\n";
} else {
if (is_enum) { code += "import \"strconv\"\n\n"; }
if (needs_math_import_) {
// math is needed to support non-finite scalar default values.
code += "import \"math\"\n\n";
}
}
}

Expand Down
15 changes: 14 additions & 1 deletion src/idl_gen_rust.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

// independent from idl_parser, since this code is not needed for most clients

#include <cmath>

#include "flatbuffers/code_generators.h"
#include "flatbuffers/flatbuffers.h"
#include "flatbuffers/idl.h"
Expand Down Expand Up @@ -1046,8 +1048,19 @@ class RustGenerator : public BaseGenerator {
if (field.IsOptional() && !IsUnion(field.value.type)) { return "None"; }
}
switch (GetFullType(field.value.type)) {
case ftInteger:
case ftInteger: {
return field.value.constant;
}
case ftFloat: {
const std::string float_prefix =
(field.value.type.base_type == BASE_TYPE_FLOAT) ? "f32::" : "f64::";
if (StringIsFlatbufferNan(field.value.constant)) {
return float_prefix + "NAN";
} else if (StringIsFlatbufferPositiveInfinity(field.value.constant)) {
return float_prefix + "INFINITY";
} else if (StringIsFlatbufferNegativeInfinity(field.value.constant)) {
return float_prefix + "NEG_INFINITY";
}
return field.value.constant;
}
case ftBool: {
Expand Down
12 changes: 10 additions & 2 deletions src/idl_gen_ts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include <algorithm>
#include <cassert>
#include <cmath>
#include <unordered_map>
#include <unordered_set>

Expand Down Expand Up @@ -454,9 +455,16 @@ class TsGenerator : public BaseGenerator {
return "BigInt('" + value.constant + "')";
}

default:
if (value.constant == "nan") { return "NaN"; }
default: {
if (StringIsFlatbufferNan(value.constant)) {
return "NaN";
} else if (StringIsFlatbufferPositiveInfinity(value.constant)) {
return "Infinity";
} else if (StringIsFlatbufferNegativeInfinity(value.constant)) {
return "-Infinity";
}
return value.constant;
}
}
}

Expand Down
Loading