Ver Fonte

LibJS: Implement Temporal.PlainTime.prototype.since/until

Timothy Flynn há 8 meses atrás
pai
commit
85eef698b9

+ 32 - 0
Libraries/LibJS/Runtime/Temporal/PlainTime.cpp

@@ -606,6 +606,38 @@ Time round_time(Time const& time, u64 increment, Unit unit, RoundingMode roundin
     VERIFY_NOT_REACHED();
 }
 
+// 4.5.17 DifferenceTemporalPlainTime ( operation, temporalTime, other, options ), https://tc39.es/proposal-temporal/#sec-temporal-differencetemporalplaintime
+ThrowCompletionOr<GC::Ref<Duration>> difference_temporal_plain_time(VM& vm, DurationOperation operation, PlainTime const& temporal_time, Value other_value, Value options)
+{
+    // 1. Set other to ? ToTemporalTime(other).
+    auto other = TRY(to_temporal_time(vm, other_value));
+
+    // 2. Let resolvedOptions be ? GetOptionsObject(options).
+    auto resolved_options = TRY(get_options_object(vm, options));
+
+    // 3. Let settings be ? GetDifferenceSettings(operation, resolvedOptions, TIME, « », NANOSECOND, HOUR).
+    auto settings = TRY(get_difference_settings(vm, operation, resolved_options, UnitGroup::Time, {}, Unit::Nanosecond, Unit::Hour));
+
+    // 4. Let timeDuration be DifferenceTime(temporalTime.[[Time]], other.[[Time]]).
+    auto time_duration = difference_time(temporal_time.time(), other->time());
+
+    // 5. Set timeDuration to ! RoundTimeDuration(timeDuration, settings.[[RoundingIncrement]], settings.[[SmallestUnit]], settings.[[RoundingMode]]).
+    time_duration = MUST(round_time_duration(vm, time_duration, Crypto::UnsignedBigInteger { settings.rounding_increment }, settings.smallest_unit, settings.rounding_mode));
+
+    // 6. Let duration be ! CombineDateAndTimeDuration(ZeroDateDuration(), timeDuration).
+    auto duration = MUST(combine_date_and_time_duration(vm, zero_date_duration(vm), move(time_duration)));
+
+    // 7. Let result be ! TemporalDurationFromInternal(duration, settings.[[LargestUnit]]).
+    auto result = MUST(temporal_duration_from_internal(vm, duration, settings.largest_unit));
+
+    // 8. If operation is SINCE, set result to CreateNegatedTemporalDuration(result).
+    if (operation == DurationOperation::Since)
+        result = create_negated_temporal_duration(vm, result);
+
+    // 9. Return result.
+    return result;
+}
+
 // 4.5.18 AddDurationToTime ( operation, temporalTime, temporalDurationLike ), https://tc39.es/proposal-temporal/#sec-temporal-adddurationtotime
 ThrowCompletionOr<GC::Ref<PlainTime>> add_duration_to_time(VM& vm, ArithmeticOperation operation, PlainTime const& temporal_time, Value temporal_duration_like)
 {

+ 1 - 0
Libraries/LibJS/Runtime/Temporal/PlainTime.h

@@ -63,6 +63,7 @@ String time_record_to_string(Time const&, SecondsStringPrecision::Precision);
 i8 compare_time_record(Time const&, Time const&);
 Time add_time(Time const&, TimeDuration const& time_duration);
 Time round_time(Time const&, u64 increment, Unit, RoundingMode);
+ThrowCompletionOr<GC::Ref<Duration>> difference_temporal_plain_time(VM&, DurationOperation, PlainTime const&, Value other, Value options);
 ThrowCompletionOr<GC::Ref<PlainTime>> add_duration_to_time(VM&, ArithmeticOperation, PlainTime const&, Value temporal_duration_like);
 
 }

+ 31 - 0
Libraries/LibJS/Runtime/Temporal/PlainTimePrototype.cpp

@@ -6,6 +6,7 @@
  * SPDX-License-Identifier: BSD-2-Clause
  */
 
+#include <LibJS/Runtime/Temporal/Duration.h>
 #include <LibJS/Runtime/Temporal/PlainTimePrototype.h>
 
 namespace JS::Temporal {
@@ -37,6 +38,8 @@ void PlainTimePrototype::initialize(Realm& realm)
     u8 attr = Attribute::Writable | Attribute::Configurable;
     define_native_function(realm, vm.names.add, add, 1, attr);
     define_native_function(realm, vm.names.subtract, subtract, 1, attr);
+    define_native_function(realm, vm.names.until, until, 1, attr);
+    define_native_function(realm, vm.names.since, since, 1, attr);
     define_native_function(realm, vm.names.toString, to_string, 0, attr);
     define_native_function(realm, vm.names.toLocaleString, to_locale_string, 0, attr);
     define_native_function(realm, vm.names.toJSON, to_json, 0, attr);
@@ -95,6 +98,34 @@ JS_DEFINE_NATIVE_FUNCTION(PlainTimePrototype::subtract)
     return TRY(add_duration_to_time(vm, ArithmeticOperation::Subtract, temporal_time, temporal_duration_like));
 }
 
+// 4.3.12 Temporal.PlainTime.prototype.until ( other [ , options ] ), https://tc39.es/proposal-temporal/#sec-temporal.plaintime.prototype.until
+JS_DEFINE_NATIVE_FUNCTION(PlainTimePrototype::until)
+{
+    auto other = vm.argument(0);
+    auto options = vm.argument(1);
+
+    // 1. Let temporalTime be the this value.
+    // 2. Perform ? RequireInternalSlot(temporalTime, [[InitializedTemporalTime]]).
+    auto temporal_time = TRY(typed_this_object(vm));
+
+    // 3. Return ? DifferenceTemporalPlainTime(UNTIL, temporalTime, other, options).
+    return TRY(difference_temporal_plain_time(vm, DurationOperation::Until, temporal_time, other, options));
+}
+
+// 4.3.13 Temporal.PlainTime.prototype.since ( other [ , options ] ), https://tc39.es/proposal-temporal/#sec-temporal.plaintime.prototype.since
+JS_DEFINE_NATIVE_FUNCTION(PlainTimePrototype::since)
+{
+    auto other = vm.argument(0);
+    auto options = vm.argument(1);
+
+    // 1. Let temporalTime be the this value.
+    // 2. Perform ? RequireInternalSlot(temporalTime, [[InitializedTemporalTime]]).
+    auto temporal_time = TRY(typed_this_object(vm));
+
+    // 3. Return ? DifferenceTemporalPlainTime(SINCE, temporalTime, other, options).
+    return TRY(difference_temporal_plain_time(vm, DurationOperation::Since, temporal_time, other, options));
+}
+
 // 4.3.16 Temporal.PlainTime.prototype.toString ( [ options ] ), https://tc39.es/proposal-temporal/#sec-temporal.plaintime.prototype.tostring
 JS_DEFINE_NATIVE_FUNCTION(PlainTimePrototype::to_string)
 {

+ 2 - 0
Libraries/LibJS/Runtime/Temporal/PlainTimePrototype.h

@@ -31,6 +31,8 @@ private:
     JS_DECLARE_NATIVE_FUNCTION(nanosecond_getter);
     JS_DECLARE_NATIVE_FUNCTION(add);
     JS_DECLARE_NATIVE_FUNCTION(subtract);
+    JS_DECLARE_NATIVE_FUNCTION(until);
+    JS_DECLARE_NATIVE_FUNCTION(since);
     JS_DECLARE_NATIVE_FUNCTION(to_string);
     JS_DECLARE_NATIVE_FUNCTION(to_locale_string);
     JS_DECLARE_NATIVE_FUNCTION(to_json);

+ 89 - 0
Libraries/LibJS/Tests/builtins/Temporal/PlainTime/PlainTime.prototype.since.js

@@ -0,0 +1,89 @@
+describe("correct behavior", () => {
+    test("length is 1", () => {
+        expect(Temporal.PlainTime.prototype.since).toHaveLength(1);
+    });
+
+    test("basic functionality", () => {
+        const values = [
+            [[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], "PT0S"],
+            [[2, 3, 4, 5, 6, 7], [1, 2, 3, 4, 5, 6], "PT1H1M1.001001001S"],
+            [[1, 2, 3, 4, 5, 6], [0, 0, 0, 0, 0, 0], "PT1H2M3.004005006S"],
+            [[0, 0, 0, 0, 0, 0], [1, 2, 3, 4, 5, 6], "-PT1H2M3.004005006S"],
+            [[23, 59, 59, 999, 999, 999], [0, 0, 0, 0, 0, 0], "PT23H59M59.999999999S"],
+            [[0, 0, 0, 0, 0, 0], [23, 59, 59, 999, 999, 999], "-PT23H59M59.999999999S"],
+        ];
+        for (const [args, argsOther, expected] of values) {
+            const plainTime = new Temporal.PlainTime(...args);
+            const other = new Temporal.PlainTime(...argsOther);
+            expect(plainTime.since(other).toString()).toBe(expected);
+        }
+    });
+
+    test("smallestUnit option", () => {
+        const plainTime = new Temporal.PlainTime(1, 2, 3, 4, 5, 6);
+        const other = new Temporal.PlainTime(0, 0, 0, 0, 0, 0);
+        const values = [
+            ["hour", "PT1H"],
+            ["minute", "PT1H2M"],
+            ["second", "PT1H2M3S"],
+            ["millisecond", "PT1H2M3.004S"],
+            ["microsecond", "PT1H2M3.004005S"],
+            ["nanosecond", "PT1H2M3.004005006S"],
+        ];
+        for (const [smallestUnit, expected] of values) {
+            expect(plainTime.since(other, { smallestUnit }).toString()).toBe(expected);
+        }
+    });
+
+    test("largestUnit option", () => {
+        const plainTime = new Temporal.PlainTime(1, 2, 3, 4, 5, 6);
+        const other = new Temporal.PlainTime(0, 0, 0, 0, 0, 0);
+        const values = [
+            ["hour", "PT1H2M3.004005006S"],
+            ["minute", "PT62M3.004005006S"],
+            ["second", "PT3723.004005006S"],
+            ["millisecond", "PT3723.004005006S"],
+            ["microsecond", "PT3723.004005006S"],
+            ["nanosecond", "PT3723.004005006S"],
+        ];
+        for (const [largestUnit, expected] of values) {
+            expect(plainTime.since(other, { largestUnit }).toString()).toBe(expected);
+        }
+    });
+});
+
+describe("errors", () => {
+    test("this value must be a Temporal.PlainTime object", () => {
+        expect(() => {
+            Temporal.PlainTime.prototype.since.call("foo", {});
+        }).toThrowWithMessage(TypeError, "Not an object of type Temporal.PlainTime");
+    });
+
+    test("disallowed smallestUnit option values", () => {
+        const values = ["year", "month", "week", "day"];
+        for (const smallestUnit of values) {
+            const plainTime = new Temporal.PlainTime();
+            const other = new Temporal.PlainTime();
+            expect(() => {
+                plainTime.since(other, { smallestUnit });
+            }).toThrowWithMessage(
+                RangeError,
+                `${smallestUnit} is not a valid value for option smallestUnit`
+            );
+        }
+    });
+
+    test("disallowed largestUnit option values", () => {
+        const values = ["year", "month", "week", "day"];
+        for (const largestUnit of values) {
+            const plainTime = new Temporal.PlainTime();
+            const other = new Temporal.PlainTime();
+            expect(() => {
+                plainTime.since(other, { largestUnit });
+            }).toThrowWithMessage(
+                RangeError,
+                `${largestUnit} is not a valid value for option largestUnit`
+            );
+        }
+    });
+});

+ 89 - 0
Libraries/LibJS/Tests/builtins/Temporal/PlainTime/PlainTime.prototype.until.js

@@ -0,0 +1,89 @@
+describe("correct behavior", () => {
+    test("length is 1", () => {
+        expect(Temporal.PlainTime.prototype.until).toHaveLength(1);
+    });
+
+    test("basic functionality", () => {
+        const values = [
+            [[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], "PT0S"],
+            [[1, 2, 3, 4, 5, 6], [2, 3, 4, 5, 6, 7], "PT1H1M1.001001001S"],
+            [[0, 0, 0, 0, 0, 0], [1, 2, 3, 4, 5, 6], "PT1H2M3.004005006S"],
+            [[1, 2, 3, 4, 5, 6], [0, 0, 0, 0, 0, 0], "-PT1H2M3.004005006S"],
+            [[0, 0, 0, 0, 0, 0], [23, 59, 59, 999, 999, 999], "PT23H59M59.999999999S"],
+            [[23, 59, 59, 999, 999, 999], [0, 0, 0, 0, 0, 0], "-PT23H59M59.999999999S"],
+        ];
+        for (const [args, argsOther, expected] of values) {
+            const plainTime = new Temporal.PlainTime(...args);
+            const other = new Temporal.PlainTime(...argsOther);
+            expect(plainTime.until(other).toString()).toBe(expected);
+        }
+    });
+
+    test("smallestUnit option", () => {
+        const plainTime = new Temporal.PlainTime(0, 0, 0, 0, 0, 0);
+        const other = new Temporal.PlainTime(1, 2, 3, 4, 5, 6);
+        const values = [
+            ["hour", "PT1H"],
+            ["minute", "PT1H2M"],
+            ["second", "PT1H2M3S"],
+            ["millisecond", "PT1H2M3.004S"],
+            ["microsecond", "PT1H2M3.004005S"],
+            ["nanosecond", "PT1H2M3.004005006S"],
+        ];
+        for (const [smallestUnit, expected] of values) {
+            expect(plainTime.until(other, { smallestUnit }).toString()).toBe(expected);
+        }
+    });
+
+    test("largestUnit option", () => {
+        const plainTime = new Temporal.PlainTime(0, 0, 0, 0, 0, 0);
+        const other = new Temporal.PlainTime(1, 2, 3, 4, 5, 6);
+        const values = [
+            ["hour", "PT1H2M3.004005006S"],
+            ["minute", "PT62M3.004005006S"],
+            ["second", "PT3723.004005006S"],
+            ["millisecond", "PT3723.004005006S"],
+            ["microsecond", "PT3723.004005006S"],
+            ["nanosecond", "PT3723.004005006S"],
+        ];
+        for (const [largestUnit, expected] of values) {
+            expect(plainTime.until(other, { largestUnit }).toString()).toBe(expected);
+        }
+    });
+});
+
+describe("errors", () => {
+    test("this value must be a Temporal.PlainTime object", () => {
+        expect(() => {
+            Temporal.PlainTime.prototype.until.call("foo", {});
+        }).toThrowWithMessage(TypeError, "Not an object of type Temporal.PlainTime");
+    });
+
+    test("disallowed smallestUnit option values", () => {
+        const values = ["year", "month", "week", "day"];
+        for (const smallestUnit of values) {
+            const plainTime = new Temporal.PlainTime();
+            const other = new Temporal.PlainTime();
+            expect(() => {
+                plainTime.until(other, { smallestUnit });
+            }).toThrowWithMessage(
+                RangeError,
+                `${smallestUnit} is not a valid value for option smallestUnit`
+            );
+        }
+    });
+
+    test("disallowed largestUnit option values", () => {
+        const values = ["year", "month", "week", "day"];
+        for (const largestUnit of values) {
+            const plainTime = new Temporal.PlainTime();
+            const other = new Temporal.PlainTime();
+            expect(() => {
+                plainTime.until(other, { largestUnit });
+            }).toThrowWithMessage(
+                RangeError,
+                `${largestUnit} is not a valid value for option largestUnit`
+            );
+        }
+    });
+});