From 5872357b56ee239922004b820a215f7b77ca9e3b Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Fri, 9 Jul 2021 12:10:16 +0100 Subject: [PATCH] LibJS: Implement Temporal.Instant.fromEpochMicroseconds() --- .../LibJS/Runtime/CommonPropertyNames.h | 1 + .../Runtime/Temporal/InstantConstructor.cpp | 22 ++++++++ .../Runtime/Temporal/InstantConstructor.h | 1 + .../Instant/Instant.fromEpochMicroseconds.js | 51 +++++++++++++++++++ 4 files changed, 75 insertions(+) create mode 100644 Userland/Libraries/LibJS/Tests/builtins/Temporal/Instant/Instant.fromEpochMicroseconds.js diff --git a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h index c9084725b3..aaf297bf12 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(fromEpochMicroseconds) \ P(fromEpochMilliseconds) \ P(fromEpochSeconds) \ P(fround) \ diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/InstantConstructor.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/InstantConstructor.cpp index 191b2c40f2..c708fe876b 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/InstantConstructor.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/InstantConstructor.cpp @@ -29,6 +29,7 @@ void InstantConstructor::initialize(GlobalObject& global_object) u8 attr = Attribute::Writable | Attribute::Configurable; define_native_function(vm.names.fromEpochSeconds, from_epoch_seconds, 1, attr); define_native_function(vm.names.fromEpochMilliseconds, from_epoch_milliseconds, 1, attr); + define_native_function(vm.names.fromEpochMicroseconds, from_epoch_microseconds, 1, attr); define_direct_property(vm.names.length, Value(1), Attribute::Configurable); } @@ -117,4 +118,25 @@ JS_DEFINE_NATIVE_FUNCTION(InstantConstructor::from_epoch_milliseconds) return create_temporal_instant(global_object, *epoch_nanoseconds); } +// 8.2.5 Temporal.Instant.fromEpochMicroseconds ( epochMicroseconds ) +JS_DEFINE_NATIVE_FUNCTION(InstantConstructor::from_epoch_microseconds) +{ + // 1. Set epochMicroseconds to ? ToBigInt(epochMicroseconds). + auto* epoch_microseconds = vm.argument(0).to_bigint(global_object); + if (vm.exception()) + return {}; + + // 2. Let epochNanoseconds be epochMicroseconds × 1000ℤ. + auto* epoch_nanoseconds = js_bigint(vm.heap(), epoch_microseconds->big_integer().multiplied_by(Crypto::UnsignedBigInteger { 1'000 })); + + // 3. If ! IsValidEpochNanoseconds(epochNanoseconds) is false, throw a RangeError exception. + if (!is_valid_epoch_nanoseconds(*epoch_nanoseconds)) { + vm.throw_exception(global_object, ErrorType::TemporalInvalidEpochNanoseconds); + return {}; + } + + // 4. 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 ae44874f13..1e9ce0e61f 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/InstantConstructor.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/InstantConstructor.h @@ -26,6 +26,7 @@ private: JS_DECLARE_NATIVE_FUNCTION(from_epoch_seconds); JS_DECLARE_NATIVE_FUNCTION(from_epoch_milliseconds); + JS_DECLARE_NATIVE_FUNCTION(from_epoch_microseconds); }; } diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/Instant/Instant.fromEpochMicroseconds.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/Instant/Instant.fromEpochMicroseconds.js new file mode 100644 index 0000000000..230f0f2b07 --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/Instant/Instant.fromEpochMicroseconds.js @@ -0,0 +1,51 @@ +describe("correct behavior", () => { + test("length is 1", () => { + expect(Temporal.Instant.fromEpochMicroseconds).toHaveLength(1); + }); + + test("basic functionality", () => { + expect(Temporal.Instant.fromEpochMicroseconds(0n).epochMicroseconds).toBe(0n); + expect(Temporal.Instant.fromEpochMicroseconds(1n).epochMicroseconds).toBe(1n); + expect(Temporal.Instant.fromEpochMicroseconds(999_999_999n).epochMicroseconds).toBe( + 999_999_999n + ); + expect( + Temporal.Instant.fromEpochMicroseconds(8_640_000_000_000_000_000n).epochMicroseconds + ).toBe(8_640_000_000_000_000_000n); + + expect(Temporal.Instant.fromEpochMicroseconds(-0n).epochMicroseconds).toBe(0n); + expect(Temporal.Instant.fromEpochMicroseconds(-1n).epochMicroseconds).toBe(-1n); + expect(Temporal.Instant.fromEpochMicroseconds(-999_999_999n).epochMicroseconds).toBe( + -999_999_999n + ); + expect( + Temporal.Instant.fromEpochMicroseconds(-8_640_000_000_000_000_000n).epochMicroseconds + ).toBe(-8_640_000_000_000_000_000n); + }); +}); + +test("errors", () => { + test("argument must be coercible to BigInt", () => { + expect(() => { + Temporal.Instant.fromEpochMicroseconds(123); + }).toThrowWithMessage(TypeError, "Cannot convert number to BigInt"); + expect(() => { + Temporal.Instant.fromEpochMicroseconds("foo"); + }).toThrowWithMessage(SyntaxError, "Invalid value for BigInt: foo"); + }); + + test("out-of-range epoch microseconds value", () => { + expect(() => { + Temporal.Instant.fromEpochMicroseconds(8_640_000_000_000_000_001n); + }).toThrowWithMessage( + RangeError, + "Invalid epoch nanoseconds value, must be in range -86400 * 10^17 to 86400 * 10^17" + ); + expect(() => { + Temporal.Instant.fromEpochMicroseconds(-8_640_000_000_000_000_001n); + }).toThrowWithMessage( + RangeError, + "Invalid epoch nanoseconds value, must be in range -86400 * 10^17 to 86400 * 10^17" + ); + }); +});