PlainMonthDay.h (3338B)
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 #ifndef builtin_temporal_PlainMonthDay_h 8 #define builtin_temporal_PlainMonthDay_h 9 10 #include "mozilla/Assertions.h" 11 #include "mozilla/Attributes.h" 12 13 #include <stdint.h> 14 15 #include "jstypes.h" 16 17 #include "builtin/temporal/Calendar.h" 18 #include "builtin/temporal/PlainDate.h" 19 #include "builtin/temporal/TemporalTypes.h" 20 #include "js/RootingAPI.h" 21 #include "js/TypeDecls.h" 22 #include "js/Value.h" 23 #include "vm/NativeObject.h" 24 25 class JS_PUBLIC_API JSTracer; 26 27 namespace js { 28 struct ClassSpec; 29 } 30 31 namespace js::temporal { 32 33 class PlainMonthDayObject : public NativeObject { 34 public: 35 static const JSClass class_; 36 static const JSClass& protoClass_; 37 38 static constexpr uint32_t PACKED_DATE_SLOT = 0; 39 static constexpr uint32_t CALENDAR_SLOT = 1; 40 static constexpr uint32_t SLOT_COUNT = 2; 41 42 /** 43 * Extract the date fields from this PlainDate object. 44 */ 45 ISODate date() const { 46 auto packed = PackedDate{getFixedSlot(PACKED_DATE_SLOT).toPrivateUint32()}; 47 return PackedDate::unpack(packed); 48 } 49 50 CalendarValue calendar() const { 51 return CalendarValue(getFixedSlot(CALENDAR_SLOT)); 52 } 53 54 private: 55 static const ClassSpec classSpec_; 56 }; 57 58 class MOZ_STACK_CLASS PlainMonthDay final { 59 ISODate date_; 60 CalendarValue calendar_; 61 62 public: 63 PlainMonthDay() = default; 64 65 PlainMonthDay(const ISODate& date, const CalendarValue& calendar) 66 : date_(date), calendar_(calendar) { 67 MOZ_ASSERT(ISODateWithinLimits(date)); 68 } 69 70 explicit PlainMonthDay(const PlainMonthDayObject* monthDay) 71 : PlainMonthDay(monthDay->date(), monthDay->calendar()) {} 72 73 const auto& date() const { return date_; } 74 const auto& calendar() const { return calendar_; } 75 76 // Allow implicit conversion to an ISODate. 77 operator const ISODate&() const { return date(); } 78 79 void trace(JSTracer* trc) { calendar_.trace(trc); } 80 81 const auto* calendarDoNotUse() const { return &calendar_; } 82 }; 83 84 /** 85 * CreateTemporalMonthDay ( isoDate, calendar [ , newTarget ] ) 86 */ 87 PlainMonthDayObject* CreateTemporalMonthDay(JSContext* cx, 88 JS::Handle<PlainMonthDay> monthDay); 89 90 /** 91 * CreateTemporalMonthDay ( isoDate, calendar [ , newTarget ] ) 92 */ 93 bool CreateTemporalMonthDay(JSContext* cx, const ISODate& isoDate, 94 JS::Handle<CalendarValue> calendar, 95 JS::MutableHandle<PlainMonthDay> result); 96 97 } /* namespace js::temporal */ 98 99 namespace js { 100 101 template <typename Wrapper> 102 class WrappedPtrOperations<temporal::PlainMonthDay, Wrapper> { 103 const auto& container() const { 104 return static_cast<const Wrapper*>(this)->get(); 105 } 106 107 public: 108 const auto& date() const { return container().date(); } 109 110 JS::Handle<temporal::CalendarValue> calendar() const { 111 return JS::Handle<temporal::CalendarValue>::fromMarkedLocation( 112 container().calendarDoNotUse()); 113 } 114 115 // Allow implicit conversion to an ISODate. 116 operator const temporal::ISODate&() const { return date(); } 117 }; 118 119 } // namespace js 120 121 #endif /* builtin_temporal_PlainMonthDay_h */