Przeglądaj źródła

LibJS: Implement Temporal.Instant.prototype.epochMilliseconds

Linus Groh 4 lat temu
rodzic
commit
b157ab3f12

+ 1 - 0
Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h

@@ -113,6 +113,7 @@ namespace JS {
     P(endsWith)                              \
     P(entries)                               \
     P(enumerable)                            \
+    P(epochMilliseconds)                     \
     P(epochSeconds)                          \
     P(error)                                 \
     P(errors)                                \

+ 20 - 0
Userland/Libraries/LibJS/Runtime/Temporal/InstantPrototype.cpp

@@ -24,6 +24,7 @@ void InstantPrototype::initialize(GlobalObject& global_object)
     auto& vm = this->vm();
 
     define_native_accessor(vm.names.epochSeconds, epoch_seconds_getter, {}, Attribute::Configurable);
+    define_native_accessor(vm.names.epochMilliseconds, epoch_milliseconds_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);
@@ -61,4 +62,23 @@ JS_DEFINE_NATIVE_FUNCTION(InstantPrototype::epoch_seconds_getter)
     return Value((double)s.to_base(10).to_int<i64>().value());
 }
 
+// 8.3.4 get Temporal.Instant.prototype.epochMilliseconds, https://tc39.es/proposal-temporal/#sec-get-temporal.instant.prototype.epochmilliseconds
+JS_DEFINE_NATIVE_FUNCTION(InstantPrototype::epoch_milliseconds_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. Let ms be RoundTowardsZero(ℝ(ns) / 10^6).
+    auto [ms, _] = ns.big_integer().divided_by(Crypto::SignedBigInteger::create_from(1'000'000));
+
+    // 5. Return 𝔽(ms).
+    return Value((double)ms.to_base(10).to_int<i64>().value());
+}
+
 }

+ 1 - 0
Userland/Libraries/LibJS/Runtime/Temporal/InstantPrototype.h

@@ -20,6 +20,7 @@ public:
 
 private:
     JS_DECLARE_NATIVE_FUNCTION(epoch_seconds_getter);
+    JS_DECLARE_NATIVE_FUNCTION(epoch_milliseconds_getter);
 };
 
 }

+ 33 - 0
Userland/Libraries/LibJS/Tests/builtins/Temporal/Instant/Instant.prototype.epochMilliseconds.js

@@ -0,0 +1,33 @@
+describe("correct behavior", () => {
+    test("basic functionality", () => {
+        expect(new Temporal.Instant(0n).epochMilliseconds).toBe(0);
+        expect(new Temporal.Instant(1n).epochMilliseconds).toBe(0);
+        expect(new Temporal.Instant(999_999n).epochMilliseconds).toBe(0);
+        expect(new Temporal.Instant(1_000_000n).epochMilliseconds).toBe(1);
+        expect(new Temporal.Instant(1_500_000n).epochMilliseconds).toBe(1);
+        expect(new Temporal.Instant(1_999_999n).epochMilliseconds).toBe(1);
+        expect(new Temporal.Instant(2_000_000n).epochMilliseconds).toBe(2);
+        expect(new Temporal.Instant(8_640_000_000_000_000_000_000n).epochMilliseconds).toBe(
+            8_640_000_000_000_000
+        );
+
+        expect(new Temporal.Instant(-0n).epochMilliseconds).toBe(0);
+        expect(new Temporal.Instant(-1n).epochMilliseconds).toBe(0);
+        expect(new Temporal.Instant(-999_999n).epochMilliseconds).toBe(0);
+        expect(new Temporal.Instant(-1_000_000n).epochMilliseconds).toBe(-1);
+        expect(new Temporal.Instant(-1_500_000n).epochMilliseconds).toBe(-1);
+        expect(new Temporal.Instant(-1_999_999n).epochMilliseconds).toBe(-1);
+        expect(new Temporal.Instant(-2_000_000n).epochMilliseconds).toBe(-2);
+        expect(new Temporal.Instant(-8_640_000_000_000_000_000_000n).epochMilliseconds).toBe(
+            -8_640_000_000_000_000
+        );
+    });
+});
+
+test("errors", () => {
+    test("this value must be a Temporal.Instant object", () => {
+        expect(() => {
+            Reflect.get(Temporal.Instant.prototype, "epochMilliseconds", "foo");
+        }).toThrowWithMessage(TypeError, "Not a Temporal.Instant");
+    });
+});