1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 14:37:46 +00:00

LibJS: Convert Temporal.Instant functions to ThrowCompletionOr

This commit is contained in:
Linus Groh 2021-10-21 19:56:52 +01:00
parent 5e9e3f9dc8
commit 149231d47d
4 changed files with 153 additions and 167 deletions

View file

@ -28,12 +28,12 @@ void InstantConstructor::initialize(GlobalObject& global_object)
define_direct_property(vm.names.prototype, global_object.temporal_instant_prototype(), 0);
u8 attr = Attribute::Writable | Attribute::Configurable;
define_old_native_function(vm.names.from, from, 1, attr);
define_old_native_function(vm.names.fromEpochSeconds, from_epoch_seconds, 1, attr);
define_old_native_function(vm.names.fromEpochMilliseconds, from_epoch_milliseconds, 1, attr);
define_old_native_function(vm.names.fromEpochMicroseconds, from_epoch_microseconds, 1, attr);
define_old_native_function(vm.names.fromEpochNanoseconds, from_epoch_nanoseconds, 1, attr);
define_old_native_function(vm.names.compare, compare, 2, attr);
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);
define_direct_property(vm.names.length, Value(1), Attribute::Configurable);
}
@ -66,7 +66,7 @@ ThrowCompletionOr<Object*> InstantConstructor::construct(FunctionObject& new_tar
}
// 8.2.2 Temporal.Instant.from ( item ), https://tc39.es/proposal-temporal/#sec-temporal.instant.from
JS_DEFINE_OLD_NATIVE_FUNCTION(InstantConstructor::from)
JS_DEFINE_NATIVE_FUNCTION(InstantConstructor::from)
{
auto item = vm.argument(0);
@ -77,100 +77,92 @@ JS_DEFINE_OLD_NATIVE_FUNCTION(InstantConstructor::from)
}
// 2. Return ? ToTemporalInstant(item).
return TRY_OR_DISCARD(to_temporal_instant(global_object, item));
return TRY(to_temporal_instant(global_object, item));
}
// 8.2.3 Temporal.Instant.fromEpochSeconds ( epochSeconds ), https://tc39.es/proposal-temporal/#sec-temporal.instant.fromepochseconds
JS_DEFINE_OLD_NATIVE_FUNCTION(InstantConstructor::from_epoch_seconds)
JS_DEFINE_NATIVE_FUNCTION(InstantConstructor::from_epoch_seconds)
{
// 1. Set epochSeconds to ? ToNumber(epochSeconds).
auto epoch_seconds_value = TRY_OR_DISCARD(vm.argument(0).to_number(global_object));
auto epoch_seconds_value = TRY(vm.argument(0).to_number(global_object));
// 2. Set epochSeconds to ? NumberToBigInt(epochSeconds).
auto* epoch_seconds = number_to_bigint(global_object, epoch_seconds_value);
if (vm.exception())
return {};
if (auto* exception = vm.exception())
return throw_completion(exception->value());
// 3. Let epochNanoseconds be epochSeconds × 10^9.
auto* epoch_nanoseconds = js_bigint(vm, epoch_seconds->big_integer().multiplied_by(Crypto::UnsignedBigInteger { 1'000'000'000 }));
// 4. If ! IsValidEpochNanoseconds(epochNanoseconds) is false, throw a RangeError exception.
if (!is_valid_epoch_nanoseconds(*epoch_nanoseconds)) {
vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidEpochNanoseconds);
return {};
}
if (!is_valid_epoch_nanoseconds(*epoch_nanoseconds))
return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidEpochNanoseconds);
// 5. Return ! CreateTemporalInstant(epochNanoseconds).
return MUST(create_temporal_instant(global_object, *epoch_nanoseconds));
}
// 8.2.4 Temporal.Instant.fromEpochMilliseconds ( epochMilliseconds ), https://tc39.es/proposal-temporal/#sec-temporal.instant.fromepochmilliseconds
JS_DEFINE_OLD_NATIVE_FUNCTION(InstantConstructor::from_epoch_milliseconds)
JS_DEFINE_NATIVE_FUNCTION(InstantConstructor::from_epoch_milliseconds)
{
// 1. Set epochMilliseconds to ? ToNumber(epochMilliseconds).
auto epoch_milliseconds_value = TRY_OR_DISCARD(vm.argument(0).to_number(global_object));
auto epoch_milliseconds_value = TRY(vm.argument(0).to_number(global_object));
// 2. Set epochMilliseconds to ? NumberToBigInt(epochMilliseconds).
auto* epoch_milliseconds = number_to_bigint(global_object, epoch_milliseconds_value);
if (vm.exception())
return {};
if (auto* exception = vm.exception())
return throw_completion(exception->value());
// 3. Let epochNanoseconds be epochMilliseconds × 10^6.
auto* epoch_nanoseconds = js_bigint(vm, epoch_milliseconds->big_integer().multiplied_by(Crypto::UnsignedBigInteger { 1'000'000 }));
// 4. If ! IsValidEpochNanoseconds(epochNanoseconds) is false, throw a RangeError exception.
if (!is_valid_epoch_nanoseconds(*epoch_nanoseconds)) {
vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidEpochNanoseconds);
return {};
}
if (!is_valid_epoch_nanoseconds(*epoch_nanoseconds))
return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidEpochNanoseconds);
// 5. Return ! CreateTemporalInstant(epochNanoseconds).
return MUST(create_temporal_instant(global_object, *epoch_nanoseconds));
}
// 8.2.5 Temporal.Instant.fromEpochMicroseconds ( epochMicroseconds ), https://tc39.es/proposal-temporal/#sec-temporal.instant.fromepochmicroseconds
JS_DEFINE_OLD_NATIVE_FUNCTION(InstantConstructor::from_epoch_microseconds)
JS_DEFINE_NATIVE_FUNCTION(InstantConstructor::from_epoch_microseconds)
{
// 1. Set epochMicroseconds to ? ToBigInt(epochMicroseconds).
auto* epoch_microseconds = TRY_OR_DISCARD(vm.argument(0).to_bigint(global_object));
auto* epoch_microseconds = TRY(vm.argument(0).to_bigint(global_object));
// 2. Let epochNanoseconds be epochMicroseconds × 1000.
auto* epoch_nanoseconds = js_bigint(vm, epoch_microseconds->big_integer().multiplied_by(Crypto::UnsignedBigInteger { 1'000 }));
// 3. If ! IsValidEpochNanoseconds(epochNanoseconds) is false, throw a RangeError exception.
if (!is_valid_epoch_nanoseconds(*epoch_nanoseconds)) {
vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidEpochNanoseconds);
return {};
}
if (!is_valid_epoch_nanoseconds(*epoch_nanoseconds))
return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidEpochNanoseconds);
// 4. Return ! CreateTemporalInstant(epochNanoseconds).
return MUST(create_temporal_instant(global_object, *epoch_nanoseconds));
}
// 8.2.6 Temporal.Instant.fromEpochNanoseconds ( epochNanoseconds ), https://tc39.es/proposal-temporal/#sec-temporal.instant.fromepochnanoseconds
JS_DEFINE_OLD_NATIVE_FUNCTION(InstantConstructor::from_epoch_nanoseconds)
JS_DEFINE_NATIVE_FUNCTION(InstantConstructor::from_epoch_nanoseconds)
{
// 1. Set epochNanoseconds to ? ToBigInt(epochNanoseconds).
auto* epoch_nanoseconds = TRY_OR_DISCARD(vm.argument(0).to_bigint(global_object));
auto* epoch_nanoseconds = TRY(vm.argument(0).to_bigint(global_object));
// 2. If ! IsValidEpochNanoseconds(epochNanoseconds) is false, throw a RangeError exception.
if (!is_valid_epoch_nanoseconds(*epoch_nanoseconds)) {
vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidEpochNanoseconds);
return {};
}
if (!is_valid_epoch_nanoseconds(*epoch_nanoseconds))
return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidEpochNanoseconds);
// 3. Return ! CreateTemporalInstant(epochNanoseconds).
return MUST(create_temporal_instant(global_object, *epoch_nanoseconds));
}
// 8.2.7 Temporal.Instant.compare ( one, two ), https://tc39.es/proposal-temporal/#sec-temporal.instant.compare
JS_DEFINE_OLD_NATIVE_FUNCTION(InstantConstructor::compare)
JS_DEFINE_NATIVE_FUNCTION(InstantConstructor::compare)
{
// 1. Set one to ? ToTemporalInstant(one).
auto* one = TRY_OR_DISCARD(to_temporal_instant(global_object, vm.argument(0)));
auto* one = TRY(to_temporal_instant(global_object, vm.argument(0)));
// 2. Set two to ? ToTemporalInstant(two).
auto* two = TRY_OR_DISCARD(to_temporal_instant(global_object, vm.argument(1)));
auto* two = TRY(to_temporal_instant(global_object, vm.argument(1)));
// 3. Return 𝔽(! CompareEpochNanoseconds(one.[[Nanoseconds]], two.[[Nanoseconds]])).
return Value(compare_epoch_nanoseconds(one->nanoseconds(), two->nanoseconds()));