LibJS: Implement Temporal.PlainYearMonth.prototype.valueOf

This commit is contained in:
Timothy Flynn 2024-11-21 19:11:16 -05:00 committed by Andreas Kling
parent 35f22dcf79
commit b64ccb95ec
Notes: github-actions[bot] 2024-11-22 18:56:17 +00:00
3 changed files with 20 additions and 0 deletions
Libraries/LibJS

View file

@ -50,6 +50,7 @@ void PlainYearMonthPrototype::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);
}
// 9.3.3 get Temporal.PlainYearMonth.prototype.calendarId, https://tc39.es/proposal-temporal/#sec-get-temporal.plainyearmonth.prototype.calendarid
@ -289,4 +290,11 @@ JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthPrototype::to_json)
return PrimitiveString::create(vm, temporal_year_month_to_string(year_month, ShowCalendar::Auto));
}
// 9.3.22 Temporal.PlainYearMonth.prototype.valueOf ( ), https://tc39.es/proposal-temporal/#sec-temporal.plainyearmonth.prototype.valueof
JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthPrototype::value_of)
{
// 1. Throw a TypeError exception.
return vm.throw_completion<TypeError>(ErrorType::Convert, "Temporal.PlainYearMonth", "a primitive value");
}
}

View file

@ -42,6 +42,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 plainYearMonth = new Temporal.PlainYearMonth(2021, 7);
expect(() => {
plainYearMonth.valueOf();
}).toThrowWithMessage(
TypeError,
"Cannot convert Temporal.PlainYearMonth to a primitive value"
);
});
});