2021-07-07 17:41:37 +01:00
|
|
|
|
/*
|
2022-05-24 18:27:09 +01:00
|
|
|
|
* Copyright (c) 2021-2022, Linus Groh <linusg@serenityos.org>
|
2021-07-07 17:41:37 +01:00
|
|
|
|
*
|
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
2021-07-31 00:43:42 -07:00
|
|
|
|
#include <AK/TypeCasts.h>
|
2021-07-09 11:51:23 +01:00
|
|
|
|
#include <LibCrypto/BigInt/UnsignedBigInteger.h>
|
2021-07-07 17:41:37 +01:00
|
|
|
|
#include <LibJS/Runtime/GlobalObject.h>
|
|
|
|
|
|
#include <LibJS/Runtime/Temporal/Instant.h>
|
|
|
|
|
|
#include <LibJS/Runtime/Temporal/InstantConstructor.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace JS::Temporal {
|
|
|
|
|
|
|
|
|
|
|
|
// 8.1 The Temporal.Instant Constructor, https://tc39.es/proposal-temporal/#sec-temporal-instant-constructor
|
2022-08-16 00:20:49 +01:00
|
|
|
|
InstantConstructor::InstantConstructor(Realm& realm)
|
|
|
|
|
|
: NativeFunction(vm().names.Instant.as_string(), *realm.global_object().function_prototype())
|
2021-07-07 17:41:37 +01:00
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-08-16 00:20:49 +01:00
|
|
|
|
void InstantConstructor::initialize(Realm& realm)
|
2021-07-07 17:41:37 +01:00
|
|
|
|
{
|
2022-08-16 00:20:49 +01:00
|
|
|
|
NativeFunction::initialize(realm);
|
2021-07-07 17:41:37 +01:00
|
|
|
|
|
|
|
|
|
|
auto& vm = this->vm();
|
|
|
|
|
|
|
2022-05-24 18:27:09 +01:00
|
|
|
|
// 8.2.1 Temporal.Instant.prototype, https://tc39.es/proposal-temporal/#sec-temporal.instant.prototype
|
2022-08-16 00:20:49 +01:00
|
|
|
|
define_direct_property(vm.names.prototype, realm.global_object().temporal_instant_prototype(), 0);
|
2021-07-07 17:41:37 +01:00
|
|
|
|
|
2021-07-09 11:51:23 +01:00
|
|
|
|
u8 attr = Attribute::Writable | Attribute::Configurable;
|
2021-10-21 19:56:52 +01:00
|
|
|
|
define_native_function(vm.names.from, from, 1, attr);
|
|
|
|
|
|
define_native_function(vm.names.fromEpochSeconds, from_epoch_seconds, 1, attr);
|
|
|
|
|
|
define_native_function(vm.names.fromEpochMilliseconds, from_epoch_milliseconds, 1, attr);
|
|
|
|
|
|
define_native_function(vm.names.fromEpochMicroseconds, from_epoch_microseconds, 1, attr);
|
|
|
|
|
|
define_native_function(vm.names.fromEpochNanoseconds, from_epoch_nanoseconds, 1, attr);
|
|
|
|
|
|
define_native_function(vm.names.compare, compare, 2, attr);
|
2021-07-09 11:51:23 +01:00
|
|
|
|
|
2021-07-07 17:41:37 +01:00
|
|
|
|
define_direct_property(vm.names.length, Value(1), Attribute::Configurable);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 8.1.1 Temporal.Instant ( epochNanoseconds ), https://tc39.es/proposal-temporal/#sec-temporal.instant
|
2021-10-20 21:16:30 +01:00
|
|
|
|
ThrowCompletionOr<Value> InstantConstructor::call()
|
2021-07-07 17:41:37 +01:00
|
|
|
|
{
|
|
|
|
|
|
auto& vm = this->vm();
|
|
|
|
|
|
|
|
|
|
|
|
// 1. If NewTarget is undefined, then
|
|
|
|
|
|
// a. Throw a TypeError exception.
|
2022-08-16 20:33:17 +01:00
|
|
|
|
return vm.throw_completion<TypeError>(ErrorType::ConstructorWithoutNew, "Temporal.Instant");
|
2021-07-07 17:41:37 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 8.1.1 Temporal.Instant ( epochNanoseconds ), https://tc39.es/proposal-temporal/#sec-temporal.instant
|
2021-10-20 21:16:30 +01:00
|
|
|
|
ThrowCompletionOr<Object*> InstantConstructor::construct(FunctionObject& new_target)
|
2021-07-07 17:41:37 +01:00
|
|
|
|
{
|
|
|
|
|
|
auto& vm = this->vm();
|
|
|
|
|
|
|
|
|
|
|
|
// 2. Let epochNanoseconds be ? ToBigInt(epochNanoseconds).
|
2022-08-21 14:00:56 +01:00
|
|
|
|
auto* epoch_nanoseconds = TRY(vm.argument(0).to_bigint(vm));
|
2021-07-07 17:41:37 +01:00
|
|
|
|
|
|
|
|
|
|
// 3. If ! IsValidEpochNanoseconds(epochNanoseconds) is false, throw a RangeError exception.
|
2021-10-20 21:16:30 +01:00
|
|
|
|
if (!is_valid_epoch_nanoseconds(*epoch_nanoseconds))
|
2022-08-16 20:33:17 +01:00
|
|
|
|
return vm.throw_completion<RangeError>(ErrorType::TemporalInvalidEpochNanoseconds);
|
2021-07-07 17:41:37 +01:00
|
|
|
|
|
|
|
|
|
|
// 4. Return ? CreateTemporalInstant(epochNanoseconds, NewTarget).
|
2022-08-20 08:52:42 +01:00
|
|
|
|
return TRY(create_temporal_instant(vm, *epoch_nanoseconds, &new_target));
|
2021-07-07 17:41:37 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-07-11 21:19:19 +03:00
|
|
|
|
// 8.2.2 Temporal.Instant.from ( item ), https://tc39.es/proposal-temporal/#sec-temporal.instant.from
|
2021-10-21 19:56:52 +01:00
|
|
|
|
JS_DEFINE_NATIVE_FUNCTION(InstantConstructor::from)
|
2021-07-11 21:19:19 +03:00
|
|
|
|
{
|
|
|
|
|
|
auto item = vm.argument(0);
|
|
|
|
|
|
|
|
|
|
|
|
// 1. If Type(item) is Object and item has an [[InitializedTemporalInstant]] internal slot, then
|
|
|
|
|
|
if (item.is_object() && is<Instant>(item.as_object())) {
|
2021-07-19 00:20:34 +01:00
|
|
|
|
// a. Return ! CreateTemporalInstant(item.[[Nanoseconds]]).
|
2022-08-20 08:52:42 +01:00
|
|
|
|
return MUST(create_temporal_instant(vm, *js_bigint(vm, static_cast<Instant&>(item.as_object()).nanoseconds().big_integer())));
|
2021-07-11 21:19:19 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 2. Return ? ToTemporalInstant(item).
|
2022-08-20 08:52:42 +01:00
|
|
|
|
return TRY(to_temporal_instant(vm, item));
|
2021-07-11 21:19:19 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-07-09 11:51:23 +01:00
|
|
|
|
// 8.2.3 Temporal.Instant.fromEpochSeconds ( epochSeconds ), https://tc39.es/proposal-temporal/#sec-temporal.instant.fromepochseconds
|
2021-10-21 19:56:52 +01:00
|
|
|
|
JS_DEFINE_NATIVE_FUNCTION(InstantConstructor::from_epoch_seconds)
|
2021-07-09 11:51:23 +01:00
|
|
|
|
{
|
|
|
|
|
|
// 1. Set epochSeconds to ? ToNumber(epochSeconds).
|
2022-08-21 14:00:56 +01:00
|
|
|
|
auto epoch_seconds_value = TRY(vm.argument(0).to_number(vm));
|
2021-07-09 11:51:23 +01:00
|
|
|
|
|
|
|
|
|
|
// 2. Set epochSeconds to ? NumberToBigInt(epochSeconds).
|
2021-10-23 03:26:55 +03:00
|
|
|
|
auto* epoch_seconds = TRY(number_to_bigint(global_object, epoch_seconds_value));
|
2021-07-09 11:51:23 +01:00
|
|
|
|
|
|
|
|
|
|
// 3. Let epochNanoseconds be epochSeconds × 10^9ℤ.
|
2021-08-03 00:14:48 +01:00
|
|
|
|
auto* epoch_nanoseconds = js_bigint(vm, epoch_seconds->big_integer().multiplied_by(Crypto::UnsignedBigInteger { 1'000'000'000 }));
|
2021-07-09 11:51:23 +01:00
|
|
|
|
|
|
|
|
|
|
// 4. If ! IsValidEpochNanoseconds(epochNanoseconds) is false, throw a RangeError exception.
|
2021-10-21 19:56:52 +01:00
|
|
|
|
if (!is_valid_epoch_nanoseconds(*epoch_nanoseconds))
|
2022-08-16 20:33:17 +01:00
|
|
|
|
return vm.throw_completion<RangeError>(ErrorType::TemporalInvalidEpochNanoseconds);
|
2021-07-09 11:51:23 +01:00
|
|
|
|
|
2021-07-19 00:20:34 +01:00
|
|
|
|
// 5. Return ! CreateTemporalInstant(epochNanoseconds).
|
2022-08-20 08:52:42 +01:00
|
|
|
|
return MUST(create_temporal_instant(vm, *epoch_nanoseconds));
|
2021-07-09 11:51:23 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-07-09 12:00:31 +01:00
|
|
|
|
// 8.2.4 Temporal.Instant.fromEpochMilliseconds ( epochMilliseconds ), https://tc39.es/proposal-temporal/#sec-temporal.instant.fromepochmilliseconds
|
2021-10-21 19:56:52 +01:00
|
|
|
|
JS_DEFINE_NATIVE_FUNCTION(InstantConstructor::from_epoch_milliseconds)
|
2021-07-09 12:00:31 +01:00
|
|
|
|
{
|
|
|
|
|
|
// 1. Set epochMilliseconds to ? ToNumber(epochMilliseconds).
|
2022-08-21 14:00:56 +01:00
|
|
|
|
auto epoch_milliseconds_value = TRY(vm.argument(0).to_number(vm));
|
2021-07-09 12:00:31 +01:00
|
|
|
|
|
|
|
|
|
|
// 2. Set epochMilliseconds to ? NumberToBigInt(epochMilliseconds).
|
2021-10-23 03:26:55 +03:00
|
|
|
|
auto* epoch_milliseconds = TRY(number_to_bigint(global_object, epoch_milliseconds_value));
|
2021-07-09 12:00:31 +01:00
|
|
|
|
|
|
|
|
|
|
// 3. Let epochNanoseconds be epochMilliseconds × 10^6ℤ.
|
2021-08-03 00:14:48 +01:00
|
|
|
|
auto* epoch_nanoseconds = js_bigint(vm, epoch_milliseconds->big_integer().multiplied_by(Crypto::UnsignedBigInteger { 1'000'000 }));
|
2021-07-09 12:00:31 +01:00
|
|
|
|
|
|
|
|
|
|
// 4. If ! IsValidEpochNanoseconds(epochNanoseconds) is false, throw a RangeError exception.
|
2021-10-21 19:56:52 +01:00
|
|
|
|
if (!is_valid_epoch_nanoseconds(*epoch_nanoseconds))
|
2022-08-16 20:33:17 +01:00
|
|
|
|
return vm.throw_completion<RangeError>(ErrorType::TemporalInvalidEpochNanoseconds);
|
2021-07-09 12:00:31 +01:00
|
|
|
|
|
2021-07-19 00:20:34 +01:00
|
|
|
|
// 5. Return ! CreateTemporalInstant(epochNanoseconds).
|
2022-08-20 08:52:42 +01:00
|
|
|
|
return MUST(create_temporal_instant(vm, *epoch_nanoseconds));
|
2021-07-09 12:00:31 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-07-10 16:44:12 +03:00
|
|
|
|
// 8.2.5 Temporal.Instant.fromEpochMicroseconds ( epochMicroseconds ), https://tc39.es/proposal-temporal/#sec-temporal.instant.fromepochmicroseconds
|
2021-10-21 19:56:52 +01:00
|
|
|
|
JS_DEFINE_NATIVE_FUNCTION(InstantConstructor::from_epoch_microseconds)
|
2021-07-09 12:10:16 +01:00
|
|
|
|
{
|
|
|
|
|
|
// 1. Set epochMicroseconds to ? ToBigInt(epochMicroseconds).
|
2022-08-21 14:00:56 +01:00
|
|
|
|
auto* epoch_microseconds = TRY(vm.argument(0).to_bigint(vm));
|
2021-07-09 12:10:16 +01:00
|
|
|
|
|
|
|
|
|
|
// 2. Let epochNanoseconds be epochMicroseconds × 1000ℤ.
|
2021-08-03 00:14:48 +01:00
|
|
|
|
auto* epoch_nanoseconds = js_bigint(vm, epoch_microseconds->big_integer().multiplied_by(Crypto::UnsignedBigInteger { 1'000 }));
|
2021-07-09 12:10:16 +01:00
|
|
|
|
|
|
|
|
|
|
// 3. If ! IsValidEpochNanoseconds(epochNanoseconds) is false, throw a RangeError exception.
|
2021-10-21 19:56:52 +01:00
|
|
|
|
if (!is_valid_epoch_nanoseconds(*epoch_nanoseconds))
|
2022-08-16 20:33:17 +01:00
|
|
|
|
return vm.throw_completion<RangeError>(ErrorType::TemporalInvalidEpochNanoseconds);
|
2021-07-09 12:10:16 +01:00
|
|
|
|
|
2021-07-19 00:20:34 +01:00
|
|
|
|
// 4. Return ! CreateTemporalInstant(epochNanoseconds).
|
2022-08-20 08:52:42 +01:00
|
|
|
|
return MUST(create_temporal_instant(vm, *epoch_nanoseconds));
|
2021-07-09 12:10:16 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-07-10 16:44:12 +03:00
|
|
|
|
// 8.2.6 Temporal.Instant.fromEpochNanoseconds ( epochNanoseconds ), https://tc39.es/proposal-temporal/#sec-temporal.instant.fromepochnanoseconds
|
2021-10-21 19:56:52 +01:00
|
|
|
|
JS_DEFINE_NATIVE_FUNCTION(InstantConstructor::from_epoch_nanoseconds)
|
2021-07-09 12:20:11 +01:00
|
|
|
|
{
|
|
|
|
|
|
// 1. Set epochNanoseconds to ? ToBigInt(epochNanoseconds).
|
2022-08-21 14:00:56 +01:00
|
|
|
|
auto* epoch_nanoseconds = TRY(vm.argument(0).to_bigint(vm));
|
2021-07-09 12:20:11 +01:00
|
|
|
|
|
|
|
|
|
|
// 2. If ! IsValidEpochNanoseconds(epochNanoseconds) is false, throw a RangeError exception.
|
2021-10-21 19:56:52 +01:00
|
|
|
|
if (!is_valid_epoch_nanoseconds(*epoch_nanoseconds))
|
2022-08-16 20:33:17 +01:00
|
|
|
|
return vm.throw_completion<RangeError>(ErrorType::TemporalInvalidEpochNanoseconds);
|
2021-07-09 12:20:11 +01:00
|
|
|
|
|
2021-07-19 00:20:34 +01:00
|
|
|
|
// 3. Return ! CreateTemporalInstant(epochNanoseconds).
|
2022-08-20 08:52:42 +01:00
|
|
|
|
return MUST(create_temporal_instant(vm, *epoch_nanoseconds));
|
2021-07-09 12:20:11 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-07-11 21:18:48 +03:00
|
|
|
|
// 8.2.7 Temporal.Instant.compare ( one, two ), https://tc39.es/proposal-temporal/#sec-temporal.instant.compare
|
2021-10-21 19:56:52 +01:00
|
|
|
|
JS_DEFINE_NATIVE_FUNCTION(InstantConstructor::compare)
|
2021-07-11 21:18:48 +03:00
|
|
|
|
{
|
|
|
|
|
|
// 1. Set one to ? ToTemporalInstant(one).
|
2022-08-20 08:52:42 +01:00
|
|
|
|
auto* one = TRY(to_temporal_instant(vm, vm.argument(0)));
|
2021-07-11 21:18:48 +03:00
|
|
|
|
|
|
|
|
|
|
// 2. Set two to ? ToTemporalInstant(two).
|
2022-08-20 08:52:42 +01:00
|
|
|
|
auto* two = TRY(to_temporal_instant(vm, vm.argument(1)));
|
2021-07-11 21:18:48 +03:00
|
|
|
|
|
|
|
|
|
|
// 3. Return 𝔽(! CompareEpochNanoseconds(one.[[Nanoseconds]], two.[[Nanoseconds]])).
|
|
|
|
|
|
return Value(compare_epoch_nanoseconds(one->nanoseconds(), two->nanoseconds()));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-07-07 17:41:37 +01:00
|
|
|
|
}
|