LibJS: Implement Temporal.PlainDateTime.prototype.valueOf

This commit is contained in:
Timothy Flynn 2024-11-23 19:16:11 -05:00 committed by Andreas Kling
parent 649328fed2
commit d22ea4db4c
Notes: github-actions[bot] 2024-11-24 10:44:57 +00:00
3 changed files with 19 additions and 0 deletions

View file

@ -68,6 +68,7 @@ void PlainDateTimePrototype::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);
define_native_function(realm, vm.names.toPlainDate, to_plain_date, 0, attr);
define_native_function(realm, vm.names.toPlainTime, to_plain_time, 0, attr);
}
@ -550,6 +551,13 @@ JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::to_json)
return PrimitiveString::create(vm, iso_date_time_to_string(date_time->iso_date_time(), date_time->calendar(), Auto {}, ShowCalendar::Auto));
}
// 5.3.37 Temporal.PlainDateTime.prototype.valueOf ( ), https://tc39.es/proposal-temporal/#sec-temporal.plaindatetime.prototype.valueof
JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::value_of)
{
// 1. Throw a TypeError exception.
return vm.throw_completion<TypeError>(ErrorType::Convert, "Temporal.PlainDateTime", "a primitive value");
}
// 5.3.39 Temporal.PlainDateTime.prototype.toPlainDate ( ), https://tc39.es/proposal-temporal/#sec-temporal.plaindatetime.prototype.toplaindate
JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::to_plain_date)
{

View file

@ -57,6 +57,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);
JS_DECLARE_NATIVE_FUNCTION(to_plain_date);
JS_DECLARE_NATIVE_FUNCTION(to_plain_time);
};

View file

@ -0,0 +1,10 @@
describe("errors", () => {
test("throws TypeError", () => {
expect(() => {
new Temporal.PlainDateTime(2021, 7, 22, 19, 54, 38).valueOf();
}).toThrowWithMessage(
TypeError,
"Cannot convert Temporal.PlainDateTime to a primitive value"
);
});
});