LibJS: Implement Temporal.Now

This commit is contained in:
Timothy Flynn 2024-11-24 17:23:31 -05:00 committed by Andreas Kling
commit f2c19f96f8
Notes: github-actions[bot] 2024-11-25 12:33:55 +00:00
12 changed files with 252 additions and 0 deletions

View file

@ -12,6 +12,7 @@
#include <AK/ScopeGuard.h>
#include <AK/String.h>
#include <AK/StringBuilder.h>
#include <AK/Time.h>
#include <LibFileSystem/FileSystem.h>
#include <LibJS/AST.h>
#include <LibJS/Bytecode/Interpreter.h>
@ -29,6 +30,7 @@
#include <LibJS/Runtime/PromiseCapability.h>
#include <LibJS/Runtime/Reference.h>
#include <LibJS/Runtime/Symbol.h>
#include <LibJS/Runtime/Temporal/Instant.h>
#include <LibJS/Runtime/VM.h>
#include <LibJS/SourceTextModule.h>
#include <LibJS/SyntheticModule.h>
@ -181,6 +183,20 @@ VM::VM(OwnPtr<CustomData> custom_data, ErrorMessages error_messages)
return {};
};
// 2.3.1 HostSystemUTCEpochNanoseconds ( global ), https://tc39.es/proposal-temporal/#sec-hostsystemutcepochnanoseconds
host_system_utc_epoch_nanoseconds = [](Object const&) {
// 1. Let ns be the approximate current UTC date and time, in nanoseconds since the epoch.
Crypto::SignedBigInteger nanoseconds { AK::UnixDateTime::now().nanoseconds_since_epoch() };
// 2. Return the result of clamping ns between nsMinInstant and nsMaxInstant.
if (nanoseconds < Temporal::NANOSECONDS_MIN_INSTANT)
nanoseconds = Temporal::NANOSECONDS_MIN_INSTANT;
if (nanoseconds > Temporal::NANOSECONDS_MAX_INSTANT)
nanoseconds = Temporal::NANOSECONDS_MAX_INSTANT;
return nanoseconds;
};
// AD-HOC: Inform the host that we received a date string we were unable to parse.
host_unrecognized_date_string = [](StringView) {
};