mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 20:17:44 +00:00
LibJS: Add Temporal.Instant.compare()
This commit is contained in:
parent
b816037739
commit
33cf6274e8
6 changed files with 49 additions and 0 deletions
|
@ -92,6 +92,7 @@ namespace JS {
|
|||
P(console) \
|
||||
P(construct) \
|
||||
P(constructor) \
|
||||
P(compare) \
|
||||
P(copyWithin) \
|
||||
P(cos) \
|
||||
P(cosh) \
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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&);
|
||||
|
||||
}
|
||||
|
|
|
@ -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()));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
describe("correct behavior", () => {
|
||||
test("length is 1", () => {
|
||||
expect(Temporal.Instant.compare).toHaveLength(2);
|
||||
});
|
||||
|
||||
test("basic functionality", () => {
|
||||
const instant1 = new Temporal.Instant(111n);
|
||||
expect(Temporal.Instant.compare(instant1, instant1)).toBe(0);
|
||||
const instant2 = new Temporal.Instant(999n);
|
||||
expect(Temporal.Instant.compare(instant1, instant2)).toBe(-1);
|
||||
expect(Temporal.Instant.compare(instant2, instant1)).toBe(1);
|
||||
});
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue