From 85eef698b95ac97a30c9853ec6e2cff71ddb7e3b Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Sat, 23 Nov 2024 13:48:39 -0500 Subject: [PATCH] LibJS: Implement Temporal.PlainTime.prototype.since/until --- .../LibJS/Runtime/Temporal/PlainTime.cpp | 32 +++++++ Libraries/LibJS/Runtime/Temporal/PlainTime.h | 1 + .../Runtime/Temporal/PlainTimePrototype.cpp | 31 +++++++ .../Runtime/Temporal/PlainTimePrototype.h | 2 + .../PlainTime/PlainTime.prototype.since.js | 89 +++++++++++++++++++ .../PlainTime/PlainTime.prototype.until.js | 89 +++++++++++++++++++ 6 files changed, 244 insertions(+) create mode 100644 Libraries/LibJS/Tests/builtins/Temporal/PlainTime/PlainTime.prototype.since.js create mode 100644 Libraries/LibJS/Tests/builtins/Temporal/PlainTime/PlainTime.prototype.until.js diff --git a/Libraries/LibJS/Runtime/Temporal/PlainTime.cpp b/Libraries/LibJS/Runtime/Temporal/PlainTime.cpp index 4562f46e487..d2b5c8807e1 100644 --- a/Libraries/LibJS/Runtime/Temporal/PlainTime.cpp +++ b/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> 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> add_duration_to_time(VM& vm, ArithmeticOperation operation, PlainTime const& temporal_time, Value temporal_duration_like) { diff --git a/Libraries/LibJS/Runtime/Temporal/PlainTime.h b/Libraries/LibJS/Runtime/Temporal/PlainTime.h index 766883cd7f5..813e1f9521c 100644 --- a/Libraries/LibJS/Runtime/Temporal/PlainTime.h +++ b/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> difference_temporal_plain_time(VM&, DurationOperation, PlainTime const&, Value other, Value options); ThrowCompletionOr> add_duration_to_time(VM&, ArithmeticOperation, PlainTime const&, Value temporal_duration_like); } diff --git a/Libraries/LibJS/Runtime/Temporal/PlainTimePrototype.cpp b/Libraries/LibJS/Runtime/Temporal/PlainTimePrototype.cpp index 18b9b47026e..1eb43e8bcd6 100644 --- a/Libraries/LibJS/Runtime/Temporal/PlainTimePrototype.cpp +++ b/Libraries/LibJS/Runtime/Temporal/PlainTimePrototype.cpp @@ -6,6 +6,7 @@ * SPDX-License-Identifier: BSD-2-Clause */ +#include #include 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) { diff --git a/Libraries/LibJS/Runtime/Temporal/PlainTimePrototype.h b/Libraries/LibJS/Runtime/Temporal/PlainTimePrototype.h index 92ad5ccbf29..8f7492ec277 100644 --- a/Libraries/LibJS/Runtime/Temporal/PlainTimePrototype.h +++ b/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); diff --git a/Libraries/LibJS/Tests/builtins/Temporal/PlainTime/PlainTime.prototype.since.js b/Libraries/LibJS/Tests/builtins/Temporal/PlainTime/PlainTime.prototype.since.js new file mode 100644 index 00000000000..134ef2961de --- /dev/null +++ b/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` + ); + } + }); +}); diff --git a/Libraries/LibJS/Tests/builtins/Temporal/PlainTime/PlainTime.prototype.until.js b/Libraries/LibJS/Tests/builtins/Temporal/PlainTime/PlainTime.prototype.until.js new file mode 100644 index 00000000000..57923f76304 --- /dev/null +++ b/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` + ); + } + }); +});