diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/InstantPrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/InstantPrototype.cpp index fa9e6d1825..0fcee65d6e 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/InstantPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/InstantPrototype.cpp @@ -41,6 +41,7 @@ void InstantPrototype::initialize(GlobalObject& global_object) define_native_function(vm.names.round, round, 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.toLocaleString, to_locale_string, 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); } +// 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 JS_DEFINE_NATIVE_FUNCTION(InstantPrototype::value_of) { diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/InstantPrototype.h b/Userland/Libraries/LibJS/Runtime/Temporal/InstantPrototype.h index 49f716a36d..9f769f1713 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/InstantPrototype.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/InstantPrototype.h @@ -28,6 +28,7 @@ private: JS_DECLARE_NATIVE_FUNCTION(round); JS_DECLARE_NATIVE_FUNCTION(equals); JS_DECLARE_NATIVE_FUNCTION(to_string); + JS_DECLARE_NATIVE_FUNCTION(to_locale_string); JS_DECLARE_NATIVE_FUNCTION(value_of); }; diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/Instant/Instant.prototype.toLocaleString.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/Instant/Instant.prototype.toLocaleString.js new file mode 100644 index 0000000000..8877cac635 --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/Instant/Instant.prototype.toLocaleString.js @@ -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"); + }); +});