From 3ee169d8e776307c79f36522095b23b1af1e5513 Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Wed, 14 Jul 2021 21:16:40 +0100 Subject: [PATCH] LibJS: Implement Temporal.Calendar.prototype.toJSON() --- .../LibJS/Runtime/Temporal/CalendarPrototype.cpp | 11 +++++++++++ .../LibJS/Runtime/Temporal/CalendarPrototype.h | 1 + .../Temporal/Calendar/Calendar.prototype.toJSON.js | 14 ++++++++++++++ 3 files changed, 26 insertions(+) create mode 100644 Userland/Libraries/LibJS/Tests/builtins/Temporal/Calendar/Calendar.prototype.toJSON.js diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.cpp index 7f16ead01b..c9328811ab 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.cpp @@ -27,6 +27,7 @@ void CalendarPrototype::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.toJSON, to_json, 0, attr); } static Calendar* typed_this(GlobalObject& global_object) @@ -55,4 +56,14 @@ JS_DEFINE_NATIVE_FUNCTION(CalendarPrototype::to_string) return js_string(vm, calendar->identifier()); } +// 12.4.24 Temporal.Calendar.prototype.toJSON ( ), https://tc39.es/proposal-temporal/#sec-temporal.calendar.prototype.tojson +JS_DEFINE_NATIVE_FUNCTION(CalendarPrototype::to_json) +{ + // 1. Let calendar be the this value. + auto calendar = vm.this_value(global_object); + + // 2. Return ? ToString(calendar). + return js_string(vm, calendar.to_string(global_object)); +} + } diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.h b/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.h index aa48b08ada..229e63d749 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.h @@ -20,6 +20,7 @@ public: private: JS_DECLARE_NATIVE_FUNCTION(to_string); + JS_DECLARE_NATIVE_FUNCTION(to_json); }; } diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/Calendar/Calendar.prototype.toJSON.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/Calendar/Calendar.prototype.toJSON.js new file mode 100644 index 0000000000..703283b897 --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/Calendar/Calendar.prototype.toJSON.js @@ -0,0 +1,14 @@ +describe("correct behavior", () => { + test("length is 0", () => { + expect(Temporal.Calendar.prototype.toJSON).toHaveLength(0); + }); + + test("basic functionality", () => { + const calendar = new Temporal.Calendar("iso8601"); + expect(calendar.toJSON()).toBe("iso8601"); + }); + + test("works with any this value", () => { + expect(Temporal.Calendar.prototype.toJSON.call("foo")).toBe("foo"); + }); +});