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

This commit is contained in:
Idan Horowitz 2021-07-11 21:23:38 +03:00 committed by Linus Groh
parent 84403ab423
commit 2382f67e0b
4 changed files with 41 additions and 0 deletions

View file

@ -118,6 +118,7 @@ namespace JS {
P(epochMilliseconds) \
P(epochNanoseconds) \
P(epochSeconds) \
P(equals) \
P(error) \
P(errors) \
P(escape) \

View file

@ -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)
{

View file

@ -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);
};

View file

@ -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");
});
});