Presently, the default cast for var
to double
results in 0
on failure, like when the type is incompatible. JS/ECMAScript state that the result should be NaN instead.
I believe the snippet I’ve provided below covers all of the cases outlined here:
- http://www.ecma-international.org/ecma-262/6.0/#sec-parsefloat-string
- http://www.ecma-international.org/ecma-262/6.0/#sec-tonumber
static double getDouble (Args a, int index) noexcept
{
if (isPositiveAndBelow (index, a.numArguments))
{
const var& v = a.arguments[index];
if (v.isDouble() || v.isInt() || v.isInt64() || v.isBool() || v.isString())
return static_cast<double> (v);
}
return std::numeric_limits<double>::quiet_NaN();
}