From c1c8d7861c42b63de7c757bd19bbb9bed81d75f1 Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Thu, 19 Aug 2021 00:49:12 +0100 Subject: [PATCH] LibJS: Implement Temporal.PlainYearMonth.prototype.toJSON() --- .../Temporal/PlainYearMonthPrototype.cpp | 18 +++++++++++++++ .../Temporal/PlainYearMonthPrototype.h | 1 + .../PlainYearMonth.prototype.toJSON.js | 23 +++++++++++++++++++ 3 files changed, 42 insertions(+) create mode 100644 Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainYearMonth/PlainYearMonth.prototype.toJSON.js diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.cpp index 7404d9a522..0f8bd28c8b 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.cpp @@ -40,6 +40,7 @@ void PlainYearMonthPrototype::initialize(GlobalObject& global_object) u8 attr = Attribute::Writable | Attribute::Configurable; 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); define_native_function(vm.names.getISOFields, get_iso_fields, 0, attr); } @@ -227,6 +228,23 @@ JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthPrototype::to_locale_string) return js_string(vm, *string); } +// 9.3.19 Temporal.PlainYearMonth.prototype.toJSON ( ), https://tc39.es/proposal-temporal/#sec-temporal.plainyearmonth.prototype.tojson +JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthPrototype::to_json) +{ + // 1. Let yearMonth be the this value. + // 2. Perform ? RequireInternalSlot(yearMonth, [[InitializedTemporalYearMonth]]). + auto* year_month = typed_this(global_object); + if (vm.exception()) + return {}; + + // 3. Return ? TemporalYearMonthToString(yearMonth, "auto"). + auto string = temporal_year_month_to_string(global_object, *year_month, "auto"sv); + if (vm.exception()) + return {}; + + return js_string(vm, *string); +} + // 9.3.20 Temporal.PlainYearMonth.prototype.valueOf ( ), https://tc39.es/proposal-temporal/#sec-temporal.plainyearmonth.prototype.valueof JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthPrototype::value_of) { diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.h b/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.h index feb9130be3..5d9472338c 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.h @@ -29,6 +29,7 @@ private: JS_DECLARE_NATIVE_FUNCTION(in_leap_year_getter); 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(get_iso_fields); }; diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainYearMonth/PlainYearMonth.prototype.toJSON.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainYearMonth/PlainYearMonth.prototype.toJSON.js new file mode 100644 index 0000000000..dff2d5679d --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainYearMonth/PlainYearMonth.prototype.toJSON.js @@ -0,0 +1,23 @@ +describe("correct behavior", () => { + test("length is 0", () => { + expect(Temporal.PlainYearMonth.prototype.toJSON).toHaveLength(0); + }); + + test("basic functionality", () => { + let plainYearMonth; + + plainYearMonth = new Temporal.PlainYearMonth(2021, 7); + expect(plainYearMonth.toJSON()).toBe("2021-07"); + + plainYearMonth = new Temporal.PlainYearMonth(2021, 7, { toString: () => "foo" }, 6); + expect(plainYearMonth.toJSON()).toBe("2021-07-06[u-ca=foo]"); + }); +}); + +describe("errors", () => { + test("this value must be a Temporal.PlainYearMonth object", () => { + expect(() => { + Temporal.PlainYearMonth.prototype.toJSON.call("foo"); + }).toThrowWithMessage(TypeError, "Not a Temporal.PlainYearMonth"); + }); +});