From 51c581f604e04dd1c5a19b64ec31d5a3cbaf5638 Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Thu, 8 Jul 2021 20:51:57 +0100 Subject: [PATCH] LibJS: Implement Temporal.TimeZone.prototype.id --- .../Libraries/LibJS/Runtime/CommonPropertyNames.h | 1 + .../LibJS/Runtime/Temporal/TimeZonePrototype.cpp | 11 +++++++++++ .../LibJS/Runtime/Temporal/TimeZonePrototype.h | 1 + .../Temporal/TimeZone/TimeZone.prototype.id.js | 10 ++++++++++ 4 files changed, 23 insertions(+) create mode 100644 Userland/Libraries/LibJS/Tests/builtins/Temporal/TimeZone/TimeZone.prototype.id.js diff --git a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h index a9bb598d967..1abea95c078 100644 --- a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h +++ b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h @@ -183,6 +183,7 @@ namespace JS { P(hasOwn) \ P(hasOwnProperty) \ P(hypot) \ + P(id) \ P(ignoreCase) \ P(imul) \ P(includes) \ diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/TimeZonePrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/TimeZonePrototype.cpp index e9d386b038e..bbaf036d96f 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/TimeZonePrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/TimeZonePrototype.cpp @@ -23,6 +23,7 @@ void TimeZonePrototype::initialize(GlobalObject& global_object) auto& vm = this->vm(); u8 attr = Attribute::Writable | Attribute::Configurable; + define_native_accessor(vm.names.id, id_getter, {}, Attribute::Configurable); define_native_function(vm.names.toString, to_string, 0, attr); define_native_function(vm.names.toJSON, to_json, 0, attr); @@ -43,6 +44,16 @@ static TimeZone* typed_this(GlobalObject& global_object) return static_cast(this_object); } +// 11.4.3 get Temporal.TimeZone.prototype.id, https://tc39.es/proposal-temporal/#sec-get-temporal.timezone.prototype.id +JS_DEFINE_NATIVE_FUNCTION(TimeZonePrototype::id_getter) +{ + // 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)); +} + // 11.4.11 Temporal.TimeZone.prototype.toString ( ), https://tc39.es/proposal-temporal/#sec-temporal.timezone.prototype.tostring JS_DEFINE_NATIVE_FUNCTION(TimeZonePrototype::to_string) { diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/TimeZonePrototype.h b/Userland/Libraries/LibJS/Runtime/Temporal/TimeZonePrototype.h index d2f47962f7b..1eceb151865 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/TimeZonePrototype.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/TimeZonePrototype.h @@ -19,6 +19,7 @@ public: virtual ~TimeZonePrototype() override = default; private: + JS_DECLARE_NATIVE_FUNCTION(id_getter); JS_DECLARE_NATIVE_FUNCTION(to_string); JS_DECLARE_NATIVE_FUNCTION(to_json); }; diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/TimeZone/TimeZone.prototype.id.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/TimeZone/TimeZone.prototype.id.js new file mode 100644 index 00000000000..7a85daf14cc --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/TimeZone/TimeZone.prototype.id.js @@ -0,0 +1,10 @@ +describe("correct behavior", () => { + test("basic functionality", () => { + const timeZone = new Temporal.TimeZone("UTC"); + expect(timeZone.id).toBe("UTC"); + }); + + test("works with any this value", () => { + expect(Reflect.get(Temporal.TimeZone.prototype, "id", "foo")).toBe("foo"); + }); +});