LibJS: Implement Temporal.PlainMonthDay.prototype.equals

This commit is contained in:
Luke Wilde 2021-09-10 17:45:32 +01:00 committed by Linus Groh
parent 2d5b15295a
commit 3548b08de2
Notes: sideshowbarker 2024-07-18 04:19:10 +09:00
3 changed files with 47 additions and 0 deletions

View file

@ -33,6 +33,7 @@ void PlainMonthDayPrototype::initialize(GlobalObject& global_object)
define_native_accessor(vm.names.day, day_getter, {}, Attribute::Configurable);
u8 attr = Attribute::Writable | Attribute::Configurable;
define_native_function(vm.names.equals, equals, 1, attr);
define_native_function(vm.names.toString, to_string, 0, attr);
define_native_function(vm.names.toLocaleString, to_locale_string, 0, attr);
define_native_function(vm.names.toJSON, to_json, 0, attr);
@ -98,6 +99,36 @@ JS_DEFINE_NATIVE_FUNCTION(PlainMonthDayPrototype::day_getter)
return Value(calendar_day(global_object, calendar, *month_day));
}
// 10.3.7 Temporal.PlainMonthDay.prototype.equals ( other ), https://tc39.es/proposal-temporal/#sec-temporal.plainmonthday.prototype.equals
JS_DEFINE_NATIVE_FUNCTION(PlainMonthDayPrototype::equals)
{
// 1. Let monthDay be the this value.
// 2. Perform ? RequireInternalSlot(monthDay, [[InitializedTemporalMonthDay]]).
auto* month_day = typed_this(global_object);
if (vm.exception())
return {};
// 3. Set other to ? ToTemporalMonthDay(other).
auto* other = to_temporal_month_day(global_object, vm.argument(0));
if (vm.exception())
return {};
// 4. If monthDay.[[ISOMonth]] ≠ other.[[ISOMonth]], return false.
if (month_day->iso_month() != other->iso_month())
return Value(false);
// 5. If monthDay.[[ISODay]] ≠ other.[[ISODay]], return false.
if (month_day->iso_day() != other->iso_day())
return Value(false);
// 6. If monthDay.[[ISOYear]] ≠ other.[[ISOYear]], return false.
if (month_day->iso_year() != other->iso_year())
return Value(false);
// 7. Return ? CalendarEquals(monthDay.[[Calendar]], other.[[Calendar]]).
return Value(calendar_equals(global_object, month_day->calendar(), other->calendar()));
}
// 10.3.8 Temporal.PlainMonthDay.prototype.toString ( [ options ] ), https://tc39.es/proposal-temporal/#sec-temporal.plainmonthday.prototype.tostring
JS_DEFINE_NATIVE_FUNCTION(PlainMonthDayPrototype::to_string)
{

View file

@ -22,6 +22,7 @@ private:
JS_DECLARE_NATIVE_FUNCTION(calendar_getter);
JS_DECLARE_NATIVE_FUNCTION(month_code_getter);
JS_DECLARE_NATIVE_FUNCTION(day_getter);
JS_DECLARE_NATIVE_FUNCTION(equals);
JS_DECLARE_NATIVE_FUNCTION(to_string);
JS_DECLARE_NATIVE_FUNCTION(to_locale_string);
JS_DECLARE_NATIVE_FUNCTION(to_json);

View file

@ -0,0 +1,15 @@
describe("correct behavior", () => {
test("length is 1", () => {
expect(Temporal.PlainMonthDay.prototype.equals).toHaveLength(1);
});
test("basic functionality", () => {
const calendar = { hello: "friends" };
const firstPlainMonthDay = new Temporal.PlainMonthDay(2, 1, calendar);
const secondPlainMonthDay = new Temporal.PlainMonthDay(1, 1, calendar);
expect(firstPlainMonthDay.equals(firstPlainMonthDay)).toBeTrue();
expect(secondPlainMonthDay.equals(secondPlainMonthDay)).toBeTrue();
expect(firstPlainMonthDay.equals(secondPlainMonthDay)).toBeFalse();
expect(secondPlainMonthDay.equals(firstPlainMonthDay)).toBeFalse();
});
});