diff --git a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h index 00f228bfef..bc5407a0cf 100644 --- a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h +++ b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h @@ -115,6 +115,7 @@ namespace JS { P(enumerable) \ P(epochMicroseconds) \ P(epochMilliseconds) \ + P(epochNanoseconds) \ P(epochSeconds) \ P(error) \ P(errors) \ diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/InstantPrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/InstantPrototype.cpp index 5a5e9c6859..7633b5d9bb 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/InstantPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/InstantPrototype.cpp @@ -26,6 +26,7 @@ void InstantPrototype::initialize(GlobalObject& global_object) define_native_accessor(vm.names.epochSeconds, epoch_seconds_getter, {}, Attribute::Configurable); define_native_accessor(vm.names.epochMilliseconds, epoch_milliseconds_getter, {}, Attribute::Configurable); define_native_accessor(vm.names.epochMicroseconds, epoch_microseconds_getter, {}, Attribute::Configurable); + define_native_accessor(vm.names.epochNanoseconds, epoch_nanoseconds_getter, {}, Attribute::Configurable); // 8.3.2 Temporal.Instant.prototype[ @@toStringTag ], https://tc39.es/proposal-temporal/#sec-temporal.instant.prototype-@@tostringtag define_direct_property(*vm.well_known_symbol_to_string_tag(), js_string(vm.heap(), "Temporal.Instant"), Attribute::Configurable); @@ -101,4 +102,20 @@ JS_DEFINE_NATIVE_FUNCTION(InstantPrototype::epoch_microseconds_getter) return js_bigint(vm.heap(), move(us)); } +// 8.3.6 get Temporal.Instant.prototype.epochNanoseconds, https://tc39.es/proposal-temporal/#sec-get-temporal.instant.prototype.epochnanoseconds +JS_DEFINE_NATIVE_FUNCTION(InstantPrototype::epoch_nanoseconds_getter) +{ + // 1. Let instant be the this value. + // 2. Perform ? RequireInternalSlot(instant, [[InitializedTemporalInstant]]). + auto* instant = typed_this(global_object); + if (vm.exception()) + return {}; + + // 3. Let ns be instant.[[Nanoseconds]]. + auto& ns = instant->nanoseconds(); + + // 4. Return ns. + return &ns; +} + } diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/InstantPrototype.h b/Userland/Libraries/LibJS/Runtime/Temporal/InstantPrototype.h index 6bc5be7912..1854c59fb1 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/InstantPrototype.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/InstantPrototype.h @@ -22,6 +22,7 @@ private: JS_DECLARE_NATIVE_FUNCTION(epoch_seconds_getter); JS_DECLARE_NATIVE_FUNCTION(epoch_milliseconds_getter); JS_DECLARE_NATIVE_FUNCTION(epoch_microseconds_getter); + JS_DECLARE_NATIVE_FUNCTION(epoch_nanoseconds_getter); }; } diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/Instant/Instant.prototype.epochNanoseconds.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/Instant/Instant.prototype.epochNanoseconds.js new file mode 100644 index 0000000000..28a9f6a0e0 --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/Instant/Instant.prototype.epochNanoseconds.js @@ -0,0 +1,25 @@ +describe("correct behavior", () => { + test("basic functionality", () => { + expect(new Temporal.Instant(0n).epochNanoseconds).toBe(0n); + expect(new Temporal.Instant(1n).epochNanoseconds).toBe(1n); + expect(new Temporal.Instant(999n).epochNanoseconds).toBe(999n); + expect(new Temporal.Instant(8_640_000_000_000_000_000_000n).epochNanoseconds).toBe( + 8_640_000_000_000_000_000_000n + ); + + expect(new Temporal.Instant(-0n).epochNanoseconds).toBe(-0n); + expect(new Temporal.Instant(-1n).epochNanoseconds).toBe(-1n); + expect(new Temporal.Instant(-999n).epochNanoseconds).toBe(-999n); + expect(new Temporal.Instant(-8_640_000_000_000_000_000_000n).epochNanoseconds).toBe( + -8_640_000_000_000_000_000_000n + ); + }); +}); + +test("errors", () => { + test("this value must be a Temporal.Instant object", () => { + expect(() => { + Reflect.get(Temporal.Instant.prototype, "epochNanoseconds", "foo"); + }).toThrowWithMessage(TypeError, "Not a Temporal.Instant"); + }); +});