| 1 |
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 2 |
* vim: set ts=8 sts=2 et sw=2 tw=80: |
| 3 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 |
|
| 7 |
#include "builtin/temporal/Temporal.h" |
| 8 |
|
| 9 |
#include "mozilla/Casting.h" |
| 10 |
#include "mozilla/CheckedInt.h" |
| 11 |
#include "mozilla/FloatingPoint.h" |
| 12 |
#include "mozilla/Likely.h" |
| 13 |
#include "mozilla/MathAlgorithms.h" |
| 14 |
|
| 15 |
#include <algorithm> |
| 16 |
#include <cmath> |
| 17 |
#include <cstdlib> |
| 18 |
#include <initializer_list> |
| 19 |
#include <iterator> |
| 20 |
#include <limits> |
| 21 |
#include <stdint.h> |
| 22 |
#include <string_view> |
| 23 |
#include <utility> |
| 24 |
|
| 25 |
#include "jsnum.h" |
| 26 |
#include "jspubtd.h" |
| 27 |
#include "NamespaceImports.h" |
| 28 |
|
| 29 |
#include "builtin/temporal/Int128.h" |
| 30 |
#include "builtin/temporal/PlainDate.h" |
| 31 |
#include "builtin/temporal/PlainDateTime.h" |
| 32 |
#include "builtin/temporal/PlainMonthDay.h" |
| 33 |
#include "builtin/temporal/PlainTime.h" |
| 34 |
#include "builtin/temporal/PlainYearMonth.h" |
| 35 |
#include "builtin/temporal/TemporalRoundingMode.h" |
| 36 |
#include "builtin/temporal/TemporalUnit.h" |
| 37 |
#include "builtin/temporal/ZonedDateTime.h" |
| 38 |
#include "gc/Barrier.h" |
| 39 |
#include "js/Class.h" |
| 40 |
#include "js/Conversions.h" |
| 41 |
#include "js/ErrorReport.h" |
| 42 |
#include "js/friend/ErrorMessages.h" |
| 43 |
#include "js/Id.h" |
| 44 |
#include "js/Printer.h" |
| 45 |
#include "js/PropertyDescriptor.h" |
| 46 |
#include "js/PropertySpec.h" |
| 47 |
#include "js/RootingAPI.h" |
| 48 |
#include "js/String.h" |
| 49 |
#include "js/Value.h" |
| 50 |
#include "vm/BytecodeUtil.h" |
| 51 |
#include "vm/GlobalObject.h" |
| 52 |
#include "vm/JSAtomState.h" |
| 53 |
#include "vm/JSAtomUtils.h" |
| 54 |
#include "vm/JSContext.h" |
| 55 |
#include "vm/JSObject.h" |
| 56 |
#include "vm/ObjectOperations.h" |
| 57 |
#include "vm/Realm.h" |
| 58 |
#include "vm/StringType.h" |
| 59 |
|
| 60 |
#include "vm/JSObject-inl.h" |
| 61 |
#include "vm/ObjectOperations-inl.h" |
| 62 |
|
| 63 |
using namespace js; |
| 64 |
using namespace js::temporal; |
| 65 |
|
| 66 |
/** |
| 67 |
* GetOption ( options, property, type, values, default ) |
| 68 |
* |
| 69 |
* GetOption specialization when `type=string`. Default value handling must |
| 70 |
* happen in the caller, so we don't provide the `default` parameter here. |
| 71 |
*/ |
| 72 |
static bool GetStringOption(JSContext* cx, Handle<JSObject*> options, |
| 73 |
Handle<PropertyName*> property, |
| 74 |
MutableHandle<JSString*> string) { |
| 75 |
// Step 1. |
| 76 |
Rooted<Value> value(cx); |
| 77 |
if (!GetProperty(cx, options, options, property, &value)) { |
| 78 |
return false; |
| 79 |
} |
| 80 |
|
| 81 |
// Step 2. (Caller should fill in the fallback.) |
| 82 |
if (value.isUndefined()) { |
| 83 |
return true; |
| 84 |
} |
| 85 |
|
| 86 |
// Steps 3-4. (Not applicable when type=string) |
| 87 |
|
| 88 |
// Step 5. |
| 89 |
string.set(JS::ToString(cx, value)); |
| 90 |
if (!string) { |
| 91 |
return false; |
| 92 |
} |
| 93 |
|
| 94 |
// Step 6. (Not applicable in our implementation) |
| 95 |
|
| 96 |
// Step 7. |
| 97 |
return true; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* GetRoundingIncrementOption ( normalizedOptions, dividend, inclusive ) |
| 102 |
*/ |
| 103 |
bool js::temporal::GetRoundingIncrementOption(JSContext* cx, |
| 104 |
Handle<JSObject*> options, |
| 105 |
Increment* increment) { |
| 106 |
// Step 1. |
| 107 |
Rooted<Value> value(cx); |
| 108 |
if (!GetProperty(cx, options, options, cx->names().roundingIncrement, |
| 109 |
&value)) { |
| 110 |
return false; |
| 111 |
} |
| 112 |
|
| 113 |
// Step 2. |
| 114 |
if (value.isUndefined()) { |
| 115 |
*increment = Increment{1}; |
| 116 |
return true; |
| 117 |
} |
| 118 |
|
| 119 |
// Step 3. |
| 120 |
double number; |
| 121 |
if (!ToIntegerWithTruncation(cx, value, "roundingIncrement", &number)) { |
| 122 |
return false; |
| 123 |
} |
| 124 |
|
| 125 |
// Step 4. |
| 126 |
if (number < 1 || number > 1'000'000'000) { |
| 127 |
ToCStringBuf cbuf; |
| 128 |
const char* numStr = NumberToCString(&cbuf, number); |
| 129 |
|
| 130 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 131 |
JSMSG_INVALID_OPTION_VALUE, "roundingIncrement", |
| 132 |
numStr); |
| 133 |
return false; |
| 134 |
} |
| 135 |
|
| 136 |
// Step 5. |
| 137 |
*increment = Increment{uint32_t(number)}; |
| 138 |
return true; |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* ValidateTemporalRoundingIncrement ( increment, dividend, inclusive ) |
| 143 |
*/ |
| 144 |
bool js::temporal::ValidateTemporalRoundingIncrement(JSContext* cx, |
| 145 |
Increment increment, |
| 146 |
int64_t dividend, |
| 147 |
bool inclusive) { |
| 148 |
MOZ_ASSERT(dividend > 0); |
| 149 |
MOZ_ASSERT_IF(!inclusive, dividend > 1); |
| 150 |
|
| 151 |
// Steps 1-2. |
| 152 |
int64_t maximum = inclusive ? dividend : dividend - 1; |
| 153 |
|
| 154 |
// Steps 3-4. |
| 155 |
if (increment.value() > maximum || dividend % increment.value() != 0) { |
| 156 |
Int32ToCStringBuf cbuf; |
| 157 |
const char* numStr = Int32ToCString(&cbuf, int32_t(increment.value())); |
| 158 |
|
| 159 |
// TODO: Better error message could be helpful. |
| 160 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 161 |
JSMSG_INVALID_OPTION_VALUE, "roundingIncrement", |
| 162 |
numStr); |
| 163 |
return false; |
| 164 |
} |
| 165 |
|
| 166 |
// Step 5. |
| 167 |
return true; |
| 168 |
} |
| 169 |
|
| 170 |
static Handle<PropertyName*> ToPropertyName(JSContext* cx, |
| 171 |
TemporalUnitKey key) { |
| 172 |
switch (key) { |
| 173 |
case TemporalUnitKey::SmallestUnit: |
| 174 |
return cx->names().smallestUnit; |
| 175 |
case TemporalUnitKey::LargestUnit: |
| 176 |
return cx->names().largestUnit; |
| 177 |
case TemporalUnitKey::Unit: |
| 178 |
return cx->names().unit; |
| 179 |
} |
| 180 |
MOZ_CRASH("invalid temporal unit group"); |
| 181 |
} |
| 182 |
|
| 183 |
static const char* ToCString(TemporalUnitKey key) { |
| 184 |
switch (key) { |
| 185 |
case TemporalUnitKey::SmallestUnit: |
| 186 |
return "smallestUnit"; |
| 187 |
case TemporalUnitKey::LargestUnit: |
| 188 |
return "largestUnit"; |
| 189 |
case TemporalUnitKey::Unit: |
| 190 |
return "unit"; |
| 191 |
} |
| 192 |
MOZ_CRASH("invalid temporal unit group"); |
| 193 |
} |
| 194 |
|
| 195 |
static bool ToTemporalUnit(JSContext* cx, JSLinearString* str, |
| 196 |
TemporalUnitKey key, TemporalUnit* unit) { |
| 197 |
struct UnitMap { |
| 198 |
std::string_view name; |
| 199 |
TemporalUnit unit; |
| 200 |
}; |
| 201 |
|
| 202 |
static constexpr UnitMap mapping[] = { |
| 203 |
{"year", TemporalUnit::Year}, |
| 204 |
{"years", TemporalUnit::Year}, |
| 205 |
{"month", TemporalUnit::Month}, |
| 206 |
{"months", TemporalUnit::Month}, |
| 207 |
{"week", TemporalUnit::Week}, |
| 208 |
{"weeks", TemporalUnit::Week}, |
| 209 |
{"day", TemporalUnit::Day}, |
| 210 |
{"days", TemporalUnit::Day}, |
| 211 |
{"hour", TemporalUnit::Hour}, |
| 212 |
{"hours", TemporalUnit::Hour}, |
| 213 |
{"minute", TemporalUnit::Minute}, |
| 214 |
{"minutes", TemporalUnit::Minute}, |
| 215 |
{"second", TemporalUnit::Second}, |
| 216 |
{"seconds", TemporalUnit::Second}, |
| 217 |
{"millisecond", TemporalUnit::Millisecond}, |
| 218 |
{"milliseconds", TemporalUnit::Millisecond}, |
| 219 |
{"microsecond", TemporalUnit::Microsecond}, |
| 220 |
{"microseconds", TemporalUnit::Microsecond}, |
| 221 |
{"nanosecond", TemporalUnit::Nanosecond}, |
| 222 |
{"nanoseconds", TemporalUnit::Nanosecond}, |
| 223 |
}; |
| 224 |
|
| 225 |
// Compute the length of the longest name. |
| 226 |
constexpr size_t maxNameLength = |
| 227 |
std::max_element(std::begin(mapping), std::end(mapping), |
| 228 |
[](const auto& x, const auto& y) { |
| 229 |
return x.name.length() < y.name.length(); |
| 230 |
}) |
| 231 |
->name.length(); |
| 232 |
|
| 233 |
// Twenty StringEqualsLiteral calls for each possible combination seems a bit |
| 234 |
// expensive, so let's instead copy the input name into a char array and rely |
| 235 |
// on the compiler to generate optimized code for the comparisons. |
| 236 |
|
| 237 |
size_t length = str->length(); |
| 238 |
if (length <= maxNameLength && StringIsAscii(str)) { |
| 239 |
char chars[maxNameLength] = {}; |
| 240 |
JS::LossyCopyLinearStringChars(chars, str, length); |
| 241 |
|
| 242 |
for (const auto& m : mapping) { |
| 243 |
if (m.name == std::string_view(chars, length)) { |
| 244 |
*unit = m.unit; |
| 245 |
return true; |
| 246 |
} |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
if (auto chars = QuoteString(cx, str, '"')) { |
| 251 |
JS_ReportErrorNumberUTF8(cx, GetErrorMessage, nullptr, |
| 252 |
JSMSG_INVALID_OPTION_VALUE, ToCString(key), |
| 253 |
chars.get()); |
| 254 |
} |
| 255 |
return false; |
| 256 |
} |
| 257 |
|
| 258 |
static std::pair<TemporalUnit, TemporalUnit> AllowedValues( |
| 259 |
TemporalUnitGroup unitGroup) { |
| 260 |
switch (unitGroup) { |
| 261 |
case TemporalUnitGroup::Date: |
| 262 |
return {TemporalUnit::Year, TemporalUnit::Day}; |
| 263 |
case TemporalUnitGroup::Time: |
| 264 |
return {TemporalUnit::Hour, TemporalUnit::Nanosecond}; |
| 265 |
case TemporalUnitGroup::DateTime: |
| 266 |
return {TemporalUnit::Year, TemporalUnit::Nanosecond}; |
| 267 |
case TemporalUnitGroup::DayTime: |
| 268 |
return {TemporalUnit::Day, TemporalUnit::Nanosecond}; |
| 269 |
} |
| 270 |
MOZ_CRASH("invalid temporal unit group"); |
| 271 |
} |
| 272 |
|
| 273 |
/** |
| 274 |
* GetTemporalUnitValuedOption ( normalizedOptions, key, unitGroup, default [ , |
| 275 |
* extraValues ] ) |
| 276 |
*/ |
| 277 |
bool js::temporal::GetTemporalUnitValuedOption(JSContext* cx, |
| 278 |
Handle<JSObject*> options, |
| 279 |
TemporalUnitKey key, |
| 280 |
TemporalUnitGroup unitGroup, |
| 281 |
TemporalUnit* unit) { |
| 282 |
// Steps 1-8. (Not applicable in our implementation.) |
| 283 |
|
| 284 |
// Step 9. |
| 285 |
Rooted<JSString*> value(cx); |
| 286 |
if (!GetStringOption(cx, options, ToPropertyName(cx, key), &value)) { |
| 287 |
return false; |
| 288 |
} |
| 289 |
|
| 290 |
// Caller should fill in the fallback. |
| 291 |
if (!value) { |
| 292 |
return true; |
| 293 |
} |
| 294 |
|
| 295 |
return GetTemporalUnitValuedOption(cx, value, key, unitGroup, unit); |
| 296 |
} |
| 297 |
|
| 298 |
/** |
| 299 |
* GetTemporalUnitValuedOption ( normalizedOptions, key, unitGroup, default [ , |
| 300 |
* extraValues ] ) |
| 301 |
*/ |
| 302 |
bool js::temporal::GetTemporalUnitValuedOption(JSContext* cx, |
| 303 |
Handle<JSString*> value, |
| 304 |
TemporalUnitKey key, |
| 305 |
TemporalUnitGroup unitGroup, |
| 306 |
TemporalUnit* unit) { |
| 307 |
// Steps 1-9. (Not applicable in our implementation.) |
| 308 |
|
| 309 |
// Step 10. (Handled in caller.) |
| 310 |
|
| 311 |
Rooted<JSLinearString*> linear(cx, value->ensureLinear(cx)); |
| 312 |
if (!linear) { |
| 313 |
return false; |
| 314 |
} |
| 315 |
|
| 316 |
// Caller should fill in the fallback. |
| 317 |
if (key == TemporalUnitKey::LargestUnit) { |
| 318 |
if (StringEqualsLiteral(linear, "auto")) { |
| 319 |
return true; |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
// Step 11. |
| 324 |
if (!ToTemporalUnit(cx, linear, key, unit)) { |
| 325 |
return false; |
| 326 |
} |
| 327 |
|
| 328 |
auto allowedValues = AllowedValues(unitGroup); |
| 329 |
if (*unit < allowedValues.first || *unit > allowedValues.second) { |
| 330 |
if (auto chars = QuoteString(cx, linear, '"')) { |
| 331 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 332 |
JSMSG_INVALID_OPTION_VALUE, ToCString(key), |
| 333 |
chars.get()); |
| 334 |
} |
| 335 |
return false; |
| 336 |
} |
| 337 |
|
| 338 |
return true; |
| 339 |
} |
| 340 |
|
| 341 |
/** |
| 342 |
* GetRoundingModeOption ( normalizedOptions, fallback ) |
| 343 |
*/ |
| 344 |
bool js::temporal::GetRoundingModeOption(JSContext* cx, |
| 345 |
Handle<JSObject*> options, |
| 346 |
TemporalRoundingMode* mode) { |
| 347 |
// Steps 1-2. |
| 348 |
Rooted<JSString*> string(cx); |
| 349 |
if (!GetStringOption(cx, options, cx->names().roundingMode, &string)) { |
| 350 |
return false; |
| 351 |
} |
| 352 |
|
| 353 |
// Caller should fill in the fallback. |
| 354 |
if (!string) { |
| 355 |
return true; |
| 356 |
} |
| 357 |
|
| 358 |
JSLinearString* linear = string->ensureLinear(cx); |
| 359 |
if (!linear) { |
| 360 |
return false; |
| 361 |
} |
| 362 |
|
| 363 |
if (StringEqualsLiteral(linear, "ceil")) { |
| 364 |
*mode = TemporalRoundingMode::Ceil; |
| 365 |
} else if (StringEqualsLiteral(linear, "floor")) { |
| 366 |
*mode = TemporalRoundingMode::Floor; |
| 367 |
} else if (StringEqualsLiteral(linear, "expand")) { |
| 368 |
*mode = TemporalRoundingMode::Expand; |
| 369 |
} else if (StringEqualsLiteral(linear, "trunc")) { |
| 370 |
*mode = TemporalRoundingMode::Trunc; |
| 371 |
} else if (StringEqualsLiteral(linear, "halfCeil")) { |
| 372 |
*mode = TemporalRoundingMode::HalfCeil; |
| 373 |
} else if (StringEqualsLiteral(linear, "halfFloor")) { |
| 374 |
*mode = TemporalRoundingMode::HalfFloor; |
| 375 |
} else if (StringEqualsLiteral(linear, "halfExpand")) { |
| 376 |
*mode = TemporalRoundingMode::HalfExpand; |
| 377 |
} else if (StringEqualsLiteral(linear, "halfTrunc")) { |
| 378 |
*mode = TemporalRoundingMode::HalfTrunc; |
| 379 |
} else if (StringEqualsLiteral(linear, "halfEven")) { |
| 380 |
*mode = TemporalRoundingMode::HalfEven; |
| 381 |
} else { |
| 382 |
if (auto chars = QuoteString(cx, linear, '"')) { |
| 383 |
JS_ReportErrorNumberUTF8(cx, GetErrorMessage, nullptr, |
| 384 |
JSMSG_INVALID_OPTION_VALUE, "roundingMode", |
| 385 |
chars.get()); |
| 386 |
} |
| 387 |
return false; |
| 388 |
} |
| 389 |
return true; |
| 390 |
} |
| 391 |
|
| 392 |
#ifdef DEBUG |
| 393 |
template <typename T> |
| 394 |
static bool IsValidMul(const T& x, const T& y) { |
| 395 |
return (mozilla::CheckedInt<T>(x) * y).isValid(); |
| 396 |
} |
| 397 |
|
| 398 |
// Copied from mozilla::CheckedInt. |
| 399 |
template <> |
| 400 |
bool IsValidMul<Int128>(const Int128& x, const Int128& y) { |
| 401 |
static constexpr auto min = Int128{1} << 127; |
| 402 |
static constexpr auto max = ~min; |
| 403 |
|
| 404 |
if (x == Int128{0} || y == Int128{0}) { |
| 405 |
return true; |
| 406 |
} |
| 407 |
if (x > Int128{0}) { |
| 408 |
return y > Int128{0} ? x <= max / y : y >= min / x; |
| 409 |
} |
| 410 |
return y > Int128{0} ? x >= min / y : y >= max / x; |
| 411 |
} |
| 412 |
#endif |
| 413 |
|
| 414 |
/** |
| 415 |
* RoundNumberToIncrement ( x, increment, roundingMode ) |
| 416 |
*/ |
| 417 |
Int128 js::temporal::RoundNumberToIncrement(const Int128& numerator, |
| 418 |
int64_t denominator, |
| 419 |
Increment increment, |
| 420 |
TemporalRoundingMode roundingMode) { |
| 421 |
MOZ_ASSERT(denominator > 0); |
| 422 |
MOZ_ASSERT(Increment::min() <= increment && increment <= Increment::max()); |
| 423 |
|
| 424 |
auto inc = Int128{increment.value()}; |
| 425 |
MOZ_ASSERT(IsValidMul(Int128{denominator}, inc), "unsupported overflow"); |
| 426 |
|
| 427 |
auto divisor = Int128{denominator} * inc; |
| 428 |
MOZ_ASSERT(divisor > Int128{0}); |
| 429 |
|
| 430 |
// Steps 1-8. |
| 431 |
auto rounded = Divide(numerator, divisor, roundingMode); |
| 432 |
|
| 433 |
// Step 9. |
| 434 |
MOZ_ASSERT(IsValidMul(rounded, inc), "unsupported overflow"); |
| 435 |
return rounded * inc; |
| 436 |
} |
| 437 |
|
| 438 |
/** |
| 439 |
* RoundNumberToIncrement ( x, increment, roundingMode ) |
| 440 |
*/ |
| 441 |
int64_t js::temporal::RoundNumberToIncrement( |
| 442 |
int64_t x, int64_t increment, TemporalRoundingMode roundingMode) { |
| 443 |
MOZ_ASSERT(increment > 0); |
| 444 |
|
| 445 |
// Steps 1-8. |
| 446 |
int64_t rounded = Divide(x, increment, roundingMode); |
| 447 |
|
| 448 |
// Step 9. |
| 449 |
MOZ_ASSERT(IsValidMul(rounded, increment), "unsupported overflow"); |
| 450 |
return rounded * increment; |
| 451 |
} |
| 452 |
|
| 453 |
/** |
| 454 |
* RoundNumberToIncrement ( x, increment, roundingMode ) |
| 455 |
*/ |
| 456 |
Int128 js::temporal::RoundNumberToIncrement(const Int128& x, |
| 457 |
const Int128& increment, |
| 458 |
TemporalRoundingMode roundingMode) { |
| 459 |
MOZ_ASSERT(increment > Int128{0}); |
| 460 |
|
| 461 |
// Steps 1-8. |
| 462 |
auto rounded = Divide(x, increment, roundingMode); |
| 463 |
|
| 464 |
// Step 9. |
| 465 |
MOZ_ASSERT(IsValidMul(rounded, increment), "unsupported overflow"); |
| 466 |
return rounded * increment; |
| 467 |
} |
| 468 |
|
| 469 |
template <typename IntT> |
| 470 |
static inline constexpr bool IsSafeInteger(const IntT& x) { |
| 471 |
constexpr IntT MaxSafeInteger = IntT{int64_t(1) << 53}; |
| 472 |
constexpr IntT MinSafeInteger = -MaxSafeInteger; |
| 473 |
return MinSafeInteger < x && x < MaxSafeInteger; |
| 474 |
} |
| 475 |
|
| 476 |
/** |
| 477 |
* Return the real number value of the fraction |numerator / denominator|. |
| 478 |
* |
| 479 |
* As an optimization we multiply the remainder by 16 when computing the number |
| 480 |
* of digits after the decimal point, i.e. we compute four instead of one bit of |
| 481 |
* the fractional digits. The denominator is therefore required to not exceed |
| 482 |
* 2**(N - log2(16)), where N is the number of non-sign bits in the mantissa. |
| 483 |
*/ |
| 484 |
template <typename T> |
| 485 |
static double FractionToDoubleSlow(const T& numerator, const T& denominator) { |
| 486 |
MOZ_ASSERT(denominator > T{0}, "expected positive denominator"); |
| 487 |
MOZ_ASSERT(denominator <= (T{1} << (std::numeric_limits<T>::digits - 4)), |
| 488 |
"denominator too large"); |
| 489 |
|
| 490 |
auto absValue = [](const T& value) { |
| 491 |
if constexpr (std::is_same_v<T, Int128>) { |
| 492 |
return value.abs(); |
| 493 |
} else { |
| 494 |
// NB: Not std::abs, because std::abs(INT64_MIN) is undefined behavior. |
| 495 |
return mozilla::Abs(value); |
| 496 |
} |
| 497 |
}; |
| 498 |
|
| 499 |
using UnsignedT = decltype(absValue(T{0})); |
| 500 |
static_assert(!std::numeric_limits<UnsignedT>::is_signed); |
| 501 |
|
| 502 |
auto divrem = [](const UnsignedT& x, const UnsignedT& y) { |
| 503 |
if constexpr (std::is_same_v<T, Int128>) { |
| 504 |
return x.divrem(y); |
| 505 |
} else { |
| 506 |
return std::pair{x / y, x % y}; |
| 507 |
} |
| 508 |
}; |
| 509 |
|
| 510 |
auto [quot, rem] = |
| 511 |
divrem(absValue(numerator), static_cast<UnsignedT>(denominator)); |
| 512 |
|
| 513 |
// Simple case when no remainder is present. |
| 514 |
if (rem == UnsignedT{0}) { |
| 515 |
double sign = numerator < T{0} ? -1 : 1; |
| 516 |
return sign * double(quot); |
| 517 |
} |
| 518 |
|
| 519 |
using Double = mozilla::FloatingPoint<double>; |
| 520 |
|
| 521 |
// Significand including the implicit one of IEEE-754 floating point numbers. |
| 522 |
static constexpr uint32_t SignificandWidthWithImplicitOne = |
| 523 |
Double::kSignificandWidth + 1; |
| 524 |
|
| 525 |
// Number of leading zeros for a correctly adjusted significand. |
| 526 |
static constexpr uint32_t SignificandLeadingZeros = |
| 527 |
64 - SignificandWidthWithImplicitOne; |
| 528 |
|
| 529 |
// Exponent bias for an integral significand. (`Double::kExponentBias` is the |
| 530 |
// bias for the binary fraction `1.xyz * 2**exp`. For an integral significand |
| 531 |
// the significand width has to be added to the bias.) |
| 532 |
static constexpr int32_t ExponentBias = |
| 533 |
Double::kExponentBias + Double::kSignificandWidth; |
| 534 |
|
| 535 |
// Significand, possibly unnormalized. |
| 536 |
uint64_t significand = 0; |
| 537 |
|
| 538 |
// Significand ignored msd bits. |
| 539 |
uint32_t ignoredBits = 0; |
| 540 |
|
| 541 |
// Read quotient, from most to least significant digit. Stop when the |
| 542 |
// significand got too large for double precision. |
| 543 |
int32_t shift = std::numeric_limits<UnsignedT>::digits; |
| 544 |
for (; shift != 0 && ignoredBits == 0; shift -= 4) { |
| 545 |
uint64_t digit = uint64_t(quot >> (shift - 4)) & 0xf; |
| 546 |
|
| 547 |
significand = significand * 16 + digit; |
| 548 |
ignoredBits = significand >> SignificandWidthWithImplicitOne; |
| 549 |
} |
| 550 |
|
| 551 |
// Read remainder, from most to least significant digit. Stop when the |
| 552 |
// remainder is zero or the significand got too large. |
| 553 |
int32_t fractionDigit = 0; |
| 554 |
for (; rem != UnsignedT{0} && ignoredBits == 0; fractionDigit++) { |
| 555 |
auto [digit, next] = |
| 556 |
divrem(rem * UnsignedT{16}, static_cast<UnsignedT>(denominator)); |
| 557 |
rem = next; |
| 558 |
|
| 559 |
significand = significand * 16 + uint64_t(digit); |
| 560 |
ignoredBits = significand >> SignificandWidthWithImplicitOne; |
| 561 |
} |
| 562 |
|
| 563 |
// Unbiased exponent. (`shift` remaining bits in the quotient, minus the |
| 564 |
// fractional digits.) |
| 565 |
int32_t exponent = shift - (fractionDigit * 4); |
| 566 |
|
| 567 |
// Significand got too large and some bits are now ignored. Adjust the |
| 568 |
// significand and exponent. |
| 569 |
if (ignoredBits != 0) { |
| 570 |
// significand |
| 571 |
// ___________|__________ |
| 572 |
// / | |
| 573 |
// [xxx················yyy| |
| 574 |
// \_/ \_/ |
| 575 |
// | | |
| 576 |
// ignoredBits extraBits |
| 577 |
// |
| 578 |
// `ignoredBits` have to be shifted back into the 53 bits of the significand |
| 579 |
// and `extraBits` has to be checked if the result has to be rounded up. |
| 580 |
|
| 581 |
// Number of ignored/extra bits in the significand. |
| 582 |
uint32_t extraBitsCount = 32 - mozilla::CountLeadingZeroes32(ignoredBits); |
| 583 |
MOZ_ASSERT(extraBitsCount > 0); |
| 584 |
|
| 585 |
// Extra bits in the significand. |
| 586 |
uint32_t extraBits = uint32_t(significand) & ((1 << extraBitsCount) - 1); |
| 587 |
|
| 588 |
// Move the ignored bits into the proper significand position and adjust the |
| 589 |
// exponent to reflect the now moved out extra bits. |
| 590 |
significand >>= extraBitsCount; |
| 591 |
exponent += extraBitsCount; |
| 592 |
|
| 593 |
MOZ_ASSERT((significand >> SignificandWidthWithImplicitOne) == 0, |
| 594 |
"no excess bits in the significand"); |
| 595 |
|
| 596 |
// When the most significant digit in the extra bits is set, we may need to |
| 597 |
// round the result. |
| 598 |
uint32_t msdExtraBit = extraBits >> (extraBitsCount - 1); |
| 599 |
if (msdExtraBit != 0) { |
| 600 |
// Extra bits, excluding the most significant digit. |
| 601 |
uint32_t extraBitExcludingMsdMask = (1 << (extraBitsCount - 1)) - 1; |
| 602 |
|
| 603 |
// Unprocessed bits in the quotient. |
| 604 |
auto bitsBelowExtraBits = quot & ((UnsignedT{1} << shift) - UnsignedT{1}); |
| 605 |
|
| 606 |
// Round up if the extra bit's msd is set and either the significand is |
| 607 |
// odd or any other bits below the extra bit's msd are non-zero. |
| 608 |
// |
| 609 |
// Bits below the extra bit's msd are: |
| 610 |
// 1. The remaining bits of the extra bits. |
| 611 |
// 2. Any bits below the extra bits. |
| 612 |
// 3. Any rest of the remainder. |
| 613 |
bool shouldRoundUp = (significand & 1) != 0 || |
| 614 |
(extraBits & extraBitExcludingMsdMask) != 0 || |
| 615 |
bitsBelowExtraBits != UnsignedT{0} || |
| 616 |
rem != UnsignedT{0}; |
| 617 |
if (shouldRoundUp) { |
| 618 |
// Add one to the significand bits. |
| 619 |
significand += 1; |
| 620 |
|
| 621 |
// If they overflow, the exponent must also be increased. |
| 622 |
if ((significand >> SignificandWidthWithImplicitOne) != 0) { |
| 623 |
exponent++; |
| 624 |
significand >>= 1; |
| 625 |
} |
| 626 |
} |
| 627 |
} |
| 628 |
} |
| 629 |
|
| 630 |
MOZ_ASSERT(significand > 0, "significand is non-zero"); |
| 631 |
MOZ_ASSERT((significand >> SignificandWidthWithImplicitOne) == 0, |
| 632 |
"no excess bits in the significand"); |
| 633 |
|
| 634 |
// Move the significand into the correct position and adjust the exponent |
| 635 |
// accordingly. |
| 636 |
uint32_t significandZeros = mozilla::CountLeadingZeroes64(significand); |
| 637 |
if (significandZeros < SignificandLeadingZeros) { |
| 638 |
uint32_t shift = SignificandLeadingZeros - significandZeros; |
| 639 |
significand >>= shift; |
| 640 |
exponent += shift; |
| 641 |
} else if (significandZeros > SignificandLeadingZeros) { |
| 642 |
uint32_t shift = significandZeros - SignificandLeadingZeros; |
| 643 |
significand <<= shift; |
| 644 |
exponent -= shift; |
| 645 |
} |
| 646 |
|
| 647 |
// Combine the individual bits of the double value and return it. |
| 648 |
uint64_t signBit = uint64_t(numerator < T{0} ? 1 : 0) |
| 649 |
<< (Double::kExponentWidth + Double::kSignificandWidth); |
| 650 |
uint64_t exponentBits = static_cast<uint64_t>(exponent + ExponentBias) |
| 651 |
<< Double::kExponentShift; |
| 652 |
uint64_t significandBits = significand & Double::kSignificandBits; |
| 653 |
return mozilla::BitwiseCast<double>(signBit | exponentBits | significandBits); |
| 654 |
} |
| 655 |
|
| 656 |
double js::temporal::FractionToDouble(int64_t numerator, int64_t denominator) { |
| 657 |
MOZ_ASSERT(denominator > 0); |
| 658 |
|
| 659 |
// Zero divided by any divisor is still zero. |
| 660 |
if (numerator == 0) { |
| 661 |
return 0; |
| 662 |
} |
| 663 |
|
| 664 |
// When both values can be represented as doubles, use double division to |
| 665 |
// compute the exact result. The result is exact, because double division is |
| 666 |
// guaranteed to return the exact result. |
| 667 |
if (MOZ_LIKELY(::IsSafeInteger(numerator) && ::IsSafeInteger(denominator))) { |
| 668 |
return double(numerator) / double(denominator); |
| 669 |
} |
| 670 |
|
| 671 |
// Otherwise call into |FractionToDoubleSlow| to compute the exact result. |
| 672 |
if (denominator <= |
| 673 |
(int64_t(1) << (std::numeric_limits<int64_t>::digits - 4))) { |
| 674 |
// Slightly faster, but still slow approach when |denominator| is small |
| 675 |
// enough to allow computing on int64 values. |
| 676 |
return FractionToDoubleSlow(numerator, denominator); |
| 677 |
} |
| 678 |
return FractionToDoubleSlow(Int128{numerator}, Int128{denominator}); |
| 679 |
} |
| 680 |
|
| 681 |
double js::temporal::FractionToDouble(const Int128& numerator, |
| 682 |
const Int128& denominator) { |
| 683 |
MOZ_ASSERT(denominator > Int128{0}); |
| 684 |
|
| 685 |
// Zero divided by any divisor is still zero. |
| 686 |
if (numerator == Int128{0}) { |
| 687 |
return 0; |
| 688 |
} |
| 689 |
|
| 690 |
// When both values can be represented as doubles, use double division to |
| 691 |
// compute the exact result. The result is exact, because double division is |
| 692 |
// guaranteed to return the exact result. |
| 693 |
if (MOZ_LIKELY(::IsSafeInteger(numerator) && ::IsSafeInteger(denominator))) { |
| 694 |
return double(numerator) / double(denominator); |
| 695 |
} |
| 696 |
|
| 697 |
// Otherwise call into |FractionToDoubleSlow| to compute the exact result. |
| 698 |
return FractionToDoubleSlow(numerator, denominator); |
| 699 |
} |
| 700 |
|
| 701 |
/** |
| 702 |
* GetTemporalShowCalendarNameOption ( normalizedOptions ) |
| 703 |
*/ |
| 704 |
bool js::temporal::GetTemporalShowCalendarNameOption(JSContext* cx, |
| 705 |
Handle<JSObject*> options, |
| 706 |
ShowCalendar* result) { |
| 707 |
// Step 1. |
| 708 |
Rooted<JSString*> calendarName(cx); |
| 709 |
if (!GetStringOption(cx, options, cx->names().calendarName, &calendarName)) { |
| 710 |
return false; |
| 711 |
} |
| 712 |
|
| 713 |
// Caller should fill in the fallback. |
| 714 |
if (!calendarName) { |
| 715 |
return true; |
| 716 |
} |
| 717 |
|
| 718 |
JSLinearString* linear = calendarName->ensureLinear(cx); |
| 719 |
if (!linear) { |
| 720 |
return false; |
| 721 |
} |
| 722 |
|
| 723 |
if (StringEqualsLiteral(linear, "auto")) { |
| 724 |
*result = ShowCalendar::Auto; |
| 725 |
} else if (StringEqualsLiteral(linear, "always")) { |
| 726 |
*result = ShowCalendar::Always; |
| 727 |
} else if (StringEqualsLiteral(linear, "never")) { |
| 728 |
*result = ShowCalendar::Never; |
| 729 |
} else if (StringEqualsLiteral(linear, "critical")) { |
| 730 |
*result = ShowCalendar::Critical; |
| 731 |
} else { |
| 732 |
if (auto chars = QuoteString(cx, linear, '"')) { |
| 733 |
JS_ReportErrorNumberUTF8(cx, GetErrorMessage, nullptr, |
| 734 |
JSMSG_INVALID_OPTION_VALUE, "calendarName", |
| 735 |
chars.get()); |
| 736 |
} |
| 737 |
return false; |
| 738 |
} |
| 739 |
return true; |
| 740 |
} |
| 741 |
|
| 742 |
/** |
| 743 |
* GetTemporalFractionalSecondDigitsOption ( normalizedOptions ) |
| 744 |
*/ |
| 745 |
bool js::temporal::GetTemporalFractionalSecondDigitsOption( |
| 746 |
JSContext* cx, Handle<JSObject*> options, Precision* precision) { |
| 747 |
// Step 1. |
| 748 |
Rooted<Value> digitsValue(cx); |
| 749 |
if (!GetProperty(cx, options, options, cx->names().fractionalSecondDigits, |
| 750 |
&digitsValue)) { |
| 751 |
return false; |
| 752 |
} |
| 753 |
|
| 754 |
// Step 2. |
| 755 |
if (digitsValue.isUndefined()) { |
| 756 |
*precision = Precision::Auto(); |
| 757 |
return true; |
| 758 |
} |
| 759 |
|
| 760 |
// Step 3. |
| 761 |
if (!digitsValue.isNumber()) { |
| 762 |
// Step 3.a. |
| 763 |
JSString* string = JS::ToString(cx, digitsValue); |
| 764 |
if (!string) { |
| 765 |
return false; |
| 766 |
} |
| 767 |
|
| 768 |
JSLinearString* linear = string->ensureLinear(cx); |
| 769 |
if (!linear) { |
| 770 |
return false; |
| 771 |
} |
| 772 |
|
| 773 |
if (!StringEqualsLiteral(linear, "auto")) { |
| 774 |
if (auto chars = QuoteString(cx, linear, '"')) { |
| 775 |
JS_ReportErrorNumberUTF8(cx, GetErrorMessage, nullptr, |
| 776 |
JSMSG_INVALID_OPTION_VALUE, |
| 777 |
"fractionalSecondDigits", chars.get()); |
| 778 |
} |
| 779 |
return false; |
| 780 |
} |
| 781 |
|
| 782 |
// Step 3.b. |
| 783 |
*precision = Precision::Auto(); |
| 784 |
return true; |
| 785 |
} |
| 786 |
|
| 787 |
// Step 4. |
| 788 |
double digitCount = digitsValue.toNumber(); |
| 789 |
if (!std::isfinite(digitCount)) { |
| 790 |
ToCStringBuf cbuf; |
| 791 |
const char* numStr = NumberToCString(&cbuf, digitCount); |
| 792 |
|
| 793 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 794 |
JSMSG_INVALID_OPTION_VALUE, |
| 795 |
"fractionalSecondDigits", numStr); |
| 796 |
return false; |
| 797 |
} |
| 798 |
|
| 799 |
// Step 5. |
| 800 |
digitCount = std::floor(digitCount); |
| 801 |
|
| 802 |
// Step 6. |
| 803 |
if (digitCount < 0 || digitCount > 9) { |
| 804 |
ToCStringBuf cbuf; |
| 805 |
const char* numStr = NumberToCString(&cbuf, digitCount); |
| 806 |
|
| 807 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 808 |
JSMSG_INVALID_OPTION_VALUE, |
| 809 |
"fractionalSecondDigits", numStr); |
| 810 |
return false; |
| 811 |
} |
| 812 |
|
| 813 |
// Step 7. |
| 814 |
*precision = Precision{uint8_t(digitCount)}; |
| 815 |
return true; |
| 816 |
} |
| 817 |
|
| 818 |
/** |
| 819 |
* ToSecondsStringPrecisionRecord ( smallestUnit, fractionalDigitCount ) |
| 820 |
*/ |
| 821 |
SecondsStringPrecision js::temporal::ToSecondsStringPrecision( |
| 822 |
TemporalUnit smallestUnit, Precision fractionalDigitCount) { |
| 823 |
MOZ_ASSERT(smallestUnit == TemporalUnit::Auto || |
| 824 |
smallestUnit >= TemporalUnit::Minute); |
| 825 |
MOZ_ASSERT(fractionalDigitCount == Precision::Auto() || |
| 826 |
fractionalDigitCount.value() <= 9); |
| 827 |
|
| 828 |
// Steps 1-5. |
| 829 |
switch (smallestUnit) { |
| 830 |
// Step 1. |
| 831 |
case TemporalUnit::Minute: |
| 832 |
return {Precision::Minute(), TemporalUnit::Minute, Increment{1}}; |
| 833 |
|
| 834 |
// Step 2. |
| 835 |
case TemporalUnit::Second: |
| 836 |
return {Precision{0}, TemporalUnit::Second, Increment{1}}; |
| 837 |
|
| 838 |
// Step 3. |
| 839 |
case TemporalUnit::Millisecond: |
| 840 |
return {Precision{3}, TemporalUnit::Millisecond, Increment{1}}; |
| 841 |
|
| 842 |
// Step 4. |
| 843 |
case TemporalUnit::Microsecond: |
| 844 |
return {Precision{6}, TemporalUnit::Microsecond, Increment{1}}; |
| 845 |
|
| 846 |
// Step 5. |
| 847 |
case TemporalUnit::Nanosecond: |
| 848 |
return {Precision{9}, TemporalUnit::Nanosecond, Increment{1}}; |
| 849 |
|
| 850 |
case TemporalUnit::Auto: |
| 851 |
break; |
| 852 |
|
| 853 |
case TemporalUnit::Year: |
| 854 |
case TemporalUnit::Month: |
| 855 |
case TemporalUnit::Week: |
| 856 |
case TemporalUnit::Day: |
| 857 |
case TemporalUnit::Hour: |
| 858 |
MOZ_CRASH("Unexpected temporal unit"); |
| 859 |
} |
| 860 |
|
| 861 |
// Step 6. (Not applicable in our implementation.) |
| 862 |
|
| 863 |
// Step 7. |
| 864 |
if (fractionalDigitCount == Precision::Auto()) { |
| 865 |
return {Precision::Auto(), TemporalUnit::Nanosecond, Increment{1}}; |
| 866 |
} |
| 867 |
|
| 868 |
static constexpr Increment increments[] = { |
| 869 |
Increment{1}, |
| 870 |
Increment{10}, |
| 871 |
Increment{100}, |
| 872 |
}; |
| 873 |
|
| 874 |
uint8_t digitCount = fractionalDigitCount.value(); |
| 875 |
|
| 876 |
// Step 8. |
| 877 |
if (digitCount == 0) { |
| 878 |
return {Precision{0}, TemporalUnit::Second, Increment{1}}; |
| 879 |
} |
| 880 |
|
| 881 |
// Step 9. |
| 882 |
if (digitCount <= 3) { |
| 883 |
return {fractionalDigitCount, TemporalUnit::Millisecond, |
| 884 |
increments[3 - digitCount]}; |
| 885 |
} |
| 886 |
|
| 887 |
// Step 10. |
| 888 |
if (digitCount <= 6) { |
| 889 |
return {fractionalDigitCount, TemporalUnit::Microsecond, |
| 890 |
increments[6 - digitCount]}; |
| 891 |
} |
| 892 |
|
| 893 |
// Step 11. |
| 894 |
MOZ_ASSERT(digitCount <= 9); |
| 895 |
|
| 896 |
// Step 12. |
| 897 |
return {fractionalDigitCount, TemporalUnit::Nanosecond, |
| 898 |
increments[9 - digitCount]}; |
| 899 |
} |
| 900 |
|
| 901 |
/** |
| 902 |
* GetTemporalOverflowOption ( normalizedOptions ) |
| 903 |
*/ |
| 904 |
bool js::temporal::GetTemporalOverflowOption(JSContext* cx, |
| 905 |
Handle<JSObject*> options, |
| 906 |
TemporalOverflow* result) { |
| 907 |
// Step 1. |
| 908 |
Rooted<JSString*> overflow(cx); |
| 909 |
if (!GetStringOption(cx, options, cx->names().overflow, &overflow)) { |
| 910 |
return false; |
| 911 |
} |
| 912 |
|
| 913 |
// Caller should fill in the fallback. |
| 914 |
if (!overflow) { |
| 915 |
return true; |
| 916 |
} |
| 917 |
|
| 918 |
JSLinearString* linear = overflow->ensureLinear(cx); |
| 919 |
if (!linear) { |
| 920 |
return false; |
| 921 |
} |
| 922 |
|
| 923 |
if (StringEqualsLiteral(linear, "constrain")) { |
| 924 |
*result = TemporalOverflow::Constrain; |
| 925 |
} else if (StringEqualsLiteral(linear, "reject")) { |
| 926 |
*result = TemporalOverflow::Reject; |
| 927 |
} else { |
| 928 |
if (auto chars = QuoteString(cx, linear, '"')) { |
| 929 |
JS_ReportErrorNumberUTF8(cx, GetErrorMessage, nullptr, |
| 930 |
JSMSG_INVALID_OPTION_VALUE, "overflow", |
| 931 |
chars.get()); |
| 932 |
} |
| 933 |
return false; |
| 934 |
} |
| 935 |
return true; |
| 936 |
} |
| 937 |
|
| 938 |
/** |
| 939 |
* GetTemporalDisambiguationOption ( options ) |
| 940 |
*/ |
| 941 |
bool js::temporal::GetTemporalDisambiguationOption( |
| 942 |
JSContext* cx, Handle<JSObject*> options, |
| 943 |
TemporalDisambiguation* disambiguation) { |
| 944 |
// Step 1. (Not applicable) |
| 945 |
|
| 946 |
// Step 2. |
| 947 |
Rooted<JSString*> string(cx); |
| 948 |
if (!GetStringOption(cx, options, cx->names().disambiguation, &string)) { |
| 949 |
return false; |
| 950 |
} |
| 951 |
|
| 952 |
// Caller should fill in the fallback. |
| 953 |
if (!string) { |
| 954 |
return true; |
| 955 |
} |
| 956 |
|
| 957 |
JSLinearString* linear = string->ensureLinear(cx); |
| 958 |
if (!linear) { |
| 959 |
return false; |
| 960 |
} |
| 961 |
|
| 962 |
if (StringEqualsLiteral(linear, "compatible")) { |
| 963 |
*disambiguation = TemporalDisambiguation::Compatible; |
| 964 |
} else if (StringEqualsLiteral(linear, "earlier")) { |
| 965 |
*disambiguation = TemporalDisambiguation::Earlier; |
| 966 |
} else if (StringEqualsLiteral(linear, "later")) { |
| 967 |
*disambiguation = TemporalDisambiguation::Later; |
| 968 |
} else if (StringEqualsLiteral(linear, "reject")) { |
| 969 |
*disambiguation = TemporalDisambiguation::Reject; |
| 970 |
} else { |
| 971 |
if (auto chars = QuoteString(cx, linear, '"')) { |
| 972 |
JS_ReportErrorNumberUTF8(cx, GetErrorMessage, nullptr, |
| 973 |
JSMSG_INVALID_OPTION_VALUE, "disambiguation", |
| 974 |
chars.get()); |
| 975 |
} |
| 976 |
return false; |
| 977 |
} |
| 978 |
return true; |
| 979 |
} |
| 980 |
|
| 981 |
/** |
| 982 |
* GetTemporalOffsetOption ( options, fallback ) |
| 983 |
*/ |
| 984 |
bool js::temporal::GetTemporalOffsetOption(JSContext* cx, |
| 985 |
Handle<JSObject*> options, |
| 986 |
TemporalOffset* offset) { |
| 987 |
// Step 1. (Not applicable in our implementation.) |
| 988 |
|
| 989 |
// Step 2. |
| 990 |
Rooted<JSString*> string(cx); |
| 991 |
if (!GetStringOption(cx, options, cx->names().offset, &string)) { |
| 992 |
return false; |
| 993 |
} |
| 994 |
|
| 995 |
// Caller should fill in the fallback. |
| 996 |
if (!string) { |
| 997 |
return true; |
| 998 |
} |
| 999 |
|
| 1000 |
JSLinearString* linear = string->ensureLinear(cx); |
| 1001 |
if (!linear) { |
| 1002 |
return false; |
| 1003 |
} |
| 1004 |
|
| 1005 |
if (StringEqualsLiteral(linear, "prefer")) { |
| 1006 |
*offset = TemporalOffset::Prefer; |
| 1007 |
} else if (StringEqualsLiteral(linear, "use")) { |
| 1008 |
*offset = TemporalOffset::Use; |
| 1009 |
} else if (StringEqualsLiteral(linear, "ignore")) { |
| 1010 |
*offset = TemporalOffset::Ignore; |
| 1011 |
} else if (StringEqualsLiteral(linear, "reject")) { |
| 1012 |
*offset = TemporalOffset::Reject; |
| 1013 |
} else { |
| 1014 |
if (auto chars = QuoteString(cx, linear, '"')) { |
| 1015 |
JS_ReportErrorNumberUTF8(cx, GetErrorMessage, nullptr, |
| 1016 |
JSMSG_INVALID_OPTION_VALUE, "offset", |
| 1017 |
chars.get()); |
| 1018 |
} |
| 1019 |
return false; |
| 1020 |
} |
| 1021 |
return true; |
| 1022 |
} |
| 1023 |
|
| 1024 |
/** |
| 1025 |
* GetTemporalShowTimeZoneNameOption ( normalizedOptions ) |
| 1026 |
*/ |
| 1027 |
bool js::temporal::GetTemporalShowTimeZoneNameOption(JSContext* cx, |
| 1028 |
Handle<JSObject*> options, |
| 1029 |
ShowTimeZoneName* result) { |
| 1030 |
// Step 1. |
| 1031 |
Rooted<JSString*> timeZoneName(cx); |
| 1032 |
if (!GetStringOption(cx, options, cx->names().timeZoneName, &timeZoneName)) { |
| 1033 |
return false; |
| 1034 |
} |
| 1035 |
|
| 1036 |
// Caller should fill in the fallback. |
| 1037 |
if (!timeZoneName) { |
| 1038 |
return true; |
| 1039 |
} |
| 1040 |
|
| 1041 |
JSLinearString* linear = timeZoneName->ensureLinear(cx); |
| 1042 |
if (!linear) { |
| 1043 |
return false; |
| 1044 |
} |
| 1045 |
|
| 1046 |
if (StringEqualsLiteral(linear, "auto")) { |
| 1047 |
*result = ShowTimeZoneName::Auto; |
| 1048 |
} else if (StringEqualsLiteral(linear, "never")) { |
| 1049 |
*result = ShowTimeZoneName::Never; |
| 1050 |
} else if (StringEqualsLiteral(linear, "critical")) { |
| 1051 |
*result = ShowTimeZoneName::Critical; |
| 1052 |
} else { |
| 1053 |
if (auto chars = QuoteString(cx, linear, '"')) { |
| 1054 |
JS_ReportErrorNumberUTF8(cx, GetErrorMessage, nullptr, |
| 1055 |
JSMSG_INVALID_OPTION_VALUE, "timeZoneName", |
| 1056 |
chars.get()); |
| 1057 |
} |
| 1058 |
return false; |
| 1059 |
} |
| 1060 |
return true; |
| 1061 |
} |
| 1062 |
|
| 1063 |
/** |
| 1064 |
* GetTemporalShowOffsetOption ( normalizedOptions ) |
| 1065 |
*/ |
| 1066 |
bool js::temporal::GetTemporalShowOffsetOption(JSContext* cx, |
| 1067 |
Handle<JSObject*> options, |
| 1068 |
ShowOffset* result) { |
| 1069 |
// Step 1. |
| 1070 |
Rooted<JSString*> offset(cx); |
| 1071 |
if (!GetStringOption(cx, options, cx->names().offset, &offset)) { |
| 1072 |
return false; |
| 1073 |
} |
| 1074 |
|
| 1075 |
// Caller should fill in the fallback. |
| 1076 |
if (!offset) { |
| 1077 |
return true; |
| 1078 |
} |
| 1079 |
|
| 1080 |
JSLinearString* linear = offset->ensureLinear(cx); |
| 1081 |
if (!linear) { |
| 1082 |
return false; |
| 1083 |
} |
| 1084 |
|
| 1085 |
if (StringEqualsLiteral(linear, "auto")) { |
| 1086 |
*result = ShowOffset::Auto; |
| 1087 |
} else if (StringEqualsLiteral(linear, "never")) { |
| 1088 |
*result = ShowOffset::Never; |
| 1089 |
} else { |
| 1090 |
if (auto chars = QuoteString(cx, linear, '"')) { |
| 1091 |
JS_ReportErrorNumberUTF8(cx, GetErrorMessage, nullptr, |
| 1092 |
JSMSG_INVALID_OPTION_VALUE, "offset", |
| 1093 |
chars.get()); |
| 1094 |
} |
| 1095 |
return false; |
| 1096 |
} |
| 1097 |
return true; |
| 1098 |
} |
| 1099 |
|
| 1100 |
/** |
| 1101 |
* GetDirectionOption ( options ) |
| 1102 |
*/ |
| 1103 |
bool js::temporal::GetDirectionOption(JSContext* cx, |
| 1104 |
Handle<JSString*> direction, |
| 1105 |
Direction* result) { |
| 1106 |
JSLinearString* linear = direction->ensureLinear(cx); |
| 1107 |
if (!linear) { |
| 1108 |
return false; |
| 1109 |
} |
| 1110 |
|
| 1111 |
if (StringEqualsLiteral(linear, "next")) { |
| 1112 |
*result = Direction::Next; |
| 1113 |
} else if (StringEqualsLiteral(linear, "previous")) { |
| 1114 |
*result = Direction::Previous; |
| 1115 |
} else { |
| 1116 |
if (auto chars = QuoteString(cx, linear, '"')) { |
| 1117 |
JS_ReportErrorNumberUTF8(cx, GetErrorMessage, nullptr, |
| 1118 |
JSMSG_INVALID_OPTION_VALUE, "direction", |
| 1119 |
chars.get()); |
| 1120 |
} |
| 1121 |
return false; |
| 1122 |
} |
| 1123 |
return true; |
| 1124 |
} |
| 1125 |
|
| 1126 |
/** |
| 1127 |
* GetDirectionOption ( options ) |
| 1128 |
*/ |
| 1129 |
bool js::temporal::GetDirectionOption(JSContext* cx, Handle<JSObject*> options, |
| 1130 |
Direction* result) { |
| 1131 |
// Step 1. |
| 1132 |
Rooted<JSString*> direction(cx); |
| 1133 |
if (!GetStringOption(cx, options, cx->names().direction, &direction)) { |
| 1134 |
return false; |
| 1135 |
} |
| 1136 |
|
| 1137 |
if (!direction) { |
| 1138 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 1139 |
JSMSG_TEMPORAL_MISSING_OPTION, "direction"); |
| 1140 |
return false; |
| 1141 |
} |
| 1142 |
|
| 1143 |
return GetDirectionOption(cx, direction, result); |
| 1144 |
} |
| 1145 |
|
| 1146 |
template <typename T, typename... Ts> |
| 1147 |
static JSObject* MaybeUnwrapIf(JSObject* object) { |
| 1148 |
if (auto* unwrapped = object->maybeUnwrapIf<T>()) { |
| 1149 |
return unwrapped; |
| 1150 |
} |
| 1151 |
if constexpr (sizeof...(Ts) > 0) { |
| 1152 |
return MaybeUnwrapIf<Ts...>(object); |
| 1153 |
} |
| 1154 |
return nullptr; |
| 1155 |
} |
| 1156 |
|
| 1157 |
/** |
| 1158 |
* IsPartialTemporalObject ( object ) |
| 1159 |
*/ |
| 1160 |
bool js::temporal::ThrowIfTemporalLikeObject(JSContext* cx, |
| 1161 |
Handle<JSObject*> object) { |
| 1162 |
// Step 1. (Handled in caller) |
| 1163 |
|
| 1164 |
// Step 2. |
| 1165 |
if (auto* unwrapped = |
| 1166 |
MaybeUnwrapIf<PlainDateObject, PlainDateTimeObject, |
| 1167 |
PlainMonthDayObject, PlainTimeObject, |
| 1168 |
PlainYearMonthObject, ZonedDateTimeObject>(object)) { |
| 1169 |
Rooted<Value> value(cx, ObjectValue(*object)); |
| 1170 |
ReportValueError(cx, JSMSG_UNEXPECTED_TYPE, JSDVG_IGNORE_STACK, value, |
| 1171 |
nullptr, unwrapped->getClass()->name); |
| 1172 |
return false; |
| 1173 |
} |
| 1174 |
|
| 1175 |
Rooted<Value> property(cx); |
| 1176 |
|
| 1177 |
// Step 3. |
| 1178 |
if (!GetProperty(cx, object, object, cx->names().calendar, &property)) { |
| 1179 |
return false; |
| 1180 |
} |
| 1181 |
|
| 1182 |
// Step 4. |
| 1183 |
if (!property.isUndefined()) { |
| 1184 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 1185 |
JSMSG_TEMPORAL_UNEXPECTED_PROPERTY, "calendar"); |
| 1186 |
return false; |
| 1187 |
} |
| 1188 |
|
| 1189 |
// Step 5. |
| 1190 |
if (!GetProperty(cx, object, object, cx->names().timeZone, &property)) { |
| 1191 |
return false; |
| 1192 |
} |
| 1193 |
|
| 1194 |
// Step 6. |
| 1195 |
if (!property.isUndefined()) { |
| 1196 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 1197 |
JSMSG_TEMPORAL_UNEXPECTED_PROPERTY, "timeZone"); |
| 1198 |
return false; |
| 1199 |
} |
| 1200 |
|
| 1201 |
// Step 7. |
| 1202 |
return true; |
| 1203 |
} |
| 1204 |
|
| 1205 |
/** |
| 1206 |
* ToPositiveIntegerWithTruncation ( argument ) |
| 1207 |
*/ |
| 1208 |
bool js::temporal::ToPositiveIntegerWithTruncation(JSContext* cx, |
| 1209 |
Handle<Value> value, |
| 1210 |
const char* name, |
| 1211 |
double* result) { |
| 1212 |
// Step 1. |
| 1213 |
double number; |
| 1214 |
if (!ToIntegerWithTruncation(cx, value, name, &number)) { |
| 1215 |
return false; |
| 1216 |
} |
| 1217 |
|
| 1218 |
// Step 2. |
| 1219 |
if (number <= 0) { |
| 1220 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 1221 |
JSMSG_TEMPORAL_INVALID_NUMBER, name); |
| 1222 |
return false; |
| 1223 |
} |
| 1224 |
|
| 1225 |
// Step 3. |
| 1226 |
*result = number; |
| 1227 |
return true; |
| 1228 |
} |
| 1229 |
|
| 1230 |
/** |
| 1231 |
* ToIntegerWithTruncation ( argument ) |
| 1232 |
*/ |
| 1233 |
bool js::temporal::ToIntegerWithTruncation(JSContext* cx, Handle<Value> value, |
| 1234 |
const char* name, double* result) { |
| 1235 |
// Step 1. |
| 1236 |
double number; |
| 1237 |
if (!JS::ToNumber(cx, value, &number)) { |
| 1238 |
return false; |
| 1239 |
} |
| 1240 |
|
| 1241 |
// Step 2. |
| 1242 |
if (!std::isfinite(number)) { |
| 1243 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 1244 |
JSMSG_TEMPORAL_INVALID_INTEGER, name); |
| 1245 |
return false; |
| 1246 |
} |
| 1247 |
|
| 1248 |
// Step 3. |
| 1249 |
*result = std::trunc(number) + (+0.0); // Add zero to convert -0 to +0. |
| 1250 |
return true; |
| 1251 |
} |
| 1252 |
|
| 1253 |
/** |
| 1254 |
* GetDifferenceSettings ( operation, options, unitGroup, disallowedUnits, |
| 1255 |
* fallbackSmallestUnit, smallestLargestDefaultUnit ) |
| 1256 |
*/ |
| 1257 |
bool js::temporal::GetDifferenceSettings( |
| 1258 |
JSContext* cx, TemporalDifference operation, Handle<JSObject*> options, |
| 1259 |
TemporalUnitGroup unitGroup, TemporalUnit smallestAllowedUnit, |
| 1260 |
TemporalUnit fallbackSmallestUnit, TemporalUnit smallestLargestDefaultUnit, |
| 1261 |
DifferenceSettings* result) { |
| 1262 |
// Steps 1-2. |
| 1263 |
auto largestUnit = TemporalUnit::Auto; |
| 1264 |
if (!GetTemporalUnitValuedOption(cx, options, TemporalUnitKey::LargestUnit, |
| 1265 |
unitGroup, &largestUnit)) { |
| 1266 |
return false; |
| 1267 |
} |
| 1268 |
|
| 1269 |
// Step 3. |
| 1270 |
if (largestUnit > smallestAllowedUnit) { |
| 1271 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 1272 |
JSMSG_TEMPORAL_INVALID_UNIT_OPTION, |
| 1273 |
TemporalUnitToString(largestUnit), "largestUnit"); |
| 1274 |
return false; |
| 1275 |
} |
| 1276 |
|
| 1277 |
// Step 4. |
| 1278 |
auto roundingIncrement = Increment{1}; |
| 1279 |
if (!GetRoundingIncrementOption(cx, options, &roundingIncrement)) { |
| 1280 |
return false; |
| 1281 |
} |
| 1282 |
|
| 1283 |
// Step 5. |
| 1284 |
auto roundingMode = TemporalRoundingMode::Trunc; |
| 1285 |
if (!GetRoundingModeOption(cx, options, &roundingMode)) { |
| 1286 |
return false; |
| 1287 |
} |
| 1288 |
|
| 1289 |
// Step 6. |
| 1290 |
if (operation == TemporalDifference::Since) { |
| 1291 |
roundingMode = NegateRoundingMode(roundingMode); |
| 1292 |
} |
| 1293 |
|
| 1294 |
// Step 7. |
| 1295 |
auto smallestUnit = fallbackSmallestUnit; |
| 1296 |
if (!GetTemporalUnitValuedOption(cx, options, TemporalUnitKey::SmallestUnit, |
| 1297 |
unitGroup, &smallestUnit)) { |
| 1298 |
return false; |
| 1299 |
} |
| 1300 |
|
| 1301 |
// Step 8. |
| 1302 |
if (smallestUnit > smallestAllowedUnit) { |
| 1303 |
JS_ReportErrorNumberASCII( |
| 1304 |
cx, GetErrorMessage, nullptr, JSMSG_TEMPORAL_INVALID_UNIT_OPTION, |
| 1305 |
TemporalUnitToString(smallestUnit), "smallestUnit"); |
| 1306 |
return false; |
| 1307 |
} |
| 1308 |
|
| 1309 |
// Step 9. (Inlined call to LargerOfTwoTemporalUnits) |
| 1310 |
auto defaultLargestUnit = std::min(smallestLargestDefaultUnit, smallestUnit); |
| 1311 |
|
| 1312 |
// Step 10. |
| 1313 |
if (largestUnit == TemporalUnit::Auto) { |
| 1314 |
largestUnit = defaultLargestUnit; |
| 1315 |
} |
| 1316 |
|
| 1317 |
// Step 11. |
| 1318 |
if (largestUnit > smallestUnit) { |
| 1319 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 1320 |
JSMSG_TEMPORAL_INVALID_UNIT_RANGE); |
| 1321 |
return false; |
| 1322 |
} |
| 1323 |
|
| 1324 |
// Steps 12-13. |
| 1325 |
if (smallestUnit > TemporalUnit::Day) { |
| 1326 |
// Step 12. |
| 1327 |
auto maximum = MaximumTemporalDurationRoundingIncrement(smallestUnit); |
| 1328 |
|
| 1329 |
// Step 13. |
| 1330 |
if (!ValidateTemporalRoundingIncrement(cx, roundingIncrement, maximum, |
| 1331 |
false)) { |
| 1332 |
return false; |
| 1333 |
} |
| 1334 |
} |
| 1335 |
|
| 1336 |
// Step 14. |
| 1337 |
*result = {smallestUnit, largestUnit, roundingMode, roundingIncrement}; |
| 1338 |
return true; |
| 1339 |
} |
| 1340 |
|
| 1341 |
static JSObject* CreateTemporalObject(JSContext* cx, JSProtoKey key) { |
| 1342 |
Rooted<JSObject*> proto(cx, &cx->global()->getObjectPrototype()); |
| 1343 |
|
| 1344 |
// The |Temporal| object is just a plain object with some "static" data |
| 1345 |
// properties and some constructor properties. |
| 1346 |
return NewTenuredObjectWithGivenProto<TemporalObject>(cx, proto); |
| 1347 |
} |
| 1348 |
|
| 1349 |
/** |
| 1350 |
* Initializes the Temporal Object and its standard built-in properties. |
| 1351 |
*/ |
| 1352 |
static bool TemporalClassFinish(JSContext* cx, Handle<JSObject*> temporal, |
| 1353 |
Handle<JSObject*> proto) { |
| 1354 |
Rooted<PropertyKey> ctorId(cx); |
| 1355 |
Rooted<Value> ctorValue(cx); |
| 1356 |
auto defineProperty = [&](JSProtoKey protoKey, Handle<PropertyName*> name) { |
| 1357 |
JSObject* ctor = GlobalObject::getOrCreateConstructor(cx, protoKey); |
| 1358 |
if (!ctor) { |
| 1359 |
return false; |
| 1360 |
} |
| 1361 |
|
| 1362 |
ctorId = NameToId(name); |
| 1363 |
ctorValue.setObject(*ctor); |
| 1364 |
return DefineDataProperty(cx, temporal, ctorId, ctorValue, 0); |
| 1365 |
}; |
| 1366 |
|
| 1367 |
// Add the constructor properties. |
| 1368 |
for (const auto& protoKey : { |
| 1369 |
JSProto_Duration, |
| 1370 |
JSProto_Instant, |
| 1371 |
JSProto_PlainDate, |
| 1372 |
JSProto_PlainDateTime, |
| 1373 |
JSProto_PlainMonthDay, |
| 1374 |
JSProto_PlainTime, |
| 1375 |
JSProto_PlainYearMonth, |
| 1376 |
JSProto_ZonedDateTime, |
| 1377 |
}) { |
| 1378 |
if (!defineProperty(protoKey, ClassName(protoKey, cx))) { |
| 1379 |
return false; |
| 1380 |
} |
| 1381 |
} |
| 1382 |
|
| 1383 |
// ClassName(JSProto_TemporalNow) returns "TemporalNow", so we need to handle |
| 1384 |
// it separately. |
| 1385 |
if (!defineProperty(JSProto_TemporalNow, cx->names().Now)) { |
| 1386 |
return false; |
| 1387 |
} |
| 1388 |
|
| 1389 |
return true; |
| 1390 |
} |
| 1391 |
|
| 1392 |
const JSClass TemporalObject::class_ = { |
| 1393 |
"Temporal", |
| 1394 |
JSCLASS_HAS_CACHED_PROTO(JSProto_Temporal), |
| 1395 |
JS_NULL_CLASS_OPS, |
| 1396 |
&TemporalObject::classSpec_, |
| 1397 |
}; |
| 1398 |
|
| 1399 |
static const JSPropertySpec Temporal_properties[] = { |
| 1400 |
JS_STRING_SYM_PS(toStringTag, "Temporal", JSPROP_READONLY), |
| 1401 |
JS_PS_END, |
| 1402 |
}; |
| 1403 |
|
| 1404 |
const ClassSpec TemporalObject::classSpec_ = { |
| 1405 |
CreateTemporalObject, nullptr, nullptr, |
| 1406 |
Temporal_properties, nullptr, nullptr, |
| 1407 |
TemporalClassFinish, |
| 1408 |
}; |