Przeglądaj źródła

LibJS: Convert Date.prototype to be a PrototypeObject

Timothy Flynn 3 lat temu
rodzic
commit
94f076a774

+ 38 - 51
Userland/Libraries/LibJS/Runtime/DatePrototype.cpp

@@ -12,7 +12,6 @@
 #include <LibCore/DateTime.h>
 #include <LibCrypto/BigInt/UnsignedBigInteger.h>
 #include <LibJS/Runtime/BigInt.h>
-#include <LibJS/Runtime/Date.h>
 #include <LibJS/Runtime/DatePrototype.h>
 #include <LibJS/Runtime/Error.h>
 #include <LibJS/Runtime/GlobalObject.h>
@@ -21,20 +20,8 @@
 
 namespace JS {
 
-static Date* typed_this(VM& vm, GlobalObject& global_object)
-{
-    auto* this_object = vm.this_value(global_object).to_object(global_object);
-    if (!this_object)
-        return nullptr;
-    if (!is<Date>(this_object)) {
-        vm.throw_exception<TypeError>(global_object, ErrorType::NotAnObjectOfType, "Date");
-        return nullptr;
-    }
-    return static_cast<Date*>(this_object);
-}
-
 DatePrototype::DatePrototype(GlobalObject& global_object)
-    : Object(*global_object.object_prototype())
+    : PrototypeObject(*global_object.object_prototype())
 {
 }
 
@@ -108,7 +95,7 @@ DatePrototype::~DatePrototype()
 // 21.4.4.2 Date.prototype.getDate ( ), https://tc39.es/ecma262/#sec-date.prototype.getdate
 JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_date)
 {
-    auto* this_object = typed_this(vm, global_object);
+    auto* this_object = typed_this_object(global_object);
     if (!this_object)
         return {};
 
@@ -121,7 +108,7 @@ JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_date)
 // 21.4.4.20 Date.prototype.setDate ( date ), https://tc39.es/ecma262/#sec-date.prototype.setdate
 JS_DEFINE_NATIVE_FUNCTION(DatePrototype::set_date)
 {
-    auto* this_object = typed_this(vm, global_object);
+    auto* this_object = typed_this_object(global_object);
     if (!this_object)
         return {};
 
@@ -149,7 +136,7 @@ JS_DEFINE_NATIVE_FUNCTION(DatePrototype::set_date)
 // 21.4.4.3 Date.prototype.getDay ( ), https://tc39.es/ecma262/#sec-date.prototype.getday
 JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_day)
 {
-    auto* this_object = typed_this(vm, global_object);
+    auto* this_object = typed_this_object(global_object);
     if (!this_object)
         return {};
 
@@ -162,7 +149,7 @@ JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_day)
 // 21.4.4.4 Date.prototype.getFullYear ( ), https://tc39.es/ecma262/#sec-date.prototype.getfullyear
 JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_full_year)
 {
-    auto* this_object = typed_this(vm, global_object);
+    auto* this_object = typed_this_object(global_object);
     if (!this_object)
         return {};
 
@@ -175,7 +162,7 @@ JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_full_year)
 // 21.4.4.21 Date.prototype.setFullYear ( year [ , month [ , date ] ] ), https://tc39.es/ecma262/#sec-date.prototype.setfullyear
 JS_DEFINE_NATIVE_FUNCTION(DatePrototype::set_full_year)
 {
-    auto* this_object = typed_this(vm, global_object);
+    auto* this_object = typed_this_object(global_object);
     if (!this_object)
         return {};
 
@@ -224,7 +211,7 @@ JS_DEFINE_NATIVE_FUNCTION(DatePrototype::set_full_year)
 // B.2.4.1 Date.prototype.getYear ( ), https://tc39.es/ecma262/#sec-date.prototype.getyear
 JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_year)
 {
-    auto* this_object = typed_this(vm, global_object);
+    auto* this_object = typed_this_object(global_object);
     if (!this_object)
         return {};
 
@@ -237,7 +224,7 @@ JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_year)
 // B.2.4.2 Date.prototype.setYear ( year ), https://tc39.es/ecma262/#sec-date.prototype.setyear
 JS_DEFINE_NATIVE_FUNCTION(DatePrototype::set_year)
 {
-    auto* this_object = typed_this(vm, global_object);
+    auto* this_object = typed_this_object(global_object);
     if (!this_object)
         return {};
 
@@ -268,7 +255,7 @@ JS_DEFINE_NATIVE_FUNCTION(DatePrototype::set_year)
 // 21.4.4.5 Date.prototype.getHours ( ), https://tc39.es/ecma262/#sec-date.prototype.gethours
 JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_hours)
 {
-    auto* this_object = typed_this(vm, global_object);
+    auto* this_object = typed_this_object(global_object);
     if (!this_object)
         return {};
 
@@ -281,7 +268,7 @@ JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_hours)
 // 21.4.4.22 Date.prototype.setHours ( hour [ , min [ , sec [ , ms ] ] ] ), https://tc39.es/ecma262/#sec-date.prototype.sethours
 JS_DEFINE_NATIVE_FUNCTION(DatePrototype::set_hours)
 {
-    auto* this_object = typed_this(vm, global_object);
+    auto* this_object = typed_this_object(global_object);
     if (!this_object)
         return {};
 
@@ -341,7 +328,7 @@ JS_DEFINE_NATIVE_FUNCTION(DatePrototype::set_hours)
 // 21.4.4.23 Date.prototype.setMilliseconds ( ms ), https://tc39.es/ecma262/#sec-date.prototype.setmilliseconds
 JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_milliseconds)
 {
-    auto* this_object = typed_this(vm, global_object);
+    auto* this_object = typed_this_object(global_object);
     if (!this_object)
         return {};
 
@@ -354,7 +341,7 @@ JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_milliseconds)
 // 21.4.4.23 Date.prototype.setMilliseconds ( ms ), https://tc39.es/ecma262/#sec-date.prototype.setmilliseconds
 JS_DEFINE_NATIVE_FUNCTION(DatePrototype::set_milliseconds)
 {
-    auto* this_object = typed_this(vm, global_object);
+    auto* this_object = typed_this_object(global_object);
     if (!this_object)
         return {};
 
@@ -390,7 +377,7 @@ JS_DEFINE_NATIVE_FUNCTION(DatePrototype::set_milliseconds)
 // 21.4.4.7 Date.prototype.getMinutes ( ), https://tc39.es/ecma262/#sec-date.prototype.getminutes
 JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_minutes)
 {
-    auto* this_object = typed_this(vm, global_object);
+    auto* this_object = typed_this_object(global_object);
     if (!this_object)
         return {};
 
@@ -403,7 +390,7 @@ JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_minutes)
 // 21.4.4.24 Date.prototype.setMinutes ( min [ , sec [ , ms ] ] ), https://tc39.es/ecma262/#sec-date.prototype.setminutes
 JS_DEFINE_NATIVE_FUNCTION(DatePrototype::set_minutes)
 {
-    auto* this_object = typed_this(vm, global_object);
+    auto* this_object = typed_this_object(global_object);
     if (!this_object)
         return {};
 
@@ -454,7 +441,7 @@ JS_DEFINE_NATIVE_FUNCTION(DatePrototype::set_minutes)
 // 21.4.4.8 Date.prototype.getMonth ( ), https://tc39.es/ecma262/#sec-date.prototype.getmonth
 JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_month)
 {
-    auto* this_object = typed_this(vm, global_object);
+    auto* this_object = typed_this_object(global_object);
     if (!this_object)
         return {};
 
@@ -467,7 +454,7 @@ JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_month)
 // 21.4.4.25 Date.prototype.setMonth ( month [ , date ] ), https://tc39.es/ecma262/#sec-date.prototype.setmonth
 JS_DEFINE_NATIVE_FUNCTION(DatePrototype::set_month)
 {
-    auto* this_object = typed_this(vm, global_object);
+    auto* this_object = typed_this_object(global_object);
     if (!this_object)
         return {};
 
@@ -506,7 +493,7 @@ JS_DEFINE_NATIVE_FUNCTION(DatePrototype::set_month)
 // 21.4.4.9 Date.prototype.getSeconds ( ), https://tc39.es/ecma262/#sec-date.prototype.getseconds
 JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_seconds)
 {
-    auto* this_object = typed_this(vm, global_object);
+    auto* this_object = typed_this_object(global_object);
     if (!this_object)
         return {};
 
@@ -519,7 +506,7 @@ JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_seconds)
 // 21.4.4.26 Date.prototype.setSeconds ( sec [ , ms ] ), https://tc39.es/ecma262/#sec-date.prototype.setseconds
 JS_DEFINE_NATIVE_FUNCTION(DatePrototype::set_seconds)
 {
-    auto* this_object = typed_this(vm, global_object);
+    auto* this_object = typed_this_object(global_object);
     if (!this_object)
         return {};
 
@@ -561,7 +548,7 @@ JS_DEFINE_NATIVE_FUNCTION(DatePrototype::set_seconds)
 // 21.4.4.10 Date.prototype.getTime ( ), https://tc39.es/ecma262/#sec-date.prototype.gettime
 JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_time)
 {
-    auto* this_object = typed_this(vm, global_object);
+    auto* this_object = typed_this_object(global_object);
     if (!this_object)
         return {};
 
@@ -574,7 +561,7 @@ JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_time)
 // 21.4.4.27 Date.prototype.setTime ( time ), https://tc39.es/ecma262/#sec-date.prototype.settime
 JS_DEFINE_NATIVE_FUNCTION(DatePrototype::set_time)
 {
-    auto* this_object = typed_this(vm, global_object);
+    auto* this_object = typed_this_object(global_object);
     if (!this_object)
         return {};
 
@@ -603,7 +590,7 @@ JS_DEFINE_NATIVE_FUNCTION(DatePrototype::set_time)
 // 21.4.4.11 Date.prototype.getTimezoneOffset ( ), https://tc39.es/ecma262/#sec-date.prototype.gettimezoneoffset
 JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_timezone_offset)
 {
-    auto* this_object = typed_this(vm, global_object);
+    auto* this_object = typed_this_object(global_object);
     if (!this_object)
         return {};
 
@@ -617,7 +604,7 @@ JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_timezone_offset)
 // 21.4.4.12 Date.prototype.getUTCDate ( ), https://tc39.es/ecma262/#sec-date.prototype.getutcdate
 JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_utc_date)
 {
-    auto* this_object = typed_this(vm, global_object);
+    auto* this_object = typed_this_object(global_object);
     if (!this_object)
         return {};
 
@@ -630,7 +617,7 @@ JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_utc_date)
 // 21.4.4.13 Date.prototype.getUTCDay ( ), https://tc39.es/ecma262/#sec-date.prototype.getutcday
 JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_utc_day)
 {
-    auto* this_object = typed_this(vm, global_object);
+    auto* this_object = typed_this_object(global_object);
     if (!this_object)
         return {};
 
@@ -643,7 +630,7 @@ JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_utc_day)
 // 21.4.4.14 Date.prototype.getUTCFullYear ( ), https://tc39.es/ecma262/#sec-date.prototype.getutcfullyear
 JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_utc_full_year)
 {
-    auto* this_object = typed_this(vm, global_object);
+    auto* this_object = typed_this_object(global_object);
     if (!this_object)
         return {};
 
@@ -656,7 +643,7 @@ JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_utc_full_year)
 // 21.4.4.15 Date.prototype.getUTCHours ( ), https://tc39.es/ecma262/#sec-date.prototype.getutchours
 JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_utc_hours)
 {
-    auto* this_object = typed_this(vm, global_object);
+    auto* this_object = typed_this_object(global_object);
     if (!this_object)
         return {};
 
@@ -669,7 +656,7 @@ JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_utc_hours)
 // 21.4.4.16 Date.prototype.getUTCMilliseconds ( ), https://tc39.es/ecma262/#sec-date.prototype.getutcmilliseconds
 JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_utc_milliseconds)
 {
-    auto* this_object = typed_this(vm, global_object);
+    auto* this_object = typed_this_object(global_object);
     if (!this_object)
         return {};
 
@@ -682,7 +669,7 @@ JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_utc_milliseconds)
 // 21.4.4.18 Date.prototype.getUTCMonth ( ), https://tc39.es/ecma262/#sec-date.prototype.getutcmonth
 JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_utc_month)
 {
-    auto* this_object = typed_this(vm, global_object);
+    auto* this_object = typed_this_object(global_object);
     if (!this_object)
         return {};
 
@@ -695,7 +682,7 @@ JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_utc_month)
 // 21.4.4.17 Date.prototype.getUTCMinutes ( ), https://tc39.es/ecma262/#sec-date.prototype.getutcminutes
 JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_utc_minutes)
 {
-    auto* this_object = typed_this(vm, global_object);
+    auto* this_object = typed_this_object(global_object);
     if (!this_object)
         return {};
 
@@ -708,7 +695,7 @@ JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_utc_minutes)
 // 21.4.4.19 Date.prototype.getUTCSeconds ( ), https://tc39.es/ecma262/#sec-date.prototype.getutcseconds
 JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_utc_seconds)
 {
-    auto* this_object = typed_this(vm, global_object);
+    auto* this_object = typed_this_object(global_object);
     if (!this_object)
         return {};
 
@@ -721,7 +708,7 @@ JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_utc_seconds)
 // 21.4.4.35 Date.prototype.toDateString ( ), https://tc39.es/ecma262/#sec-date.prototype.todatestring
 JS_DEFINE_NATIVE_FUNCTION(DatePrototype::to_date_string)
 {
-    auto* this_object = typed_this(vm, global_object);
+    auto* this_object = typed_this_object(global_object);
     if (!this_object)
         return {};
 
@@ -742,7 +729,7 @@ JS_DEFINE_NATIVE_FUNCTION(DatePrototype::to_gmt_string)
 // 21.4.4.43 Date.prototype.toUTCString ( ), https://tc39.es/ecma262/#sec-date.prototype.toutcstring
 JS_DEFINE_NATIVE_FUNCTION(DatePrototype::to_utc_string)
 {
-    auto* this_object = typed_this(vm, global_object);
+    auto* this_object = typed_this_object(global_object);
     if (!this_object)
         return {};
 
@@ -757,7 +744,7 @@ JS_DEFINE_NATIVE_FUNCTION(DatePrototype::to_utc_string)
 // 21.4.4.36 Date.prototype.toISOString ( ), https://tc39.es/ecma262/#sec-date.prototype.toisostring
 JS_DEFINE_NATIVE_FUNCTION(DatePrototype::to_iso_string)
 {
-    auto* this_object = typed_this(vm, global_object);
+    auto* this_object = typed_this_object(global_object);
     if (!this_object)
         return {};
 
@@ -773,7 +760,7 @@ JS_DEFINE_NATIVE_FUNCTION(DatePrototype::to_iso_string)
 // 21.4.4.38 Date.prototype.toLocaleDateString ( [ reserved1 [ , reserved2 ] ] ), https://tc39.es/ecma262/#sec-date.prototype.tolocaledatestring
 JS_DEFINE_NATIVE_FUNCTION(DatePrototype::to_locale_date_string)
 {
-    auto* this_object = typed_this(vm, global_object);
+    auto* this_object = typed_this_object(global_object);
     if (!this_object)
         return {};
 
@@ -788,7 +775,7 @@ JS_DEFINE_NATIVE_FUNCTION(DatePrototype::to_locale_date_string)
 // 21.4.4.39 Date.prototype.toLocaleString ( [ reserved1 [ , reserved2 ] ] ), https://tc39.es/ecma262/#sec-date.prototype.tolocalestring
 JS_DEFINE_NATIVE_FUNCTION(DatePrototype::to_locale_string)
 {
-    auto* this_object = typed_this(vm, global_object);
+    auto* this_object = typed_this_object(global_object);
     if (!this_object)
         return {};
 
@@ -803,7 +790,7 @@ JS_DEFINE_NATIVE_FUNCTION(DatePrototype::to_locale_string)
 // 21.4.4.40 Date.prototype.toLocaleTimeString ( [ reserved1 [ , reserved2 ] ] ), https://tc39.es/ecma262/#sec-date.prototype.tolocaletimestring
 JS_DEFINE_NATIVE_FUNCTION(DatePrototype::to_locale_time_string)
 {
-    auto* this_object = typed_this(vm, global_object);
+    auto* this_object = typed_this_object(global_object);
     if (!this_object)
         return {};
 
@@ -818,7 +805,7 @@ JS_DEFINE_NATIVE_FUNCTION(DatePrototype::to_locale_time_string)
 // 21.4.4.42 Date.prototype.toTimeString ( ), https://tc39.es/ecma262/#sec-date.prototype.totimestring
 JS_DEFINE_NATIVE_FUNCTION(DatePrototype::to_time_string)
 {
-    auto* this_object = typed_this(vm, global_object);
+    auto* this_object = typed_this_object(global_object);
     if (!this_object)
         return {};
 
@@ -832,7 +819,7 @@ JS_DEFINE_NATIVE_FUNCTION(DatePrototype::to_time_string)
 // 21.4.4.41 Date.prototype.toString ( ), https://tc39.es/ecma262/#sec-date.prototype.tostring
 JS_DEFINE_NATIVE_FUNCTION(DatePrototype::to_string)
 {
-    auto* this_object = typed_this(vm, global_object);
+    auto* this_object = typed_this_object(global_object);
     if (!this_object)
         return {};
 
@@ -862,7 +849,7 @@ JS_DEFINE_NATIVE_FUNCTION(DatePrototype::to_json)
 JS_DEFINE_NATIVE_FUNCTION(DatePrototype::to_temporal_instant)
 {
     // 1. Let t be ? thisTimeValue(this value).
-    auto* this_object = typed_this(vm, global_object);
+    auto* this_object = typed_this_object(global_object);
     if (vm.exception())
         return {};
     auto t = this_object->value_of();

+ 4 - 3
Userland/Libraries/LibJS/Runtime/DatePrototype.h

@@ -6,12 +6,13 @@
 
 #pragma once
 
-#include <LibJS/Runtime/Object.h>
+#include <LibJS/Runtime/Date.h>
+#include <LibJS/Runtime/PrototypeObject.h>
 
 namespace JS {
 
-class DatePrototype final : public Object {
-    JS_OBJECT(DatePrototype, Object);
+class DatePrototype final : public PrototypeObject<DatePrototype, Date> {
+    JS_PROTOTYPE_OBJECT(DatePrototype, Date, Date);
 
 public:
     explicit DatePrototype(GlobalObject&);