| 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/PlainYearMonth.h" |
| 8 |
|
| 9 |
#include "mozilla/Assertions.h" |
| 10 |
#include "mozilla/EnumSet.h" |
| 11 |
|
| 12 |
#include <utility> |
| 13 |
|
| 14 |
#include "jspubtd.h" |
| 15 |
#include "NamespaceImports.h" |
| 16 |
|
| 17 |
#include "builtin/intl/DateTimeFormat.h" |
| 18 |
#include "builtin/temporal/Calendar.h" |
| 19 |
#include "builtin/temporal/CalendarFields.h" |
| 20 |
#include "builtin/temporal/Duration.h" |
| 21 |
#include "builtin/temporal/Instant.h" |
| 22 |
#include "builtin/temporal/PlainDate.h" |
| 23 |
#include "builtin/temporal/PlainDateTime.h" |
| 24 |
#include "builtin/temporal/PlainMonthDay.h" |
| 25 |
#include "builtin/temporal/Temporal.h" |
| 26 |
#include "builtin/temporal/TemporalParser.h" |
| 27 |
#include "builtin/temporal/TemporalRoundingMode.h" |
| 28 |
#include "builtin/temporal/TemporalTypes.h" |
| 29 |
#include "builtin/temporal/TemporalUnit.h" |
| 30 |
#include "builtin/temporal/TimeZone.h" |
| 31 |
#include "builtin/temporal/ToString.h" |
| 32 |
#include "gc/AllocKind.h" |
| 33 |
#include "gc/Barrier.h" |
| 34 |
#include "gc/GCEnum.h" |
| 35 |
#include "js/CallArgs.h" |
| 36 |
#include "js/CallNonGenericMethod.h" |
| 37 |
#include "js/Class.h" |
| 38 |
#include "js/ErrorReport.h" |
| 39 |
#include "js/friend/ErrorMessages.h" |
| 40 |
#include "js/PropertyDescriptor.h" |
| 41 |
#include "js/PropertySpec.h" |
| 42 |
#include "js/RootingAPI.h" |
| 43 |
#include "js/TypeDecls.h" |
| 44 |
#include "js/Value.h" |
| 45 |
#include "vm/BytecodeUtil.h" |
| 46 |
#include "vm/GlobalObject.h" |
| 47 |
#include "vm/JSAtomState.h" |
| 48 |
#include "vm/JSContext.h" |
| 49 |
#include "vm/JSObject.h" |
| 50 |
#include "vm/PlainObject.h" |
| 51 |
#include "vm/StringType.h" |
| 52 |
|
| 53 |
#include "vm/JSObject-inl.h" |
| 54 |
#include "vm/NativeObject-inl.h" |
| 55 |
|
| 56 |
using namespace js; |
| 57 |
using namespace js::temporal; |
| 58 |
|
| 59 |
static inline bool IsPlainYearMonth(Handle<Value> v) { |
| 60 |
return v.isObject() && v.toObject().is<PlainYearMonthObject>(); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* ISOYearMonthWithinLimits ( isoDate ) |
| 65 |
*/ |
| 66 |
bool js::temporal::ISOYearMonthWithinLimits(const ISODate& isoDate) { |
| 67 |
MOZ_ASSERT(IsValidISODate(isoDate)); |
| 68 |
|
| 69 |
constexpr auto min = ISODate::min(); |
| 70 |
constexpr auto max = ISODate::max(); |
| 71 |
|
| 72 |
const auto& year = isoDate.year; |
| 73 |
|
| 74 |
// Fast-path when the input is definitely in range. |
| 75 |
if (min.year < year && year < max.year) { |
| 76 |
return true; |
| 77 |
} |
| 78 |
|
| 79 |
// Check |isoDate| is within the valid limits. |
| 80 |
if (year < 0) { |
| 81 |
return isoDate >= ISODate{min.year, min.month, 1}; |
| 82 |
} |
| 83 |
return isoDate < ISODate{max.year, max.month + 1, 1}; |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* CreateTemporalYearMonth ( isoDate, calendar [ , newTarget ] ) |
| 88 |
*/ |
| 89 |
static PlainYearMonthObject* CreateTemporalYearMonth( |
| 90 |
JSContext* cx, const CallArgs& args, const ISODate& isoDate, |
| 91 |
Handle<CalendarValue> calendar) { |
| 92 |
// Step 1. |
| 93 |
if (!ISOYearMonthWithinLimits(isoDate)) { |
| 94 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 95 |
JSMSG_TEMPORAL_PLAIN_YEAR_MONTH_INVALID); |
| 96 |
return nullptr; |
| 97 |
} |
| 98 |
|
| 99 |
// Steps 2-3. |
| 100 |
Rooted<JSObject*> proto(cx); |
| 101 |
if (!GetPrototypeFromBuiltinConstructor(cx, args, JSProto_PlainYearMonth, |
| 102 |
&proto)) { |
| 103 |
return nullptr; |
| 104 |
} |
| 105 |
|
| 106 |
auto* object = NewObjectWithClassProto<PlainYearMonthObject>(cx, proto); |
| 107 |
if (!object) { |
| 108 |
return nullptr; |
| 109 |
} |
| 110 |
|
| 111 |
// Step 4. |
| 112 |
auto packedDate = PackedDate::pack(isoDate); |
| 113 |
object->setFixedSlot(PlainYearMonthObject::PACKED_DATE_SLOT, |
| 114 |
PrivateUint32Value(packedDate.value)); |
| 115 |
|
| 116 |
// Step 5. |
| 117 |
object->setFixedSlot(PlainYearMonthObject::CALENDAR_SLOT, |
| 118 |
calendar.toSlotValue()); |
| 119 |
|
| 120 |
// Step 6. |
| 121 |
return object; |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* CreateTemporalYearMonth ( isoDate, calendar [ , newTarget ] ) |
| 126 |
*/ |
| 127 |
PlainYearMonthObject* js::temporal::CreateTemporalYearMonth( |
| 128 |
JSContext* cx, Handle<PlainYearMonth> yearMonth) { |
| 129 |
MOZ_ASSERT(IsValidISODate(yearMonth)); |
| 130 |
|
| 131 |
// Step 1. |
| 132 |
MOZ_ASSERT(ISOYearMonthWithinLimits(yearMonth)); |
| 133 |
|
| 134 |
// Steps 2-3. |
| 135 |
auto* object = NewBuiltinClassInstance<PlainYearMonthObject>(cx); |
| 136 |
if (!object) { |
| 137 |
return nullptr; |
| 138 |
} |
| 139 |
|
| 140 |
// Step 4. |
| 141 |
auto packedDate = PackedDate::pack(yearMonth); |
| 142 |
object->setFixedSlot(PlainYearMonthObject::PACKED_DATE_SLOT, |
| 143 |
PrivateUint32Value(packedDate.value)); |
| 144 |
|
| 145 |
// Step 5. |
| 146 |
object->setFixedSlot(PlainYearMonthObject::CALENDAR_SLOT, |
| 147 |
yearMonth.calendar().toSlotValue()); |
| 148 |
|
| 149 |
// Step 6. |
| 150 |
return object; |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* CreateTemporalYearMonth ( isoDate, calendar [ , newTarget ] ) |
| 155 |
*/ |
| 156 |
bool js::temporal::CreateTemporalYearMonth( |
| 157 |
JSContext* cx, const ISODate& isoDate, Handle<CalendarValue> calendar, |
| 158 |
MutableHandle<PlainYearMonth> result) { |
| 159 |
MOZ_ASSERT(IsValidISODate(isoDate)); |
| 160 |
|
| 161 |
// Step 1. |
| 162 |
if (!ISOYearMonthWithinLimits(isoDate)) { |
| 163 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 164 |
JSMSG_TEMPORAL_PLAIN_YEAR_MONTH_INVALID); |
| 165 |
return false; |
| 166 |
} |
| 167 |
|
| 168 |
// Steps 2-6. |
| 169 |
result.set(PlainYearMonth{isoDate, calendar}); |
| 170 |
return true; |
| 171 |
} |
| 172 |
|
| 173 |
struct YearMonthOptions { |
| 174 |
TemporalOverflow overflow = TemporalOverflow::Constrain; |
| 175 |
}; |
| 176 |
|
| 177 |
/** |
| 178 |
* ToTemporalYearMonth ( item [ , options ] ) |
| 179 |
*/ |
| 180 |
static bool ToTemporalYearMonthOptions(JSContext* cx, Handle<Value> options, |
| 181 |
YearMonthOptions* result) { |
| 182 |
if (options.isUndefined()) { |
| 183 |
*result = {}; |
| 184 |
return true; |
| 185 |
} |
| 186 |
|
| 187 |
// NOTE: |options| are only passed from `Temporal.PlainYearMonth.from`. |
| 188 |
|
| 189 |
Rooted<JSObject*> resolvedOptions( |
| 190 |
cx, RequireObjectArg(cx, "options", "from", options)); |
| 191 |
if (!resolvedOptions) { |
| 192 |
return false; |
| 193 |
} |
| 194 |
|
| 195 |
auto overflow = TemporalOverflow::Constrain; |
| 196 |
if (!GetTemporalOverflowOption(cx, resolvedOptions, &overflow)) { |
| 197 |
return false; |
| 198 |
} |
| 199 |
|
| 200 |
*result = {overflow}; |
| 201 |
return true; |
| 202 |
} |
| 203 |
|
| 204 |
/** |
| 205 |
* ToTemporalYearMonth ( item [ , options ] ) |
| 206 |
*/ |
| 207 |
static bool ToTemporalYearMonth(JSContext* cx, Handle<JSObject*> item, |
| 208 |
Handle<Value> options, |
| 209 |
MutableHandle<PlainYearMonth> result) { |
| 210 |
// Step 1. (Not applicable in our implementation.) |
| 211 |
|
| 212 |
// Step 2.a. |
| 213 |
if (auto* plainYearMonth = item->maybeUnwrapIf<PlainYearMonthObject>()) { |
| 214 |
auto date = plainYearMonth->date(); |
| 215 |
Rooted<CalendarValue> calendar(cx, plainYearMonth->calendar()); |
| 216 |
if (!calendar.wrap(cx)) { |
| 217 |
return false; |
| 218 |
} |
| 219 |
|
| 220 |
// Steps 2.a.i-ii. |
| 221 |
YearMonthOptions ignoredOptions; |
| 222 |
if (!ToTemporalYearMonthOptions(cx, options, &ignoredOptions)) { |
| 223 |
return false; |
| 224 |
} |
| 225 |
|
| 226 |
// Step 2.a.iii. |
| 227 |
result.set(PlainYearMonth{date, calendar}); |
| 228 |
return true; |
| 229 |
} |
| 230 |
|
| 231 |
// Step 2.b. |
| 232 |
Rooted<CalendarValue> calendar(cx); |
| 233 |
if (!GetTemporalCalendarWithISODefault(cx, item, &calendar)) { |
| 234 |
return false; |
| 235 |
} |
| 236 |
|
| 237 |
// Step 2.c. |
| 238 |
Rooted<CalendarFields> fields(cx); |
| 239 |
if (!PrepareCalendarFields(cx, calendar, item, |
| 240 |
{ |
| 241 |
CalendarField::Year, |
| 242 |
CalendarField::Month, |
| 243 |
CalendarField::MonthCode, |
| 244 |
}, |
| 245 |
&fields)) { |
| 246 |
return false; |
| 247 |
} |
| 248 |
|
| 249 |
// Steps 2.d-e. |
| 250 |
YearMonthOptions resolvedOptions; |
| 251 |
if (!ToTemporalYearMonthOptions(cx, options, &resolvedOptions)) { |
| 252 |
return false; |
| 253 |
} |
| 254 |
auto [overflow] = resolvedOptions; |
| 255 |
|
| 256 |
// Step 2.f. |
| 257 |
return CalendarYearMonthFromFields(cx, calendar, fields, overflow, result); |
| 258 |
} |
| 259 |
|
| 260 |
/** |
| 261 |
* ToTemporalYearMonth ( item [ , options ] ) |
| 262 |
*/ |
| 263 |
static bool ToTemporalYearMonth(JSContext* cx, Handle<Value> item, |
| 264 |
Handle<Value> options, |
| 265 |
MutableHandle<PlainYearMonth> result) { |
| 266 |
// Step 1. (Not applicable in our implementation.) |
| 267 |
|
| 268 |
// Step 2. |
| 269 |
if (item.isObject()) { |
| 270 |
Rooted<JSObject*> itemObj(cx, &item.toObject()); |
| 271 |
return ToTemporalYearMonth(cx, itemObj, options, result); |
| 272 |
} |
| 273 |
|
| 274 |
// Step 3. |
| 275 |
if (!item.isString()) { |
| 276 |
ReportValueError(cx, JSMSG_UNEXPECTED_TYPE, JSDVG_IGNORE_STACK, item, |
| 277 |
nullptr, "not a string"); |
| 278 |
return false; |
| 279 |
} |
| 280 |
Rooted<JSString*> string(cx, item.toString()); |
| 281 |
|
| 282 |
// Step 4. |
| 283 |
ISODate date; |
| 284 |
Rooted<JSString*> calendarString(cx); |
| 285 |
if (!ParseTemporalYearMonthString(cx, string, &date, &calendarString)) { |
| 286 |
return false; |
| 287 |
} |
| 288 |
|
| 289 |
// Steps 5-7. |
| 290 |
Rooted<CalendarValue> calendar(cx, CalendarValue(CalendarId::ISO8601)); |
| 291 |
if (calendarString) { |
| 292 |
if (!CanonicalizeCalendar(cx, calendarString, &calendar)) { |
| 293 |
return false; |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
// Steps 8-9. |
| 298 |
YearMonthOptions ignoredOptions; |
| 299 |
if (!ToTemporalYearMonthOptions(cx, options, &ignoredOptions)) { |
| 300 |
return false; |
| 301 |
} |
| 302 |
|
| 303 |
// Step 10-11. |
| 304 |
Rooted<PlainYearMonth> yearMonth(cx); |
| 305 |
if (!CreateTemporalYearMonth(cx, date, calendar, &yearMonth)) { |
| 306 |
return false; |
| 307 |
} |
| 308 |
|
| 309 |
// Step 12. |
| 310 |
Rooted<CalendarFields> fields(cx); |
| 311 |
if (!ISODateToFields(cx, yearMonth, &fields)) { |
| 312 |
return false; |
| 313 |
} |
| 314 |
|
| 315 |
// Steps 13-14. |
| 316 |
return CalendarYearMonthFromFields(cx, calendar, fields, |
| 317 |
TemporalOverflow::Constrain, result); |
| 318 |
} |
| 319 |
|
| 320 |
/** |
| 321 |
* ToTemporalYearMonth ( item [ , options ] ) |
| 322 |
*/ |
| 323 |
static bool ToTemporalYearMonth(JSContext* cx, Handle<Value> item, |
| 324 |
MutableHandle<PlainYearMonth> result) { |
| 325 |
return ToTemporalYearMonth(cx, item, UndefinedHandleValue, result); |
| 326 |
} |
| 327 |
|
| 328 |
/** |
| 329 |
* DifferenceTemporalPlainYearMonth ( operation, yearMonth, other, options ) |
| 330 |
*/ |
| 331 |
static bool DifferenceTemporalPlainYearMonth(JSContext* cx, |
| 332 |
TemporalDifference operation, |
| 333 |
const CallArgs& args) { |
| 334 |
Rooted<PlainYearMonth> yearMonth( |
| 335 |
cx, &args.thisv().toObject().as<PlainYearMonthObject>()); |
| 336 |
|
| 337 |
// Step 1. |
| 338 |
Rooted<PlainYearMonth> other(cx); |
| 339 |
if (!ToTemporalYearMonth(cx, args.get(0), &other)) { |
| 340 |
return false; |
| 341 |
} |
| 342 |
|
| 343 |
// Step 2. |
| 344 |
auto calendar = yearMonth.calendar(); |
| 345 |
|
| 346 |
// Step 3. |
| 347 |
if (!CalendarEquals(calendar, other.calendar())) { |
| 348 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 349 |
JSMSG_TEMPORAL_CALENDAR_INCOMPATIBLE, |
| 350 |
CalendarIdentifier(calendar).data(), |
| 351 |
CalendarIdentifier(other.calendar()).data()); |
| 352 |
return false; |
| 353 |
} |
| 354 |
|
| 355 |
// Steps 4-5. |
| 356 |
DifferenceSettings settings; |
| 357 |
Rooted<PlainObject*> resolvedOptions(cx); |
| 358 |
if (args.hasDefined(1)) { |
| 359 |
// Step 4. |
| 360 |
Rooted<JSObject*> options( |
| 361 |
cx, RequireObjectArg(cx, "options", ToName(operation), args[1])); |
| 362 |
if (!options) { |
| 363 |
return false; |
| 364 |
} |
| 365 |
|
| 366 |
// Step 5. |
| 367 |
if (!GetDifferenceSettings(cx, operation, options, TemporalUnitGroup::Date, |
| 368 |
TemporalUnit::Month, TemporalUnit::Month, |
| 369 |
TemporalUnit::Year, &settings)) { |
| 370 |
return false; |
| 371 |
} |
| 372 |
} else { |
| 373 |
// Steps 4-5. |
| 374 |
settings = { |
| 375 |
TemporalUnit::Month, |
| 376 |
TemporalUnit::Year, |
| 377 |
TemporalRoundingMode::Trunc, |
| 378 |
Increment{1}, |
| 379 |
}; |
| 380 |
} |
| 381 |
|
| 382 |
// Step 6. |
| 383 |
if (yearMonth.date() == other.date()) { |
| 384 |
auto* obj = CreateTemporalDuration(cx, {}); |
| 385 |
if (!obj) { |
| 386 |
return false; |
| 387 |
} |
| 388 |
|
| 389 |
args.rval().setObject(*obj); |
| 390 |
return true; |
| 391 |
} |
| 392 |
|
| 393 |
// Step 7. |
| 394 |
Rooted<CalendarFields> thisFields(cx); |
| 395 |
if (!ISODateToFields(cx, yearMonth, &thisFields)) { |
| 396 |
return false; |
| 397 |
} |
| 398 |
|
| 399 |
// Step 8. |
| 400 |
MOZ_ASSERT(!thisFields.has(CalendarField::Day)); |
| 401 |
thisFields.setDay(1); |
| 402 |
|
| 403 |
// Step 9. |
| 404 |
Rooted<PlainDate> thisDate(cx); |
| 405 |
if (!CalendarDateFromFields(cx, calendar, thisFields, |
| 406 |
TemporalOverflow::Constrain, &thisDate)) { |
| 407 |
return false; |
| 408 |
} |
| 409 |
|
| 410 |
// Step 10. |
| 411 |
Rooted<CalendarFields> otherFields(cx); |
| 412 |
if (!ISODateToFields(cx, other, &otherFields)) { |
| 413 |
return false; |
| 414 |
} |
| 415 |
|
| 416 |
// Step 11. |
| 417 |
MOZ_ASSERT(!otherFields.has(CalendarField::Day)); |
| 418 |
otherFields.setDay(1); |
| 419 |
|
| 420 |
// Step 12. |
| 421 |
Rooted<PlainDate> otherDate(cx); |
| 422 |
if (!CalendarDateFromFields(cx, calendar, otherFields, |
| 423 |
TemporalOverflow::Constrain, &otherDate)) { |
| 424 |
return false; |
| 425 |
} |
| 426 |
|
| 427 |
// Step 13. |
| 428 |
DateDuration until; |
| 429 |
if (!CalendarDateUntil(cx, calendar, thisDate, otherDate, |
| 430 |
settings.largestUnit, &until)) { |
| 431 |
return false; |
| 432 |
} |
| 433 |
|
| 434 |
// Step 14. (Inlined AdjustDateDurationRecord) |
| 435 |
// |
| 436 |
// We only care about years and months here, all other fields are set to zero. |
| 437 |
auto dateDuration = DateDuration{until.years, until.months}; |
| 438 |
|
| 439 |
// Step 15. |
| 440 |
auto duration = InternalDuration{dateDuration, {}}; |
| 441 |
|
| 442 |
// Step 16. |
| 443 |
if (settings.smallestUnit != TemporalUnit::Month || |
| 444 |
settings.roundingIncrement != Increment{1}) { |
| 445 |
// Step 16.a. |
| 446 |
auto destEpochNs = GetUTCEpochNanoseconds(ISODateTime{otherDate, {}}); |
| 447 |
|
| 448 |
// Steps 16.b-c. |
| 449 |
auto dateTime = ISODateTime{thisDate, {}}; |
| 450 |
|
| 451 |
// Step 16.d. |
| 452 |
Rooted<TimeZoneValue> timeZone(cx, TimeZoneValue{}); |
| 453 |
if (!RoundRelativeDuration( |
| 454 |
cx, duration, destEpochNs, dateTime, timeZone, calendar, |
| 455 |
settings.largestUnit, settings.roundingIncrement, |
| 456 |
settings.smallestUnit, settings.roundingMode, &duration)) { |
| 457 |
return false; |
| 458 |
} |
| 459 |
} |
| 460 |
MOZ_ASSERT(IsValidDuration(duration)); |
| 461 |
MOZ_ASSERT(duration.date.weeks == 0); |
| 462 |
MOZ_ASSERT(duration.date.days == 0); |
| 463 |
MOZ_ASSERT(duration.time == TimeDuration{}); |
| 464 |
|
| 465 |
// Step 17. (Inlined TemporalDurationFromInternal) |
| 466 |
auto result = duration.date.toDuration(); |
| 467 |
|
| 468 |
// Step 18. |
| 469 |
if (operation == TemporalDifference::Since) { |
| 470 |
result = result.negate(); |
| 471 |
} |
| 472 |
|
| 473 |
// Step 19. |
| 474 |
auto* obj = CreateTemporalDuration(cx, result); |
| 475 |
if (!obj) { |
| 476 |
return false; |
| 477 |
} |
| 478 |
|
| 479 |
args.rval().setObject(*obj); |
| 480 |
return true; |
| 481 |
} |
| 482 |
|
| 483 |
/** |
| 484 |
* AddDurationToYearMonth ( operation, yearMonth, temporalDurationLike, options |
| 485 |
* ) |
| 486 |
*/ |
| 487 |
static bool AddDurationToYearMonth(JSContext* cx, TemporalAddDuration operation, |
| 488 |
const CallArgs& args) { |
| 489 |
Rooted<PlainYearMonth> yearMonth( |
| 490 |
cx, &args.thisv().toObject().as<PlainYearMonthObject>()); |
| 491 |
|
| 492 |
// Step 1. |
| 493 |
Duration duration; |
| 494 |
if (!ToTemporalDuration(cx, args.get(0), &duration)) { |
| 495 |
return false; |
| 496 |
} |
| 497 |
|
| 498 |
// Step 2. |
| 499 |
if (operation == TemporalAddDuration::Subtract) { |
| 500 |
duration = duration.negate(); |
| 501 |
} |
| 502 |
|
| 503 |
// Steps 3-4. |
| 504 |
auto overflow = TemporalOverflow::Constrain; |
| 505 |
if (args.hasDefined(1)) { |
| 506 |
// Step 3. |
| 507 |
Rooted<JSObject*> options( |
| 508 |
cx, RequireObjectArg(cx, "options", ToName(operation), args[1])); |
| 509 |
if (!options) { |
| 510 |
return false; |
| 511 |
} |
| 512 |
|
| 513 |
// Step 4. |
| 514 |
if (!GetTemporalOverflowOption(cx, options, &overflow)) { |
| 515 |
return false; |
| 516 |
} |
| 517 |
} |
| 518 |
|
| 519 |
// Step 5. |
| 520 |
int32_t sign = DurationSign(duration); |
| 521 |
|
| 522 |
// Step 6. |
| 523 |
auto calendar = yearMonth.calendar(); |
| 524 |
|
| 525 |
// Step 7. |
| 526 |
Rooted<CalendarFields> fields(cx); |
| 527 |
if (!ISODateToFields(cx, yearMonth, &fields)) { |
| 528 |
return false; |
| 529 |
} |
| 530 |
|
| 531 |
// Step 8. |
| 532 |
MOZ_ASSERT(!fields.has(CalendarField::Day)); |
| 533 |
fields.setDay(1); |
| 534 |
|
| 535 |
// Step 9. |
| 536 |
Rooted<PlainDate> intermediateDate(cx); |
| 537 |
if (!CalendarDateFromFields(cx, calendar, fields, TemporalOverflow::Constrain, |
| 538 |
&intermediateDate)) { |
| 539 |
return false; |
| 540 |
} |
| 541 |
|
| 542 |
// Steps 10-11. |
| 543 |
ISODate date; |
| 544 |
if (sign < 0) { |
| 545 |
// |intermediateDate| is initialized to the first day of |yearMonth|'s |
| 546 |
// month. Compute the last day of |yearMonth|'s month by first adding one |
| 547 |
// month and then subtracting one day. |
| 548 |
// |
| 549 |
// This is roughly equivalent to these calls: |
| 550 |
// |
| 551 |
// js> var ym = new Temporal.PlainYearMonth(2023, 1); |
| 552 |
// js> ym.toPlainDate({day: 1}).add({months: 1}).subtract({days: 1}).day |
| 553 |
// 31 |
| 554 |
// |
| 555 |
// For many calendars this is equivalent to `ym.daysInMonth`, except when |
| 556 |
// some days are skipped, for example consider the Julian-to-Gregorian |
| 557 |
// calendar transition. |
| 558 |
|
| 559 |
// Step 10.a. |
| 560 |
auto oneMonthDuration = DateDuration{0, 1}; |
| 561 |
|
| 562 |
// Step 10.b. |
| 563 |
ISODate nextMonth; |
| 564 |
if (!CalendarDateAdd(cx, calendar, intermediateDate, oneMonthDuration, |
| 565 |
TemporalOverflow::Constrain, &nextMonth)) { |
| 566 |
return false; |
| 567 |
} |
| 568 |
|
| 569 |
// Step 10.c. |
| 570 |
date = BalanceISODate(nextMonth, -1); |
| 571 |
|
| 572 |
// Step 10.d. |
| 573 |
MOZ_ASSERT(ISODateWithinLimits(date)); |
| 574 |
} else { |
| 575 |
// Step 11.a. |
| 576 |
date = intermediateDate; |
| 577 |
} |
| 578 |
|
| 579 |
// Steps 12. |
| 580 |
auto durationToAdd = ToDateDurationRecordWithoutTime(duration); |
| 581 |
|
| 582 |
// Step 13. |
| 583 |
ISODate addedDate; |
| 584 |
if (!CalendarDateAdd(cx, calendar, date, durationToAdd, overflow, |
| 585 |
&addedDate)) { |
| 586 |
return false; |
| 587 |
} |
| 588 |
MOZ_ASSERT(ISODateWithinLimits(addedDate)); |
| 589 |
|
| 590 |
Rooted<PlainYearMonth> addedYearMonth(cx, |
| 591 |
PlainYearMonth{addedDate, calendar}); |
| 592 |
|
| 593 |
// Step 14. |
| 594 |
Rooted<CalendarFields> addedDateFields(cx); |
| 595 |
if (!ISODateToFields(cx, addedYearMonth, &addedDateFields)) { |
| 596 |
return false; |
| 597 |
} |
| 598 |
|
| 599 |
// Step 15. |
| 600 |
Rooted<PlainYearMonth> result(cx); |
| 601 |
if (!CalendarYearMonthFromFields(cx, calendar, addedDateFields, overflow, |
| 602 |
&result)) { |
| 603 |
return false; |
| 604 |
} |
| 605 |
|
| 606 |
// Step 16. |
| 607 |
auto* obj = CreateTemporalYearMonth(cx, result); |
| 608 |
if (!obj) { |
| 609 |
return false; |
| 610 |
} |
| 611 |
|
| 612 |
args.rval().setObject(*obj); |
| 613 |
return true; |
| 614 |
} |
| 615 |
|
| 616 |
/** |
| 617 |
* Temporal.PlainYearMonth ( isoYear, isoMonth [ , calendar [ , referenceISODay |
| 618 |
* ] ] ) |
| 619 |
*/ |
| 620 |
static bool PlainYearMonthConstructor(JSContext* cx, unsigned argc, Value* vp) { |
| 621 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 622 |
|
| 623 |
// Step 1. |
| 624 |
if (!ThrowIfNotConstructing(cx, args, "Temporal.PlainYearMonth")) { |
| 625 |
return false; |
| 626 |
} |
| 627 |
|
| 628 |
// Step 3. |
| 629 |
double isoYear; |
| 630 |
if (!ToIntegerWithTruncation(cx, args.get(0), "year", &isoYear)) { |
| 631 |
return false; |
| 632 |
} |
| 633 |
|
| 634 |
// Step 4. |
| 635 |
double isoMonth; |
| 636 |
if (!ToIntegerWithTruncation(cx, args.get(1), "month", &isoMonth)) { |
| 637 |
return false; |
| 638 |
} |
| 639 |
|
| 640 |
// Steps 5-7. |
| 641 |
Rooted<CalendarValue> calendar(cx, CalendarValue(CalendarId::ISO8601)); |
| 642 |
if (args.hasDefined(2)) { |
| 643 |
// Step 6. |
| 644 |
if (!args[2].isString()) { |
| 645 |
ReportValueError(cx, JSMSG_UNEXPECTED_TYPE, JSDVG_IGNORE_STACK, args[2], |
| 646 |
nullptr, "not a string"); |
| 647 |
return false; |
| 648 |
} |
| 649 |
|
| 650 |
// Step 7. |
| 651 |
Rooted<JSString*> calendarString(cx, args[2].toString()); |
| 652 |
if (!CanonicalizeCalendar(cx, calendarString, &calendar)) { |
| 653 |
return false; |
| 654 |
} |
| 655 |
} |
| 656 |
|
| 657 |
// Steps 2 and 8. |
| 658 |
double isoDay = 1; |
| 659 |
if (args.hasDefined(3)) { |
| 660 |
if (!ToIntegerWithTruncation(cx, args[3], "day", &isoDay)) { |
| 661 |
return false; |
| 662 |
} |
| 663 |
} |
| 664 |
|
| 665 |
// Step 9. |
| 666 |
if (!ThrowIfInvalidISODate(cx, isoYear, isoMonth, isoDay)) { |
| 667 |
return false; |
| 668 |
} |
| 669 |
|
| 670 |
// Step 10. |
| 671 |
auto isoDate = ISODate{int32_t(isoYear), int32_t(isoMonth), int32_t(isoDay)}; |
| 672 |
|
| 673 |
// Step 11. |
| 674 |
auto* yearMonth = CreateTemporalYearMonth(cx, args, isoDate, calendar); |
| 675 |
if (!yearMonth) { |
| 676 |
return false; |
| 677 |
} |
| 678 |
|
| 679 |
args.rval().setObject(*yearMonth); |
| 680 |
return true; |
| 681 |
} |
| 682 |
|
| 683 |
/** |
| 684 |
* Temporal.PlainYearMonth.from ( item [ , options ] ) |
| 685 |
*/ |
| 686 |
static bool PlainYearMonth_from(JSContext* cx, unsigned argc, Value* vp) { |
| 687 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 688 |
|
| 689 |
// Step 1. |
| 690 |
Rooted<PlainYearMonth> yearMonth(cx); |
| 691 |
if (!ToTemporalYearMonth(cx, args.get(0), args.get(1), &yearMonth)) { |
| 692 |
return false; |
| 693 |
} |
| 694 |
|
| 695 |
auto* result = CreateTemporalYearMonth(cx, yearMonth); |
| 696 |
if (!result) { |
| 697 |
return false; |
| 698 |
} |
| 699 |
|
| 700 |
args.rval().setObject(*result); |
| 701 |
return true; |
| 702 |
} |
| 703 |
|
| 704 |
/** |
| 705 |
* Temporal.PlainYearMonth.compare ( one, two ) |
| 706 |
*/ |
| 707 |
static bool PlainYearMonth_compare(JSContext* cx, unsigned argc, Value* vp) { |
| 708 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 709 |
|
| 710 |
// Step 1. |
| 711 |
Rooted<PlainYearMonth> one(cx); |
| 712 |
if (!ToTemporalYearMonth(cx, args.get(0), &one)) { |
| 713 |
return false; |
| 714 |
} |
| 715 |
|
| 716 |
// Step 2. |
| 717 |
Rooted<PlainYearMonth> two(cx); |
| 718 |
if (!ToTemporalYearMonth(cx, args.get(1), &two)) { |
| 719 |
return false; |
| 720 |
} |
| 721 |
|
| 722 |
// Step 3. |
| 723 |
args.rval().setInt32(CompareISODate(one, two)); |
| 724 |
return true; |
| 725 |
} |
| 726 |
|
| 727 |
/** |
| 728 |
* get Temporal.PlainYearMonth.prototype.calendarId |
| 729 |
*/ |
| 730 |
static bool PlainYearMonth_calendarId(JSContext* cx, const CallArgs& args) { |
| 731 |
auto* yearMonth = &args.thisv().toObject().as<PlainYearMonthObject>(); |
| 732 |
|
| 733 |
// Step 3. |
| 734 |
auto* str = |
| 735 |
NewStringCopy<CanGC>(cx, CalendarIdentifier(yearMonth->calendar())); |
| 736 |
if (!str) { |
| 737 |
return false; |
| 738 |
} |
| 739 |
|
| 740 |
args.rval().setString(str); |
| 741 |
return true; |
| 742 |
} |
| 743 |
|
| 744 |
/** |
| 745 |
* get Temporal.PlainYearMonth.prototype.calendarId |
| 746 |
*/ |
| 747 |
static bool PlainYearMonth_calendarId(JSContext* cx, unsigned argc, Value* vp) { |
| 748 |
// Steps 1-2. |
| 749 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 750 |
return CallNonGenericMethod<IsPlainYearMonth, PlainYearMonth_calendarId>( |
| 751 |
cx, args); |
| 752 |
} |
| 753 |
|
| 754 |
/** |
| 755 |
* get Temporal.PlainYearMonth.prototype.era |
| 756 |
*/ |
| 757 |
static bool PlainYearMonth_era(JSContext* cx, const CallArgs& args) { |
| 758 |
auto* yearMonth = &args.thisv().toObject().as<PlainYearMonthObject>(); |
| 759 |
Rooted<CalendarValue> calendar(cx, yearMonth->calendar()); |
| 760 |
|
| 761 |
// Step 3. |
| 762 |
return CalendarEra(cx, calendar, yearMonth->date(), args.rval()); |
| 763 |
} |
| 764 |
|
| 765 |
/** |
| 766 |
* get Temporal.PlainYearMonth.prototype.era |
| 767 |
*/ |
| 768 |
static bool PlainYearMonth_era(JSContext* cx, unsigned argc, Value* vp) { |
| 769 |
// Steps 1-2. |
| 770 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 771 |
return CallNonGenericMethod<IsPlainYearMonth, PlainYearMonth_era>(cx, args); |
| 772 |
} |
| 773 |
|
| 774 |
/** |
| 775 |
* get Temporal.PlainYearMonth.prototype.eraYear |
| 776 |
*/ |
| 777 |
static bool PlainYearMonth_eraYear(JSContext* cx, const CallArgs& args) { |
| 778 |
auto* yearMonth = &args.thisv().toObject().as<PlainYearMonthObject>(); |
| 779 |
Rooted<CalendarValue> calendar(cx, yearMonth->calendar()); |
| 780 |
|
| 781 |
// Steps 3-5. |
| 782 |
return CalendarEraYear(cx, calendar, yearMonth->date(), args.rval()); |
| 783 |
} |
| 784 |
|
| 785 |
/** |
| 786 |
* get Temporal.PlainYearMonth.prototype.eraYear |
| 787 |
*/ |
| 788 |
static bool PlainYearMonth_eraYear(JSContext* cx, unsigned argc, Value* vp) { |
| 789 |
// Steps 1-2. |
| 790 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 791 |
return CallNonGenericMethod<IsPlainYearMonth, PlainYearMonth_eraYear>(cx, |
| 792 |
args); |
| 793 |
} |
| 794 |
|
| 795 |
/** |
| 796 |
* get Temporal.PlainYearMonth.prototype.year |
| 797 |
*/ |
| 798 |
static bool PlainYearMonth_year(JSContext* cx, const CallArgs& args) { |
| 799 |
auto* yearMonth = &args.thisv().toObject().as<PlainYearMonthObject>(); |
| 800 |
Rooted<CalendarValue> calendar(cx, yearMonth->calendar()); |
| 801 |
|
| 802 |
// Step 3. |
| 803 |
return CalendarYear(cx, calendar, yearMonth->date(), args.rval()); |
| 804 |
} |
| 805 |
|
| 806 |
/** |
| 807 |
* get Temporal.PlainYearMonth.prototype.year |
| 808 |
*/ |
| 809 |
static bool PlainYearMonth_year(JSContext* cx, unsigned argc, Value* vp) { |
| 810 |
// Steps 1-2. |
| 811 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 812 |
return CallNonGenericMethod<IsPlainYearMonth, PlainYearMonth_year>(cx, args); |
| 813 |
} |
| 814 |
|
| 815 |
/** |
| 816 |
* get Temporal.PlainYearMonth.prototype.month |
| 817 |
*/ |
| 818 |
static bool PlainYearMonth_month(JSContext* cx, const CallArgs& args) { |
| 819 |
auto* yearMonth = &args.thisv().toObject().as<PlainYearMonthObject>(); |
| 820 |
Rooted<CalendarValue> calendar(cx, yearMonth->calendar()); |
| 821 |
|
| 822 |
// Step 3. |
| 823 |
return CalendarMonth(cx, calendar, yearMonth->date(), args.rval()); |
| 824 |
} |
| 825 |
|
| 826 |
/** |
| 827 |
* get Temporal.PlainYearMonth.prototype.month |
| 828 |
*/ |
| 829 |
static bool PlainYearMonth_month(JSContext* cx, unsigned argc, Value* vp) { |
| 830 |
// Steps 1-2. |
| 831 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 832 |
return CallNonGenericMethod<IsPlainYearMonth, PlainYearMonth_month>(cx, args); |
| 833 |
} |
| 834 |
|
| 835 |
/** |
| 836 |
* get Temporal.PlainYearMonth.prototype.monthCode |
| 837 |
*/ |
| 838 |
static bool PlainYearMonth_monthCode(JSContext* cx, const CallArgs& args) { |
| 839 |
auto* yearMonth = &args.thisv().toObject().as<PlainYearMonthObject>(); |
| 840 |
Rooted<CalendarValue> calendar(cx, yearMonth->calendar()); |
| 841 |
|
| 842 |
// Step 3. |
| 843 |
return CalendarMonthCode(cx, calendar, yearMonth->date(), args.rval()); |
| 844 |
} |
| 845 |
|
| 846 |
/** |
| 847 |
* get Temporal.PlainYearMonth.prototype.monthCode |
| 848 |
*/ |
| 849 |
static bool PlainYearMonth_monthCode(JSContext* cx, unsigned argc, Value* vp) { |
| 850 |
// Steps 1-2. |
| 851 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 852 |
return CallNonGenericMethod<IsPlainYearMonth, PlainYearMonth_monthCode>(cx, |
| 853 |
args); |
| 854 |
} |
| 855 |
|
| 856 |
/** |
| 857 |
* get Temporal.PlainYearMonth.prototype.daysInYear |
| 858 |
*/ |
| 859 |
static bool PlainYearMonth_daysInYear(JSContext* cx, const CallArgs& args) { |
| 860 |
auto* yearMonth = &args.thisv().toObject().as<PlainYearMonthObject>(); |
| 861 |
Rooted<CalendarValue> calendar(cx, yearMonth->calendar()); |
| 862 |
|
| 863 |
// Step 3. |
| 864 |
return CalendarDaysInYear(cx, calendar, yearMonth->date(), args.rval()); |
| 865 |
} |
| 866 |
|
| 867 |
/** |
| 868 |
* get Temporal.PlainYearMonth.prototype.daysInYear |
| 869 |
*/ |
| 870 |
static bool PlainYearMonth_daysInYear(JSContext* cx, unsigned argc, Value* vp) { |
| 871 |
// Steps 1-2. |
| 872 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 873 |
return CallNonGenericMethod<IsPlainYearMonth, PlainYearMonth_daysInYear>( |
| 874 |
cx, args); |
| 875 |
} |
| 876 |
|
| 877 |
/** |
| 878 |
* get Temporal.PlainYearMonth.prototype.daysInMonth |
| 879 |
*/ |
| 880 |
static bool PlainYearMonth_daysInMonth(JSContext* cx, const CallArgs& args) { |
| 881 |
auto* yearMonth = &args.thisv().toObject().as<PlainYearMonthObject>(); |
| 882 |
Rooted<CalendarValue> calendar(cx, yearMonth->calendar()); |
| 883 |
|
| 884 |
// Step 3. |
| 885 |
return CalendarDaysInMonth(cx, calendar, yearMonth->date(), args.rval()); |
| 886 |
} |
| 887 |
|
| 888 |
/** |
| 889 |
* get Temporal.PlainYearMonth.prototype.daysInMonth |
| 890 |
*/ |
| 891 |
static bool PlainYearMonth_daysInMonth(JSContext* cx, unsigned argc, |
| 892 |
Value* vp) { |
| 893 |
// Steps 1-2. |
| 894 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 895 |
return CallNonGenericMethod<IsPlainYearMonth, PlainYearMonth_daysInMonth>( |
| 896 |
cx, args); |
| 897 |
} |
| 898 |
|
| 899 |
/** |
| 900 |
* get Temporal.PlainYearMonth.prototype.monthsInYear |
| 901 |
*/ |
| 902 |
static bool PlainYearMonth_monthsInYear(JSContext* cx, const CallArgs& args) { |
| 903 |
auto* yearMonth = &args.thisv().toObject().as<PlainYearMonthObject>(); |
| 904 |
Rooted<CalendarValue> calendar(cx, yearMonth->calendar()); |
| 905 |
|
| 906 |
// Step 3. |
| 907 |
return CalendarMonthsInYear(cx, calendar, yearMonth->date(), args.rval()); |
| 908 |
} |
| 909 |
|
| 910 |
/** |
| 911 |
* get Temporal.PlainYearMonth.prototype.monthsInYear |
| 912 |
*/ |
| 913 |
static bool PlainYearMonth_monthsInYear(JSContext* cx, unsigned argc, |
| 914 |
Value* vp) { |
| 915 |
// Steps 1-2. |
| 916 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 917 |
return CallNonGenericMethod<IsPlainYearMonth, PlainYearMonth_monthsInYear>( |
| 918 |
cx, args); |
| 919 |
} |
| 920 |
|
| 921 |
/** |
| 922 |
* get Temporal.PlainYearMonth.prototype.inLeapYear |
| 923 |
*/ |
| 924 |
static bool PlainYearMonth_inLeapYear(JSContext* cx, const CallArgs& args) { |
| 925 |
auto* yearMonth = &args.thisv().toObject().as<PlainYearMonthObject>(); |
| 926 |
Rooted<CalendarValue> calendar(cx, yearMonth->calendar()); |
| 927 |
|
| 928 |
// Step 3. |
| 929 |
return CalendarInLeapYear(cx, calendar, yearMonth->date(), args.rval()); |
| 930 |
} |
| 931 |
|
| 932 |
/** |
| 933 |
* get Temporal.PlainYearMonth.prototype.inLeapYear |
| 934 |
*/ |
| 935 |
static bool PlainYearMonth_inLeapYear(JSContext* cx, unsigned argc, Value* vp) { |
| 936 |
// Steps 1-2. |
| 937 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 938 |
return CallNonGenericMethod<IsPlainYearMonth, PlainYearMonth_inLeapYear>( |
| 939 |
cx, args); |
| 940 |
} |
| 941 |
|
| 942 |
/** |
| 943 |
* Temporal.PlainYearMonth.prototype.with ( temporalYearMonthLike [ , options ] |
| 944 |
* ) |
| 945 |
*/ |
| 946 |
static bool PlainYearMonth_with(JSContext* cx, const CallArgs& args) { |
| 947 |
Rooted<PlainYearMonth> yearMonth( |
| 948 |
cx, &args.thisv().toObject().as<PlainYearMonthObject>()); |
| 949 |
|
| 950 |
// Step 3. |
| 951 |
Rooted<JSObject*> temporalYearMonthLike( |
| 952 |
cx, RequireObjectArg(cx, "temporalYearMonthLike", "with", args.get(0))); |
| 953 |
if (!temporalYearMonthLike) { |
| 954 |
return false; |
| 955 |
} |
| 956 |
if (!ThrowIfTemporalLikeObject(cx, temporalYearMonthLike)) { |
| 957 |
return false; |
| 958 |
} |
| 959 |
|
| 960 |
// Step 4. |
| 961 |
auto calendar = yearMonth.calendar(); |
| 962 |
|
| 963 |
// Step 5. |
| 964 |
Rooted<CalendarFields> fields(cx); |
| 965 |
if (!ISODateToFields(cx, yearMonth, &fields)) { |
| 966 |
return false; |
| 967 |
} |
| 968 |
|
| 969 |
// Step 6. |
| 970 |
Rooted<CalendarFields> partialYearMonth(cx); |
| 971 |
if (!PreparePartialCalendarFields(cx, calendar, temporalYearMonthLike, |
| 972 |
{ |
| 973 |
CalendarField::Year, |
| 974 |
CalendarField::Month, |
| 975 |
CalendarField::MonthCode, |
| 976 |
}, |
| 977 |
&partialYearMonth)) { |
| 978 |
return false; |
| 979 |
} |
| 980 |
MOZ_ASSERT(!partialYearMonth.keys().isEmpty()); |
| 981 |
|
| 982 |
// Step 7. |
| 983 |
fields = CalendarMergeFields(calendar, fields, partialYearMonth); |
| 984 |
|
| 985 |
// Steps 8-9. |
| 986 |
auto overflow = TemporalOverflow::Constrain; |
| 987 |
if (args.hasDefined(1)) { |
| 988 |
// Step 8. |
| 989 |
Rooted<JSObject*> options(cx, |
| 990 |
RequireObjectArg(cx, "options", "with", args[1])); |
| 991 |
if (!options) { |
| 992 |
return false; |
| 993 |
} |
| 994 |
|
| 995 |
// Step 9. |
| 996 |
if (!GetTemporalOverflowOption(cx, options, &overflow)) { |
| 997 |
return false; |
| 998 |
} |
| 999 |
} |
| 1000 |
|
| 1001 |
// Step 10. |
| 1002 |
Rooted<PlainYearMonth> result(cx); |
| 1003 |
if (!CalendarYearMonthFromFields(cx, calendar, fields, overflow, &result)) { |
| 1004 |
return false; |
| 1005 |
} |
| 1006 |
|
| 1007 |
// Step 11. |
| 1008 |
auto* obj = CreateTemporalYearMonth(cx, result); |
| 1009 |
if (!obj) { |
| 1010 |
return false; |
| 1011 |
} |
| 1012 |
|
| 1013 |
args.rval().setObject(*obj); |
| 1014 |
return true; |
| 1015 |
} |
| 1016 |
|
| 1017 |
/** |
| 1018 |
* Temporal.PlainYearMonth.prototype.with ( temporalYearMonthLike [ , options ] |
| 1019 |
* ) |
| 1020 |
*/ |
| 1021 |
static bool PlainYearMonth_with(JSContext* cx, unsigned argc, Value* vp) { |
| 1022 |
// Steps 1-2. |
| 1023 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1024 |
return CallNonGenericMethod<IsPlainYearMonth, PlainYearMonth_with>(cx, args); |
| 1025 |
} |
| 1026 |
|
| 1027 |
/** |
| 1028 |
* Temporal.PlainYearMonth.prototype.add ( temporalDurationLike [ , options ] ) |
| 1029 |
*/ |
| 1030 |
static bool PlainYearMonth_add(JSContext* cx, const CallArgs& args) { |
| 1031 |
// Step 3. |
| 1032 |
return AddDurationToYearMonth(cx, TemporalAddDuration::Add, args); |
| 1033 |
} |
| 1034 |
|
| 1035 |
/** |
| 1036 |
* Temporal.PlainYearMonth.prototype.add ( temporalDurationLike [ , options ] ) |
| 1037 |
*/ |
| 1038 |
static bool PlainYearMonth_add(JSContext* cx, unsigned argc, Value* vp) { |
| 1039 |
// Steps 1-2. |
| 1040 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1041 |
return CallNonGenericMethod<IsPlainYearMonth, PlainYearMonth_add>(cx, args); |
| 1042 |
} |
| 1043 |
|
| 1044 |
/** |
| 1045 |
* Temporal.PlainYearMonth.prototype.subtract ( temporalDurationLike [ , options |
| 1046 |
* ] ) |
| 1047 |
*/ |
| 1048 |
static bool PlainYearMonth_subtract(JSContext* cx, const CallArgs& args) { |
| 1049 |
// Step 3. |
| 1050 |
return AddDurationToYearMonth(cx, TemporalAddDuration::Subtract, args); |
| 1051 |
} |
| 1052 |
|
| 1053 |
/** |
| 1054 |
* Temporal.PlainYearMonth.prototype.subtract ( temporalDurationLike [ , options |
| 1055 |
* ] ) |
| 1056 |
*/ |
| 1057 |
static bool PlainYearMonth_subtract(JSContext* cx, unsigned argc, Value* vp) { |
| 1058 |
// Steps 1-2. |
| 1059 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1060 |
return CallNonGenericMethod<IsPlainYearMonth, PlainYearMonth_subtract>(cx, |
| 1061 |
args); |
| 1062 |
} |
| 1063 |
|
| 1064 |
/** |
| 1065 |
* Temporal.PlainYearMonth.prototype.until ( other [ , options ] ) |
| 1066 |
*/ |
| 1067 |
static bool PlainYearMonth_until(JSContext* cx, const CallArgs& args) { |
| 1068 |
// Step 3. |
| 1069 |
return DifferenceTemporalPlainYearMonth(cx, TemporalDifference::Until, args); |
| 1070 |
} |
| 1071 |
|
| 1072 |
/** |
| 1073 |
* Temporal.PlainYearMonth.prototype.until ( other [ , options ] ) |
| 1074 |
*/ |
| 1075 |
static bool PlainYearMonth_until(JSContext* cx, unsigned argc, Value* vp) { |
| 1076 |
// Steps 1-2. |
| 1077 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1078 |
return CallNonGenericMethod<IsPlainYearMonth, PlainYearMonth_until>(cx, args); |
| 1079 |
} |
| 1080 |
|
| 1081 |
/** |
| 1082 |
* Temporal.PlainYearMonth.prototype.since ( other [ , options ] ) |
| 1083 |
*/ |
| 1084 |
static bool PlainYearMonth_since(JSContext* cx, const CallArgs& args) { |
| 1085 |
// Step 3. |
| 1086 |
return DifferenceTemporalPlainYearMonth(cx, TemporalDifference::Since, args); |
| 1087 |
} |
| 1088 |
|
| 1089 |
/** |
| 1090 |
* Temporal.PlainYearMonth.prototype.since ( other [ , options ] ) |
| 1091 |
*/ |
| 1092 |
static bool PlainYearMonth_since(JSContext* cx, unsigned argc, Value* vp) { |
| 1093 |
// Steps 1-2. |
| 1094 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1095 |
return CallNonGenericMethod<IsPlainYearMonth, PlainYearMonth_since>(cx, args); |
| 1096 |
} |
| 1097 |
|
| 1098 |
/** |
| 1099 |
* Temporal.PlainYearMonth.prototype.equals ( other ) |
| 1100 |
*/ |
| 1101 |
static bool PlainYearMonth_equals(JSContext* cx, const CallArgs& args) { |
| 1102 |
auto* yearMonth = &args.thisv().toObject().as<PlainYearMonthObject>(); |
| 1103 |
auto date = yearMonth->date(); |
| 1104 |
Rooted<CalendarValue> calendar(cx, yearMonth->calendar()); |
| 1105 |
|
| 1106 |
// Step 3. |
| 1107 |
Rooted<PlainYearMonth> other(cx); |
| 1108 |
if (!ToTemporalYearMonth(cx, args.get(0), &other)) { |
| 1109 |
return false; |
| 1110 |
} |
| 1111 |
|
| 1112 |
// Steps 4-7. |
| 1113 |
bool equals = |
| 1114 |
date == other.date() && CalendarEquals(calendar, other.calendar()); |
| 1115 |
|
| 1116 |
args.rval().setBoolean(equals); |
| 1117 |
return true; |
| 1118 |
} |
| 1119 |
|
| 1120 |
/** |
| 1121 |
* Temporal.PlainYearMonth.prototype.equals ( other ) |
| 1122 |
*/ |
| 1123 |
static bool PlainYearMonth_equals(JSContext* cx, unsigned argc, Value* vp) { |
| 1124 |
// Steps 1-2. |
| 1125 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1126 |
return CallNonGenericMethod<IsPlainYearMonth, PlainYearMonth_equals>(cx, |
| 1127 |
args); |
| 1128 |
} |
| 1129 |
|
| 1130 |
/** |
| 1131 |
* Temporal.PlainYearMonth.prototype.toString ( [ options ] ) |
| 1132 |
*/ |
| 1133 |
static bool PlainYearMonth_toString(JSContext* cx, const CallArgs& args) { |
| 1134 |
Rooted<PlainYearMonthObject*> yearMonth( |
| 1135 |
cx, &args.thisv().toObject().as<PlainYearMonthObject>()); |
| 1136 |
|
| 1137 |
auto showCalendar = ShowCalendar::Auto; |
| 1138 |
if (args.hasDefined(0)) { |
| 1139 |
// Step 3. |
| 1140 |
Rooted<JSObject*> options( |
| 1141 |
cx, RequireObjectArg(cx, "options", "toString", args[0])); |
| 1142 |
if (!options) { |
| 1143 |
return false; |
| 1144 |
} |
| 1145 |
|
| 1146 |
// Step 4. |
| 1147 |
if (!GetTemporalShowCalendarNameOption(cx, options, &showCalendar)) { |
| 1148 |
return false; |
| 1149 |
} |
| 1150 |
} |
| 1151 |
|
| 1152 |
// Step 5. |
| 1153 |
JSString* str = TemporalYearMonthToString(cx, yearMonth, showCalendar); |
| 1154 |
if (!str) { |
| 1155 |
return false; |
| 1156 |
} |
| 1157 |
|
| 1158 |
args.rval().setString(str); |
| 1159 |
return true; |
| 1160 |
} |
| 1161 |
|
| 1162 |
/** |
| 1163 |
* Temporal.PlainYearMonth.prototype.toString ( [ options ] ) |
| 1164 |
*/ |
| 1165 |
static bool PlainYearMonth_toString(JSContext* cx, unsigned argc, Value* vp) { |
| 1166 |
// Steps 1-2. |
| 1167 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1168 |
return CallNonGenericMethod<IsPlainYearMonth, PlainYearMonth_toString>(cx, |
| 1169 |
args); |
| 1170 |
} |
| 1171 |
|
| 1172 |
/** |
| 1173 |
* Temporal.PlainYearMonth.prototype.toLocaleString ( [ locales [ , options ] ] |
| 1174 |
* ) |
| 1175 |
*/ |
| 1176 |
static bool PlainYearMonth_toLocaleString(JSContext* cx, const CallArgs& args) { |
| 1177 |
// Steps 3-4. |
| 1178 |
Handle<PropertyName*> required = cx->names().date; |
| 1179 |
Handle<PropertyName*> defaults = cx->names().date; |
| 1180 |
return TemporalObjectToLocaleString(cx, args, required, defaults); |
| 1181 |
} |
| 1182 |
|
| 1183 |
/** |
| 1184 |
* Temporal.PlainYearMonth.prototype.toLocaleString ( [ locales [ , options ] ] |
| 1185 |
* ) |
| 1186 |
*/ |
| 1187 |
static bool PlainYearMonth_toLocaleString(JSContext* cx, unsigned argc, |
| 1188 |
Value* vp) { |
| 1189 |
// Steps 1-2. |
| 1190 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1191 |
return CallNonGenericMethod<IsPlainYearMonth, PlainYearMonth_toLocaleString>( |
| 1192 |
cx, args); |
| 1193 |
} |
| 1194 |
|
| 1195 |
/** |
| 1196 |
* Temporal.PlainYearMonth.prototype.toJSON ( ) |
| 1197 |
*/ |
| 1198 |
static bool PlainYearMonth_toJSON(JSContext* cx, const CallArgs& args) { |
| 1199 |
Rooted<PlainYearMonthObject*> yearMonth( |
| 1200 |
cx, &args.thisv().toObject().as<PlainYearMonthObject>()); |
| 1201 |
|
| 1202 |
// Step 3. |
| 1203 |
JSString* str = TemporalYearMonthToString(cx, yearMonth, ShowCalendar::Auto); |
| 1204 |
if (!str) { |
| 1205 |
return false; |
| 1206 |
} |
| 1207 |
|
| 1208 |
args.rval().setString(str); |
| 1209 |
return true; |
| 1210 |
} |
| 1211 |
|
| 1212 |
/** |
| 1213 |
* Temporal.PlainYearMonth.prototype.toJSON ( ) |
| 1214 |
*/ |
| 1215 |
static bool PlainYearMonth_toJSON(JSContext* cx, unsigned argc, Value* vp) { |
| 1216 |
// Steps 1-2. |
| 1217 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1218 |
return CallNonGenericMethod<IsPlainYearMonth, PlainYearMonth_toJSON>(cx, |
| 1219 |
args); |
| 1220 |
} |
| 1221 |
|
| 1222 |
/** |
| 1223 |
* Temporal.PlainYearMonth.prototype.valueOf ( ) |
| 1224 |
*/ |
| 1225 |
static bool PlainYearMonth_valueOf(JSContext* cx, unsigned argc, Value* vp) { |
| 1226 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, JSMSG_CANT_CONVERT_TO, |
| 1227 |
"PlainYearMonth", "primitive type"); |
| 1228 |
return false; |
| 1229 |
} |
| 1230 |
|
| 1231 |
/** |
| 1232 |
* Temporal.PlainYearMonth.prototype.toPlainDate ( item ) |
| 1233 |
*/ |
| 1234 |
static bool PlainYearMonth_toPlainDate(JSContext* cx, const CallArgs& args) { |
| 1235 |
Rooted<PlainYearMonth> yearMonth( |
| 1236 |
cx, &args.thisv().toObject().as<PlainYearMonthObject>()); |
| 1237 |
|
| 1238 |
// Step 3. |
| 1239 |
Rooted<JSObject*> item( |
| 1240 |
cx, RequireObjectArg(cx, "item", "toPlainDate", args.get(0))); |
| 1241 |
if (!item) { |
| 1242 |
return false; |
| 1243 |
} |
| 1244 |
|
| 1245 |
// Step 4. |
| 1246 |
auto calendar = yearMonth.calendar(); |
| 1247 |
|
| 1248 |
// Step 5. |
| 1249 |
Rooted<CalendarFields> fields(cx); |
| 1250 |
if (!ISODateToFields(cx, yearMonth, &fields)) { |
| 1251 |
return false; |
| 1252 |
} |
| 1253 |
|
| 1254 |
// Step 6. |
| 1255 |
Rooted<CalendarFields> inputFields(cx); |
| 1256 |
if (!PrepareCalendarFields(cx, calendar, item, |
| 1257 |
{ |
| 1258 |
CalendarField::Day, |
| 1259 |
}, |
| 1260 |
&inputFields)) { |
| 1261 |
return false; |
| 1262 |
} |
| 1263 |
|
| 1264 |
// Step 7. |
| 1265 |
fields = CalendarMergeFields(calendar, fields, inputFields); |
| 1266 |
|
| 1267 |
// Step 8. |
| 1268 |
Rooted<PlainDate> result(cx); |
| 1269 |
if (!CalendarDateFromFields(cx, calendar, fields, TemporalOverflow::Constrain, |
| 1270 |
&result)) { |
| 1271 |
return false; |
| 1272 |
} |
| 1273 |
|
| 1274 |
// Step 9. |
| 1275 |
auto* obj = CreateTemporalDate(cx, result); |
| 1276 |
if (!obj) { |
| 1277 |
return false; |
| 1278 |
} |
| 1279 |
|
| 1280 |
args.rval().setObject(*obj); |
| 1281 |
return true; |
| 1282 |
} |
| 1283 |
|
| 1284 |
/** |
| 1285 |
* Temporal.PlainYearMonth.prototype.toPlainDate ( item ) |
| 1286 |
*/ |
| 1287 |
static bool PlainYearMonth_toPlainDate(JSContext* cx, unsigned argc, |
| 1288 |
Value* vp) { |
| 1289 |
// Steps 1-2. |
| 1290 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1291 |
return CallNonGenericMethod<IsPlainYearMonth, PlainYearMonth_toPlainDate>( |
| 1292 |
cx, args); |
| 1293 |
} |
| 1294 |
|
| 1295 |
const JSClass PlainYearMonthObject::class_ = { |
| 1296 |
"Temporal.PlainYearMonth", |
| 1297 |
JSCLASS_HAS_RESERVED_SLOTS(PlainYearMonthObject::SLOT_COUNT) | |
| 1298 |
JSCLASS_HAS_CACHED_PROTO(JSProto_PlainYearMonth), |
| 1299 |
JS_NULL_CLASS_OPS, |
| 1300 |
&PlainYearMonthObject::classSpec_, |
| 1301 |
}; |
| 1302 |
|
| 1303 |
const JSClass& PlainYearMonthObject::protoClass_ = PlainObject::class_; |
| 1304 |
|
| 1305 |
static const JSFunctionSpec PlainYearMonth_methods[] = { |
| 1306 |
JS_FN("from", PlainYearMonth_from, 1, 0), |
| 1307 |
JS_FN("compare", PlainYearMonth_compare, 2, 0), |
| 1308 |
JS_FS_END, |
| 1309 |
}; |
| 1310 |
|
| 1311 |
static const JSFunctionSpec PlainYearMonth_prototype_methods[] = { |
| 1312 |
JS_FN("with", PlainYearMonth_with, 1, 0), |
| 1313 |
JS_FN("add", PlainYearMonth_add, 1, 0), |
| 1314 |
JS_FN("subtract", PlainYearMonth_subtract, 1, 0), |
| 1315 |
JS_FN("until", PlainYearMonth_until, 1, 0), |
| 1316 |
JS_FN("since", PlainYearMonth_since, 1, 0), |
| 1317 |
JS_FN("equals", PlainYearMonth_equals, 1, 0), |
| 1318 |
JS_FN("toString", PlainYearMonth_toString, 0, 0), |
| 1319 |
JS_FN("toLocaleString", PlainYearMonth_toLocaleString, 0, 0), |
| 1320 |
JS_FN("toJSON", PlainYearMonth_toJSON, 0, 0), |
| 1321 |
JS_FN("valueOf", PlainYearMonth_valueOf, 0, 0), |
| 1322 |
JS_FN("toPlainDate", PlainYearMonth_toPlainDate, 1, 0), |
| 1323 |
JS_FS_END, |
| 1324 |
}; |
| 1325 |
|
| 1326 |
static const JSPropertySpec PlainYearMonth_prototype_properties[] = { |
| 1327 |
JS_PSG("calendarId", PlainYearMonth_calendarId, 0), |
| 1328 |
JS_PSG("era", PlainYearMonth_era, 0), |
| 1329 |
JS_PSG("eraYear", PlainYearMonth_eraYear, 0), |
| 1330 |
JS_PSG("year", PlainYearMonth_year, 0), |
| 1331 |
JS_PSG("month", PlainYearMonth_month, 0), |
| 1332 |
JS_PSG("monthCode", PlainYearMonth_monthCode, 0), |
| 1333 |
JS_PSG("daysInYear", PlainYearMonth_daysInYear, 0), |
| 1334 |
JS_PSG("daysInMonth", PlainYearMonth_daysInMonth, 0), |
| 1335 |
JS_PSG("monthsInYear", PlainYearMonth_monthsInYear, 0), |
| 1336 |
JS_PSG("inLeapYear", PlainYearMonth_inLeapYear, 0), |
| 1337 |
JS_STRING_SYM_PS(toStringTag, "Temporal.PlainYearMonth", JSPROP_READONLY), |
| 1338 |
JS_PS_END, |
| 1339 |
}; |
| 1340 |
|
| 1341 |
const ClassSpec PlainYearMonthObject::classSpec_ = { |
| 1342 |
GenericCreateConstructor<PlainYearMonthConstructor, 2, |
| 1343 |
gc::AllocKind::FUNCTION>, |
| 1344 |
GenericCreatePrototype<PlainYearMonthObject>, |
| 1345 |
PlainYearMonth_methods, |
| 1346 |
nullptr, |
| 1347 |
PlainYearMonth_prototype_methods, |
| 1348 |
PlainYearMonth_prototype_properties, |
| 1349 |
nullptr, |
| 1350 |
ClassSpec::DontDefineConstructor, |
| 1351 |
}; |