LibJS: Implement Temporal.PlainMonthDay.prototype.valueOf

This commit is contained in:
Timothy Flynn 2024-11-20 18:12:23 -05:00
parent 9823f43827
commit d1a988930f
3 changed files with 20 additions and 0 deletions

View file

@ -40,6 +40,7 @@ void PlainMonthDayPrototype::initialize(Realm& realm)
define_native_function(realm, vm.names.toString, to_string, 0, attr);
define_native_function(realm, vm.names.toLocaleString, to_locale_string, 0, attr);
define_native_function(realm, vm.names.toJSON, to_json, 0, attr);
define_native_function(realm, vm.names.valueOf, value_of, 0, attr);
}
// 10.3.3 get Temporal.PlainMonthDay.prototype.calendarId, https://tc39.es/proposal-temporal/#sec-get-temporal.plainmonthday.prototype.calendarid
@ -172,4 +173,11 @@ JS_DEFINE_NATIVE_FUNCTION(PlainMonthDayPrototype::to_json)
return PrimitiveString::create(vm, temporal_month_day_to_string(month_day, ShowCalendar::Auto));
}
// 10.3.11 Temporal.PlainMonthDay.prototype.valueOf ( ), https://tc39.es/proposal-temporal/#sec-temporal.plainmonthday.prototype.valueof
JS_DEFINE_NATIVE_FUNCTION(PlainMonthDayPrototype::value_of)
{
// 1. Throw a TypeError exception.
return vm.throw_completion<TypeError>(ErrorType::Convert, "Temporal.PlainMonthDay", "a primitive value");
}
}

View file

@ -31,6 +31,7 @@ private:
JS_DECLARE_NATIVE_FUNCTION(to_string);
JS_DECLARE_NATIVE_FUNCTION(to_locale_string);
JS_DECLARE_NATIVE_FUNCTION(to_json);
JS_DECLARE_NATIVE_FUNCTION(value_of);
};
}

View file

@ -0,0 +1,11 @@
describe("errors", () => {
test("throws TypeError", () => {
const plainMonthDay = new Temporal.PlainMonthDay(7, 6);
expect(() => {
plainMonthDay.valueOf();
}).toThrowWithMessage(
TypeError,
"Cannot convert Temporal.PlainMonthDay to a primitive value"
);
});
});