Skip to content

JS_NewFloat64 is a misleading name #287

@chqrlie

Description

@chqrlie

JS_NewFloat64 tests if its double argument is an int32_t and if so creates a JS_TAG_INT value instead of a JS_TAG_FLOAT64 value.

This is somewhat misleading and prevents creation of actual JS_TAG_FLOAT64 values in places where it would be more appropriate than JS_TAG_INT values, such as Map keys. There is a __JS_NewFloat64 for this but it is debatable whether it should be documented if published at all.

I propose we add a JS_NewNumber API that keeps the current heuristics, change JS_NewFloat64 to always create JS_TAG_FLOAT64 values and remove __JS_NewFloat64.

Note also that float_is_int32 returns false for INT32_MIN. I did some benchmarking for various alternatives to implement this test and it seems a much simpler test code is at least as efficient and much more readable, portable and reliable:

static BOOL float_is_int32(double d)
{
    JSFloat64Union t1, t2;

    if (d >= INT32_MIN && d <= INT32_MAX) {
        t1.d = d;
        t2.d = (int32_t)d;
        return t1.u64 == t2.u64;
    } else {
        return FALSE;
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions