Преглед изворни кода

LibJS: Implement Date.prototype.toTemporalInstant()

Linus Groh пре 4 година
родитељ
комит
7e4b0681e1

+ 1 - 0
Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h

@@ -312,6 +312,7 @@ namespace JS {
     P(toLocaleTimeString)                    \
     P(toLowerCase)                           \
     P(toString)                              \
+    P(toTemporalInstant)                     \
     P(toTimeString)                          \
     P(toUpperCase)                           \
     P(toUTCString)                           \

+ 23 - 0
Userland/Libraries/LibJS/Runtime/DatePrototype.cpp

@@ -9,10 +9,13 @@
 #include <AK/Function.h>
 #include <AK/String.h>
 #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>
+#include <LibJS/Runtime/Temporal/Instant.h>
 #include <LibJS/Runtime/Value.h>
 
 namespace JS {
@@ -83,6 +86,7 @@ void DatePrototype::initialize(GlobalObject& global_object)
     define_native_function(vm.names.toTimeString, to_time_string, 0, attr);
     define_native_function(vm.names.toString, to_string, 0, attr);
     define_native_function(vm.names.toJSON, to_json, 1, attr);
+    define_native_function(vm.names.toTemporalInstant, to_temporal_instant, 0, attr);
 
     // 21.4.4.45 Date.prototype [ @@toPrimitive ] ( hint ), https://tc39.es/ecma262/#sec-date.prototype-@@toprimitive
     define_native_function(*vm.well_known_symbol_to_primitive(), symbol_to_primitive, 1, Attribute::Configurable);
@@ -855,6 +859,25 @@ JS_DEFINE_NATIVE_FUNCTION(DatePrototype::to_json)
     return this_object->invoke(vm.names.toISOString.as_string());
 }
 
+// 14.1.1 Date.prototype.toTemporalInstant ( ), https://tc39.es/proposal-temporal/#sec-date.prototype.totemporalinstant
+JS_DEFINE_NATIVE_FUNCTION(DatePrototype::to_temporal_instant)
+{
+    // 1. Let t be ? thisTimeValue(this value).
+    auto* this_object = typed_this(vm, global_object);
+    if (vm.exception())
+        return {};
+    auto t = this_object->value_of();
+
+    // 2. Let ns be ? NumberToBigInt(t) × 10^6.
+    auto* ns = number_to_bigint(global_object, t);
+    if (vm.exception())
+        return {};
+    ns = js_bigint(vm.heap(), ns->big_integer().multiplied_by(Crypto::UnsignedBigInteger { 1'000'000 }));
+
+    // 3. Return ? CreateTemporalInstant(ns).
+    return Temporal::create_temporal_instant(global_object, *ns);
+}
+
 // 21.4.4.45 Date.prototype [ @@toPrimitive ] ( hint ), https://tc39.es/ecma262/#sec-date.prototype-@@toprimitive
 JS_DEFINE_NATIVE_FUNCTION(DatePrototype::symbol_to_primitive)
 {

+ 1 - 0
Userland/Libraries/LibJS/Runtime/DatePrototype.h

@@ -57,6 +57,7 @@ private:
     JS_DECLARE_NATIVE_FUNCTION(to_time_string);
     JS_DECLARE_NATIVE_FUNCTION(to_string);
     JS_DECLARE_NATIVE_FUNCTION(to_json);
+    JS_DECLARE_NATIVE_FUNCTION(to_temporal_instant);
     JS_DECLARE_NATIVE_FUNCTION(symbol_to_primitive);
 };