summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibJS
diff options
context:
space:
mode:
authorLinus Groh <mail@linusgroh.de>2021-07-09 12:00:31 +0100
committerLinus Groh <mail@linusgroh.de>2021-07-09 13:20:51 +0100
commit66ff7722543b14b7bfdafb08646ad53b499cb191 (patch)
tree56b78ef2c66c134b81955d2c3d4f66d98987e4cb /Userland/Libraries/LibJS
parent2401e45720e8d7ed01aa715a06dfb81904c3a2fc (diff)
downloadserenity-66ff7722543b14b7bfdafb08646ad53b499cb191.zip
LibJS: Implement Temporal.Instant.fromEpochMilliseconds()
Diffstat (limited to 'Userland/Libraries/LibJS')
-rw-r--r--Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h1
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/InstantConstructor.cpp27
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/InstantConstructor.h1
-rw-r--r--Userland/Libraries/LibJS/Tests/builtins/Temporal/Instant/Instant.fromEpochMilliseconds.js52
4 files changed, 81 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h
index 79201db3cc..c9084725b3 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(fromEpochMilliseconds) \
P(fromEpochSeconds) \
P(fround) \
P(gc) \
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/InstantConstructor.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/InstantConstructor.cpp
index 5afa9a69f7..191b2c40f2 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/InstantConstructor.cpp
+++ b/Userland/Libraries/LibJS/Runtime/Temporal/InstantConstructor.cpp
@@ -28,6 +28,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_direct_property(vm.names.length, Value(1), Attribute::Configurable);
}
@@ -90,4 +91,30 @@ JS_DEFINE_NATIVE_FUNCTION(InstantConstructor::from_epoch_seconds)
return create_temporal_instant(global_object, *epoch_nanoseconds);
}
+// 8.2.4 Temporal.Instant.fromEpochMilliseconds ( epochMilliseconds ), https://tc39.es/proposal-temporal/#sec-temporal.instant.fromepochmilliseconds
+JS_DEFINE_NATIVE_FUNCTION(InstantConstructor::from_epoch_milliseconds)
+{
+ // 1. Set epochMilliseconds to ? ToNumber(epochMilliseconds).
+ auto epoch_milliseconds_value = vm.argument(0).to_number(global_object);
+ if (vm.exception())
+ return {};
+
+ // 2. Set epochMilliseconds to ? NumberToBigInt(epochMilliseconds).
+ auto* epoch_milliseconds = number_to_bigint(global_object, epoch_milliseconds_value);
+ if (vm.exception())
+ return {};
+
+ // 3. Let epochNanoseconds be epochMilliseconds × 10^6ℤ.
+ auto* epoch_nanoseconds = js_bigint(vm.heap(), epoch_milliseconds->big_integer().multiplied_by(Crypto::UnsignedBigInteger { 1'000'000 }));
+
+ // 4. If ! IsValidEpochNanoseconds(epochNanoseconds) is false, throw a RangeError exception.
+ if (!is_valid_epoch_nanoseconds(*epoch_nanoseconds)) {
+ vm.throw_exception<RangeError>(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 3b8df1969a..ae44874f13 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/InstantConstructor.h
+++ b/Userland/Libraries/LibJS/Runtime/Temporal/InstantConstructor.h
@@ -25,6 +25,7 @@ private:
virtual bool has_constructor() const override { return true; }
JS_DECLARE_NATIVE_FUNCTION(from_epoch_seconds);
+ JS_DECLARE_NATIVE_FUNCTION(from_epoch_milliseconds);
};
}
diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/Instant/Instant.fromEpochMilliseconds.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/Instant/Instant.fromEpochMilliseconds.js
new file mode 100644
index 0000000000..e950b50164
--- /dev/null
+++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/Instant/Instant.fromEpochMilliseconds.js
@@ -0,0 +1,52 @@
+describe("correct behavior", () => {
+ test("length is 1", () => {
+ expect(Temporal.Instant.fromEpochMilliseconds).toHaveLength(1);
+ });
+
+ test("basic functionality", () => {
+ expect(Temporal.Instant.fromEpochMilliseconds(0).epochMilliseconds).toBe(0);
+ expect(Temporal.Instant.fromEpochMilliseconds(1).epochMilliseconds).toBe(1);
+ expect(Temporal.Instant.fromEpochMilliseconds(999_999_999).epochMilliseconds).toBe(
+ 999_999_999
+ );
+ expect(
+ Temporal.Instant.fromEpochMilliseconds(8_640_000_000_000_000).epochMilliseconds
+ ).toBe(8_640_000_000_000_000);
+
+ expect(Temporal.Instant.fromEpochMilliseconds(-0).epochMilliseconds).toBe(0);
+ expect(Temporal.Instant.fromEpochMilliseconds(-1).epochMilliseconds).toBe(-1);
+ expect(Temporal.Instant.fromEpochMilliseconds(-999_999_999).epochMilliseconds).toBe(
+ -999_999_999
+ );
+ expect(
+ Temporal.Instant.fromEpochMilliseconds(-8_640_000_000_000_000).epochMilliseconds
+ ).toBe(-8_640_000_000_000_000);
+ });
+});
+
+test("errors", () => {
+ test("argument must be coercible to BigInt", () => {
+ expect(() => {
+ Temporal.Instant.fromEpochMilliseconds(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.fromEpochMilliseconds("foo");
+ }).toThrowWithMessage(RangeError, "Cannot convert non-integral number to BigInt");
+ });
+
+ test("out-of-range epoch milliseconds value", () => {
+ expect(() => {
+ Temporal.Instant.fromEpochMilliseconds(8_640_000_000_000_001);
+ }).toThrowWithMessage(
+ RangeError,
+ "Invalid epoch nanoseconds value, must be in range -86400 * 10^17 to 86400 * 10^17"
+ );
+ expect(() => {
+ Temporal.Instant.fromEpochMilliseconds(-8_640_000_000_000_001);
+ }).toThrowWithMessage(
+ RangeError,
+ "Invalid epoch nanoseconds value, must be in range -86400 * 10^17 to 86400 * 10^17"
+ );
+ });
+});