Ver Fonte

LibJS: Add Temporal.Instant.prototype.equals()

Idan Horowitz há 4 anos atrás
pai
commit
2382f67e0b

+ 1 - 0
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)                                \

+ 23 - 0
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)
 {

+ 1 - 0
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);
 };
 

+ 16 - 0
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");
+    });
+});