From d3bed13f4ba7c105bc96119c8d498b2dd35a240a Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Wed, 28 Jul 2021 19:07:29 +0100 Subject: [PATCH] LibJS: Implement Temporal.PlainTime.prototype.nanosecond --- .../Libraries/LibJS/Runtime/CommonPropertyNames.h | 1 + .../LibJS/Runtime/Temporal/PlainTimePrototype.cpp | 14 ++++++++++++++ .../LibJS/Runtime/Temporal/PlainTimePrototype.h | 1 + .../PlainTime/PlainTime.prototype.nanosecond.js | 14 ++++++++++++++ 4 files changed, 30 insertions(+) create mode 100644 Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainTime/PlainTime.prototype.nanosecond.js diff --git a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h index 12861d0a67..1ee234dafe 100644 --- a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h +++ b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h @@ -275,6 +275,7 @@ namespace JS { P(monthsInYear) \ P(multiline) \ P(name) \ + P(nanosecond) \ P(nanoseconds) \ P(negated) \ P(next) \ diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainTimePrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/PlainTimePrototype.cpp index a1b152c61e..b233444e25 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainTimePrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainTimePrototype.cpp @@ -32,6 +32,7 @@ void PlainTimePrototype::initialize(GlobalObject& global_object) define_native_accessor(vm.names.second, second_getter, {}, Attribute::Configurable); define_native_accessor(vm.names.millisecond, millisecond_getter, {}, Attribute::Configurable); define_native_accessor(vm.names.microsecond, microsecond_getter, {}, Attribute::Configurable); + define_native_accessor(vm.names.nanosecond, nanosecond_getter, {}, Attribute::Configurable); u8 attr = Attribute::Writable | Attribute::Configurable; define_native_function(vm.names.valueOf, value_of, 0, attr); @@ -128,6 +129,19 @@ JS_DEFINE_NATIVE_FUNCTION(PlainTimePrototype::microsecond_getter) return Value(temporal_time->iso_microsecond()); } +// 4.3.9 get Temporal.PlainTime.prototype.nanosecond, https://tc39.es/proposal-temporal/#sec-get-temporal.plaintime.prototype.nanosecond +JS_DEFINE_NATIVE_FUNCTION(PlainTimePrototype::nanosecond_getter) +{ + // 1. Let temporalTime be the this value. + // 2. Perform ? RequireInternalSlot(temporalTime, [[InitializedTemporalTime]]). + auto* temporal_time = typed_this(global_object); + if (vm.exception()) + return {}; + + // 3. Return 𝔽(temporalTime.[[ISONanosecond]]). + return Value(temporal_time->iso_nanosecond()); +} + // 4.3.23 Temporal.PlainTime.prototype.valueOf ( ), https://tc39.es/proposal-temporal/#sec-temporal.plaintime.prototype.valueof JS_DEFINE_NATIVE_FUNCTION(PlainTimePrototype::value_of) { diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainTimePrototype.h b/Userland/Libraries/LibJS/Runtime/Temporal/PlainTimePrototype.h index f6eb822b49..4c1694a019 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainTimePrototype.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainTimePrototype.h @@ -25,6 +25,7 @@ private: JS_DECLARE_NATIVE_FUNCTION(second_getter); JS_DECLARE_NATIVE_FUNCTION(millisecond_getter); JS_DECLARE_NATIVE_FUNCTION(microsecond_getter); + JS_DECLARE_NATIVE_FUNCTION(nanosecond_getter); JS_DECLARE_NATIVE_FUNCTION(value_of); }; diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainTime/PlainTime.prototype.nanosecond.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainTime/PlainTime.prototype.nanosecond.js new file mode 100644 index 0000000000..5f9923e27f --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainTime/PlainTime.prototype.nanosecond.js @@ -0,0 +1,14 @@ +describe("correct behavior", () => { + test("basic functionality", () => { + const plainTime = new Temporal.PlainTime(0, 0, 0, 0, 0, 12); + expect(plainTime.nanosecond).toBe(12); + }); +}); + +test("errors", () => { + test("this value must be a Temporal.PlainTime object", () => { + expect(() => { + Reflect.get(Temporal.PlainTime.prototype, "nanosecond", "foo"); + }).toThrowWithMessage(TypeError, "Not a Temporal.PlainTime"); + }); +});