1
0
mirror of https://github.com/OpenRCT2/OpenRCT2 synced 2026-01-22 06:23:04 +01:00

Improve stringify of expressions

This commit is contained in:
Ted John
2020-02-24 19:25:18 +00:00
parent ae0c2638e3
commit dfd45651dc
4 changed files with 122 additions and 67 deletions

View File

@@ -13,6 +13,7 @@
# include "../interface/InteractiveConsole.h"
# include "Duktape.hpp"
# include "ScriptEngine.h"
namespace OpenRCT2::Scripting
{
@@ -34,49 +35,7 @@ namespace OpenRCT2::Scripting
void log(DukValue val)
{
std::string str;
switch (val.type())
{
case DukValue::Type::UNDEFINED:
str = "undefined";
break;
case DukValue::Type::NULLREF:
str = "null";
break;
case DukValue::Type::BOOLEAN:
str = val.as_bool() ? "true" : "false";
break;
case DukValue::Type::NUMBER:
{
const auto d = val.as_double();
const duk_int_t i = val.as_int();
if (AlmostEqual<double>(d, i))
{
str = std::to_string(i);
}
else
{
str = std::to_string(d);
}
break;
}
case DukValue::Type::STRING:
str = val.as_string();
break;
case DukValue::Type::OBJECT:
str = "{}";
break;
case DukValue::Type::BUFFER:
str = "buffer";
break;
case DukValue::Type::POINTER:
str = "pointer";
break;
case DukValue::Type::LIGHTFUNC:
break;
}
_console.WriteLine(str);
_console.WriteLine(Stringify(val));
}
static void Register(duk_context* ctx)
@@ -84,19 +43,6 @@ namespace OpenRCT2::Scripting
dukglue_register_method(ctx, &ScConsole::clear, "clear");
dukglue_register_method(ctx, &ScConsole::log, "log");
}
private:
// Taken from http://en.cppreference.com/w/cpp/types/numeric_limits/epsilon
template<class T>
static typename std::enable_if<!std::numeric_limits<T>::is_integer, bool>::type AlmostEqual(T x, T y, int32_t ulp = 20)
{
// the machine epsilon has to be scaled to the magnitude of the values used
// and multiplied by the desired precision in ULPs (units in the last place)
return std::abs(x - y) <= std::numeric_limits<T>::epsilon() * std::abs(x + y) * ulp
// unless the result is subnormal
|| std::abs(x - y)
< (std::numeric_limits<T>::min)(); // TODO: Remove parentheses around min once the macro is removed
}
};
} // namespace OpenRCT2::Scripting