diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/InstantPrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/InstantPrototype.cpp index 0fcee65d6e..b4d80ec349 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/InstantPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/InstantPrototype.cpp @@ -42,6 +42,7 @@ void InstantPrototype::initialize(GlobalObject& global_object) 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.toJSON, to_json, 0, attr); define_native_function(vm.names.valueOf, value_of, 0, attr); } @@ -354,6 +355,23 @@ JS_DEFINE_NATIVE_FUNCTION(InstantPrototype::to_locale_string) return js_string(vm, *string); } +// 8.3.15 Temporal.Instant.prototype.toJSON ( ), https://tc39.es/proposal-temporal/#sec-temporal.instant.prototype.tojson +JS_DEFINE_NATIVE_FUNCTION(InstantPrototype::to_json) +{ + // 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 9f769f1713..6149e6a7f6 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/InstantPrototype.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/InstantPrototype.h @@ -29,6 +29,7 @@ private: JS_DECLARE_NATIVE_FUNCTION(equals); 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); }; diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/Instant/Instant.prototype.toJSON.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/Instant/Instant.prototype.toJSON.js new file mode 100644 index 0000000000..63f965ac49 --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/Instant/Instant.prototype.toJSON.js @@ -0,0 +1,18 @@ +describe("correct behavior", () => { + test("length is 0", () => { + expect(Temporal.Instant.prototype.toJSON).toHaveLength(0); + }); + + test("basic functionality", () => { + const instant = new Temporal.Instant(1625614921123456789n); + expect(instant.toJSON()).toBe("2021-07-06T23:42:01.123456789Z"); + }); +}); + +describe("errors", () => { + test("this value must be a Temporal.Instant object", () => { + expect(() => { + Temporal.Instant.prototype.toJSON.call("foo"); + }).toThrowWithMessage(TypeError, "Not a Temporal.Instant"); + }); +});