libexpr: Replace Value::mkFloat with constructor calls
The pseudo-constructor `Value::mkFloat` would previously be called on an default-constructed (under-initialized) `Value` to create a properly initialized `Value` that represents a float. This change removes mkFloat and constructs float `Value`s directly. Change-Id: I48ae3a836842ce5e5eda6323404ba7576a6a6964
This commit is contained in:
@@ -530,7 +530,7 @@ void ExprConcatStrings::eval(EvalState & state, Env & env, Value & v)
|
||||
if (firstType == nInt) {
|
||||
v.mkInt(n);
|
||||
} else if (firstType == nFloat) {
|
||||
v.mkFloat(nf);
|
||||
v = {NewValueAs::floating, nf};
|
||||
} else if (firstType == nPath) {
|
||||
if (!context.empty()) {
|
||||
state.ctx.errors
|
||||
|
||||
@@ -128,7 +128,7 @@ public:
|
||||
|
||||
bool number_float(number_float_t val, const string_t & s) override
|
||||
{
|
||||
rs->value().mkFloat(val);
|
||||
rs->value() = {NewValueAs::floating, val};
|
||||
rs->add();
|
||||
return true;
|
||||
}
|
||||
|
||||
+21
-7
@@ -2581,8 +2581,11 @@ static void prim_add(EvalState & state, Value * * args, Value & v)
|
||||
state.forceValue(*args[0], noPos);
|
||||
state.forceValue(*args[1], noPos);
|
||||
if (args[0]->type() == nFloat || args[1]->type() == nFloat)
|
||||
v.mkFloat(state.forceFloat(*args[0], noPos, "while evaluating the first argument of the addition")
|
||||
+ state.forceFloat(*args[1], noPos, "while evaluating the second argument of the addition"));
|
||||
v = {
|
||||
NewValueAs::floating,
|
||||
state.forceFloat(*args[0], noPos, "while evaluating the first argument of the addition")
|
||||
+ state.forceFloat(*args[1], noPos, "while evaluating the second argument of the addition")
|
||||
};
|
||||
else {
|
||||
auto i1 = state.forceInt(*args[0], noPos, "while evaluating the first argument of the addition");
|
||||
auto i2 = state.forceInt(*args[1], noPos, "while evaluating the second argument of the addition");
|
||||
@@ -2601,8 +2604,11 @@ static void prim_sub(EvalState & state, Value * * args, Value & v)
|
||||
state.forceValue(*args[0], noPos);
|
||||
state.forceValue(*args[1], noPos);
|
||||
if (args[0]->type() == nFloat || args[1]->type() == nFloat)
|
||||
v.mkFloat(state.forceFloat(*args[0], noPos, "while evaluating the first argument of the subtraction")
|
||||
- state.forceFloat(*args[1], noPos, "while evaluating the second argument of the subtraction"));
|
||||
v = {
|
||||
NewValueAs::floating,
|
||||
state.forceFloat(*args[0], noPos, "while evaluating the first argument of the subtraction")
|
||||
- state.forceFloat(*args[1], noPos, "while evaluating the second argument of the subtraction")
|
||||
};
|
||||
else {
|
||||
auto i1 = state.forceInt(*args[0], noPos, "while evaluating the first argument of the subtraction");
|
||||
auto i2 = state.forceInt(*args[1], noPos, "while evaluating the second argument of the subtraction");
|
||||
@@ -2622,8 +2628,13 @@ static void prim_mul(EvalState & state, Value * * args, Value & v)
|
||||
state.forceValue(*args[0], noPos);
|
||||
state.forceValue(*args[1], noPos);
|
||||
if (args[0]->type() == nFloat || args[1]->type() == nFloat)
|
||||
v.mkFloat(state.forceFloat(*args[0], noPos, "while evaluating the first of the multiplication")
|
||||
* state.forceFloat(*args[1], noPos, "while evaluating the second argument of the multiplication"));
|
||||
v = {
|
||||
NewValueAs::floating,
|
||||
state.forceFloat(*args[0], noPos, "while evaluating the first of the multiplication")
|
||||
* state.forceFloat(
|
||||
*args[1], noPos, "while evaluating the second argument of the multiplication"
|
||||
)
|
||||
};
|
||||
else {
|
||||
auto i1 = state.forceInt(*args[0], noPos, "while evaluating the first argument of the multiplication");
|
||||
auto i2 = state.forceInt(*args[1], noPos, "while evaluating the second argument of the multiplication");
|
||||
@@ -2648,7 +2659,10 @@ static void prim_div(EvalState & state, Value * * args, Value & v)
|
||||
state.ctx.errors.make<EvalError>("division by zero").debugThrow();
|
||||
|
||||
if (args[0]->type() == nFloat || args[1]->type() == nFloat) {
|
||||
v.mkFloat(state.forceFloat(*args[0], noPos, "while evaluating the first operand of the division") / f2);
|
||||
v = {
|
||||
NewValueAs::floating,
|
||||
state.forceFloat(*args[0], noPos, "while evaluating the first operand of the division") / f2
|
||||
};
|
||||
} else {
|
||||
NixInt i1 = state.forceInt(*args[0], noPos, "while evaluating the first operand of the division");
|
||||
NixInt i2 = state.forceInt(*args[1], noPos, "while evaluating the second operand of the division");
|
||||
|
||||
@@ -44,7 +44,7 @@ void prim_fromTOML(EvalState & state, Value ** args, Value & val)
|
||||
v.mkInt(toml::get<int64_t>(t));
|
||||
break;
|
||||
case toml::value_t::floating:
|
||||
v.mkFloat(toml::get<NixFloat>(t));
|
||||
v = {NewValueAs::floating, toml::get<NixFloat>(t)};
|
||||
break;
|
||||
case toml::value_t::string:
|
||||
v.mkString(toml::get<std::string>(t));
|
||||
|
||||
@@ -802,11 +802,6 @@ public:
|
||||
*this = {NewValueAs::external, *e};
|
||||
}
|
||||
|
||||
inline void mkFloat(NixFloat n)
|
||||
{
|
||||
*this = {NewValueAs::floating, n};
|
||||
}
|
||||
|
||||
bool isList() const
|
||||
{
|
||||
return internalType() == tList;
|
||||
|
||||
@@ -180,8 +180,7 @@ TEST_F(ValuePrintingTests, vExternal)
|
||||
|
||||
TEST_F(ValuePrintingTests, vFloat)
|
||||
{
|
||||
Value vFloat;
|
||||
vFloat.mkFloat(2.0);
|
||||
Value vFloat = {NewValueAs::floating, 2.0};
|
||||
|
||||
test(vFloat, "2");
|
||||
}
|
||||
@@ -332,8 +331,7 @@ TEST_F(ValuePrintingTests, ansiColorsInt)
|
||||
|
||||
TEST_F(ValuePrintingTests, ansiColorsFloat)
|
||||
{
|
||||
Value v;
|
||||
v.mkFloat(1.6);
|
||||
Value v = {NewValueAs::floating, 1.6};
|
||||
|
||||
test(v,
|
||||
ANSI_CYAN "1.6" ANSI_NORMAL,
|
||||
|
||||
Reference in New Issue
Block a user