From 04e2d215a1aec0aeecd9a1b43265d96deb18bf7f Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Thu, 15 Jul 2021 23:57:07 +0100 Subject: [PATCH] LibJS: Implement Temporal.Duration.prototype.microseconds --- .../Libraries/LibJS/Runtime/CommonPropertyNames.h | 1 + .../LibJS/Runtime/Temporal/DurationPrototype.cpp | 14 ++++++++++++++ .../LibJS/Runtime/Temporal/DurationPrototype.h | 1 + .../Duration/Duration.prototype.microseconds.js | 14 ++++++++++++++ 4 files changed, 30 insertions(+) create mode 100644 Userland/Libraries/LibJS/Tests/builtins/Temporal/Duration/Duration.prototype.microseconds.js diff --git a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h index a341e61499..4a8f7bf19e 100644 --- a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h +++ b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h @@ -237,6 +237,7 @@ namespace JS { P(map) \ P(max) \ P(message) \ + P(microseconds) \ P(milliseconds) \ P(min) \ P(minutes) \ diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/DurationPrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/DurationPrototype.cpp index e1fb08dc6d..7277bcac20 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/DurationPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/DurationPrototype.cpp @@ -33,6 +33,7 @@ void DurationPrototype::initialize(GlobalObject& global_object) define_native_accessor(vm.names.minutes, minutes_getter, {}, Attribute::Configurable); define_native_accessor(vm.names.seconds, seconds_getter, {}, Attribute::Configurable); define_native_accessor(vm.names.milliseconds, milliseconds_getter, {}, Attribute::Configurable); + define_native_accessor(vm.names.microseconds, microseconds_getter, {}, Attribute::Configurable); } static Duration* typed_this(GlobalObject& global_object) @@ -152,4 +153,17 @@ JS_DEFINE_NATIVE_FUNCTION(DurationPrototype::milliseconds_getter) return Value(duration->milliseconds()); } +// 7.3.11 get Temporal.Duration.prototype.microseconds, https://tc39.es/proposal-temporal/#sec-get-temporal.duration.prototype.microseconds +JS_DEFINE_NATIVE_FUNCTION(DurationPrototype::microseconds_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.[[Microseconds]]. + return Value(duration->microseconds()); +} + } diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/DurationPrototype.h b/Userland/Libraries/LibJS/Runtime/Temporal/DurationPrototype.h index cad8729ab6..2ca56107f9 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/DurationPrototype.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/DurationPrototype.h @@ -27,6 +27,7 @@ private: JS_DECLARE_NATIVE_FUNCTION(minutes_getter); JS_DECLARE_NATIVE_FUNCTION(seconds_getter); JS_DECLARE_NATIVE_FUNCTION(milliseconds_getter); + JS_DECLARE_NATIVE_FUNCTION(microseconds_getter); }; } diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/Duration/Duration.prototype.microseconds.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/Duration/Duration.prototype.microseconds.js new file mode 100644 index 0000000000..8abf810aef --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/Duration/Duration.prototype.microseconds.js @@ -0,0 +1,14 @@ +describe("correct behavior", () => { + test("basic functionality", () => { + const duration = new Temporal.Duration(0, 0, 0, 0, 0, 0, 0, 0, 123); + expect(duration.microseconds).toBe(123); + }); +}); + +test("errors", () => { + test("this value must be a Temporal.Duration object", () => { + expect(() => { + Reflect.get(Temporal.Duration.prototype, "microseconds", "foo"); + }).toThrowWithMessage(TypeError, "Not a Temporal.Duration"); + }); +});