Bug in Javascript interpreter: bad casts to float/double should result in NaN, not 0

javascript

#1

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:

    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();
    }

#2

Right… but that’d also make one of the most-called functions run about 10x slower. It might make more sense to modify the var class to return NaN? Since everything else about var tries to be compatible with the way JS works, that would be OK, and it’d only happen when you try to do something silly like cast a method to a double.


#3

Indeed that does make more sense! I had thought about it but wasn’t sure if var was deliberately designed to be more JS-like.