PlainYearMonth.h (3344B)
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_PlainYearMonth_h 8 #define builtin_temporal_PlainYearMonth_h 9 10 #include "mozilla/Assertions.h" 11 #include "mozilla/Attributes.h" 12 13 #include <stdint.h> 14 15 #include "builtin/temporal/Calendar.h" 16 #include "builtin/temporal/TemporalTypes.h" 17 #include "js/RootingAPI.h" 18 #include "js/TypeDecls.h" 19 #include "js/Value.h" 20 #include "vm/NativeObject.h" 21 22 namespace js { 23 struct ClassSpec; 24 } 25 26 namespace js::temporal { 27 28 class PlainYearMonthObject : public NativeObject { 29 public: 30 static const JSClass class_; 31 static const JSClass& protoClass_; 32 33 static constexpr uint32_t PACKED_DATE_SLOT = 0; 34 static constexpr uint32_t CALENDAR_SLOT = 1; 35 static constexpr uint32_t SLOT_COUNT = 2; 36 37 /** 38 * Extract the date fields from this PlainYearMonth object. 39 */ 40 ISODate date() const { 41 auto packed = PackedDate{getFixedSlot(PACKED_DATE_SLOT).toPrivateUint32()}; 42 return PackedDate::unpack(packed); 43 } 44 45 CalendarValue calendar() const { 46 return CalendarValue(getFixedSlot(CALENDAR_SLOT)); 47 } 48 49 private: 50 static const ClassSpec classSpec_; 51 }; 52 53 /** 54 * ISOYearMonthWithinLimits ( isoDate ) 55 */ 56 bool ISOYearMonthWithinLimits(const ISODate& isoDate); 57 58 class MOZ_STACK_CLASS PlainYearMonth final { 59 ISODate date_; 60 CalendarValue calendar_; 61 62 public: 63 PlainYearMonth() = default; 64 65 PlainYearMonth(const ISODate& date, const CalendarValue& calendar) 66 : date_(date), calendar_(calendar) { 67 MOZ_ASSERT(ISOYearMonthWithinLimits(date)); 68 } 69 70 explicit PlainYearMonth(const PlainYearMonthObject* yearMonth) 71 : PlainYearMonth(yearMonth->date(), yearMonth->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 * CreateTemporalYearMonth ( isoDate, calendar [ , newTarget ] ) 86 */ 87 PlainYearMonthObject* CreateTemporalYearMonth( 88 JSContext* cx, JS::Handle<PlainYearMonth> yearMonth); 89 90 /** 91 * CreateTemporalYearMonth ( isoDate, calendar [ , newTarget ] ) 92 */ 93 bool CreateTemporalYearMonth(JSContext* cx, const ISODate& isoDate, 94 JS::Handle<CalendarValue> calendar, 95 JS::MutableHandle<PlainYearMonth> result); 96 97 } /* namespace js::temporal */ 98 99 namespace js { 100 101 template <typename Wrapper> 102 class WrappedPtrOperations<temporal::PlainYearMonth, 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_PlainYearMonth_h */