diff options
author | Idan Horowitz <idan.horowitz@gmail.com> | 2021-07-11 21:18:48 +0300 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2021-07-12 19:05:17 +0100 |
commit | 33cf6274e87ac7fcc4eadd07cc453694b98bd8e8 (patch) | |
tree | 54272e6c374b2d6d62d741c425379e9723618f49 /Userland/Libraries/LibJS/Runtime | |
parent | b816037739e323f331f43c33a8eb808cd933c465 (diff) | |
download | serenity-33cf6274e87ac7fcc4eadd07cc453694b98bd8e8.zip |
LibJS: Add Temporal.Instant.compare()
Diffstat (limited to 'Userland/Libraries/LibJS/Runtime')
5 files changed, 36 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h index fc712db5e4..5e664423dc 100644 --- a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h +++ b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h @@ -92,6 +92,7 @@ namespace JS { P(console) \ P(construct) \ P(constructor) \ + P(compare) \ P(copyWithin) \ P(cos) \ P(cosh) \ diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/Instant.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/Instant.cpp index 409992417a..f1f87a13c1 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/Instant.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/Instant.cpp @@ -138,4 +138,19 @@ BigInt* parse_temporal_instant(GlobalObject& global_object, String const& iso_st return js_bigint(vm.heap(), utc->big_integer().minus(Crypto::SignedBigInteger::create_from(offset_nanoseconds))); } +// 8.5.5 CompareEpochNanoseconds ( epochNanosecondsOne, epochNanosecondsTwo ) +i32 compare_epoch_nanoseconds(BigInt const& epoch_nanoseconds_one, BigInt const& epoch_nanoseconds_two) +{ + // 1. If epochNanosecondsOne > epochNanosecondsTwo, return 1. + if (epoch_nanoseconds_one.big_integer() > epoch_nanoseconds_two.big_integer()) + return 1; + + // 2. If epochNanosecondsOne < epochNanosecondsTwo, return -1. + if (epoch_nanoseconds_one.big_integer() < epoch_nanoseconds_two.big_integer()) + return -1; + + // 3. Return 0. + return 0; +} + } diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/Instant.h b/Userland/Libraries/LibJS/Runtime/Temporal/Instant.h index 2c8a0e7871..631cea06ef 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/Instant.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/Instant.h @@ -40,5 +40,6 @@ bool is_valid_epoch_nanoseconds(BigInt const& epoch_nanoseconds); Instant* create_temporal_instant(GlobalObject&, BigInt& nanoseconds, FunctionObject* new_target = nullptr); Instant* to_temporal_instant(GlobalObject&, Value item); BigInt* parse_temporal_instant(GlobalObject&, String const& iso_string); +i32 compare_epoch_nanoseconds(BigInt const&, BigInt const&); } diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/InstantConstructor.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/InstantConstructor.cpp index a40e81dc94..f59dabdf1a 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/InstantConstructor.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/InstantConstructor.cpp @@ -31,6 +31,7 @@ void InstantConstructor::initialize(GlobalObject& global_object) 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); } @@ -158,4 +159,21 @@ JS_DEFINE_NATIVE_FUNCTION(InstantConstructor::from_epoch_nanoseconds) return 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_NATIVE_FUNCTION(InstantConstructor::compare) +{ + // 1. Set one to ? ToTemporalInstant(one). + auto* one = to_temporal_instant(global_object, vm.argument(0)); + if (vm.exception()) + return {}; + + // 2. Set two to ? ToTemporalInstant(two). + auto* two = to_temporal_instant(global_object, vm.argument(1)); + if (vm.exception()) + return {}; + + // 3. Return 𝔽(! CompareEpochNanoseconds(one.[[Nanoseconds]], two.[[Nanoseconds]])). + return Value(compare_epoch_nanoseconds(one->nanoseconds(), two->nanoseconds())); +} + } diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/InstantConstructor.h b/Userland/Libraries/LibJS/Runtime/Temporal/InstantConstructor.h index 44f502d90b..1a7a700234 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/InstantConstructor.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/InstantConstructor.h @@ -28,6 +28,7 @@ private: JS_DECLARE_NATIVE_FUNCTION(from_epoch_milliseconds); JS_DECLARE_NATIVE_FUNCTION(from_epoch_microseconds); JS_DECLARE_NATIVE_FUNCTION(from_epoch_nanoseconds); + JS_DECLARE_NATIVE_FUNCTION(compare); }; } |