Skip to content

Commit 95e55eb

Browse files
committed
Fix of Infinity handling
1 parent e682f63 commit 95e55eb

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

quickjs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47540,7 +47540,7 @@ static const JSCFunctionListEntry js_global_funcs[] = {
4754047540
JS_CFUNC_MAGIC_DEF("encodeURIComponent", 1, js_global_encodeURI, 1 ),
4754147541
JS_CFUNC_DEF("escape", 1, js_global_escape ),
4754247542
JS_CFUNC_DEF("unescape", 1, js_global_unescape ),
47543-
JS_PROP_DOUBLE_DEF("Infinity", 1.0 / 0.0, 0 ),
47543+
JS_PROP_DOUBLE_DEF("Infinity", INFINITY, 0 ),
4754447544
JS_PROP_DOUBLE_DEF("NaN", NAN, 0 ),
4754547545
JS_PROP_UNDEFINED_DEF("undefined", 0 ),
4754647546

quickjs.h

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,25 +132,39 @@ typedef struct JSRefCountHeader {
132132
#define JS_MKVAL(tag, val) (((uint64_t)(0xF & tag) << 48) | (uint32_t)(val))
133133
#define JS_MKPTR(tag, ptr) (((uint64_t)(0xF & tag) << 48) | ((uint64_t)(ptr) & 0x0000FFFFFFFFFFFFull))
134134

135-
#define JS_NAN JS_MKVAL(JS_TAG_FLOAT64,1)
135+
#define JS_NAN JS_MKVAL(JS_TAG_FLOAT64,0)
136+
#define JS_INFINITY_NEGATIVE JS_MKVAL(JS_TAG_FLOAT64,1)
137+
#define JS_INFINITY_POSITIVE JS_MKVAL(JS_TAG_FLOAT64,2)
136138

137139
static inline double JS_VALUE_GET_FLOAT64(JSValue v)
138140
{
141+
if (v > 0xFFFFFFFFFFFFFull) {
139142
union { JSValue v; double d; } u;
140-
if (v == JS_NAN)
141-
return JS_FLOAT64_NAN;
142143
u.v = ~v;
143144
return u.d;
144145
}
146+
else if (v == JS_NAN)
147+
return JS_FLOAT64_NAN;
148+
else if (v == JS_INFINITY_POSITIVE)
149+
return INFINITY;
150+
else
151+
return -INFINITY;
152+
}
145153

146154
static inline JSValue __JS_NewFloat64(JSContext *ctx, double d)
147155
{
148156
union { double d; uint64_t u64; } u;
149157
JSValue v;
150158
u.d = d;
151159
/* normalize NaN */
152-
if (js_unlikely((u.u64 & 0x7ff0000000000000) == 0x7ff0000000000000))
160+
if (js_unlikely((u.u64 & 0x7ff0000000000000) == 0x7ff0000000000000)) {
161+
if( isnan(d))
153162
v = JS_NAN;
163+
else if (d < 0.0)
164+
v = JS_INFINITY_NEGATIVE;
165+
else
166+
v = JS_INFINITY_POSITIVE;
167+
}
154168
else
155169
v = ~u.u64;
156170
return v;

0 commit comments

Comments
 (0)