diff --git a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h index 5e664423dc4..2c047727f89 100644 --- a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h +++ b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h @@ -118,6 +118,7 @@ namespace JS { P(epochMilliseconds) \ P(epochNanoseconds) \ P(epochSeconds) \ + P(equals) \ P(error) \ P(errors) \ P(escape) \ diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/InstantPrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/InstantPrototype.cpp index 4535502ab93..abd7a9305b5 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/InstantPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/InstantPrototype.cpp @@ -33,6 +33,7 @@ void InstantPrototype::initialize(GlobalObject& global_object) u8 attr = Attribute::Writable | Attribute::Configurable; define_native_function(vm.names.valueOf, value_of, 0, attr); + define_native_function(vm.names.equals, equals, 1, attr); } static Instant* typed_this(GlobalObject& global_object) @@ -121,6 +122,28 @@ JS_DEFINE_NATIVE_FUNCTION(InstantPrototype::epoch_nanoseconds_getter) return &ns; } +// 8.3.12 Temporal.Instant.prototype.equals ( other ), https://tc39.es/proposal-temporal/#sec-temporal.instant.prototype.equals +JS_DEFINE_NATIVE_FUNCTION(InstantPrototype::equals) +{ + // 1. Let instant be the this value. + // 2. Perform ? RequireInternalSlot(instant, [[InitializedTemporalInstant]]). + auto* instant = typed_this(global_object); + if (vm.exception()) + return {}; + + // 3. Set other to ? ToTemporalInstant(other). + auto other = to_temporal_instant(global_object, vm.argument(0)); + if (vm.exception()) + return {}; + + // 4. If instant.[[Nanoseconds]] ≠ other.[[Nanoseconds]], return false. + if (instant->nanoseconds().big_integer() != other->nanoseconds().big_integer()) + return Value(false); + + // 5. Return true. + return Value(true); +} + // 8.3.16 Temporal.Instant.prototype.valueOf ( ), https://tc39.es/proposal-temporal/#sec-temporal.instant.prototype.valueof JS_DEFINE_NATIVE_FUNCTION(InstantPrototype::value_of) { diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/InstantPrototype.h b/Userland/Libraries/LibJS/Runtime/Temporal/InstantPrototype.h index 1f81ad62a01..f6a69e4346e 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/InstantPrototype.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/InstantPrototype.h @@ -24,6 +24,7 @@ private: JS_DECLARE_NATIVE_FUNCTION(epoch_microseconds_getter); JS_DECLARE_NATIVE_FUNCTION(epoch_nanoseconds_getter); + JS_DECLARE_NATIVE_FUNCTION(equals); JS_DECLARE_NATIVE_FUNCTION(value_of); }; diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/Instant/Instant.prototype.equals.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/Instant/Instant.prototype.equals.js new file mode 100644 index 00000000000..1e42d6f2392 --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/Instant/Instant.prototype.equals.js @@ -0,0 +1,16 @@ +describe("correct behavior", () => { + test("basic functionality", () => { + const instant1 = new Temporal.Instant(111n); + expect(instant1.equals(instant1)); + const instant2 = new Temporal.Instant(999n); + expect(!instant1.equals(instant2)); + }); +}); + +test("errors", () => { + test("this value must be a Temporal.Instant object", () => { + expect(() => { + Temporal.Instant.prototype.equals.call("foo", 1, 2); + }).toThrowWithMessage(TypeError, "Not a Temporal.Instant"); + }); +});