Force evaluation order in set_date_fields (#268)

This commit is contained in:
Charlie Gordon 2024-02-22 14:08:29 +01:00 committed by GitHub
parent 33f72491a9
commit ef4d8ab2ed
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 112 additions and 39 deletions

View file

@ -46793,7 +46793,7 @@ static __exception int get_date_fields(JSContext *ctx, JSValue obj,
return FALSE; /* NaN */ return FALSE; /* NaN */
d = 0; /* initialize all fields to 0 */ d = 0; /* initialize all fields to 0 */
} else { } else {
d = dval; d = dval; /* assuming -8.64e15 <= dval <= -8.64e15 */
if (is_local) { if (is_local) {
tz = -getTimezoneOffset(d); tz = -getTimezoneOffset(d);
d += tz * 60000; d += tz * 60000;
@ -46839,33 +46839,63 @@ static double time_clip(double t) {
return NAN; return NAN;
} }
/* The spec mandates the use of 'double' and it fixes the order /* The spec mandates the use of 'double' and it specifies the order
of the operations */ of the operations */
static double set_date_fields(double fields[], int is_local) { static double set_date_fields(double fields[], int is_local) {
int64_t y; double y, m, dt, ym, mn, day, h, s, milli, time, tv;
double days, d, h, m1; int yi, mi, i;
int i, m, md; int64_t days;
volatile double temp; /* enforce evaluation order */
m1 = fields[1]; /* emulate 21.4.1.15 MakeDay ( year, month, date ) */
m = fmod(m1, 12); y = fields[0];
if (m < 0) m = fields[1];
m += 12; dt = fields[2];
y = (int64_t)(fields[0] + floor(m1 / 12)); ym = y + floor(m / 12);
days = days_from_year(y); mn = fmod(m, 12);
if (mn < 0)
mn += 12;
if (ym < -271821 || ym > 275760)
return NAN;
for(i = 0; i < m; i++) { yi = ym;
md = month_days[i]; mi = mn;
days = days_from_year(yi);
for(i = 0; i < mi; i++) {
days += month_days[i];
if (i == 1) if (i == 1)
md += days_in_year(y) - 365; days += days_in_year(yi) - 365;
days += md;
} }
days += fields[2] - 1; day = days + dt - 1;
h = fields[3] * 3600000 + fields[4] * 60000 +
fields[5] * 1000 + fields[6]; /* emulate 21.4.1.14 MakeTime ( hour, min, sec, ms ) */
d = days * 86400000 + h; h = fields[3];
if (is_local) m = fields[4];
d += getTimezoneOffset(d) * 60000; s = fields[5];
return time_clip(d); milli = fields[6];
/* Use a volatile intermediary variable to ensure order of evaluation
* as specified in ECMA. This fixes a test262 error on
* test262/test/built-ins/Date/UTC/fp-evaluation-order.js.
* Without the volatile qualifier, the compile can generate code
* that performs the computation in a different order or with instructions
* that produce a different result such as FMA (float multiply and add).
*/
time = h * 3600000;
time += (temp = m * 60000);
time += (temp = s * 1000);
time += milli;
/* emulate 21.4.1.16 MakeDate ( day, time ) */
tv = (temp = day * 86400000) + time; /* prevent generation of FMA */
if (!isfinite(tv))
return NAN;
/* adjust for local time and clip */
if (is_local) {
int64_t ti = tv < INT64_MIN ? INT64_MIN : tv >= 0x1p63 ? INT64_MAX : (int64_t)tv;
tv += getTimezoneOffset(ti) * 60000;
}
return time_clip(tv);
} }
static JSValue get_date_field(JSContext *ctx, JSValue this_val, static JSValue get_date_field(JSContext *ctx, JSValue this_val,
@ -47475,6 +47505,7 @@ static JSValue js_date_getTimezoneOffset(JSContext *ctx, JSValue this_val,
if (isnan(v)) if (isnan(v))
return JS_NAN; return JS_NAN;
else else
/* assuming -8.64e15 <= v <= -8.64e15 */
return JS_NewInt64(ctx, getTimezoneOffset((int64_t)trunc(v))); return JS_NewInt64(ctx, getTimezoneOffset((int64_t)trunc(v)));
} }

View file

@ -84,14 +84,22 @@ function assert(actual, expected, message) {
if (arguments.length == 1) if (arguments.length == 1)
expected = true; expected = true;
if (actual === expected) if (typeof actual === typeof expected) {
if (actual === expected) {
if (actual !== 0 || (1 / actual) === (1 / expected))
return; return;
}
if (typeof actual === 'number') {
if (isNaN(actual) && isNaN(expected))
return true;
}
if (typeof actual === 'object') {
if (actual !== null && expected !== null if (actual !== null && expected !== null
&& typeof actual == 'object' && typeof expected == 'object' && actual.constructor === expected.constructor
&& actual.toString() === expected.toString()) && actual.toString() === expected.toString())
return; return;
}
}
throw Error("assertion failed: got |" + actual + "|" + throw Error("assertion failed: got |" + actual + "|" +
", expected |" + expected + "|" + ", expected |" + expected + "|" +
(message ? " (" + message + ")" : "")); (message ? " (" + message + ")" : ""));
@ -594,20 +602,54 @@ function test_date()
assert(d.toISOString(), "2017-09-22T18:10:11.091Z"); assert(d.toISOString(), "2017-09-22T18:10:11.091Z");
a = Date.parse(d.toISOString()); a = Date.parse(d.toISOString());
assert((new Date(a)).toISOString(), d.toISOString()); assert((new Date(a)).toISOString(), d.toISOString());
s = new Date("2020-01-01T01:01:01.1Z").toISOString();
assert(s == "2020-01-01T01:01:01.100Z");
s = new Date("2020-01-01T01:01:01.12Z").toISOString();
assert(s == "2020-01-01T01:01:01.120Z");
s = new Date("2020-01-01T01:01:01.123Z").toISOString(); s = new Date("2020-01-01T01:01:01.123Z").toISOString();
assert(s == "2020-01-01T01:01:01.123Z"); assert(s, "2020-01-01T01:01:01.123Z");
// implementation defined behavior
s = new Date("2020-01-01T01:01:01.1Z").toISOString();
assert(s, "2020-01-01T01:01:01.100Z");
s = new Date("2020-01-01T01:01:01.12Z").toISOString();
assert(s, "2020-01-01T01:01:01.120Z");
s = new Date("2020-01-01T01:01:01.1234Z").toISOString(); s = new Date("2020-01-01T01:01:01.1234Z").toISOString();
assert(s == "2020-01-01T01:01:01.123Z"); assert(s, "2020-01-01T01:01:01.123Z");
s = new Date("2020-01-01T01:01:01.12345Z").toISOString(); s = new Date("2020-01-01T01:01:01.12345Z").toISOString();
assert(s == "2020-01-01T01:01:01.123Z"); assert(s, "2020-01-01T01:01:01.123Z");
s = new Date("2020-01-01T01:01:01.1235Z").toISOString(); s = new Date("2020-01-01T01:01:01.1235Z").toISOString();
assert(s == "2020-01-01T01:01:01.124Z"); assert(s == "2020-01-01T01:01:01.124Z" || // QuickJS
s == "2020-01-01T01:01:01.123Z"); // nodeJS
s = new Date("2020-01-01T01:01:01.9999Z").toISOString(); s = new Date("2020-01-01T01:01:01.9999Z").toISOString();
assert(s == "2020-01-01T01:01:02.000Z"); assert(s == "2020-01-01T01:01:02.000Z" || // QuickJS
s == "2020-01-01T01:01:01.999Z"); // nodeJS
assert(Date.UTC(2017), 1483228800000);
assert(Date.UTC(2017, 9), 1506816000000);
assert(Date.UTC(2017, 9, 22), 1508630400000);
assert(Date.UTC(2017, 9, 22, 18), 1508695200000);
assert(Date.UTC(2017, 9, 22, 18, 10), 1508695800000);
assert(Date.UTC(2017, 9, 22, 18, 10, 11), 1508695811000);
assert(Date.UTC(2017, 9, 22, 18, 10, 11, 91), 1508695811091);
assert(Date.UTC(NaN), NaN);
assert(Date.UTC(2017, NaN), NaN);
assert(Date.UTC(2017, 9, NaN), NaN);
assert(Date.UTC(2017, 9, 22, NaN), NaN);
assert(Date.UTC(2017, 9, 22, 18, NaN), NaN);
assert(Date.UTC(2017, 9, 22, 18, 10, NaN), NaN);
assert(Date.UTC(2017, 9, 22, 18, 10, 11, NaN), NaN);
assert(Date.UTC(2017, 9, 22, 18, 10, 11, 91, NaN), 1508695811091);
// TODO: Fix rounding errors on Windows/Cygwin.
if (!['win32', 'cygwin'].includes(os.platform)) {
// from test262/test/built-ins/Date/UTC/fp-evaluation-order.js
assert(Date.UTC(1970, 0, 1, 80063993375, 29, 1, -288230376151711740), 29312,
'order of operations / precision in MakeTime');
assert(Date.UTC(1970, 0, 213503982336, 0, 0, 0, -18446744073709552000), 34447360,
'precision in MakeDate');
}
//assert(Date.UTC(2017 - 1e9, 9 + 12e9), 1506816000000); // node fails this
assert(Date.UTC(2017, 9, 22 - 1e10, 18 + 24e10), 1508695200000);
assert(Date.UTC(2017, 9, 22, 18 - 1e10, 10 + 60e10), 1508695800000);
assert(Date.UTC(2017, 9, 22, 18, 10 - 1e10, 11 + 60e10), 1508695811000);
assert(Date.UTC(2017, 9, 22, 18, 10, 11 - 1e12, 91 + 1000e12), 1508695811091);
} }
function test_regexp() function test_regexp()