From 300a22f9b9f289f4e4a0733facbe1cad4c8712f4 Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Thu, 15 Jul 2021 23:34:53 +0100 Subject: [PATCH] LibJS: Implement Temporal.Duration.prototype.years --- .../LibJS/Runtime/CommonPropertyNames.h | 3 +- .../Runtime/Temporal/DurationPrototype.cpp | 29 +++++++++++++++++++ .../Runtime/Temporal/DurationPrototype.h | 3 ++ .../Duration/Duration.prototype.years.js | 14 +++++++++ 4 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 Userland/Libraries/LibJS/Tests/builtins/Temporal/Duration/Duration.prototype.years.js diff --git a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h index b555d8501c798c..80e03f834707a2 100644 --- a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h +++ b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h @@ -352,7 +352,8 @@ namespace JS { P(valueOf) \ P(values) \ P(warn) \ - P(writable) + P(writable) \ + P(years) struct CommonPropertyNames { PropertyName and_ { "and", PropertyName::StringMayBeNumber::No }; diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/DurationPrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/DurationPrototype.cpp index 7310ed3a14c581..7f6d72da86b9e2 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/DurationPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/DurationPrototype.cpp @@ -5,6 +5,7 @@ */ #include +#include #include namespace JS::Temporal { @@ -23,6 +24,34 @@ void DurationPrototype::initialize(GlobalObject& global_object) // 7.3.2 Temporal.Duration.prototype[ @@toStringTag ], https://tc39.es/proposal-temporal/#sec-temporal.duration.prototype-@@tostringtag define_direct_property(*vm.well_known_symbol_to_string_tag(), js_string(vm.heap(), "Temporal.Duration"), Attribute::Configurable); + + define_native_accessor(vm.names.years, years_getter, {}, Attribute::Configurable); +} + +static Duration* typed_this(GlobalObject& global_object) +{ + auto& vm = global_object.vm(); + auto* this_object = vm.this_value(global_object).to_object(global_object); + if (!this_object) + return {}; + if (!is(this_object)) { + vm.throw_exception(global_object, ErrorType::NotA, "Temporal.Duration"); + return {}; + } + return static_cast(this_object); +} + +// 7.3.3 get Temporal.Duration.prototype.years, https://tc39.es/proposal-temporal/#sec-get-temporal.duration.prototype.years +JS_DEFINE_NATIVE_FUNCTION(DurationPrototype::years_getter) +{ + // 1. Let duration be the this value. + // 2. Perform ? RequireInternalSlot(duration, [[InitializedTemporalDuration]]). + auto* duration = typed_this(global_object); + if (vm.exception()) + return {}; + + // 3. Return duration.[[Years]]. + return Value(duration->years()); } } diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/DurationPrototype.h b/Userland/Libraries/LibJS/Runtime/Temporal/DurationPrototype.h index b758b546aa149c..ee5b1f7ab89179 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/DurationPrototype.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/DurationPrototype.h @@ -17,6 +17,9 @@ class DurationPrototype final : public Object { explicit DurationPrototype(GlobalObject&); virtual void initialize(GlobalObject&) override; virtual ~DurationPrototype() override = default; + +private: + JS_DECLARE_NATIVE_FUNCTION(years_getter); }; } diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/Duration/Duration.prototype.years.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/Duration/Duration.prototype.years.js new file mode 100644 index 00000000000000..3b59d4c9ee7057 --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/Duration/Duration.prototype.years.js @@ -0,0 +1,14 @@ +describe("correct behavior", () => { + test("basic functionality", () => { + const duration = new Temporal.Duration(123); + expect(duration.years).toBe(123); + }); +}); + +test("errors", () => { + test("this value must be a Temporal.Duration object", () => { + expect(() => { + Reflect.get(Temporal.Duration.prototype, "years", "foo"); + }).toThrowWithMessage(TypeError, "Not a Temporal.Duration"); + }); +});