From 7f0fe352e25a1be7022cf8f8fa1b7525de2540f8 Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Thu, 8 Jul 2021 20:43:15 +0100 Subject: [PATCH] LibJS: Implement Temporal.TimeZone.prototype.toJSON() --- .../LibJS/Runtime/Temporal/TimeZonePrototype.cpp | 11 +++++++++++ .../LibJS/Runtime/Temporal/TimeZonePrototype.h | 1 + .../Temporal/TimeZone/TimeZone.prototype.toJSON.js | 14 ++++++++++++++ 3 files changed, 26 insertions(+) create mode 100644 Userland/Libraries/LibJS/Tests/builtins/Temporal/TimeZone/TimeZone.prototype.toJSON.js diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/TimeZonePrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/TimeZonePrototype.cpp index 5a5c9cda37..e9d386b038 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/TimeZonePrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/TimeZonePrototype.cpp @@ -24,6 +24,7 @@ void TimeZonePrototype::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); // 11.4.2 Temporal.TimeZone.prototype[ @@toStringTag ], https://tc39.es/proposal-temporal/#sec-temporal.timezone.prototype-@@tostringtag define_direct_property(*vm.well_known_symbol_to_string_tag(), js_string(vm.heap(), "Temporal.TimeZone"), Attribute::Configurable); @@ -55,4 +56,14 @@ JS_DEFINE_NATIVE_FUNCTION(TimeZonePrototype::to_string) return js_string(vm, time_zone->identifier()); } +// 11.4.12 Temporal.TimeZone.prototype.toJSON ( ), https://tc39.es/proposal-temporal/#sec-temporal.timezone.prototype.tojson +JS_DEFINE_NATIVE_FUNCTION(TimeZonePrototype::to_json) +{ + // 1. Let timeZone be the this value. + auto time_zone = vm.this_value(global_object); + + // 2. Return ? ToString(timeZone). + return js_string(vm, time_zone.to_string(global_object)); +} + } diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/TimeZonePrototype.h b/Userland/Libraries/LibJS/Runtime/Temporal/TimeZonePrototype.h index 049a803042..d2f47962f7 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/TimeZonePrototype.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/TimeZonePrototype.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/TimeZone/TimeZone.prototype.toJSON.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/TimeZone/TimeZone.prototype.toJSON.js new file mode 100644 index 0000000000..aec6c23e2e --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/TimeZone/TimeZone.prototype.toJSON.js @@ -0,0 +1,14 @@ +describe("correct behavior", () => { + test("length is 0", () => { + expect(Temporal.TimeZone.prototype.toJSON).toHaveLength(0); + }); + + test("basic functionality", () => { + const timeZone = new Temporal.TimeZone("UTC"); + expect(timeZone.toJSON()).toBe("UTC"); + }); + + test("works with any this value", () => { + expect(Temporal.TimeZone.prototype.toJSON.call("foo")).toBe("foo"); + }); +});