diff --git a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h index 0f28adb88d..a9bb598d96 100644 --- a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h +++ b/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) \ diff --git a/Userland/Libraries/LibJS/Runtime/DatePrototype.cpp b/Userland/Libraries/LibJS/Runtime/DatePrototype.cpp index d77896f550..4627d3ebb4 100644 --- a/Userland/Libraries/LibJS/Runtime/DatePrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/DatePrototype.cpp @@ -9,10 +9,13 @@ #include #include #include +#include +#include #include #include #include #include +#include #include 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) { diff --git a/Userland/Libraries/LibJS/Runtime/DatePrototype.h b/Userland/Libraries/LibJS/Runtime/DatePrototype.h index 5256659fb9..dd8f5ba647 100644 --- a/Userland/Libraries/LibJS/Runtime/DatePrototype.h +++ b/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); };