From 2401e45720e8d7ed01aa715a06dfb81904c3a2fc Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Fri, 9 Jul 2021 11:51:23 +0100 Subject: [PATCH] LibJS: Implement Temporal.Instant.fromEpochSeconds() --- .../LibJS/Runtime/CommonPropertyNames.h | 1 + .../Runtime/Temporal/InstantConstructor.cpp | 30 ++++++++++++ .../Runtime/Temporal/InstantConstructor.h | 2 + .../Instant/Instant.fromEpochSeconds.js | 48 +++++++++++++++++++ 4 files changed, 81 insertions(+) create mode 100644 Userland/Libraries/LibJS/Tests/builtins/Temporal/Instant/Instant.fromEpochSeconds.js diff --git a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h index bc5407a0cf..79201db3cc 100644 --- a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h +++ b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h @@ -143,6 +143,7 @@ namespace JS { P(fromCharCode) \ P(fromCodePoint) \ P(fromEntries) \ + P(fromEpochSeconds) \ P(fround) \ P(gc) \ P(get) \ diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/InstantConstructor.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/InstantConstructor.cpp index c30def6cfd..5afa9a69f7 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/InstantConstructor.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/InstantConstructor.cpp @@ -4,6 +4,7 @@ * SPDX-License-Identifier: BSD-2-Clause */ +#include #include #include #include @@ -25,6 +26,9 @@ void InstantConstructor::initialize(GlobalObject& global_object) // 8.2.1 Temporal.Instant.prototype, https://tc39.es/proposal-temporal/#sec-temporal-instant-prototype define_direct_property(vm.names.prototype, global_object.temporal_instant_prototype(), 0); + u8 attr = Attribute::Writable | Attribute::Configurable; + define_native_function(vm.names.fromEpochSeconds, from_epoch_seconds, 1, attr); + define_direct_property(vm.names.length, Value(1), Attribute::Configurable); } @@ -60,4 +64,30 @@ Value InstantConstructor::construct(FunctionObject& new_target) return create_temporal_instant(global_object, *epoch_nanoseconds, &new_target); } +// 8.2.3 Temporal.Instant.fromEpochSeconds ( epochSeconds ), https://tc39.es/proposal-temporal/#sec-temporal.instant.fromepochseconds +JS_DEFINE_NATIVE_FUNCTION(InstantConstructor::from_epoch_seconds) +{ + // 1. Set epochSeconds to ? ToNumber(epochSeconds). + auto epoch_seconds_value = vm.argument(0).to_number(global_object); + if (vm.exception()) + return {}; + + // 2. Set epochSeconds to ? NumberToBigInt(epochSeconds). + auto* epoch_seconds = number_to_bigint(global_object, epoch_seconds_value); + if (vm.exception()) + return {}; + + // 3. Let epochNanoseconds be epochSeconds × 10^9ℤ. + auto* epoch_nanoseconds = js_bigint(vm.heap(), epoch_seconds->big_integer().multiplied_by(Crypto::UnsignedBigInteger { 1'000'000'000 })); + + // 4. If ! IsValidEpochNanoseconds(epochNanoseconds) is false, throw a RangeError exception. + if (!is_valid_epoch_nanoseconds(*epoch_nanoseconds)) { + vm.throw_exception(global_object, ErrorType::TemporalInvalidEpochNanoseconds); + return {}; + } + + // 5. Return ? CreateTemporalInstant(epochNanoseconds). + return create_temporal_instant(global_object, *epoch_nanoseconds); +} + } diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/InstantConstructor.h b/Userland/Libraries/LibJS/Runtime/Temporal/InstantConstructor.h index d449b0a096..3b8df1969a 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/InstantConstructor.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/InstantConstructor.h @@ -23,6 +23,8 @@ public: private: virtual bool has_constructor() const override { return true; } + + JS_DECLARE_NATIVE_FUNCTION(from_epoch_seconds); }; } diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/Instant/Instant.fromEpochSeconds.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/Instant/Instant.fromEpochSeconds.js new file mode 100644 index 0000000000..a5596c5d86 --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/Instant/Instant.fromEpochSeconds.js @@ -0,0 +1,48 @@ +describe("correct behavior", () => { + test("length is 1", () => { + expect(Temporal.Instant.fromEpochSeconds).toHaveLength(1); + }); + + test("basic functionality", () => { + expect(Temporal.Instant.fromEpochSeconds(0).epochSeconds).toBe(0); + expect(Temporal.Instant.fromEpochSeconds(1).epochSeconds).toBe(1); + expect(Temporal.Instant.fromEpochSeconds(999_999_999).epochSeconds).toBe(999_999_999); + expect(Temporal.Instant.fromEpochSeconds(8_640_000_000_000).epochSeconds).toBe( + 8_640_000_000_000 + ); + + expect(Temporal.Instant.fromEpochSeconds(-0).epochSeconds).toBe(0); + expect(Temporal.Instant.fromEpochSeconds(-1).epochSeconds).toBe(-1); + expect(Temporal.Instant.fromEpochSeconds(-999_999_999).epochSeconds).toBe(-999_999_999); + expect(Temporal.Instant.fromEpochSeconds(-8_640_000_000_000).epochSeconds).toBe( + -8_640_000_000_000 + ); + }); +}); + +test("errors", () => { + test("argument must be coercible to BigInt", () => { + expect(() => { + Temporal.Instant.fromEpochSeconds(1.23); + }).toThrowWithMessage(RangeError, "Cannot convert non-integral number to BigInt"); + // NOTE: ToNumber is called on the argument first, so this is effectively NaN. + expect(() => { + Temporal.Instant.fromEpochSeconds("foo"); + }).toThrowWithMessage(RangeError, "Cannot convert non-integral number to BigInt"); + }); + + test("out-of-range epoch seconds value", () => { + expect(() => { + Temporal.Instant.fromEpochSeconds(8_640_000_000_001); + }).toThrowWithMessage( + RangeError, + "Invalid epoch nanoseconds value, must be in range -86400 * 10^17 to 86400 * 10^17" + ); + expect(() => { + Temporal.Instant.fromEpochSeconds(-8_640_000_000_001); + }).toThrowWithMessage( + RangeError, + "Invalid epoch nanoseconds value, must be in range -86400 * 10^17 to 86400 * 10^17" + ); + }); +});