diff --git a/Libraries/LibJS/Runtime/Temporal/PlainTimePrototype.cpp b/Libraries/LibJS/Runtime/Temporal/PlainTimePrototype.cpp index ad4facd86d7..8466178f6cb 100644 --- a/Libraries/LibJS/Runtime/Temporal/PlainTimePrototype.cpp +++ b/Libraries/LibJS/Runtime/Temporal/PlainTimePrototype.cpp @@ -41,6 +41,8 @@ void PlainTimePrototype::initialize(Realm& realm) define_native_function(realm, vm.names.with, with, 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.round, round, 1, attr); + define_native_function(realm, vm.names.equals, equals, 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); @@ -193,6 +195,89 @@ JS_DEFINE_NATIVE_FUNCTION(PlainTimePrototype::since) return TRY(difference_temporal_plain_time(vm, DurationOperation::Since, temporal_time, other, options)); } +// 4.3.14 Temporal.PlainTime.prototype.round ( roundTo ), https://tc39.es/proposal-temporal/#sec-temporal.plaintime.prototype.round +JS_DEFINE_NATIVE_FUNCTION(PlainTimePrototype::round) +{ + auto& realm = *vm.current_realm(); + + auto round_to_value = vm.argument(0); + + // 1. Let temporalTime be the this value. + // 2. Perform ? RequireInternalSlot(temporalTime, [[InitializedTemporalTime]]). + auto temporal_time = TRY(typed_this_object(vm)); + + // 3. If roundTo is undefined, then + if (round_to_value.is_undefined()) { + // a. Throw a TypeError exception. + return vm.throw_completion(ErrorType::TemporalMissingOptionsObject); + } + + GC::Ptr round_to; + + // 4. If roundTo is a String, then + if (round_to_value.is_string()) { + // a. Let paramString be roundTo. + auto param_string = round_to_value; + + // b. Set roundTo to OrdinaryObjectCreate(null). + round_to = Object::create(realm, nullptr); + + // c. Perform ! CreateDataPropertyOrThrow(roundTo, "smallestUnit", paramString). + MUST(round_to->create_data_property_or_throw(vm.names.smallestUnit, param_string)); + } + // 5. Else, + else { + // a. Set roundTo to ? GetOptionsObject(roundTo). + round_to = TRY(get_options_object(vm, round_to_value)); + } + + // 6. NOTE: The following steps read options and perform independent validation in alphabetical order + // (GetRoundingIncrementOption reads "roundingIncrement" and GetRoundingModeOption reads "roundingMode"). + + // 7. Let roundingIncrement be ? GetRoundingIncrementOption(roundTo). + auto rounding_increment = TRY(get_rounding_increment_option(vm, *round_to)); + + // 8. Let roundingMode be ? GetRoundingModeOption(roundTo, HALF-EXPAND). + auto rounding_mode = TRY(get_rounding_mode_option(vm, *round_to, RoundingMode::HalfExpand)); + + // 9. Let smallestUnit be ? GetTemporalUnitValuedOption(roundTo, "smallestUnit", TIME, REQUIRED). + auto smallest_unit = TRY(get_temporal_unit_valued_option(vm, *round_to, vm.names.smallestUnit, UnitGroup::Time, Required {})); + auto smallest_unit_value = smallest_unit.get(); + + // 10. Let maximum be MaximumTemporalDurationRoundingIncrement(smallestUnit). + auto maximum = maximum_temporal_duration_rounding_increment(smallest_unit_value); + + // 11. Assert: maximum is not UNSET. + VERIFY(!maximum.has()); + + // 12. Perform ? ValidateTemporalRoundingIncrement(roundingIncrement, maximum, false). + TRY(validate_temporal_rounding_increment(vm, rounding_increment, maximum.get(), false)); + + // 13. Let result be RoundTime(temporalTime.[[Time]], roundingIncrement, smallestUnit, roundingMode). + auto result = round_time(temporal_time->time(), rounding_increment, smallest_unit_value, rounding_mode); + + // 14. Return ! CreateTemporalTime(result). + return MUST(create_temporal_time(vm, result)); +} + +// 4.3.15 Temporal.PlainTime.prototype.equals ( other ), https://tc39.es/proposal-temporal/#sec-temporal.plaintime.prototype.equals +JS_DEFINE_NATIVE_FUNCTION(PlainTimePrototype::equals) +{ + // 1. Let temporalTime be the this value. + // 2. Perform ? RequireInternalSlot(temporalTime, [[InitializedTemporalTime]]). + auto temporal_time = TRY(typed_this_object(vm)); + + // 3. Set other to ? ToTemporalTime(other). + auto other = TRY(to_temporal_time(vm, vm.argument(0))); + + // 4. If CompareTimeRecord(temporalTime.[[Time]], other.[[Time]]) = 0, return true. + if (compare_time_record(temporal_time->time(), other->time()) == 0) + return true; + + // 5. Return false. + return false; +} + // 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 ae2c19d525a..ef940aa6749 100644 --- a/Libraries/LibJS/Runtime/Temporal/PlainTimePrototype.h +++ b/Libraries/LibJS/Runtime/Temporal/PlainTimePrototype.h @@ -34,6 +34,8 @@ private: JS_DECLARE_NATIVE_FUNCTION(with); JS_DECLARE_NATIVE_FUNCTION(until); JS_DECLARE_NATIVE_FUNCTION(since); + JS_DECLARE_NATIVE_FUNCTION(round); + JS_DECLARE_NATIVE_FUNCTION(equals); 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.equals.js b/Libraries/LibJS/Tests/builtins/Temporal/PlainTime/PlainTime.prototype.equals.js new file mode 100644 index 00000000000..4d810b3d37f --- /dev/null +++ b/Libraries/LibJS/Tests/builtins/Temporal/PlainTime/PlainTime.prototype.equals.js @@ -0,0 +1,13 @@ +describe("correct behavior", () => { + test("length is 1", () => { + expect(Temporal.PlainTime.prototype.equals).toHaveLength(1); + }); + + test("basic functionality", () => { + const firstPlainTime = new Temporal.PlainTime(1, 1, 1, 1, 1, 1); + const secondPlainTime = new Temporal.PlainTime(0, 1, 1, 1, 1, 1); + expect(firstPlainTime.equals(firstPlainTime)).toBeTrue(); + expect(secondPlainTime.equals(secondPlainTime)).toBeTrue(); + expect(secondPlainTime.equals(firstPlainTime)).toBeFalse(); + }); +}); diff --git a/Libraries/LibJS/Tests/builtins/Temporal/PlainTime/PlainTime.prototype.round.js b/Libraries/LibJS/Tests/builtins/Temporal/PlainTime/PlainTime.prototype.round.js new file mode 100644 index 00000000000..6d9402699b4 --- /dev/null +++ b/Libraries/LibJS/Tests/builtins/Temporal/PlainTime/PlainTime.prototype.round.js @@ -0,0 +1,147 @@ +describe("correct behavior", () => { + test("length is 1", () => { + expect(Temporal.PlainTime.prototype.round).toHaveLength(1); + }); + + test("basic functionality", () => { + const plainTime = new Temporal.PlainTime(18, 15, 9, 100, 200, 300); + + const firstRoundedPlainTime = plainTime.round({ smallestUnit: "minute" }); + expect(firstRoundedPlainTime.hour).toBe(18); + expect(firstRoundedPlainTime.minute).toBe(15); + expect(firstRoundedPlainTime.second).toBe(0); + expect(firstRoundedPlainTime.millisecond).toBe(0); + expect(firstRoundedPlainTime.microsecond).toBe(0); + expect(firstRoundedPlainTime.nanosecond).toBe(0); + + const secondRoundedPlainTime = plainTime.round({ + smallestUnit: "minute", + roundingMode: "ceil", + }); + expect(secondRoundedPlainTime.hour).toBe(18); + expect(secondRoundedPlainTime.minute).toBe(16); + expect(secondRoundedPlainTime.second).toBe(0); + expect(secondRoundedPlainTime.millisecond).toBe(0); + expect(secondRoundedPlainTime.microsecond).toBe(0); + expect(secondRoundedPlainTime.nanosecond).toBe(0); + + const thirdRoundedPlainTime = plainTime.round({ + smallestUnit: "minute", + roundingMode: "ceil", + roundingIncrement: 30, + }); + expect(thirdRoundedPlainTime.hour).toBe(18); + expect(thirdRoundedPlainTime.minute).toBe(30); + expect(thirdRoundedPlainTime.second).toBe(0); + expect(thirdRoundedPlainTime.millisecond).toBe(0); + expect(thirdRoundedPlainTime.microsecond).toBe(0); + expect(thirdRoundedPlainTime.nanosecond).toBe(0); + + const fourthRoundedPlainTime = plainTime.round({ + smallestUnit: "minute", + roundingMode: "floor", + roundingIncrement: 30, + }); + expect(fourthRoundedPlainTime.hour).toBe(18); + expect(fourthRoundedPlainTime.minute).toBe(0); + expect(fourthRoundedPlainTime.second).toBe(0); + expect(fourthRoundedPlainTime.millisecond).toBe(0); + expect(fourthRoundedPlainTime.microsecond).toBe(0); + expect(fourthRoundedPlainTime.nanosecond).toBe(0); + + const fifthRoundedPlainTime = plainTime.round({ + smallestUnit: "hour", + roundingMode: "halfExpand", + roundingIncrement: 4, + }); + expect(fifthRoundedPlainTime.hour).toBe(20); + expect(fifthRoundedPlainTime.minute).toBe(0); + expect(fifthRoundedPlainTime.second).toBe(0); + expect(fifthRoundedPlainTime.millisecond).toBe(0); + expect(fifthRoundedPlainTime.microsecond).toBe(0); + expect(fifthRoundedPlainTime.nanosecond).toBe(0); + }); + + test("string argument is implicitly converted to options object", () => { + const plainTime = new Temporal.PlainTime(18, 15, 9, 100, 200, 300); + expect( + plainTime.round("minute").equals(plainTime.round({ smallestUnit: "minute" })) + ).toBeTrue(); + }); +}); + +describe("errors", () => { + test("this value must be a Temporal.PlainTime object", () => { + expect(() => { + Temporal.PlainTime.prototype.round.call("foo", {}); + }).toThrowWithMessage(TypeError, "Not an object of type Temporal.PlainTime"); + }); + + test("missing options object", () => { + expect(() => { + const plainTime = new Temporal.PlainTime(18, 15, 9, 100, 200, 300); + plainTime.round(); + }).toThrowWithMessage(TypeError, "Required options object is missing or undefined"); + }); + + test("invalid rounding mode", () => { + expect(() => { + const plainTime = new Temporal.PlainTime(18, 15, 9, 100, 200, 300); + plainTime.round({ smallestUnit: "second", roundingMode: "serenityOS" }); + }).toThrowWithMessage( + RangeError, + "serenityOS is not a valid value for option roundingMode" + ); + }); + + test("invalid smallest unit", () => { + expect(() => { + const plainTime = new Temporal.PlainTime(18, 15, 9, 100, 200, 300); + plainTime.round({ smallestUnit: "serenityOS" }); + }).toThrowWithMessage( + RangeError, + "serenityOS is not a valid value for option smallestUnit" + ); + }); + + test("increment may not be NaN", () => { + expect(() => { + const plainTime = new Temporal.PlainTime(18, 15, 9, 100, 200, 300); + plainTime.round({ smallestUnit: "second", roundingIncrement: NaN }); + }).toThrowWithMessage(RangeError, "NaN is not a valid value for option roundingIncrement"); + }); + + test("increment may not be smaller than 1 or larger than maximum", () => { + const plainTime = new Temporal.PlainTime(18, 15, 9, 100, 200, 300); + expect(() => { + plainTime.round({ smallestUnit: "second", roundingIncrement: -1 }); + }).toThrowWithMessage(RangeError, "-1 is not a valid value for option roundingIncrement"); + expect(() => { + plainTime.round({ smallestUnit: "second", roundingIncrement: 0 }); + }).toThrowWithMessage(RangeError, "0 is not a valid value for option roundingIncrement"); + expect(() => { + plainTime.round({ smallestUnit: "second", roundingIncrement: Infinity }); + }).toThrowWithMessage( + RangeError, + "Infinity is not a valid value for option roundingIncrement" + ); + expect(() => { + plainTime.round({ smallestUnit: "hours", roundingIncrement: 24 }); + }).toThrowWithMessage(RangeError, "24 is not a valid value for option roundingIncrement"); + expect(() => { + plainTime.round({ smallestUnit: "minutes", roundingIncrement: 60 }); + }).toThrowWithMessage(RangeError, "60 is not a valid value for option roundingIncrement"); + expect(() => { + plainTime.round({ smallestUnit: "seconds", roundingIncrement: 60 }); + }).toThrowWithMessage(RangeError, "60 is not a valid value for option roundingIncrement"); + expect(() => { + plainTime.round({ smallestUnit: "milliseconds", roundingIncrement: 1000 }); + }).toThrowWithMessage(RangeError, "1000 is not a valid value for option roundingIncrement"); + expect(() => { + plainTime.round({ smallestUnit: "microseconds", roundingIncrement: 1000 }); + }).toThrowWithMessage(RangeError, "1000 is not a valid value for option roundingIncrement"); + expect(() => { + plainTime.round({ smallestUnit: "nanoseconds", roundingIncrement: 1000 }); + }).toThrowWithMessage(RangeError, "1000 is not a valid value for option roundingIncrement"); + }); +});