| 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/Instant.h" |
| 8 |
|
| 9 |
#include "mozilla/Assertions.h" |
| 10 |
#include "mozilla/Casting.h" |
| 11 |
#include "mozilla/Span.h" |
| 12 |
|
| 13 |
#include <algorithm> |
| 14 |
#include <array> |
| 15 |
#include <cstdlib> |
| 16 |
#include <iterator> |
| 17 |
#include <stddef.h> |
| 18 |
#include <stdint.h> |
| 19 |
#include <utility> |
| 20 |
|
| 21 |
#include "jsnum.h" |
| 22 |
#include "jspubtd.h" |
| 23 |
#include "NamespaceImports.h" |
| 24 |
|
| 25 |
#include "builtin/intl/DateTimeFormat.h" |
| 26 |
#include "builtin/temporal/Calendar.h" |
| 27 |
#include "builtin/temporal/Duration.h" |
| 28 |
#include "builtin/temporal/Int128.h" |
| 29 |
#include "builtin/temporal/Int96.h" |
| 30 |
#include "builtin/temporal/PlainDateTime.h" |
| 31 |
#include "builtin/temporal/Temporal.h" |
| 32 |
#include "builtin/temporal/TemporalParser.h" |
| 33 |
#include "builtin/temporal/TemporalRoundingMode.h" |
| 34 |
#include "builtin/temporal/TemporalTypes.h" |
| 35 |
#include "builtin/temporal/TemporalUnit.h" |
| 36 |
#include "builtin/temporal/TimeZone.h" |
| 37 |
#include "builtin/temporal/ToString.h" |
| 38 |
#include "builtin/temporal/ZonedDateTime.h" |
| 39 |
#include "gc/AllocKind.h" |
| 40 |
#include "gc/Barrier.h" |
| 41 |
#include "js/CallArgs.h" |
| 42 |
#include "js/CallNonGenericMethod.h" |
| 43 |
#include "js/Class.h" |
| 44 |
#include "js/Conversions.h" |
| 45 |
#include "js/ErrorReport.h" |
| 46 |
#include "js/friend/ErrorMessages.h" |
| 47 |
#include "js/PropertyDescriptor.h" |
| 48 |
#include "js/PropertySpec.h" |
| 49 |
#include "js/RootingAPI.h" |
| 50 |
#include "js/TypeDecls.h" |
| 51 |
#include "js/Value.h" |
| 52 |
#include "vm/BigIntType.h" |
| 53 |
#include "vm/BytecodeUtil.h" |
| 54 |
#include "vm/GlobalObject.h" |
| 55 |
#include "vm/JSAtomState.h" |
| 56 |
#include "vm/JSContext.h" |
| 57 |
#include "vm/JSObject.h" |
| 58 |
#include "vm/PlainObject.h" |
| 59 |
|
| 60 |
#include "vm/JSObject-inl.h" |
| 61 |
#include "vm/NativeObject-inl.h" |
| 62 |
#include "vm/ObjectOperations-inl.h" |
| 63 |
|
| 64 |
using namespace js; |
| 65 |
using namespace js::temporal; |
| 66 |
|
| 67 |
static inline bool IsInstant(Handle<Value> v) { |
| 68 |
return v.isObject() && v.toObject().is<InstantObject>(); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Check if the absolute value is less-or-equal to the given limit. |
| 73 |
*/ |
| 74 |
template <const auto& digits> |
| 75 |
static bool AbsoluteValueIsLessOrEqual(const BigInt* bigInt) { |
| 76 |
size_t length = bigInt->digitLength(); |
| 77 |
|
| 78 |
// Fewer digits than the limit, so definitely in range. |
| 79 |
if (length < std::size(digits)) { |
| 80 |
return true; |
| 81 |
} |
| 82 |
|
| 83 |
// More digits than the limit, so definitely out of range. |
| 84 |
if (length > std::size(digits)) { |
| 85 |
return false; |
| 86 |
} |
| 87 |
|
| 88 |
// Compare each digit when the input has the same number of digits. |
| 89 |
size_t index = std::size(digits); |
| 90 |
for (auto digit : digits) { |
| 91 |
auto d = bigInt->digit(--index); |
| 92 |
if (d < digit) { |
| 93 |
return true; |
| 94 |
} |
| 95 |
if (d > digit) { |
| 96 |
return false; |
| 97 |
} |
| 98 |
} |
| 99 |
return true; |
| 100 |
} |
| 101 |
|
| 102 |
static constexpr auto NanosecondsMaxInstant() { |
| 103 |
static_assert(BigInt::DigitBits == 64 || BigInt::DigitBits == 32); |
| 104 |
|
| 105 |
// ±8.64 × 10^21 is the nanoseconds from epoch limit. |
| 106 |
// 8.64 × 10^21 is 86_40000_00000_00000_00000 or 0x1d4_60162f51_6f000000. |
| 107 |
// Return the BigInt digits of that number for fast BigInt comparisons. |
| 108 |
if constexpr (BigInt::DigitBits == 64) { |
| 109 |
return std::array{ |
| 110 |
BigInt::Digit(0x1d4), |
| 111 |
BigInt::Digit(0x6016'2f51'6f00'0000), |
| 112 |
}; |
| 113 |
} else { |
| 114 |
return std::array{ |
| 115 |
BigInt::Digit(0x1d4), |
| 116 |
BigInt::Digit(0x6016'2f51), |
| 117 |
BigInt::Digit(0x6f00'0000), |
| 118 |
}; |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
// Can't be defined in IsValidEpochNanoseconds when compiling with GCC 8. |
| 123 |
static constexpr auto EpochLimitBigIntDigits = NanosecondsMaxInstant(); |
| 124 |
|
| 125 |
/** |
| 126 |
* IsValidEpochNanoseconds ( epochNanoseconds ) |
| 127 |
*/ |
| 128 |
bool js::temporal::IsValidEpochNanoseconds(const BigInt* epochNanoseconds) { |
| 129 |
// Steps 1-2. |
| 130 |
return AbsoluteValueIsLessOrEqual<EpochLimitBigIntDigits>(epochNanoseconds); |
| 131 |
} |
| 132 |
|
| 133 |
static bool IsValidEpochMilliseconds(double epochMilliseconds) { |
| 134 |
MOZ_ASSERT(IsInteger(epochMilliseconds)); |
| 135 |
|
| 136 |
constexpr int64_t MillisecondsMaxInstant = |
| 137 |
EpochNanoseconds::max().toMilliseconds(); |
| 138 |
return std::abs(epochMilliseconds) <= double(MillisecondsMaxInstant); |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* IsValidEpochNanoseconds ( epochNanoseconds ) |
| 143 |
*/ |
| 144 |
bool js::temporal::IsValidEpochNanoseconds( |
| 145 |
const EpochNanoseconds& epochNanoseconds) { |
| 146 |
MOZ_ASSERT(0 <= epochNanoseconds.nanoseconds && |
| 147 |
epochNanoseconds.nanoseconds <= 999'999'999); |
| 148 |
|
| 149 |
// Steps 1-2. |
| 150 |
return EpochNanoseconds::min() <= epochNanoseconds && |
| 151 |
epochNanoseconds <= EpochNanoseconds::max(); |
| 152 |
} |
| 153 |
|
| 154 |
#ifdef DEBUG |
| 155 |
/** |
| 156 |
* Validates a nanoseconds amount is at most as large as the difference |
| 157 |
* between two valid epoch nanoseconds. |
| 158 |
*/ |
| 159 |
bool js::temporal::IsValidEpochDuration(const EpochDuration& duration) { |
| 160 |
MOZ_ASSERT(0 <= duration.nanoseconds && duration.nanoseconds <= 999'999'999); |
| 161 |
|
| 162 |
// Steps 1-2. |
| 163 |
return EpochDuration::min() <= duration && duration <= EpochDuration::max(); |
| 164 |
} |
| 165 |
#endif |
| 166 |
|
| 167 |
/** |
| 168 |
* Return the BigInt as a 96-bit integer. The BigInt digits must not consist of |
| 169 |
* more than 96-bits. |
| 170 |
*/ |
| 171 |
static Int96 ToInt96(const BigInt* ns) { |
| 172 |
static_assert(BigInt::DigitBits == 64 || BigInt::DigitBits == 32); |
| 173 |
|
| 174 |
auto digits = ns->digits(); |
| 175 |
if constexpr (BigInt::DigitBits == 64) { |
| 176 |
BigInt::Digit x = 0, y = 0; |
| 177 |
switch (digits.size()) { |
| 178 |
case 2: |
| 179 |
y = digits[1]; |
| 180 |
[[fallthrough]]; |
| 181 |
case 1: |
| 182 |
x = digits[0]; |
| 183 |
[[fallthrough]]; |
| 184 |
case 0: |
| 185 |
break; |
| 186 |
default: |
| 187 |
MOZ_ASSERT_UNREACHABLE("unexpected digit length"); |
| 188 |
} |
| 189 |
return Int96{ |
| 190 |
Int96::Digits{Int96::Digit(x), Int96::Digit(x >> 32), Int96::Digit(y)}, |
| 191 |
ns->isNegative()}; |
| 192 |
} else { |
| 193 |
BigInt::Digit x = 0, y = 0, z = 0; |
| 194 |
switch (digits.size()) { |
| 195 |
case 3: |
| 196 |
z = digits[2]; |
| 197 |
[[fallthrough]]; |
| 198 |
case 2: |
| 199 |
y = digits[1]; |
| 200 |
[[fallthrough]]; |
| 201 |
case 1: |
| 202 |
x = digits[0]; |
| 203 |
[[fallthrough]]; |
| 204 |
case 0: |
| 205 |
break; |
| 206 |
default: |
| 207 |
MOZ_ASSERT_UNREACHABLE("unexpected digit length"); |
| 208 |
} |
| 209 |
return Int96{ |
| 210 |
Int96::Digits{Int96::Digit(x), Int96::Digit(y), Int96::Digit(z)}, |
| 211 |
ns->isNegative()}; |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
EpochNanoseconds js::temporal::ToEpochNanoseconds( |
| 216 |
const BigInt* epochNanoseconds) { |
| 217 |
MOZ_ASSERT(IsValidEpochNanoseconds(epochNanoseconds)); |
| 218 |
|
| 219 |
auto [seconds, nanos] = |
| 220 |
ToInt96(epochNanoseconds) / ToNanoseconds(TemporalUnit::Second); |
| 221 |
return {{seconds, nanos}}; |
| 222 |
} |
| 223 |
|
| 224 |
static BigInt* CreateBigInt(JSContext* cx, |
| 225 |
const std::array<uint32_t, 3>& digits, |
| 226 |
bool negative) { |
| 227 |
static_assert(BigInt::DigitBits == 64 || BigInt::DigitBits == 32); |
| 228 |
|
| 229 |
if constexpr (BigInt::DigitBits == 64) { |
| 230 |
uint64_t x = (uint64_t(digits[1]) << 32) | digits[0]; |
| 231 |
uint64_t y = digits[2]; |
| 232 |
|
| 233 |
size_t length = y ? 2 : x ? 1 : 0; |
| 234 |
auto* result = BigInt::createUninitialized(cx, length, negative); |
| 235 |
if (!result) { |
| 236 |
return nullptr; |
| 237 |
} |
| 238 |
if (length > 1) { |
| 239 |
result->setDigit(1, y); |
| 240 |
} |
| 241 |
if (length > 0) { |
| 242 |
result->setDigit(0, x); |
| 243 |
} |
| 244 |
return result; |
| 245 |
} else { |
| 246 |
size_t length = digits[2] ? 3 : digits[1] ? 2 : digits[0] ? 1 : 0; |
| 247 |
auto* result = BigInt::createUninitialized(cx, length, negative); |
| 248 |
if (!result) { |
| 249 |
return nullptr; |
| 250 |
} |
| 251 |
while (length--) { |
| 252 |
result->setDigit(length, digits[length]); |
| 253 |
} |
| 254 |
return result; |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
static auto ToBigIntDigits(uint64_t seconds, uint32_t nanoseconds) { |
| 259 |
// Multiplies two uint32_t values and returns the lower 32-bits. The higher |
| 260 |
// 32-bits are stored in |high|. |
| 261 |
auto digitMul = [](uint32_t a, uint32_t b, uint32_t* high) { |
| 262 |
uint64_t result = static_cast<uint64_t>(a) * static_cast<uint64_t>(b); |
| 263 |
*high = result >> 32; |
| 264 |
return static_cast<uint32_t>(result); |
| 265 |
}; |
| 266 |
|
| 267 |
// Adds two uint32_t values and returns the result. Overflow is added to the |
| 268 |
// out-param |carry|. |
| 269 |
auto digitAdd = [](uint32_t a, uint32_t b, uint32_t* carry) { |
| 270 |
uint32_t result = a + b; |
| 271 |
*carry += static_cast<uint32_t>(result < a); |
| 272 |
return result; |
| 273 |
}; |
| 274 |
|
| 275 |
constexpr uint32_t secToNanos = ToNanoseconds(TemporalUnit::Second); |
| 276 |
|
| 277 |
// uint32_t digits stored in the same order as BigInt digits, i.e. the least |
| 278 |
// significant digit is stored at index zero. |
| 279 |
std::array<uint32_t, 2> multiplicand = {uint32_t(seconds), |
| 280 |
uint32_t(seconds >> 32)}; |
| 281 |
std::array<uint32_t, 3> accumulator = {nanoseconds, 0, 0}; |
| 282 |
|
| 283 |
// This code follows the implementation of |BigInt::multiplyAccumulate()|. |
| 284 |
|
| 285 |
uint32_t carry = 0; |
| 286 |
{ |
| 287 |
uint32_t high = 0; |
| 288 |
uint32_t low = digitMul(secToNanos, multiplicand[0], &high); |
| 289 |
|
| 290 |
uint32_t newCarry = 0; |
| 291 |
accumulator[0] = digitAdd(accumulator[0], low, &newCarry); |
| 292 |
accumulator[1] = digitAdd(high, newCarry, &carry); |
| 293 |
} |
| 294 |
{ |
| 295 |
uint32_t high = 0; |
| 296 |
uint32_t low = digitMul(secToNanos, multiplicand[1], &high); |
| 297 |
|
| 298 |
uint32_t newCarry = 0; |
| 299 |
accumulator[1] = digitAdd(accumulator[1], low, &carry); |
| 300 |
accumulator[2] = digitAdd(high, carry, &newCarry); |
| 301 |
MOZ_ASSERT(newCarry == 0); |
| 302 |
} |
| 303 |
|
| 304 |
return accumulator; |
| 305 |
} |
| 306 |
|
| 307 |
BigInt* js::temporal::ToBigInt(JSContext* cx, |
| 308 |
const EpochNanoseconds& epochNanoseconds) { |
| 309 |
MOZ_ASSERT(IsValidEpochNanoseconds(epochNanoseconds)); |
| 310 |
|
| 311 |
auto [seconds, nanoseconds] = epochNanoseconds.abs(); |
| 312 |
auto digits = ToBigIntDigits(uint64_t(seconds), uint32_t(nanoseconds)); |
| 313 |
return CreateBigInt(cx, digits, epochNanoseconds.seconds < 0); |
| 314 |
} |
| 315 |
|
| 316 |
/** |
| 317 |
* GetUTCEpochNanoseconds ( isoDateTime ) |
| 318 |
*/ |
| 319 |
EpochNanoseconds js::temporal::GetUTCEpochNanoseconds( |
| 320 |
const ISODateTime& isoDateTime) { |
| 321 |
MOZ_ASSERT(ISODateTimeWithinLimits(isoDateTime)); |
| 322 |
|
| 323 |
const auto& [date, time] = isoDateTime; |
| 324 |
|
| 325 |
// Steps 1-4. |
| 326 |
int64_t ms = MakeDate(isoDateTime); |
| 327 |
|
| 328 |
// Propagate the input range to the compiler. |
| 329 |
int32_t nanos = |
| 330 |
std::clamp(time.microsecond * 1'000 + time.nanosecond, 0, 999'999); |
| 331 |
|
| 332 |
// Step 5. |
| 333 |
// |
| 334 |
// The returned epoch nanoseconds value can exceed ±8.64 × 10^21, because it |
| 335 |
// includes the local time zone offset. |
| 336 |
return EpochNanoseconds::fromMilliseconds(ms) + EpochDuration{{0, nanos}}; |
| 337 |
} |
| 338 |
|
| 339 |
/** |
| 340 |
* CompareEpochNanoseconds ( epochNanosecondsOne, epochNanosecondsTwo ) |
| 341 |
*/ |
| 342 |
static int32_t CompareEpochNanoseconds( |
| 343 |
const EpochNanoseconds& epochNanosecondsOne, |
| 344 |
const EpochNanoseconds& epochNanosecondsTwo) { |
| 345 |
// Step 1. |
| 346 |
if (epochNanosecondsOne > epochNanosecondsTwo) { |
| 347 |
return 1; |
| 348 |
} |
| 349 |
|
| 350 |
// Step 2. |
| 351 |
if (epochNanosecondsOne < epochNanosecondsTwo) { |
| 352 |
return -1; |
| 353 |
} |
| 354 |
|
| 355 |
// Step 3. |
| 356 |
return 0; |
| 357 |
} |
| 358 |
|
| 359 |
/** |
| 360 |
* CreateTemporalInstant ( epochNanoseconds [ , newTarget ] ) |
| 361 |
*/ |
| 362 |
InstantObject* js::temporal::CreateTemporalInstant( |
| 363 |
JSContext* cx, const EpochNanoseconds& epochNanoseconds) { |
| 364 |
// Step 1. |
| 365 |
MOZ_ASSERT(IsValidEpochNanoseconds(epochNanoseconds)); |
| 366 |
|
| 367 |
// Steps 2-3. |
| 368 |
auto* object = NewBuiltinClassInstance<InstantObject>(cx); |
| 369 |
if (!object) { |
| 370 |
return nullptr; |
| 371 |
} |
| 372 |
|
| 373 |
// Step 4. |
| 374 |
object->setFixedSlot(InstantObject::SECONDS_SLOT, |
| 375 |
NumberValue(epochNanoseconds.seconds)); |
| 376 |
object->setFixedSlot(InstantObject::NANOSECONDS_SLOT, |
| 377 |
Int32Value(epochNanoseconds.nanoseconds)); |
| 378 |
|
| 379 |
// Step 5. |
| 380 |
return object; |
| 381 |
} |
| 382 |
|
| 383 |
/** |
| 384 |
* CreateTemporalInstant ( epochNanoseconds [ , newTarget ] ) |
| 385 |
*/ |
| 386 |
static InstantObject* CreateTemporalInstant(JSContext* cx, const CallArgs& args, |
| 387 |
Handle<BigInt*> epochNanoseconds) { |
| 388 |
// Step 1. |
| 389 |
MOZ_ASSERT(IsValidEpochNanoseconds(epochNanoseconds)); |
| 390 |
|
| 391 |
// Steps 2-3. |
| 392 |
Rooted<JSObject*> proto(cx); |
| 393 |
if (!GetPrototypeFromBuiltinConstructor(cx, args, JSProto_Instant, &proto)) { |
| 394 |
return nullptr; |
| 395 |
} |
| 396 |
|
| 397 |
auto* object = NewObjectWithClassProto<InstantObject>(cx, proto); |
| 398 |
if (!object) { |
| 399 |
return nullptr; |
| 400 |
} |
| 401 |
|
| 402 |
// Step 4. |
| 403 |
auto epochNs = ToEpochNanoseconds(epochNanoseconds); |
| 404 |
object->setFixedSlot(InstantObject::SECONDS_SLOT, |
| 405 |
NumberValue(epochNs.seconds)); |
| 406 |
object->setFixedSlot(InstantObject::NANOSECONDS_SLOT, |
| 407 |
Int32Value(epochNs.nanoseconds)); |
| 408 |
|
| 409 |
// Step 5. |
| 410 |
return object; |
| 411 |
} |
| 412 |
|
| 413 |
/** |
| 414 |
* ToTemporalInstant ( item ) |
| 415 |
*/ |
| 416 |
static bool ToTemporalInstant(JSContext* cx, Handle<Value> item, |
| 417 |
EpochNanoseconds* result) { |
| 418 |
// Step 1. |
| 419 |
Rooted<Value> primitiveValue(cx, item); |
| 420 |
if (item.isObject()) { |
| 421 |
JSObject* itemObj = &item.toObject(); |
| 422 |
|
| 423 |
// Step 1.a. |
| 424 |
if (auto* instant = itemObj->maybeUnwrapIf<InstantObject>()) { |
| 425 |
*result = instant->epochNanoseconds(); |
| 426 |
return true; |
| 427 |
} |
| 428 |
if (auto* zonedDateTime = itemObj->maybeUnwrapIf<ZonedDateTimeObject>()) { |
| 429 |
*result = zonedDateTime->epochNanoseconds(); |
| 430 |
return true; |
| 431 |
} |
| 432 |
|
| 433 |
// Steps 1.b-c. |
| 434 |
if (!ToPrimitive(cx, JSTYPE_STRING, &primitiveValue)) { |
| 435 |
return false; |
| 436 |
} |
| 437 |
} |
| 438 |
|
| 439 |
// Step 2. |
| 440 |
if (!primitiveValue.isString()) { |
| 441 |
// The value is always on the stack, so JSDVG_SEARCH_STACK can be used for |
| 442 |
// better error reporting. |
| 443 |
ReportValueError(cx, JSMSG_UNEXPECTED_TYPE, JSDVG_SEARCH_STACK, |
| 444 |
primitiveValue, nullptr, "not a string"); |
| 445 |
return false; |
| 446 |
} |
| 447 |
Rooted<JSString*> string(cx, primitiveValue.toString()); |
| 448 |
|
| 449 |
// Steps 3-7. |
| 450 |
ISODateTime dateTime; |
| 451 |
int64_t offset; |
| 452 |
if (!ParseTemporalInstantString(cx, string, &dateTime, &offset)) { |
| 453 |
return false; |
| 454 |
} |
| 455 |
MOZ_ASSERT(std::abs(offset) < ToNanoseconds(TemporalUnit::Day)); |
| 456 |
|
| 457 |
// Steps 8-9. |
| 458 |
// |
| 459 |
// Modified to call ISODateTimeWithinLimits instead of BalanceISODateTime. |
| 460 |
if (!ISODateTimeWithinLimits(dateTime)) { |
| 461 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 462 |
JSMSG_TEMPORAL_INSTANT_INVALID); |
| 463 |
return false; |
| 464 |
} |
| 465 |
|
| 466 |
// Step 10. |
| 467 |
auto epochNanoseconds = |
| 468 |
GetUTCEpochNanoseconds(dateTime) - EpochDuration::fromNanoseconds(offset); |
| 469 |
|
| 470 |
// Step 11. |
| 471 |
if (!IsValidEpochNanoseconds(epochNanoseconds)) { |
| 472 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 473 |
JSMSG_TEMPORAL_INSTANT_INVALID); |
| 474 |
return false; |
| 475 |
} |
| 476 |
|
| 477 |
// Step 12. |
| 478 |
*result = epochNanoseconds; |
| 479 |
return true; |
| 480 |
} |
| 481 |
|
| 482 |
/** |
| 483 |
* AddInstant ( epochNanoseconds, hours, minutes, seconds, milliseconds, |
| 484 |
* microseconds, nanoseconds ) |
| 485 |
*/ |
| 486 |
bool js::temporal::AddInstant(JSContext* cx, |
| 487 |
const EpochNanoseconds& epochNanoseconds, |
| 488 |
const TimeDuration& duration, |
| 489 |
EpochNanoseconds* result) { |
| 490 |
MOZ_ASSERT(IsValidEpochNanoseconds(epochNanoseconds)); |
| 491 |
MOZ_ASSERT(IsValidTimeDuration(duration)); |
| 492 |
|
| 493 |
// Step 1. (Inlined AddTimeDurationToEpochNanoseconds) |
| 494 |
auto r = epochNanoseconds + duration.to<EpochDuration>(); |
| 495 |
|
| 496 |
// Step 2. |
| 497 |
if (!IsValidEpochNanoseconds(r)) { |
| 498 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 499 |
JSMSG_TEMPORAL_INSTANT_INVALID); |
| 500 |
return false; |
| 501 |
} |
| 502 |
|
| 503 |
// Step 3. |
| 504 |
*result = r; |
| 505 |
return true; |
| 506 |
} |
| 507 |
|
| 508 |
/** |
| 509 |
* DifferenceInstant ( ns1, ns2, roundingIncrement, smallestUnit, roundingMode ) |
| 510 |
*/ |
| 511 |
TimeDuration js::temporal::DifferenceInstant( |
| 512 |
const EpochNanoseconds& ns1, const EpochNanoseconds& ns2, |
| 513 |
Increment roundingIncrement, TemporalUnit smallestUnit, |
| 514 |
TemporalRoundingMode roundingMode) { |
| 515 |
MOZ_ASSERT(IsValidEpochNanoseconds(ns1)); |
| 516 |
MOZ_ASSERT(IsValidEpochNanoseconds(ns2)); |
| 517 |
MOZ_ASSERT(smallestUnit > TemporalUnit::Day); |
| 518 |
MOZ_ASSERT(roundingIncrement <= |
| 519 |
MaximumTemporalDurationRoundingIncrement(smallestUnit)); |
| 520 |
|
| 521 |
// Step 1. |
| 522 |
auto diff = TimeDurationFromEpochNanosecondsDifference(ns2, ns1); |
| 523 |
MOZ_ASSERT(IsValidEpochDuration(diff.to<EpochDuration>())); |
| 524 |
|
| 525 |
// Steps 2-3. |
| 526 |
return RoundTimeDuration(diff, roundingIncrement, smallestUnit, roundingMode); |
| 527 |
} |
| 528 |
|
| 529 |
/** |
| 530 |
* RoundNumberToIncrementAsIfPositive ( x, increment, roundingMode ) |
| 531 |
*/ |
| 532 |
static EpochNanoseconds RoundNumberToIncrementAsIfPositive( |
| 533 |
const EpochNanoseconds& x, int64_t increment, |
| 534 |
TemporalRoundingMode roundingMode) { |
| 535 |
MOZ_ASSERT(IsValidEpochNanoseconds(x)); |
| 536 |
MOZ_ASSERT(increment > 0); |
| 537 |
MOZ_ASSERT(increment <= ToNanoseconds(TemporalUnit::Day)); |
| 538 |
|
| 539 |
// This operation is equivalent to adjusting the rounding mode through |
| 540 |
// |ToPositiveRoundingMode| and then calling |RoundNumberToIncrement|. |
| 541 |
auto rounded = RoundNumberToIncrement(x.toNanoseconds(), Int128{increment}, |
| 542 |
ToPositiveRoundingMode(roundingMode)); |
| 543 |
return EpochNanoseconds::fromNanoseconds(rounded); |
| 544 |
} |
| 545 |
|
| 546 |
/** |
| 547 |
* RoundTemporalInstant ( ns, increment, unit, roundingMode ) |
| 548 |
*/ |
| 549 |
EpochNanoseconds js::temporal::RoundTemporalInstant( |
| 550 |
const EpochNanoseconds& ns, Increment increment, TemporalUnit unit, |
| 551 |
TemporalRoundingMode roundingMode) { |
| 552 |
MOZ_ASSERT(IsValidEpochNanoseconds(ns)); |
| 553 |
MOZ_ASSERT(increment >= Increment::min()); |
| 554 |
MOZ_ASSERT(uint64_t(increment.value()) <= ToNanoseconds(TemporalUnit::Day)); |
| 555 |
MOZ_ASSERT(unit > TemporalUnit::Day); |
| 556 |
|
| 557 |
// Step 1. |
| 558 |
int64_t unitLength = ToNanoseconds(unit); |
| 559 |
|
| 560 |
// Step 2. |
| 561 |
int64_t incrementNs = increment.value() * unitLength; |
| 562 |
MOZ_ASSERT(incrementNs <= ToNanoseconds(TemporalUnit::Day), |
| 563 |
"incrementNs doesn't overflow epoch nanoseconds resolution"); |
| 564 |
|
| 565 |
// Step 3. |
| 566 |
return RoundNumberToIncrementAsIfPositive(ns, incrementNs, roundingMode); |
| 567 |
} |
| 568 |
|
| 569 |
/** |
| 570 |
* DifferenceTemporalInstant ( operation, instant, other, options ) |
| 571 |
*/ |
| 572 |
static bool DifferenceTemporalInstant(JSContext* cx, |
| 573 |
TemporalDifference operation, |
| 574 |
const CallArgs& args) { |
| 575 |
auto epochNs = args.thisv().toObject().as<InstantObject>().epochNanoseconds(); |
| 576 |
|
| 577 |
// Step 1. |
| 578 |
EpochNanoseconds other; |
| 579 |
if (!ToTemporalInstant(cx, args.get(0), &other)) { |
| 580 |
return false; |
| 581 |
} |
| 582 |
|
| 583 |
// Steps 2-3. |
| 584 |
DifferenceSettings settings; |
| 585 |
if (args.hasDefined(1)) { |
| 586 |
// Step 2. |
| 587 |
Rooted<JSObject*> options( |
| 588 |
cx, RequireObjectArg(cx, "options", ToName(operation), args[1])); |
| 589 |
if (!options) { |
| 590 |
return false; |
| 591 |
} |
| 592 |
|
| 593 |
// Step 3. |
| 594 |
if (!GetDifferenceSettings(cx, operation, options, TemporalUnitGroup::Time, |
| 595 |
TemporalUnit::Nanosecond, TemporalUnit::Second, |
| 596 |
&settings)) { |
| 597 |
return false; |
| 598 |
} |
| 599 |
} else { |
| 600 |
// Steps 2-3. |
| 601 |
settings = { |
| 602 |
TemporalUnit::Nanosecond, |
| 603 |
TemporalUnit::Second, |
| 604 |
TemporalRoundingMode::Trunc, |
| 605 |
Increment{1}, |
| 606 |
}; |
| 607 |
} |
| 608 |
|
| 609 |
// Steps 4. |
| 610 |
auto timeDuration = |
| 611 |
DifferenceInstant(epochNs, other, settings.roundingIncrement, |
| 612 |
settings.smallestUnit, settings.roundingMode); |
| 613 |
|
| 614 |
// Step 5. |
| 615 |
Duration duration; |
| 616 |
if (!TemporalDurationFromInternal(cx, timeDuration, settings.largestUnit, |
| 617 |
&duration)) { |
| 618 |
return false; |
| 619 |
} |
| 620 |
|
| 621 |
// Step 6. |
| 622 |
if (operation == TemporalDifference::Since) { |
| 623 |
duration = duration.negate(); |
| 624 |
} |
| 625 |
|
| 626 |
// Step 7. |
| 627 |
auto* obj = CreateTemporalDuration(cx, duration); |
| 628 |
if (!obj) { |
| 629 |
return false; |
| 630 |
} |
| 631 |
|
| 632 |
args.rval().setObject(*obj); |
| 633 |
return true; |
| 634 |
} |
| 635 |
|
| 636 |
/** |
| 637 |
* AddDurationToInstant ( operation, instant, temporalDurationLike ) |
| 638 |
*/ |
| 639 |
static bool AddDurationToInstant(JSContext* cx, TemporalAddDuration operation, |
| 640 |
const CallArgs& args) { |
| 641 |
auto* instant = &args.thisv().toObject().as<InstantObject>(); |
| 642 |
auto epochNanoseconds = instant->epochNanoseconds(); |
| 643 |
|
| 644 |
// Step 1. |
| 645 |
Duration duration; |
| 646 |
if (!ToTemporalDuration(cx, args.get(0), &duration)) { |
| 647 |
return false; |
| 648 |
} |
| 649 |
|
| 650 |
// Step 2. |
| 651 |
if (operation == TemporalAddDuration::Subtract) { |
| 652 |
duration = duration.negate(); |
| 653 |
} |
| 654 |
|
| 655 |
// Steps 3-4. (Inlined DefaultTemporalLargestUnit and TemporalUnitCategory.) |
| 656 |
if (duration.years != 0 || duration.months != 0 || duration.weeks != 0 || |
| 657 |
duration.days != 0) { |
| 658 |
const char* part = duration.years != 0 ? "years" |
| 659 |
: duration.months != 0 ? "months" |
| 660 |
: duration.weeks != 0 ? "weeks" |
| 661 |
: "days"; |
| 662 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 663 |
JSMSG_TEMPORAL_INSTANT_BAD_DURATION, part); |
| 664 |
return false; |
| 665 |
} |
| 666 |
|
| 667 |
// Step 5. (Inlined ToInternalDurationRecordWith24HourDays.) |
| 668 |
auto timeDuration = TimeDurationFromComponents(duration); |
| 669 |
|
| 670 |
// Step 6. |
| 671 |
EpochNanoseconds ns; |
| 672 |
if (!AddInstant(cx, epochNanoseconds, timeDuration, &ns)) { |
| 673 |
return false; |
| 674 |
} |
| 675 |
|
| 676 |
// Step 7. |
| 677 |
auto* result = CreateTemporalInstant(cx, ns); |
| 678 |
if (!result) { |
| 679 |
return false; |
| 680 |
} |
| 681 |
|
| 682 |
args.rval().setObject(*result); |
| 683 |
return true; |
| 684 |
} |
| 685 |
|
| 686 |
/** |
| 687 |
* Temporal.Instant ( epochNanoseconds ) |
| 688 |
*/ |
| 689 |
static bool InstantConstructor(JSContext* cx, unsigned argc, Value* vp) { |
| 690 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 691 |
|
| 692 |
// Step 1. |
| 693 |
if (!ThrowIfNotConstructing(cx, args, "Temporal.Instant")) { |
| 694 |
return false; |
| 695 |
} |
| 696 |
|
| 697 |
// Step 2. |
| 698 |
Rooted<BigInt*> epochNanoseconds(cx, js::ToBigInt(cx, args.get(0))); |
| 699 |
if (!epochNanoseconds) { |
| 700 |
return false; |
| 701 |
} |
| 702 |
|
| 703 |
// Step 3. |
| 704 |
if (!IsValidEpochNanoseconds(epochNanoseconds)) { |
| 705 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 706 |
JSMSG_TEMPORAL_INSTANT_INVALID); |
| 707 |
return false; |
| 708 |
} |
| 709 |
|
| 710 |
// Step 4. |
| 711 |
auto* result = CreateTemporalInstant(cx, args, epochNanoseconds); |
| 712 |
if (!result) { |
| 713 |
return false; |
| 714 |
} |
| 715 |
|
| 716 |
args.rval().setObject(*result); |
| 717 |
return true; |
| 718 |
} |
| 719 |
|
| 720 |
/** |
| 721 |
* Temporal.Instant.from ( item ) |
| 722 |
*/ |
| 723 |
static bool Instant_from(JSContext* cx, unsigned argc, Value* vp) { |
| 724 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 725 |
|
| 726 |
// Step 1. |
| 727 |
EpochNanoseconds epochNs; |
| 728 |
if (!ToTemporalInstant(cx, args.get(0), &epochNs)) { |
| 729 |
return false; |
| 730 |
} |
| 731 |
|
| 732 |
auto* result = CreateTemporalInstant(cx, epochNs); |
| 733 |
if (!result) { |
| 734 |
return false; |
| 735 |
} |
| 736 |
args.rval().setObject(*result); |
| 737 |
return true; |
| 738 |
} |
| 739 |
|
| 740 |
/** |
| 741 |
* Temporal.Instant.fromEpochMilliseconds ( epochMilliseconds ) |
| 742 |
*/ |
| 743 |
static bool Instant_fromEpochMilliseconds(JSContext* cx, unsigned argc, |
| 744 |
Value* vp) { |
| 745 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 746 |
|
| 747 |
// Step 1. |
| 748 |
double epochMilliseconds; |
| 749 |
if (!JS::ToNumber(cx, args.get(0), &epochMilliseconds)) { |
| 750 |
return false; |
| 751 |
} |
| 752 |
|
| 753 |
// Step 2. |
| 754 |
// |
| 755 |
// NumberToBigInt throws a RangeError for non-integral numbers. |
| 756 |
if (!IsInteger(epochMilliseconds)) { |
| 757 |
ToCStringBuf cbuf; |
| 758 |
const char* str = NumberToCString(&cbuf, epochMilliseconds); |
| 759 |
|
| 760 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 761 |
JSMSG_TEMPORAL_INSTANT_NONINTEGER, str); |
| 762 |
return false; |
| 763 |
} |
| 764 |
|
| 765 |
// Step 3. (Not applicable) |
| 766 |
|
| 767 |
// Step 4. |
| 768 |
if (!IsValidEpochMilliseconds(epochMilliseconds)) { |
| 769 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 770 |
JSMSG_TEMPORAL_INSTANT_INVALID); |
| 771 |
return false; |
| 772 |
} |
| 773 |
|
| 774 |
// Step 5. |
| 775 |
int64_t milliseconds = mozilla::AssertedCast<int64_t>(epochMilliseconds); |
| 776 |
auto* result = CreateTemporalInstant( |
| 777 |
cx, EpochNanoseconds::fromMilliseconds(milliseconds)); |
| 778 |
if (!result) { |
| 779 |
return false; |
| 780 |
} |
| 781 |
args.rval().setObject(*result); |
| 782 |
return true; |
| 783 |
} |
| 784 |
|
| 785 |
/** |
| 786 |
* Temporal.Instant.fromEpochNanoseconds ( epochNanoseconds ) |
| 787 |
*/ |
| 788 |
static bool Instant_fromEpochNanoseconds(JSContext* cx, unsigned argc, |
| 789 |
Value* vp) { |
| 790 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 791 |
|
| 792 |
// Step 1. |
| 793 |
Rooted<BigInt*> epochNanoseconds(cx, js::ToBigInt(cx, args.get(0))); |
| 794 |
if (!epochNanoseconds) { |
| 795 |
return false; |
| 796 |
} |
| 797 |
|
| 798 |
// Step 2. |
| 799 |
if (!IsValidEpochNanoseconds(epochNanoseconds)) { |
| 800 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 801 |
JSMSG_TEMPORAL_INSTANT_INVALID); |
| 802 |
return false; |
| 803 |
} |
| 804 |
|
| 805 |
// Step 3. |
| 806 |
auto* result = |
| 807 |
CreateTemporalInstant(cx, ToEpochNanoseconds(epochNanoseconds)); |
| 808 |
if (!result) { |
| 809 |
return false; |
| 810 |
} |
| 811 |
args.rval().setObject(*result); |
| 812 |
return true; |
| 813 |
} |
| 814 |
|
| 815 |
/** |
| 816 |
* Temporal.Instant.compare ( one, two ) |
| 817 |
*/ |
| 818 |
static bool Instant_compare(JSContext* cx, unsigned argc, Value* vp) { |
| 819 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 820 |
|
| 821 |
// Step 1. |
| 822 |
EpochNanoseconds one; |
| 823 |
if (!ToTemporalInstant(cx, args.get(0), &one)) { |
| 824 |
return false; |
| 825 |
} |
| 826 |
|
| 827 |
// Step 2. |
| 828 |
EpochNanoseconds two; |
| 829 |
if (!ToTemporalInstant(cx, args.get(1), &two)) { |
| 830 |
return false; |
| 831 |
} |
| 832 |
|
| 833 |
// Step 3. |
| 834 |
args.rval().setInt32(CompareEpochNanoseconds(one, two)); |
| 835 |
return true; |
| 836 |
} |
| 837 |
|
| 838 |
/** |
| 839 |
* get Temporal.Instant.prototype.epochMilliseconds |
| 840 |
*/ |
| 841 |
static bool Instant_epochMilliseconds(JSContext* cx, const CallArgs& args) { |
| 842 |
// Step 3. |
| 843 |
auto epochNs = args.thisv().toObject().as<InstantObject>().epochNanoseconds(); |
| 844 |
|
| 845 |
// Step 4-5. |
| 846 |
args.rval().setNumber(epochNs.floorToMilliseconds()); |
| 847 |
return true; |
| 848 |
} |
| 849 |
|
| 850 |
/** |
| 851 |
* get Temporal.Instant.prototype.epochMilliseconds |
| 852 |
*/ |
| 853 |
static bool Instant_epochMilliseconds(JSContext* cx, unsigned argc, Value* vp) { |
| 854 |
// Steps 1-2. |
| 855 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 856 |
return CallNonGenericMethod<IsInstant, Instant_epochMilliseconds>(cx, args); |
| 857 |
} |
| 858 |
|
| 859 |
/** |
| 860 |
* get Temporal.Instant.prototype.epochNanoseconds |
| 861 |
*/ |
| 862 |
static bool Instant_epochNanoseconds(JSContext* cx, const CallArgs& args) { |
| 863 |
// Step 3. |
| 864 |
auto epochNs = args.thisv().toObject().as<InstantObject>().epochNanoseconds(); |
| 865 |
auto* nanoseconds = ToBigInt(cx, epochNs); |
| 866 |
if (!nanoseconds) { |
| 867 |
return false; |
| 868 |
} |
| 869 |
|
| 870 |
args.rval().setBigInt(nanoseconds); |
| 871 |
return true; |
| 872 |
} |
| 873 |
|
| 874 |
/** |
| 875 |
* get Temporal.Instant.prototype.epochNanoseconds |
| 876 |
*/ |
| 877 |
static bool Instant_epochNanoseconds(JSContext* cx, unsigned argc, Value* vp) { |
| 878 |
// Steps 1-2. |
| 879 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 880 |
return CallNonGenericMethod<IsInstant, Instant_epochNanoseconds>(cx, args); |
| 881 |
} |
| 882 |
|
| 883 |
/** |
| 884 |
* Temporal.Instant.prototype.add ( temporalDurationLike ) |
| 885 |
*/ |
| 886 |
static bool Instant_add(JSContext* cx, const CallArgs& args) { |
| 887 |
// Step 3. |
| 888 |
return AddDurationToInstant(cx, TemporalAddDuration::Add, args); |
| 889 |
} |
| 890 |
|
| 891 |
/** |
| 892 |
* Temporal.Instant.prototype.add ( temporalDurationLike ) |
| 893 |
*/ |
| 894 |
static bool Instant_add(JSContext* cx, unsigned argc, Value* vp) { |
| 895 |
// Steps 1-2. |
| 896 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 897 |
return CallNonGenericMethod<IsInstant, Instant_add>(cx, args); |
| 898 |
} |
| 899 |
|
| 900 |
/** |
| 901 |
* Temporal.Instant.prototype.subtract ( temporalDurationLike ) |
| 902 |
*/ |
| 903 |
static bool Instant_subtract(JSContext* cx, const CallArgs& args) { |
| 904 |
// Step 3. |
| 905 |
return AddDurationToInstant(cx, TemporalAddDuration::Subtract, args); |
| 906 |
} |
| 907 |
|
| 908 |
/** |
| 909 |
* Temporal.Instant.prototype.subtract ( temporalDurationLike ) |
| 910 |
*/ |
| 911 |
static bool Instant_subtract(JSContext* cx, unsigned argc, Value* vp) { |
| 912 |
// Steps 1-2. |
| 913 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 914 |
return CallNonGenericMethod<IsInstant, Instant_subtract>(cx, args); |
| 915 |
} |
| 916 |
|
| 917 |
/** |
| 918 |
* Temporal.Instant.prototype.until ( other [ , options ] ) |
| 919 |
*/ |
| 920 |
static bool Instant_until(JSContext* cx, const CallArgs& args) { |
| 921 |
// Step 3. |
| 922 |
return DifferenceTemporalInstant(cx, TemporalDifference::Until, args); |
| 923 |
} |
| 924 |
|
| 925 |
/** |
| 926 |
* Temporal.Instant.prototype.until ( other [ , options ] ) |
| 927 |
*/ |
| 928 |
static bool Instant_until(JSContext* cx, unsigned argc, Value* vp) { |
| 929 |
// Steps 1-2. |
| 930 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 931 |
return CallNonGenericMethod<IsInstant, Instant_until>(cx, args); |
| 932 |
} |
| 933 |
|
| 934 |
/** |
| 935 |
* Temporal.Instant.prototype.since ( other [ , options ] ) |
| 936 |
*/ |
| 937 |
static bool Instant_since(JSContext* cx, const CallArgs& args) { |
| 938 |
// Step 3. |
| 939 |
return DifferenceTemporalInstant(cx, TemporalDifference::Since, args); |
| 940 |
} |
| 941 |
|
| 942 |
/** |
| 943 |
* Temporal.Instant.prototype.since ( other [ , options ] ) |
| 944 |
*/ |
| 945 |
static bool Instant_since(JSContext* cx, unsigned argc, Value* vp) { |
| 946 |
// Steps 1-2. |
| 947 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 948 |
return CallNonGenericMethod<IsInstant, Instant_since>(cx, args); |
| 949 |
} |
| 950 |
|
| 951 |
/** |
| 952 |
* Temporal.Instant.prototype.round ( roundTo ) |
| 953 |
*/ |
| 954 |
static bool Instant_round(JSContext* cx, const CallArgs& args) { |
| 955 |
auto epochNs = args.thisv().toObject().as<InstantObject>().epochNanoseconds(); |
| 956 |
|
| 957 |
// Steps 3-16. |
| 958 |
auto smallestUnit = TemporalUnit::Auto; |
| 959 |
auto roundingMode = TemporalRoundingMode::HalfExpand; |
| 960 |
auto roundingIncrement = Increment{1}; |
| 961 |
if (args.get(0).isString()) { |
| 962 |
// Steps 4 and 6-8. (Not applicable in our implementation.) |
| 963 |
|
| 964 |
// Step 9. |
| 965 |
Rooted<JSString*> paramString(cx, args[0].toString()); |
| 966 |
if (!GetTemporalUnitValuedOption(cx, paramString, |
| 967 |
TemporalUnitKey::SmallestUnit, |
| 968 |
TemporalUnitGroup::Time, &smallestUnit)) { |
| 969 |
return false; |
| 970 |
} |
| 971 |
|
| 972 |
// Steps 10-16. (Not applicable in our implementation.) |
| 973 |
} else { |
| 974 |
// Steps 3 and 5. |
| 975 |
Rooted<JSObject*> options( |
| 976 |
cx, RequireObjectArg(cx, "roundTo", "round", args.get(0))); |
| 977 |
if (!options) { |
| 978 |
return false; |
| 979 |
} |
| 980 |
|
| 981 |
// Steps 6-7. |
| 982 |
if (!GetRoundingIncrementOption(cx, options, &roundingIncrement)) { |
| 983 |
return false; |
| 984 |
} |
| 985 |
|
| 986 |
// Step 8. |
| 987 |
if (!GetRoundingModeOption(cx, options, &roundingMode)) { |
| 988 |
return false; |
| 989 |
} |
| 990 |
|
| 991 |
// Step 9. |
| 992 |
if (!GetTemporalUnitValuedOption(cx, options, TemporalUnitKey::SmallestUnit, |
| 993 |
TemporalUnitGroup::Time, &smallestUnit)) { |
| 994 |
return false; |
| 995 |
} |
| 996 |
if (smallestUnit == TemporalUnit::Auto) { |
| 997 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 998 |
JSMSG_TEMPORAL_MISSING_OPTION, "smallestUnit"); |
| 999 |
return false; |
| 1000 |
} |
| 1001 |
|
| 1002 |
// Steps 10-15. |
| 1003 |
int64_t maximum = UnitsPerDay(smallestUnit); |
| 1004 |
|
| 1005 |
// Step 16. |
| 1006 |
if (!ValidateTemporalRoundingIncrement(cx, roundingIncrement, maximum, |
| 1007 |
true)) { |
| 1008 |
return false; |
| 1009 |
} |
| 1010 |
} |
| 1011 |
|
| 1012 |
// Step 17. |
| 1013 |
auto roundedNs = RoundTemporalInstant(epochNs, roundingIncrement, |
| 1014 |
smallestUnit, roundingMode); |
| 1015 |
|
| 1016 |
// Step 18. |
| 1017 |
auto* result = CreateTemporalInstant(cx, roundedNs); |
| 1018 |
if (!result) { |
| 1019 |
return false; |
| 1020 |
} |
| 1021 |
args.rval().setObject(*result); |
| 1022 |
return true; |
| 1023 |
} |
| 1024 |
|
| 1025 |
/** |
| 1026 |
* Temporal.Instant.prototype.round ( options ) |
| 1027 |
*/ |
| 1028 |
static bool Instant_round(JSContext* cx, unsigned argc, Value* vp) { |
| 1029 |
// Steps 1-2. |
| 1030 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1031 |
return CallNonGenericMethod<IsInstant, Instant_round>(cx, args); |
| 1032 |
} |
| 1033 |
|
| 1034 |
/** |
| 1035 |
* Temporal.Instant.prototype.equals ( other ) |
| 1036 |
*/ |
| 1037 |
static bool Instant_equals(JSContext* cx, const CallArgs& args) { |
| 1038 |
auto epochNs = args.thisv().toObject().as<InstantObject>().epochNanoseconds(); |
| 1039 |
|
| 1040 |
// Step 3. |
| 1041 |
EpochNanoseconds other; |
| 1042 |
if (!ToTemporalInstant(cx, args.get(0), &other)) { |
| 1043 |
return false; |
| 1044 |
} |
| 1045 |
|
| 1046 |
// Steps 4-5. |
| 1047 |
args.rval().setBoolean(epochNs == other); |
| 1048 |
return true; |
| 1049 |
} |
| 1050 |
|
| 1051 |
/** |
| 1052 |
* Temporal.Instant.prototype.equals ( other ) |
| 1053 |
*/ |
| 1054 |
static bool Instant_equals(JSContext* cx, unsigned argc, Value* vp) { |
| 1055 |
// Steps 1-2. |
| 1056 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1057 |
return CallNonGenericMethod<IsInstant, Instant_equals>(cx, args); |
| 1058 |
} |
| 1059 |
|
| 1060 |
/** |
| 1061 |
* Temporal.Instant.prototype.toString ( [ options ] ) |
| 1062 |
*/ |
| 1063 |
static bool Instant_toString(JSContext* cx, const CallArgs& args) { |
| 1064 |
auto epochNs = args.thisv().toObject().as<InstantObject>().epochNanoseconds(); |
| 1065 |
|
| 1066 |
Rooted<TimeZoneValue> timeZone(cx); |
| 1067 |
auto roundingMode = TemporalRoundingMode::Trunc; |
| 1068 |
SecondsStringPrecision precision = {Precision::Auto(), |
| 1069 |
TemporalUnit::Nanosecond, Increment{1}}; |
| 1070 |
if (args.hasDefined(0)) { |
| 1071 |
// Step 3. |
| 1072 |
Rooted<JSObject*> options( |
| 1073 |
cx, RequireObjectArg(cx, "options", "toString", args[0])); |
| 1074 |
if (!options) { |
| 1075 |
return false; |
| 1076 |
} |
| 1077 |
|
| 1078 |
// Steps 4-5. |
| 1079 |
auto digits = Precision::Auto(); |
| 1080 |
if (!GetTemporalFractionalSecondDigitsOption(cx, options, &digits)) { |
| 1081 |
return false; |
| 1082 |
} |
| 1083 |
|
| 1084 |
// Step 6. |
| 1085 |
if (!GetRoundingModeOption(cx, options, &roundingMode)) { |
| 1086 |
return false; |
| 1087 |
} |
| 1088 |
|
| 1089 |
// Step 7. |
| 1090 |
auto smallestUnit = TemporalUnit::Auto; |
| 1091 |
if (!GetTemporalUnitValuedOption(cx, options, TemporalUnitKey::SmallestUnit, |
| 1092 |
TemporalUnitGroup::Time, &smallestUnit)) { |
| 1093 |
return false; |
| 1094 |
} |
| 1095 |
|
| 1096 |
// Step 8. |
| 1097 |
if (smallestUnit == TemporalUnit::Hour) { |
| 1098 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 1099 |
JSMSG_TEMPORAL_INVALID_UNIT_OPTION, "hour", |
| 1100 |
"smallestUnit"); |
| 1101 |
return false; |
| 1102 |
} |
| 1103 |
|
| 1104 |
// Step 9. |
| 1105 |
Rooted<Value> value(cx); |
| 1106 |
if (!GetProperty(cx, options, options, cx->names().timeZone, &value)) { |
| 1107 |
return false; |
| 1108 |
} |
| 1109 |
|
| 1110 |
// Step 10. |
| 1111 |
if (!value.isUndefined()) { |
| 1112 |
if (!ToTemporalTimeZone(cx, value, &timeZone)) { |
| 1113 |
return false; |
| 1114 |
} |
| 1115 |
} |
| 1116 |
|
| 1117 |
// Step 11. |
| 1118 |
precision = ToSecondsStringPrecision(smallestUnit, digits); |
| 1119 |
} |
| 1120 |
|
| 1121 |
// Steps 12-13. |
| 1122 |
auto roundedNs = RoundTemporalInstant(epochNs, precision.increment, |
| 1123 |
precision.unit, roundingMode); |
| 1124 |
|
| 1125 |
// Step 14. |
| 1126 |
JSString* str = |
| 1127 |
TemporalInstantToString(cx, roundedNs, timeZone, precision.precision); |
| 1128 |
if (!str) { |
| 1129 |
return false; |
| 1130 |
} |
| 1131 |
|
| 1132 |
args.rval().setString(str); |
| 1133 |
return true; |
| 1134 |
} |
| 1135 |
|
| 1136 |
/** |
| 1137 |
* Temporal.Instant.prototype.toString ( [ options ] ) |
| 1138 |
*/ |
| 1139 |
static bool Instant_toString(JSContext* cx, unsigned argc, Value* vp) { |
| 1140 |
// Steps 1-2. |
| 1141 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1142 |
return CallNonGenericMethod<IsInstant, Instant_toString>(cx, args); |
| 1143 |
} |
| 1144 |
|
| 1145 |
/** |
| 1146 |
* Temporal.Instant.prototype.toLocaleString ( [ locales [ , options ] ] ) |
| 1147 |
*/ |
| 1148 |
static bool Instant_toLocaleString(JSContext* cx, const CallArgs& args) { |
| 1149 |
// Steps 3-4. |
| 1150 |
Handle<PropertyName*> required = cx->names().any; |
| 1151 |
Handle<PropertyName*> defaults = cx->names().all; |
| 1152 |
return TemporalObjectToLocaleString(cx, args, required, defaults); |
| 1153 |
} |
| 1154 |
|
| 1155 |
/** |
| 1156 |
* Temporal.Instant.prototype.toLocaleString ( [ locales [ , options ] ] ) |
| 1157 |
*/ |
| 1158 |
static bool Instant_toLocaleString(JSContext* cx, unsigned argc, Value* vp) { |
| 1159 |
// Steps 1-2. |
| 1160 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1161 |
return CallNonGenericMethod<IsInstant, Instant_toLocaleString>(cx, args); |
| 1162 |
} |
| 1163 |
|
| 1164 |
/** |
| 1165 |
* Temporal.Instant.prototype.toJSON ( ) |
| 1166 |
*/ |
| 1167 |
static bool Instant_toJSON(JSContext* cx, const CallArgs& args) { |
| 1168 |
auto epochNs = args.thisv().toObject().as<InstantObject>().epochNanoseconds(); |
| 1169 |
|
| 1170 |
// Step 3. |
| 1171 |
Rooted<TimeZoneValue> timeZone(cx); |
| 1172 |
JSString* str = |
| 1173 |
TemporalInstantToString(cx, epochNs, timeZone, Precision::Auto()); |
| 1174 |
if (!str) { |
| 1175 |
return false; |
| 1176 |
} |
| 1177 |
|
| 1178 |
args.rval().setString(str); |
| 1179 |
return true; |
| 1180 |
} |
| 1181 |
|
| 1182 |
/** |
| 1183 |
* Temporal.Instant.prototype.toJSON ( ) |
| 1184 |
*/ |
| 1185 |
static bool Instant_toJSON(JSContext* cx, unsigned argc, Value* vp) { |
| 1186 |
// Steps 1-2. |
| 1187 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1188 |
return CallNonGenericMethod<IsInstant, Instant_toJSON>(cx, args); |
| 1189 |
} |
| 1190 |
|
| 1191 |
/** |
| 1192 |
* Temporal.Instant.prototype.valueOf ( ) |
| 1193 |
*/ |
| 1194 |
static bool Instant_valueOf(JSContext* cx, unsigned argc, Value* vp) { |
| 1195 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, JSMSG_CANT_CONVERT_TO, |
| 1196 |
"Instant", "primitive type"); |
| 1197 |
return false; |
| 1198 |
} |
| 1199 |
|
| 1200 |
/** |
| 1201 |
* Temporal.Instant.prototype.toZonedDateTimeISO ( item ) |
| 1202 |
*/ |
| 1203 |
static bool Instant_toZonedDateTimeISO(JSContext* cx, const CallArgs& args) { |
| 1204 |
auto epochNs = args.thisv().toObject().as<InstantObject>().epochNanoseconds(); |
| 1205 |
|
| 1206 |
// Step 3. |
| 1207 |
Rooted<TimeZoneValue> timeZone(cx); |
| 1208 |
if (!ToTemporalTimeZone(cx, args.get(0), &timeZone)) { |
| 1209 |
return false; |
| 1210 |
} |
| 1211 |
|
| 1212 |
// Step 4. |
| 1213 |
Rooted<CalendarValue> calendar(cx, CalendarValue(CalendarId::ISO8601)); |
| 1214 |
auto* result = CreateTemporalZonedDateTime(cx, epochNs, timeZone, calendar); |
| 1215 |
if (!result) { |
| 1216 |
return false; |
| 1217 |
} |
| 1218 |
|
| 1219 |
args.rval().setObject(*result); |
| 1220 |
return true; |
| 1221 |
} |
| 1222 |
|
| 1223 |
/** |
| 1224 |
* Temporal.Instant.prototype.toZonedDateTimeISO ( item ) |
| 1225 |
*/ |
| 1226 |
static bool Instant_toZonedDateTimeISO(JSContext* cx, unsigned argc, |
| 1227 |
Value* vp) { |
| 1228 |
// Steps 1-2. |
| 1229 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1230 |
return CallNonGenericMethod<IsInstant, Instant_toZonedDateTimeISO>(cx, args); |
| 1231 |
} |
| 1232 |
|
| 1233 |
const JSClass InstantObject::class_ = { |
| 1234 |
"Temporal.Instant", |
| 1235 |
JSCLASS_HAS_RESERVED_SLOTS(InstantObject::SLOT_COUNT) | |
| 1236 |
JSCLASS_HAS_CACHED_PROTO(JSProto_Instant), |
| 1237 |
JS_NULL_CLASS_OPS, |
| 1238 |
&InstantObject::classSpec_, |
| 1239 |
}; |
| 1240 |
|
| 1241 |
const JSClass& InstantObject::protoClass_ = PlainObject::class_; |
| 1242 |
|
| 1243 |
static const JSFunctionSpec Instant_methods[] = { |
| 1244 |
JS_FN("from", Instant_from, 1, 0), |
| 1245 |
JS_FN("fromEpochMilliseconds", Instant_fromEpochMilliseconds, 1, 0), |
| 1246 |
JS_FN("fromEpochNanoseconds", Instant_fromEpochNanoseconds, 1, 0), |
| 1247 |
JS_FN("compare", Instant_compare, 2, 0), |
| 1248 |
JS_FS_END, |
| 1249 |
}; |
| 1250 |
|
| 1251 |
static const JSFunctionSpec Instant_prototype_methods[] = { |
| 1252 |
JS_FN("add", Instant_add, 1, 0), |
| 1253 |
JS_FN("subtract", Instant_subtract, 1, 0), |
| 1254 |
JS_FN("until", Instant_until, 1, 0), |
| 1255 |
JS_FN("since", Instant_since, 1, 0), |
| 1256 |
JS_FN("round", Instant_round, 1, 0), |
| 1257 |
JS_FN("equals", Instant_equals, 1, 0), |
| 1258 |
JS_FN("toString", Instant_toString, 0, 0), |
| 1259 |
JS_FN("toLocaleString", Instant_toLocaleString, 0, 0), |
| 1260 |
JS_FN("toJSON", Instant_toJSON, 0, 0), |
| 1261 |
JS_FN("valueOf", Instant_valueOf, 0, 0), |
| 1262 |
JS_FN("toZonedDateTimeISO", Instant_toZonedDateTimeISO, 1, 0), |
| 1263 |
JS_FS_END, |
| 1264 |
}; |
| 1265 |
|
| 1266 |
static const JSPropertySpec Instant_prototype_properties[] = { |
| 1267 |
JS_PSG("epochMilliseconds", Instant_epochMilliseconds, 0), |
| 1268 |
JS_PSG("epochNanoseconds", Instant_epochNanoseconds, 0), |
| 1269 |
JS_STRING_SYM_PS(toStringTag, "Temporal.Instant", JSPROP_READONLY), |
| 1270 |
JS_PS_END, |
| 1271 |
}; |
| 1272 |
|
| 1273 |
const ClassSpec InstantObject::classSpec_ = { |
| 1274 |
GenericCreateConstructor<InstantConstructor, 1, gc::AllocKind::FUNCTION>, |
| 1275 |
GenericCreatePrototype<InstantObject>, |
| 1276 |
Instant_methods, |
| 1277 |
nullptr, |
| 1278 |
Instant_prototype_methods, |
| 1279 |
Instant_prototype_properties, |
| 1280 |
nullptr, |
| 1281 |
ClassSpec::DontDefineConstructor, |
| 1282 |
}; |