From 3a39ff8f40d39305ce761a52adb155c1b3128501 Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Wed, 7 Jul 2021 17:42:21 +0100 Subject: [PATCH] LibJS: Implement Temporal.now.instant() --- .../LibJS/Runtime/CommonPropertyNames.h | 1 + .../Libraries/LibJS/Runtime/Temporal/Now.cpp | 44 +++++++++++++++++++ .../Libraries/LibJS/Runtime/Temporal/Now.h | 3 ++ 3 files changed, 48 insertions(+) diff --git a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h index 8f0a484f6ac..0f28adb88d1 100644 --- a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h +++ b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h @@ -190,6 +190,7 @@ namespace JS { P(indexOf) \ P(info) \ P(input) \ + P(instant) \ P(is) \ P(isArray) \ P(isExtensible) \ diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/Now.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/Now.cpp index 1d3be0ad61d..1a0a59721fd 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/Now.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/Now.cpp @@ -4,9 +4,12 @@ * SPDX-License-Identifier: BSD-2-Clause */ +#include #include +#include #include #include +#include namespace JS::Temporal { @@ -24,6 +27,7 @@ void Now::initialize(GlobalObject& global_object) u8 attr = Attribute::Writable | Attribute::Configurable; define_native_function(vm.names.timeZone, time_zone, 0, attr); + define_native_function(vm.names.instant, instant, 0, attr); } // 2.1.1 Temporal.now.timeZone ( ), https://tc39.es/proposal-temporal/#sec-temporal.now.timezone @@ -33,6 +37,13 @@ JS_DEFINE_NATIVE_FUNCTION(Now::time_zone) return system_time_zone(global_object); } +// 2.1.2 Temporal.now.instant ( ), https://tc39.es/proposal-temporal/#sec-temporal.now.instant +JS_DEFINE_NATIVE_FUNCTION(Now::instant) +{ + // 1. Return ? SystemInstant(). + return system_instant(global_object); +} + // 2.2.1 SystemTimeZone ( ), https://tc39.es/proposal-temporal/#sec-temporal-systemtimezone Object* system_time_zone(GlobalObject& global_object) { @@ -43,4 +54,37 @@ Object* system_time_zone(GlobalObject& global_object) return create_temporal_time_zone(global_object, identifier); } +// 2.2.2 SystemUTCEpochNanoseconds ( ) +BigInt* system_utc_epoch_nanoseconds(GlobalObject& global_object) +{ + // 1. Let ns be the approximate current UTC date and time, in nanoseconds since the epoch. + struct timespec now; + clock_gettime(CLOCK_REALTIME, &now); + Checked ns_timestamp; + ns_timestamp += now.tv_sec; + ns_timestamp *= 1000000000; + ns_timestamp += now.tv_nsec; + if (ns_timestamp.has_overflow()) { + // TODO: Deal with this before 2262-04-21T00:47:16Z. + VERIFY_NOT_REACHED(); + } + auto ns = Crypto::SignedBigInteger::create_from(ns_timestamp.value()); + + // 2. Set ns to the result of clamping ns between −8.64 × 10^21 and 8.64 × 10^21. + // Uhh, these don't even fit in an i64... ¯\_(ツ)_/¯ + + // 3. Return ℤ(ns). + return js_bigint(global_object.heap(), move(ns)); +} + +// 2.2.3 SystemInstant ( ) +Object* system_instant(GlobalObject& global_object) +{ + // 1. Let ns be ! SystemUTCEpochNanoseconds(). + auto* ns = system_utc_epoch_nanoseconds(global_object); + + // 2. Return ? CreateTemporalInstant(ns). + return create_temporal_instant(global_object, *ns); +} + } diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/Now.h b/Userland/Libraries/LibJS/Runtime/Temporal/Now.h index c04a5013bbd..05ed506595d 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/Now.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/Now.h @@ -20,8 +20,11 @@ public: private: JS_DECLARE_NATIVE_FUNCTION(time_zone); + JS_DECLARE_NATIVE_FUNCTION(instant); }; Object* system_time_zone(GlobalObject&); +BigInt* system_utc_epoch_nanoseconds(GlobalObject&); +Object* system_instant(GlobalObject&); }