| 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/PlainMonthDay.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/PlainDate.h" |
| 21 |
#include "builtin/temporal/PlainDateTime.h" |
| 22 |
#include "builtin/temporal/PlainYearMonth.h" |
| 23 |
#include "builtin/temporal/Temporal.h" |
| 24 |
#include "builtin/temporal/TemporalParser.h" |
| 25 |
#include "builtin/temporal/TemporalTypes.h" |
| 26 |
#include "builtin/temporal/ToString.h" |
| 27 |
#include "gc/AllocKind.h" |
| 28 |
#include "gc/Barrier.h" |
| 29 |
#include "gc/GCEnum.h" |
| 30 |
#include "js/CallArgs.h" |
| 31 |
#include "js/CallNonGenericMethod.h" |
| 32 |
#include "js/Class.h" |
| 33 |
#include "js/ErrorReport.h" |
| 34 |
#include "js/friend/ErrorMessages.h" |
| 35 |
#include "js/PropertyDescriptor.h" |
| 36 |
#include "js/PropertySpec.h" |
| 37 |
#include "js/RootingAPI.h" |
| 38 |
#include "js/TypeDecls.h" |
| 39 |
#include "js/Value.h" |
| 40 |
#include "vm/BytecodeUtil.h" |
| 41 |
#include "vm/GlobalObject.h" |
| 42 |
#include "vm/JSAtomState.h" |
| 43 |
#include "vm/JSContext.h" |
| 44 |
#include "vm/JSObject.h" |
| 45 |
#include "vm/PlainObject.h" |
| 46 |
#include "vm/StringType.h" |
| 47 |
|
| 48 |
#include "vm/JSObject-inl.h" |
| 49 |
#include "vm/NativeObject-inl.h" |
| 50 |
|
| 51 |
using namespace js; |
| 52 |
using namespace js::temporal; |
| 53 |
|
| 54 |
static inline bool IsPlainMonthDay(Handle<Value> v) { |
| 55 |
return v.isObject() && v.toObject().is<PlainMonthDayObject>(); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* CreateTemporalMonthDay ( isoDate, calendar [ , newTarget ] ) |
| 60 |
*/ |
| 61 |
static PlainMonthDayObject* CreateTemporalMonthDay( |
| 62 |
JSContext* cx, const CallArgs& args, const ISODate& isoDate, |
| 63 |
Handle<CalendarValue> calendar) { |
| 64 |
// Step 1. |
| 65 |
if (!ISODateWithinLimits(isoDate)) { |
| 66 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 67 |
JSMSG_TEMPORAL_PLAIN_MONTH_DAY_INVALID); |
| 68 |
return nullptr; |
| 69 |
} |
| 70 |
|
| 71 |
// Steps 2-3. |
| 72 |
Rooted<JSObject*> proto(cx); |
| 73 |
if (!GetPrototypeFromBuiltinConstructor(cx, args, JSProto_PlainMonthDay, |
| 74 |
&proto)) { |
| 75 |
return nullptr; |
| 76 |
} |
| 77 |
|
| 78 |
auto* object = NewObjectWithClassProto<PlainMonthDayObject>(cx, proto); |
| 79 |
if (!object) { |
| 80 |
return nullptr; |
| 81 |
} |
| 82 |
|
| 83 |
// Step 4. |
| 84 |
auto packedDate = PackedDate::pack(isoDate); |
| 85 |
object->setFixedSlot(PlainMonthDayObject::PACKED_DATE_SLOT, |
| 86 |
PrivateUint32Value(packedDate.value)); |
| 87 |
|
| 88 |
// Step 5. |
| 89 |
object->setFixedSlot(PlainMonthDayObject::CALENDAR_SLOT, |
| 90 |
calendar.toSlotValue()); |
| 91 |
|
| 92 |
// Step 6. |
| 93 |
return object; |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* CreateTemporalMonthDay ( isoDate, calendar [ , newTarget ] ) |
| 98 |
*/ |
| 99 |
PlainMonthDayObject* js::temporal::CreateTemporalMonthDay( |
| 100 |
JSContext* cx, Handle<PlainMonthDay> monthDay) { |
| 101 |
MOZ_ASSERT(IsValidISODate(monthDay)); |
| 102 |
|
| 103 |
// Step 1. |
| 104 |
MOZ_ASSERT(ISODateWithinLimits(monthDay)); |
| 105 |
|
| 106 |
// Steps 2-3. |
| 107 |
auto* object = NewBuiltinClassInstance<PlainMonthDayObject>(cx); |
| 108 |
if (!object) { |
| 109 |
return nullptr; |
| 110 |
} |
| 111 |
|
| 112 |
// Step 4. |
| 113 |
auto packedDate = PackedDate::pack(monthDay); |
| 114 |
object->setFixedSlot(PlainMonthDayObject::PACKED_DATE_SLOT, |
| 115 |
PrivateUint32Value(packedDate.value)); |
| 116 |
|
| 117 |
// Step 5. |
| 118 |
object->setFixedSlot(PlainMonthDayObject::CALENDAR_SLOT, |
| 119 |
monthDay.calendar().toSlotValue()); |
| 120 |
|
| 121 |
// Step 6. |
| 122 |
return object; |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* CreateTemporalMonthDay ( isoDate, calendar [ , newTarget ] ) |
| 127 |
*/ |
| 128 |
bool js::temporal::CreateTemporalMonthDay(JSContext* cx, const ISODate& isoDate, |
| 129 |
Handle<CalendarValue> calendar, |
| 130 |
MutableHandle<PlainMonthDay> result) { |
| 131 |
MOZ_ASSERT(IsValidISODate(isoDate)); |
| 132 |
|
| 133 |
// Step 1. |
| 134 |
if (!ISODateWithinLimits(isoDate)) { |
| 135 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 136 |
JSMSG_TEMPORAL_PLAIN_MONTH_DAY_INVALID); |
| 137 |
return false; |
| 138 |
} |
| 139 |
|
| 140 |
// Steps 2-6. |
| 141 |
result.set(PlainMonthDay{isoDate, calendar}); |
| 142 |
return true; |
| 143 |
} |
| 144 |
|
| 145 |
struct MonthDayOptions { |
| 146 |
TemporalOverflow overflow = TemporalOverflow::Constrain; |
| 147 |
}; |
| 148 |
|
| 149 |
/** |
| 150 |
* ToTemporalMonthDay ( item [ , options ] ) |
| 151 |
*/ |
| 152 |
static bool ToTemporalMonthDayOptions(JSContext* cx, Handle<Value> options, |
| 153 |
MonthDayOptions* result) { |
| 154 |
if (options.isUndefined()) { |
| 155 |
*result = {}; |
| 156 |
return true; |
| 157 |
} |
| 158 |
|
| 159 |
// NOTE: |options| are only passed from `Temporal.PlainMonthDay.from`. |
| 160 |
|
| 161 |
Rooted<JSObject*> resolvedOptions( |
| 162 |
cx, RequireObjectArg(cx, "options", "from", options)); |
| 163 |
if (!resolvedOptions) { |
| 164 |
return false; |
| 165 |
} |
| 166 |
|
| 167 |
auto overflow = TemporalOverflow::Constrain; |
| 168 |
if (!GetTemporalOverflowOption(cx, resolvedOptions, &overflow)) { |
| 169 |
return false; |
| 170 |
} |
| 171 |
|
| 172 |
*result = {overflow}; |
| 173 |
return true; |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* ToTemporalMonthDay ( item [ , options ] ) |
| 178 |
*/ |
| 179 |
static bool ToTemporalMonthDay(JSContext* cx, Handle<JSObject*> item, |
| 180 |
Handle<Value> options, |
| 181 |
MutableHandle<PlainMonthDay> result) { |
| 182 |
// Step 2.a. |
| 183 |
if (auto* plainMonthDay = item->maybeUnwrapIf<PlainMonthDayObject>()) { |
| 184 |
auto date = plainMonthDay->date(); |
| 185 |
Rooted<CalendarValue> calendar(cx, plainMonthDay->calendar()); |
| 186 |
if (!calendar.wrap(cx)) { |
| 187 |
return false; |
| 188 |
} |
| 189 |
|
| 190 |
// Steps 2.a.i-ii. |
| 191 |
MonthDayOptions ignoredOptions; |
| 192 |
if (!ToTemporalMonthDayOptions(cx, options, &ignoredOptions)) { |
| 193 |
return false; |
| 194 |
} |
| 195 |
|
| 196 |
// Step 2.a.iii. |
| 197 |
result.set(PlainMonthDay{date, calendar}); |
| 198 |
return true; |
| 199 |
} |
| 200 |
|
| 201 |
// Step 2.b. |
| 202 |
Rooted<CalendarValue> calendar(cx); |
| 203 |
if (!GetTemporalCalendarWithISODefault(cx, item, &calendar)) { |
| 204 |
return false; |
| 205 |
} |
| 206 |
|
| 207 |
// Step 2.c. |
| 208 |
Rooted<CalendarFields> fields(cx); |
| 209 |
if (!PrepareCalendarFields(cx, calendar, item, |
| 210 |
{ |
| 211 |
CalendarField::Year, |
| 212 |
CalendarField::Month, |
| 213 |
CalendarField::MonthCode, |
| 214 |
CalendarField::Day, |
| 215 |
}, |
| 216 |
&fields)) { |
| 217 |
return false; |
| 218 |
} |
| 219 |
|
| 220 |
// Steps 2.d-e. |
| 221 |
MonthDayOptions resolvedOptions; |
| 222 |
if (!ToTemporalMonthDayOptions(cx, options, &resolvedOptions)) { |
| 223 |
return false; |
| 224 |
} |
| 225 |
auto [overflow] = resolvedOptions; |
| 226 |
|
| 227 |
// Step 2.f. |
| 228 |
return CalendarMonthDayFromFields(cx, calendar, fields, overflow, result); |
| 229 |
} |
| 230 |
|
| 231 |
/** |
| 232 |
* ToTemporalMonthDay ( item [ , options ] ) |
| 233 |
*/ |
| 234 |
static bool ToTemporalMonthDay(JSContext* cx, Handle<Value> item, |
| 235 |
Handle<Value> options, |
| 236 |
MutableHandle<PlainMonthDay> result) { |
| 237 |
// Step 1. (Not applicable in our implementation.) |
| 238 |
|
| 239 |
// Step 2. |
| 240 |
if (item.isObject()) { |
| 241 |
Rooted<JSObject*> itemObj(cx, &item.toObject()); |
| 242 |
return ToTemporalMonthDay(cx, itemObj, options, result); |
| 243 |
} |
| 244 |
|
| 245 |
// Step 3. |
| 246 |
if (!item.isString()) { |
| 247 |
ReportValueError(cx, JSMSG_UNEXPECTED_TYPE, JSDVG_IGNORE_STACK, item, |
| 248 |
nullptr, "not a string"); |
| 249 |
return false; |
| 250 |
} |
| 251 |
Rooted<JSString*> string(cx, item.toString()); |
| 252 |
|
| 253 |
// Step 4. |
| 254 |
ISODate date; |
| 255 |
bool hasYear; |
| 256 |
Rooted<JSString*> calendarString(cx); |
| 257 |
if (!ParseTemporalMonthDayString(cx, string, &date, &hasYear, |
| 258 |
&calendarString)) { |
| 259 |
return false; |
| 260 |
} |
| 261 |
|
| 262 |
// Steps 5-7. |
| 263 |
Rooted<CalendarValue> calendar(cx, CalendarValue(CalendarId::ISO8601)); |
| 264 |
if (calendarString) { |
| 265 |
if (!CanonicalizeCalendar(cx, calendarString, &calendar)) { |
| 266 |
return false; |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
// Steps 8-9. |
| 271 |
MonthDayOptions ignoredOptions; |
| 272 |
if (!ToTemporalMonthDayOptions(cx, options, &ignoredOptions)) { |
| 273 |
return false; |
| 274 |
} |
| 275 |
|
| 276 |
// Step 10. |
| 277 |
if (calendar.identifier() == CalendarId::ISO8601) { |
| 278 |
// Step 10.a. |
| 279 |
constexpr int32_t referenceISOYear = 1972; |
| 280 |
|
| 281 |
// Step 10.b. |
| 282 |
auto isoDate = ISODate{referenceISOYear, date.month, date.day}; |
| 283 |
|
| 284 |
// Step 10.c. |
| 285 |
return CreateTemporalMonthDay(cx, isoDate, calendar, result); |
| 286 |
} |
| 287 |
|
| 288 |
// Steps 11-12. |
| 289 |
Rooted<PlainMonthDay> monthDay(cx); |
| 290 |
if (!CreateTemporalMonthDay(cx, date, calendar, &monthDay)) { |
| 291 |
return false; |
| 292 |
} |
| 293 |
|
| 294 |
// Step 13. |
| 295 |
Rooted<CalendarFields> fields(cx); |
| 296 |
if (!ISODateToFields(cx, monthDay, &fields)) { |
| 297 |
return false; |
| 298 |
} |
| 299 |
|
| 300 |
// Steps 14-15. |
| 301 |
return CalendarMonthDayFromFields(cx, calendar, fields, |
| 302 |
TemporalOverflow::Constrain, result); |
| 303 |
} |
| 304 |
|
| 305 |
/** |
| 306 |
* ToTemporalMonthDay ( item [ , options ] ) |
| 307 |
*/ |
| 308 |
static bool ToTemporalMonthDay(JSContext* cx, Handle<Value> item, |
| 309 |
MutableHandle<PlainMonthDay> result) { |
| 310 |
return ToTemporalMonthDay(cx, item, UndefinedHandleValue, result); |
| 311 |
} |
| 312 |
|
| 313 |
/** |
| 314 |
* Temporal.PlainMonthDay ( isoMonth, isoDay [ , calendar [ , referenceISOYear ] |
| 315 |
* ] ) |
| 316 |
*/ |
| 317 |
static bool PlainMonthDayConstructor(JSContext* cx, unsigned argc, Value* vp) { |
| 318 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 319 |
|
| 320 |
// Step 1. |
| 321 |
if (!ThrowIfNotConstructing(cx, args, "Temporal.PlainMonthDay")) { |
| 322 |
return false; |
| 323 |
} |
| 324 |
|
| 325 |
// Step 3. |
| 326 |
double isoMonth; |
| 327 |
if (!ToIntegerWithTruncation(cx, args.get(0), "month", &isoMonth)) { |
| 328 |
return false; |
| 329 |
} |
| 330 |
|
| 331 |
// Step 4. |
| 332 |
double isoDay; |
| 333 |
if (!ToIntegerWithTruncation(cx, args.get(1), "day", &isoDay)) { |
| 334 |
return false; |
| 335 |
} |
| 336 |
|
| 337 |
// Steps 5-7. |
| 338 |
Rooted<CalendarValue> calendar(cx, CalendarValue(CalendarId::ISO8601)); |
| 339 |
if (args.hasDefined(2)) { |
| 340 |
// Step 6. |
| 341 |
if (!args[2].isString()) { |
| 342 |
ReportValueError(cx, JSMSG_UNEXPECTED_TYPE, JSDVG_IGNORE_STACK, args[2], |
| 343 |
nullptr, "not a string"); |
| 344 |
return false; |
| 345 |
} |
| 346 |
|
| 347 |
// Step 7. |
| 348 |
Rooted<JSString*> calendarString(cx, args[2].toString()); |
| 349 |
if (!CanonicalizeCalendar(cx, calendarString, &calendar)) { |
| 350 |
return false; |
| 351 |
} |
| 352 |
} |
| 353 |
|
| 354 |
// Steps 2 and 8. |
| 355 |
double isoYear = 1972; |
| 356 |
if (args.hasDefined(3)) { |
| 357 |
if (!ToIntegerWithTruncation(cx, args[3], "year", &isoYear)) { |
| 358 |
return false; |
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
// Step 9. |
| 363 |
if (!ThrowIfInvalidISODate(cx, isoYear, isoMonth, isoDay)) { |
| 364 |
return false; |
| 365 |
} |
| 366 |
|
| 367 |
// Step 10. |
| 368 |
auto isoDate = ISODate{int32_t(isoYear), int32_t(isoMonth), int32_t(isoDay)}; |
| 369 |
|
| 370 |
// Step 9. |
| 371 |
auto* monthDay = CreateTemporalMonthDay(cx, args, isoDate, calendar); |
| 372 |
if (!monthDay) { |
| 373 |
return false; |
| 374 |
} |
| 375 |
|
| 376 |
args.rval().setObject(*monthDay); |
| 377 |
return true; |
| 378 |
} |
| 379 |
|
| 380 |
/** |
| 381 |
* Temporal.PlainMonthDay.from ( item [ , options ] ) |
| 382 |
*/ |
| 383 |
static bool PlainMonthDay_from(JSContext* cx, unsigned argc, Value* vp) { |
| 384 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 385 |
|
| 386 |
// Step 1. |
| 387 |
Rooted<PlainMonthDay> monthDay(cx); |
| 388 |
if (!ToTemporalMonthDay(cx, args.get(0), args.get(1), &monthDay)) { |
| 389 |
return false; |
| 390 |
} |
| 391 |
|
| 392 |
auto* result = CreateTemporalMonthDay(cx, monthDay); |
| 393 |
if (!result) { |
| 394 |
return false; |
| 395 |
} |
| 396 |
|
| 397 |
args.rval().setObject(*result); |
| 398 |
return true; |
| 399 |
} |
| 400 |
|
| 401 |
/** |
| 402 |
* get Temporal.PlainMonthDay.prototype.calendarId |
| 403 |
*/ |
| 404 |
static bool PlainMonthDay_calendarId(JSContext* cx, const CallArgs& args) { |
| 405 |
auto* monthDay = &args.thisv().toObject().as<PlainMonthDayObject>(); |
| 406 |
|
| 407 |
// Step 3. |
| 408 |
auto* str = |
| 409 |
NewStringCopy<CanGC>(cx, CalendarIdentifier(monthDay->calendar())); |
| 410 |
if (!str) { |
| 411 |
return false; |
| 412 |
} |
| 413 |
|
| 414 |
args.rval().setString(str); |
| 415 |
return true; |
| 416 |
} |
| 417 |
|
| 418 |
/** |
| 419 |
* get Temporal.PlainMonthDay.prototype.calendarId |
| 420 |
*/ |
| 421 |
static bool PlainMonthDay_calendarId(JSContext* cx, unsigned argc, Value* vp) { |
| 422 |
// Steps 1-2. |
| 423 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 424 |
return CallNonGenericMethod<IsPlainMonthDay, PlainMonthDay_calendarId>(cx, |
| 425 |
args); |
| 426 |
} |
| 427 |
|
| 428 |
/** |
| 429 |
* get Temporal.PlainMonthDay.prototype.monthCode |
| 430 |
*/ |
| 431 |
static bool PlainMonthDay_monthCode(JSContext* cx, const CallArgs& args) { |
| 432 |
auto* monthDay = &args.thisv().toObject().as<PlainMonthDayObject>(); |
| 433 |
Rooted<CalendarValue> calendar(cx, monthDay->calendar()); |
| 434 |
|
| 435 |
// Step 3. |
| 436 |
return CalendarMonthCode(cx, calendar, monthDay->date(), args.rval()); |
| 437 |
} |
| 438 |
|
| 439 |
/** |
| 440 |
* get Temporal.PlainMonthDay.prototype.monthCode |
| 441 |
*/ |
| 442 |
static bool PlainMonthDay_monthCode(JSContext* cx, unsigned argc, Value* vp) { |
| 443 |
// Steps 1-2. |
| 444 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 445 |
return CallNonGenericMethod<IsPlainMonthDay, PlainMonthDay_monthCode>(cx, |
| 446 |
args); |
| 447 |
} |
| 448 |
|
| 449 |
/** |
| 450 |
* get Temporal.PlainMonthDay.prototype.day |
| 451 |
*/ |
| 452 |
static bool PlainMonthDay_day(JSContext* cx, const CallArgs& args) { |
| 453 |
auto* monthDay = &args.thisv().toObject().as<PlainMonthDayObject>(); |
| 454 |
Rooted<CalendarValue> calendar(cx, monthDay->calendar()); |
| 455 |
|
| 456 |
// Step 3. |
| 457 |
return CalendarDay(cx, calendar, monthDay->date(), args.rval()); |
| 458 |
} |
| 459 |
|
| 460 |
/** |
| 461 |
* get Temporal.PlainMonthDay.prototype.day |
| 462 |
*/ |
| 463 |
static bool PlainMonthDay_day(JSContext* cx, unsigned argc, Value* vp) { |
| 464 |
// Steps 1-2. |
| 465 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 466 |
return CallNonGenericMethod<IsPlainMonthDay, PlainMonthDay_day>(cx, args); |
| 467 |
} |
| 468 |
|
| 469 |
/** |
| 470 |
* Temporal.PlainMonthDay.prototype.with ( temporalMonthDayLike [ , options ] ) |
| 471 |
*/ |
| 472 |
static bool PlainMonthDay_with(JSContext* cx, const CallArgs& args) { |
| 473 |
Rooted<PlainMonthDay> monthDay( |
| 474 |
cx, &args.thisv().toObject().as<PlainMonthDayObject>()); |
| 475 |
|
| 476 |
// Step 3. |
| 477 |
Rooted<JSObject*> temporalMonthDayLike( |
| 478 |
cx, RequireObjectArg(cx, "temporalMonthDayLike", "with", args.get(0))); |
| 479 |
if (!temporalMonthDayLike) { |
| 480 |
return false; |
| 481 |
} |
| 482 |
if (!ThrowIfTemporalLikeObject(cx, temporalMonthDayLike)) { |
| 483 |
return false; |
| 484 |
} |
| 485 |
|
| 486 |
// Step 4. |
| 487 |
auto calendar = monthDay.calendar(); |
| 488 |
|
| 489 |
// Step 5. |
| 490 |
Rooted<CalendarFields> fields(cx); |
| 491 |
if (!ISODateToFields(cx, monthDay, &fields)) { |
| 492 |
return false; |
| 493 |
} |
| 494 |
|
| 495 |
// Step 6. |
| 496 |
Rooted<CalendarFields> partialMonthDay(cx); |
| 497 |
if (!PreparePartialCalendarFields(cx, calendar, temporalMonthDayLike, |
| 498 |
{ |
| 499 |
CalendarField::Year, |
| 500 |
CalendarField::Month, |
| 501 |
CalendarField::MonthCode, |
| 502 |
CalendarField::Day, |
| 503 |
}, |
| 504 |
&partialMonthDay)) { |
| 505 |
return false; |
| 506 |
} |
| 507 |
MOZ_ASSERT(!partialMonthDay.keys().isEmpty()); |
| 508 |
|
| 509 |
// Step 7. |
| 510 |
fields = CalendarMergeFields(calendar, fields, partialMonthDay); |
| 511 |
|
| 512 |
// Steps 8-9. |
| 513 |
auto overflow = TemporalOverflow::Constrain; |
| 514 |
if (args.hasDefined(1)) { |
| 515 |
// Step 8. |
| 516 |
Rooted<JSObject*> options(cx, |
| 517 |
RequireObjectArg(cx, "options", "with", args[1])); |
| 518 |
if (!options) { |
| 519 |
return false; |
| 520 |
} |
| 521 |
|
| 522 |
// Step 9. |
| 523 |
if (!GetTemporalOverflowOption(cx, options, &overflow)) { |
| 524 |
return false; |
| 525 |
} |
| 526 |
} |
| 527 |
|
| 528 |
// Step 10. |
| 529 |
Rooted<PlainMonthDay> result(cx); |
| 530 |
if (!CalendarMonthDayFromFields(cx, calendar, fields, overflow, &result)) { |
| 531 |
return false; |
| 532 |
} |
| 533 |
MOZ_ASSERT(ISODateWithinLimits(result)); |
| 534 |
|
| 535 |
// Step 11. |
| 536 |
auto* obj = CreateTemporalMonthDay(cx, result); |
| 537 |
if (!obj) { |
| 538 |
return false; |
| 539 |
} |
| 540 |
|
| 541 |
args.rval().setObject(*obj); |
| 542 |
return true; |
| 543 |
} |
| 544 |
|
| 545 |
/** |
| 546 |
* Temporal.PlainMonthDay.prototype.with ( temporalMonthDayLike [ , options ] ) |
| 547 |
*/ |
| 548 |
static bool PlainMonthDay_with(JSContext* cx, unsigned argc, Value* vp) { |
| 549 |
// Steps 1-2. |
| 550 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 551 |
return CallNonGenericMethod<IsPlainMonthDay, PlainMonthDay_with>(cx, args); |
| 552 |
} |
| 553 |
|
| 554 |
/** |
| 555 |
* Temporal.PlainMonthDay.prototype.equals ( other ) |
| 556 |
*/ |
| 557 |
static bool PlainMonthDay_equals(JSContext* cx, const CallArgs& args) { |
| 558 |
auto* monthDay = &args.thisv().toObject().as<PlainMonthDayObject>(); |
| 559 |
auto date = monthDay->date(); |
| 560 |
Rooted<CalendarValue> calendar(cx, monthDay->calendar()); |
| 561 |
|
| 562 |
// Step 3. |
| 563 |
Rooted<PlainMonthDay> other(cx); |
| 564 |
if (!ToTemporalMonthDay(cx, args.get(0), &other)) { |
| 565 |
return false; |
| 566 |
} |
| 567 |
|
| 568 |
// Steps 4-7. |
| 569 |
bool equals = |
| 570 |
date == other.date() && CalendarEquals(calendar, other.calendar()); |
| 571 |
|
| 572 |
args.rval().setBoolean(equals); |
| 573 |
return true; |
| 574 |
} |
| 575 |
|
| 576 |
/** |
| 577 |
* Temporal.PlainMonthDay.prototype.equals ( other ) |
| 578 |
*/ |
| 579 |
static bool PlainMonthDay_equals(JSContext* cx, unsigned argc, Value* vp) { |
| 580 |
// Steps 1-2. |
| 581 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 582 |
return CallNonGenericMethod<IsPlainMonthDay, PlainMonthDay_equals>(cx, args); |
| 583 |
} |
| 584 |
|
| 585 |
/** |
| 586 |
* Temporal.PlainMonthDay.prototype.toString ( [ options ] ) |
| 587 |
*/ |
| 588 |
static bool PlainMonthDay_toString(JSContext* cx, const CallArgs& args) { |
| 589 |
Rooted<PlainMonthDayObject*> monthDay( |
| 590 |
cx, &args.thisv().toObject().as<PlainMonthDayObject>()); |
| 591 |
|
| 592 |
auto showCalendar = ShowCalendar::Auto; |
| 593 |
if (args.hasDefined(0)) { |
| 594 |
// Step 3. |
| 595 |
Rooted<JSObject*> options( |
| 596 |
cx, RequireObjectArg(cx, "options", "toString", args[0])); |
| 597 |
if (!options) { |
| 598 |
return false; |
| 599 |
} |
| 600 |
|
| 601 |
// Step 4. |
| 602 |
if (!GetTemporalShowCalendarNameOption(cx, options, &showCalendar)) { |
| 603 |
return false; |
| 604 |
} |
| 605 |
} |
| 606 |
|
| 607 |
// Step 5. |
| 608 |
JSString* str = TemporalMonthDayToString(cx, monthDay, showCalendar); |
| 609 |
if (!str) { |
| 610 |
return false; |
| 611 |
} |
| 612 |
|
| 613 |
args.rval().setString(str); |
| 614 |
return true; |
| 615 |
} |
| 616 |
|
| 617 |
/** |
| 618 |
* Temporal.PlainMonthDay.prototype.toString ( [ options ] ) |
| 619 |
*/ |
| 620 |
static bool PlainMonthDay_toString(JSContext* cx, unsigned argc, Value* vp) { |
| 621 |
// Steps 1-2. |
| 622 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 623 |
return CallNonGenericMethod<IsPlainMonthDay, PlainMonthDay_toString>(cx, |
| 624 |
args); |
| 625 |
} |
| 626 |
|
| 627 |
/** |
| 628 |
* Temporal.PlainMonthDay.prototype.toLocaleString ( [ locales [ , options ] ] ) |
| 629 |
*/ |
| 630 |
static bool PlainMonthDay_toLocaleString(JSContext* cx, const CallArgs& args) { |
| 631 |
// Steps 3-4. |
| 632 |
Handle<PropertyName*> required = cx->names().date; |
| 633 |
Handle<PropertyName*> defaults = cx->names().date; |
| 634 |
return TemporalObjectToLocaleString(cx, args, required, defaults); |
| 635 |
} |
| 636 |
|
| 637 |
/** |
| 638 |
* Temporal.PlainMonthDay.prototype.toLocaleString ( [ locales [ , options ] ] ) |
| 639 |
*/ |
| 640 |
static bool PlainMonthDay_toLocaleString(JSContext* cx, unsigned argc, |
| 641 |
Value* vp) { |
| 642 |
// Steps 1-2. |
| 643 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 644 |
return CallNonGenericMethod<IsPlainMonthDay, PlainMonthDay_toLocaleString>( |
| 645 |
cx, args); |
| 646 |
} |
| 647 |
|
| 648 |
/** |
| 649 |
* Temporal.PlainMonthDay.prototype.toJSON ( ) |
| 650 |
*/ |
| 651 |
static bool PlainMonthDay_toJSON(JSContext* cx, const CallArgs& args) { |
| 652 |
Rooted<PlainMonthDayObject*> monthDay( |
| 653 |
cx, &args.thisv().toObject().as<PlainMonthDayObject>()); |
| 654 |
|
| 655 |
// Step 3. |
| 656 |
JSString* str = TemporalMonthDayToString(cx, monthDay, ShowCalendar::Auto); |
| 657 |
if (!str) { |
| 658 |
return false; |
| 659 |
} |
| 660 |
|
| 661 |
args.rval().setString(str); |
| 662 |
return true; |
| 663 |
} |
| 664 |
|
| 665 |
/** |
| 666 |
* Temporal.PlainMonthDay.prototype.toJSON ( ) |
| 667 |
*/ |
| 668 |
static bool PlainMonthDay_toJSON(JSContext* cx, unsigned argc, Value* vp) { |
| 669 |
// Steps 1-2. |
| 670 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 671 |
return CallNonGenericMethod<IsPlainMonthDay, PlainMonthDay_toJSON>(cx, args); |
| 672 |
} |
| 673 |
|
| 674 |
/** |
| 675 |
* Temporal.PlainMonthDay.prototype.valueOf ( ) |
| 676 |
*/ |
| 677 |
static bool PlainMonthDay_valueOf(JSContext* cx, unsigned argc, Value* vp) { |
| 678 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, JSMSG_CANT_CONVERT_TO, |
| 679 |
"PlainMonthDay", "primitive type"); |
| 680 |
return false; |
| 681 |
} |
| 682 |
|
| 683 |
/** |
| 684 |
* Temporal.PlainMonthDay.prototype.toPlainDate ( item ) |
| 685 |
*/ |
| 686 |
static bool PlainMonthDay_toPlainDate(JSContext* cx, const CallArgs& args) { |
| 687 |
Rooted<PlainMonthDay> monthDay( |
| 688 |
cx, &args.thisv().toObject().as<PlainMonthDayObject>()); |
| 689 |
|
| 690 |
// Step 3. |
| 691 |
Rooted<JSObject*> item( |
| 692 |
cx, RequireObjectArg(cx, "item", "toPlainDate", args.get(0))); |
| 693 |
if (!item) { |
| 694 |
return false; |
| 695 |
} |
| 696 |
|
| 697 |
// Step 4. |
| 698 |
auto calendar = monthDay.calendar(); |
| 699 |
|
| 700 |
// Step 5. |
| 701 |
Rooted<CalendarFields> fields(cx); |
| 702 |
if (!ISODateToFields(cx, monthDay, &fields)) { |
| 703 |
return false; |
| 704 |
} |
| 705 |
|
| 706 |
// Step 6. |
| 707 |
Rooted<CalendarFields> inputFields(cx); |
| 708 |
if (!PrepareCalendarFields(cx, calendar, item, |
| 709 |
{ |
| 710 |
CalendarField::Year, |
| 711 |
}, |
| 712 |
&inputFields)) { |
| 713 |
return false; |
| 714 |
} |
| 715 |
|
| 716 |
// Step 7. |
| 717 |
fields = CalendarMergeFields(calendar, fields, inputFields); |
| 718 |
|
| 719 |
// Step 8. |
| 720 |
Rooted<PlainDate> date(cx); |
| 721 |
if (!CalendarDateFromFields(cx, calendar, fields, TemporalOverflow::Constrain, |
| 722 |
&date)) { |
| 723 |
return false; |
| 724 |
} |
| 725 |
MOZ_ASSERT(ISODateWithinLimits(date)); |
| 726 |
|
| 727 |
// Step 9. |
| 728 |
auto* obj = CreateTemporalDate(cx, date); |
| 729 |
if (!obj) { |
| 730 |
return false; |
| 731 |
} |
| 732 |
|
| 733 |
args.rval().setObject(*obj); |
| 734 |
return true; |
| 735 |
} |
| 736 |
|
| 737 |
/** |
| 738 |
* Temporal.PlainMonthDay.prototype.toPlainDate ( item ) |
| 739 |
*/ |
| 740 |
static bool PlainMonthDay_toPlainDate(JSContext* cx, unsigned argc, Value* vp) { |
| 741 |
// Steps 1-2. |
| 742 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 743 |
return CallNonGenericMethod<IsPlainMonthDay, PlainMonthDay_toPlainDate>(cx, |
| 744 |
args); |
| 745 |
} |
| 746 |
|
| 747 |
const JSClass PlainMonthDayObject::class_ = { |
| 748 |
"Temporal.PlainMonthDay", |
| 749 |
JSCLASS_HAS_RESERVED_SLOTS(PlainMonthDayObject::SLOT_COUNT) | |
| 750 |
JSCLASS_HAS_CACHED_PROTO(JSProto_PlainMonthDay), |
| 751 |
JS_NULL_CLASS_OPS, |
| 752 |
&PlainMonthDayObject::classSpec_, |
| 753 |
}; |
| 754 |
|
| 755 |
const JSClass& PlainMonthDayObject::protoClass_ = PlainObject::class_; |
| 756 |
|
| 757 |
static const JSFunctionSpec PlainMonthDay_methods[] = { |
| 758 |
JS_FN("from", PlainMonthDay_from, 1, 0), |
| 759 |
JS_FS_END, |
| 760 |
}; |
| 761 |
|
| 762 |
static const JSFunctionSpec PlainMonthDay_prototype_methods[] = { |
| 763 |
JS_FN("with", PlainMonthDay_with, 1, 0), |
| 764 |
JS_FN("equals", PlainMonthDay_equals, 1, 0), |
| 765 |
JS_FN("toString", PlainMonthDay_toString, 0, 0), |
| 766 |
JS_FN("toLocaleString", PlainMonthDay_toLocaleString, 0, 0), |
| 767 |
JS_FN("toJSON", PlainMonthDay_toJSON, 0, 0), |
| 768 |
JS_FN("valueOf", PlainMonthDay_valueOf, 0, 0), |
| 769 |
JS_FN("toPlainDate", PlainMonthDay_toPlainDate, 1, 0), |
| 770 |
JS_FS_END, |
| 771 |
}; |
| 772 |
|
| 773 |
static const JSPropertySpec PlainMonthDay_prototype_properties[] = { |
| 774 |
JS_PSG("calendarId", PlainMonthDay_calendarId, 0), |
| 775 |
JS_PSG("monthCode", PlainMonthDay_monthCode, 0), |
| 776 |
JS_PSG("day", PlainMonthDay_day, 0), |
| 777 |
JS_STRING_SYM_PS(toStringTag, "Temporal.PlainMonthDay", JSPROP_READONLY), |
| 778 |
JS_PS_END, |
| 779 |
}; |
| 780 |
|
| 781 |
const ClassSpec PlainMonthDayObject::classSpec_ = { |
| 782 |
GenericCreateConstructor<PlainMonthDayConstructor, 2, |
| 783 |
gc::AllocKind::FUNCTION>, |
| 784 |
GenericCreatePrototype<PlainMonthDayObject>, |
| 785 |
PlainMonthDay_methods, |
| 786 |
nullptr, |
| 787 |
PlainMonthDay_prototype_methods, |
| 788 |
PlainMonthDay_prototype_properties, |
| 789 |
nullptr, |
| 790 |
ClassSpec::DontDefineConstructor, |
| 791 |
}; |