LibJS: Implement Temporal.PlainDate.prototype.valueOf

This commit is contained in:
Timothy Flynn 2024-11-22 15:18:58 -05:00 committed by Andreas Kling
parent f8b593a7df
commit 06ef32818e
Notes: github-actions[bot] 2024-11-23 13:47:36 +00:00
3 changed files with 16 additions and 0 deletions

View file

@ -62,6 +62,7 @@ void PlainDatePrototype::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);
}
// 3.3.3 get Temporal.PlainDate.prototype.calendarId, https://tc39.es/proposal-temporal/#sec-get-temporal.plaindate.prototype.calendarid
@ -401,4 +402,11 @@ JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::to_json)
return PrimitiveString::create(vm, temporal_date_to_string(temporal_date, ShowCalendar::Auto));
}
// 3.3.33 Temporal.PlainDate.prototype.valueOf ( ), https://tc39.es/proposal-temporal/#sec-temporal.plaindate.prototype.valueof
JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::value_of)
{
// 1. Throw a TypeError exception.
return vm.throw_completion<TypeError>(ErrorType::Convert, "Temporal.PlainDate", "a primitive value");
}
}

View file

@ -51,6 +51,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,7 @@
describe("errors", () => {
test("throws TypeError", () => {
expect(() => {
new Temporal.PlainDate(2021, 7, 21).valueOf();
}).toThrowWithMessage(TypeError, "Cannot convert Temporal.PlainDate to a primitive value");
});
});