Skip to content

Commit 60c6eed

Browse files
committed
Simplify definition of NaN
Replace u64->double bitcast with 0.0/0.0. Change definition of JS_FLOAT64_NAN in the same way because that lets us drop <math.h> from quickjs.h. Replace INFINITY and -INFINITY with 1.0/0.0 and -1.0/0.0 respectively because we already defined globalThis.INFINITY that way and so we might as well go all the way.
1 parent f93dd58 commit 60c6eed

File tree

3 files changed

+12
-16
lines changed

3 files changed

+12
-16
lines changed

cutils.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ static inline double fromfp16(uint16_t v) {
362362
double d, s;
363363
int e;
364364
if ((v & 0x7C00) == 0x7C00) {
365-
d = (v & 0x3FF) ? NAN : INFINITY;
365+
d = (v & 0x3FF) ? 0.0/0.0 : 1.0/0.0; // nan or +inf
366366
} else {
367367
d = (v & 0x3FF) / 1024.;
368368
e = (v & 0x7C00) >> 10;

quickjs.c

+10-10
Original file line numberDiff line numberDiff line change
@@ -41116,9 +41116,9 @@ static const JSCFunctionListEntry js_number_funcs[] = {
4111641116
JS_CFUNC_DEF("isSafeInteger", 1, js_number_isSafeInteger ),
4111741117
JS_PROP_DOUBLE_DEF("MAX_VALUE", 1.7976931348623157e+308, 0 ),
4111841118
JS_PROP_DOUBLE_DEF("MIN_VALUE", 5e-324, 0 ),
41119-
JS_PROP_U2D_DEF("NaN", 0x7FF8ull<<48, 0 ), // workaround for msvc
41120-
JS_PROP_DOUBLE_DEF("NEGATIVE_INFINITY", -INFINITY, 0 ),
41121-
JS_PROP_DOUBLE_DEF("POSITIVE_INFINITY", INFINITY, 0 ),
41119+
JS_PROP_DOUBLE_DEF("NaN", 0.0/0.0, 0 ),
41120+
JS_PROP_DOUBLE_DEF("NEGATIVE_INFINITY", -1.0/0.0, 0 ),
41121+
JS_PROP_DOUBLE_DEF("POSITIVE_INFINITY", 1.0/0.0, 0 ),
4112241122
JS_PROP_DOUBLE_DEF("EPSILON", 2.220446049250313e-16, 0 ), /* ES6 */
4112341123
JS_PROP_DOUBLE_DEF("MAX_SAFE_INTEGER", 9007199254740991.0, 0 ), /* ES6 */
4112441124
JS_PROP_DOUBLE_DEF("MIN_SAFE_INTEGER", -9007199254740991.0, 0 ), /* ES6 */
@@ -50196,8 +50196,8 @@ static const JSCFunctionListEntry js_global_funcs[] = {
5019650196
JS_CFUNC_MAGIC_DEF("encodeURIComponent", 1, js_global_encodeURI, 1 ),
5019750197
JS_CFUNC_DEF("escape", 1, js_global_escape ),
5019850198
JS_CFUNC_DEF("unescape", 1, js_global_unescape ),
50199-
JS_PROP_DOUBLE_DEF("Infinity", 1.0 / 0.0, 0 ),
50200-
JS_PROP_U2D_DEF("NaN", 0x7FF8ull<<48, 0 ), // workaround for msvc
50199+
JS_PROP_DOUBLE_DEF("Infinity", 1.0/0.0, 0 ),
50200+
JS_PROP_DOUBLE_DEF("NaN", 0.0/0.0, 0 ),
5020150201
JS_PROP_UNDEFINED_DEF("undefined", 0 ),
5020250202
JS_PROP_STRING_DEF("[Symbol.toStringTag]", "global", JS_PROP_CONFIGURABLE ),
5020350203
};
@@ -50337,7 +50337,7 @@ static double time_clip(double t) {
5033750337
if (t >= -8.64e15 && t <= 8.64e15)
5033850338
return trunc(t) + 0.0; /* convert -0 to +0 */
5033950339
else
50340-
return NAN;
50340+
return JS_FLOAT64_NAN;
5034150341
}
5034250342

5034350343
/* The spec mandates the use of 'double' and it specifies the order
@@ -50357,7 +50357,7 @@ static double set_date_fields(double fields[minimum_length(7)], int is_local) {
5035750357
if (mn < 0)
5035850358
mn += 12;
5035950359
if (ym < -271821 || ym > 275760)
50360-
return NAN;
50360+
return JS_FLOAT64_NAN;
5036150361

5036250362
yi = ym;
5036350363
mi = mn;
@@ -50389,7 +50389,7 @@ static double set_date_fields(double fields[minimum_length(7)], int is_local) {
5038950389
/* emulate 21.4.1.16 MakeDate ( day, time ) */
5039050390
tv = (temp = day * 86400000) + time; /* prevent generation of FMA */
5039150391
if (!isfinite(tv))
50392-
return NAN;
50392+
return JS_FLOAT64_NAN;
5039350393

5039450394
/* adjust for local time and clip */
5039550395
if (is_local) {
@@ -50428,7 +50428,7 @@ static JSValue set_date_field(JSContext *ctx, JSValue this_val,
5042850428
int res, first_field, end_field, is_local, i, n;
5042950429
double d, a;
5043050430

50431-
d = NAN;
50431+
d = JS_FLOAT64_NAN;
5043250432
first_field = (magic >> 8) & 0x0F;
5043350433
end_field = (magic >> 4) & 0x0F;
5043450434
is_local = magic & 0x0F;
@@ -50628,7 +50628,7 @@ static JSValue js_date_constructor(JSContext *ctx, JSValue new_target,
5062850628
if (i == 0 && fields[0] >= 0 && fields[0] < 100)
5062950629
fields[0] += 1900;
5063050630
}
50631-
val = (i == n) ? set_date_fields(fields, 1) : NAN;
50631+
val = (i == n) ? set_date_fields(fields, 1) : JS_FLOAT64_NAN;
5063250632
}
5063350633
has_val:
5063450634
rv = js_create_from_ctor(ctx, new_target, JS_CLASS_DATE);

quickjs.h

+1-5
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,8 @@
2727
#ifndef QUICKJS_H
2828
#define QUICKJS_H
2929

30-
#include <stdio.h>
3130
#include <stdint.h>
3231
#include <string.h>
33-
#include <math.h>
3432

3533
#ifdef __cplusplus
3634
extern "C" {
@@ -89,7 +87,7 @@ enum {
8987
/* any larger tag is FLOAT64 if JS_NAN_BOXING */
9088
};
9189

92-
#define JS_FLOAT64_NAN NAN
90+
#define JS_FLOAT64_NAN (0.0/0.0)
9391
#define JSValueConst JSValue /* For backwards compatibility. */
9492

9593
#if defined(JS_NAN_BOXING) && JS_NAN_BOXING
@@ -970,7 +968,6 @@ typedef struct JSCFunctionListEntry {
970968
const char *str; /* pure ASCII or UTF-8 encoded */
971969
int32_t i32;
972970
int64_t i64;
973-
uint64_t u64;
974971
double f64;
975972
} u;
976973
} JSCFunctionListEntry;
@@ -999,7 +996,6 @@ typedef struct JSCFunctionListEntry {
999996
#define JS_PROP_INT32_DEF(name, val, prop_flags) { name, prop_flags, JS_DEF_PROP_INT32, 0, { .i32 = val } }
1000997
#define JS_PROP_INT64_DEF(name, val, prop_flags) { name, prop_flags, JS_DEF_PROP_INT64, 0, { .i64 = val } }
1001998
#define JS_PROP_DOUBLE_DEF(name, val, prop_flags) { name, prop_flags, JS_DEF_PROP_DOUBLE, 0, { .f64 = val } }
1002-
#define JS_PROP_U2D_DEF(name, val, prop_flags) { name, prop_flags, JS_DEF_PROP_DOUBLE, 0, { .u64 = val } }
1003999
#define JS_PROP_UNDEFINED_DEF(name, prop_flags) { name, prop_flags, JS_DEF_PROP_UNDEFINED, 0, { .i32 = 0 } }
10041000
#define JS_OBJECT_DEF(name, tab, len, prop_flags) { name, prop_flags, JS_DEF_OBJECT, 0, { .prop_list = { tab, len } } }
10051001
#define JS_ALIAS_DEF(name, from) { name, JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE, JS_DEF_ALIAS, 0, { .alias = { from, -1 } } }

0 commit comments

Comments
 (0)