1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 12:17:44 +00:00

LibJS: Implement Temporal.Instant.prototype.toLocaleString()

This commit is contained in:
Linus Groh 2021-08-31 00:22:00 +01:00 committed by Andreas Kling
parent 576be0f8e7
commit 463eb361ad
3 changed files with 38 additions and 0 deletions

View file

@ -41,6 +41,7 @@ void InstantPrototype::initialize(GlobalObject& global_object)
define_native_function(vm.names.round, round, 1, attr); define_native_function(vm.names.round, round, 1, attr);
define_native_function(vm.names.equals, equals, 1, attr); define_native_function(vm.names.equals, equals, 1, attr);
define_native_function(vm.names.toString, to_string, 0, 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.valueOf, value_of, 0, attr); define_native_function(vm.names.valueOf, value_of, 0, attr);
} }
@ -335,6 +336,24 @@ JS_DEFINE_NATIVE_FUNCTION(InstantPrototype::to_string)
return js_string(vm, *string); return js_string(vm, *string);
} }
// 8.3.14 Temporal.Instant.prototype.toLocaleString ( [ locales [ , options ] ] ), https://tc39.es/proposal-temporal/#sec-temporal.instant.prototype.tolocalestring
// NOTE: This is the minimum toLocaleString implementation for engines without ECMA-402.
JS_DEFINE_NATIVE_FUNCTION(InstantPrototype::to_locale_string)
{
// 1. Let instant be the this value.
// 2. Perform ? RequireInternalSlot(instant, [[InitializedTemporalInstant]]).
auto* instant = typed_this(global_object);
if (vm.exception())
return {};
// 3. Return ? TemporalInstantToString(instant, undefined, "auto").
auto string = temporal_instant_to_string(global_object, *instant, js_undefined(), String { "auto"sv });
if (vm.exception())
return {};
return js_string(vm, *string);
}
// 8.3.16 Temporal.Instant.prototype.valueOf ( ), https://tc39.es/proposal-temporal/#sec-temporal.instant.prototype.valueof // 8.3.16 Temporal.Instant.prototype.valueOf ( ), https://tc39.es/proposal-temporal/#sec-temporal.instant.prototype.valueof
JS_DEFINE_NATIVE_FUNCTION(InstantPrototype::value_of) JS_DEFINE_NATIVE_FUNCTION(InstantPrototype::value_of)
{ {

View file

@ -28,6 +28,7 @@ private:
JS_DECLARE_NATIVE_FUNCTION(round); JS_DECLARE_NATIVE_FUNCTION(round);
JS_DECLARE_NATIVE_FUNCTION(equals); JS_DECLARE_NATIVE_FUNCTION(equals);
JS_DECLARE_NATIVE_FUNCTION(to_string); JS_DECLARE_NATIVE_FUNCTION(to_string);
JS_DECLARE_NATIVE_FUNCTION(to_locale_string);
JS_DECLARE_NATIVE_FUNCTION(value_of); JS_DECLARE_NATIVE_FUNCTION(value_of);
}; };

View file

@ -0,0 +1,18 @@
describe("correct behavior", () => {
test("length is 0", () => {
expect(Temporal.Instant.prototype.toLocaleString).toHaveLength(0);
});
test("basic functionality", () => {
const instant = new Temporal.Instant(1625614921123456789n);
expect(instant.toLocaleString()).toBe("2021-07-06T23:42:01.123456789Z");
});
});
describe("errors", () => {
test("this value must be a Temporal.Instant object", () => {
expect(() => {
Temporal.Instant.prototype.toLocaleString.call("foo");
}).toThrowWithMessage(TypeError, "Not a Temporal.Instant");
});
});