mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-01-24 02:03:06 -05:00
LibJS: Implement Temporal.PlainDateTime.prototype.add/subtract/equals
This commit is contained in:
parent
d314fcce7a
commit
85ffacacde
Notes:
github-actions[bot]
2024-11-24 10:45:28 +00:00
Author: https://github.com/trflynn89 Commit: https://github.com/LadybirdBrowser/ladybird/commit/85ffacacde1 Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/2544
7 changed files with 139 additions and 0 deletions
|
@ -367,4 +367,39 @@ ThrowCompletionOr<Crypto::BigFraction> difference_plain_date_time_with_total(VM&
|
|||
return TRY(total_relative_duration(vm, diff, dest_epoch_ns, iso_date_time1, {}, calendar, unit));
|
||||
}
|
||||
|
||||
// 5.5.16 AddDurationToDateTime ( operation, dateTime, temporalDurationLike, options ), https://tc39.es/proposal-temporal/#sec-temporal-adddurationtodatetime
|
||||
ThrowCompletionOr<GC::Ref<PlainDateTime>> add_duration_to_date_time(VM& vm, ArithmeticOperation operation, PlainDateTime const& date_time, Value temporal_duration_like, Value options)
|
||||
{
|
||||
// 1. Let duration be ? ToTemporalDuration(temporalDurationLike).
|
||||
auto duration = TRY(to_temporal_duration(vm, temporal_duration_like));
|
||||
|
||||
// 2. If operation is SUBTRACT, set duration to CreateNegatedTemporalDuration(duration).
|
||||
if (operation == ArithmeticOperation::Subtract)
|
||||
duration = create_negated_temporal_duration(vm, duration);
|
||||
|
||||
// 3. Let resolvedOptions be ? GetOptionsObject(options).
|
||||
auto resolved_options = TRY(get_options_object(vm, options));
|
||||
|
||||
// 4. Let overflow be ? GetTemporalOverflowOption(resolvedOptions).
|
||||
auto overflow = TRY(get_temporal_overflow_option(vm, resolved_options));
|
||||
|
||||
// 5. Let internalDuration be ToInternalDurationRecordWith24HourDays(duration).
|
||||
auto internal_duration = to_internal_duration_record_with_24_hour_days(vm, duration);
|
||||
|
||||
// 6. Let timeResult be AddTime(dateTime.[[ISODateTime]].[[Time]], internalDuration.[[Time]]).
|
||||
auto time_result = add_time(date_time.iso_date_time().time, internal_duration.time);
|
||||
|
||||
// 7. Let dateDuration be ? AdjustDateDurationRecord(internalDuration.[[Date]], timeResult.[[Days]]).
|
||||
auto date_duration = TRY(adjust_date_duration_record(vm, internal_duration.date, time_result.days));
|
||||
|
||||
// 8. Let addedDate be ? CalendarDateAdd(dateTime.[[Calendar]], dateTime.[[ISODateTime]].[[ISODate]], dateDuration, overflow).
|
||||
auto added_date = TRY(calendar_date_add(vm, date_time.calendar(), date_time.iso_date_time().iso_date, date_duration, overflow));
|
||||
|
||||
// 9. Let result be CombineISODateAndTimeRecord(addedDate, timeResult).
|
||||
auto result = combine_iso_date_and_time_record(added_date, time_result);
|
||||
|
||||
// 10. Return ? CreateTemporalDateTime(result, dateTime.[[Calendar]]).
|
||||
return TRY(create_temporal_date_time(vm, result, date_time.calendar()));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -44,5 +44,6 @@ ISODateTime round_iso_date_time(ISODateTime const&, u64 increment, Unit, Roundin
|
|||
ThrowCompletionOr<InternalDuration> difference_iso_date_time(VM&, ISODateTime const&, ISODateTime const&, StringView calendar, Unit largest_unit);
|
||||
ThrowCompletionOr<InternalDuration> difference_plain_date_time_with_rounding(VM&, ISODateTime const&, ISODateTime const&, StringView calendar, Unit largest_unit, u64 rounding_increment, Unit smallest_unit, RoundingMode);
|
||||
ThrowCompletionOr<Crypto::BigFraction> difference_plain_date_time_with_total(VM&, ISODateTime const&, ISODateTime const&, StringView calendar, Unit);
|
||||
ThrowCompletionOr<GC::Ref<PlainDateTime>> add_duration_to_date_time(VM&, ArithmeticOperation, PlainDateTime const&, Value temporal_duration_like, Value options);
|
||||
|
||||
}
|
||||
|
|
|
@ -53,6 +53,9 @@ void PlainDateTimePrototype::initialize(Realm& realm)
|
|||
define_native_accessor(realm, vm.names.inLeapYear, in_leap_year_getter, {}, Attribute::Configurable);
|
||||
|
||||
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.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);
|
||||
|
@ -213,6 +216,52 @@ JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::year_of_week_getter)
|
|||
return *result;
|
||||
}
|
||||
|
||||
// 5.3.28 Temporal.PlainDateTime.prototype.add ( temporalDurationLike [ , options ] ), https://tc39.es/proposal-temporal/#sec-temporal.plaindatetime.prototype.add
|
||||
JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::add)
|
||||
{
|
||||
auto temporal_duration_like = vm.argument(0);
|
||||
auto options = vm.argument(1);
|
||||
|
||||
// 1. Let dateTime be the this value.
|
||||
// 2. Perform ? RequireInternalSlot(dateTime, [[InitializedTemporalDateTime]]).
|
||||
auto date_time = TRY(typed_this_object(vm));
|
||||
|
||||
// 3. Return ? AddDurationToDateTime(ADD, dateTime, temporalDurationLike, options).
|
||||
return TRY(add_duration_to_date_time(vm, ArithmeticOperation::Add, date_time, temporal_duration_like, options));
|
||||
}
|
||||
|
||||
// 5.3.29 Temporal.PlainDateTime.prototype.subtract ( temporalDurationLike [ , options ] ), https://tc39.es/proposal-temporal/#sec-temporal.plaindatetime.prototype.add
|
||||
JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::subtract)
|
||||
{
|
||||
auto temporal_duration_like = vm.argument(0);
|
||||
auto options = vm.argument(1);
|
||||
|
||||
// 1. Let dateTime be the this value.
|
||||
// 2. Perform ? RequireInternalSlot(dateTime, [[InitializedTemporalDateTime]]).
|
||||
auto date_time = TRY(typed_this_object(vm));
|
||||
|
||||
// 3. Return ? AddDurationToDateTime(SUBTRACT, dateTime, temporalDurationLike, options).
|
||||
return TRY(add_duration_to_date_time(vm, ArithmeticOperation::Subtract, date_time, temporal_duration_like, options));
|
||||
}
|
||||
|
||||
// 5.3.33 Temporal.PlainDateTime.prototype.equals ( other ), https://tc39.es/proposal-temporal/#sec-temporal.plaindatetime.prototype.equals
|
||||
JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::equals)
|
||||
{
|
||||
// 1. Let dateTime be the this value.
|
||||
// 2. Perform ? RequireInternalSlot(dateTime, [[InitializedTemporalDateTime]]).
|
||||
auto date_time = TRY(typed_this_object(vm));
|
||||
|
||||
// 3. Set other to ? ToTemporalDateTime(other).
|
||||
auto other = TRY(to_temporal_date_time(vm, vm.argument(0)));
|
||||
|
||||
// 4. If CompareISODateTime(dateTime.[[ISODateTime]], other.[[ISODateTime]]) ≠ 0, return false.
|
||||
if (compare_iso_date_time(date_time->iso_date_time(), other->iso_date_time()) != 0)
|
||||
return false;
|
||||
|
||||
// 5. Return CalendarEquals(dateTime.[[Calendar]], other.[[Calendar]]).
|
||||
return calendar_equals(date_time->calendar(), other->calendar());
|
||||
}
|
||||
|
||||
// 5.3.34 Temporal.PlainDateTime.prototype.toString ( [ options ] ), https://tc39.es/proposal-temporal/#sec-temporal.plaindatetime.prototype.tostring
|
||||
JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::to_string)
|
||||
{
|
||||
|
|
|
@ -45,6 +45,9 @@ private:
|
|||
JS_DECLARE_NATIVE_FUNCTION(days_in_year_getter);
|
||||
JS_DECLARE_NATIVE_FUNCTION(months_in_year_getter);
|
||||
JS_DECLARE_NATIVE_FUNCTION(in_leap_year_getter);
|
||||
JS_DECLARE_NATIVE_FUNCTION(add);
|
||||
JS_DECLARE_NATIVE_FUNCTION(subtract);
|
||||
JS_DECLARE_NATIVE_FUNCTION(equals);
|
||||
JS_DECLARE_NATIVE_FUNCTION(to_string);
|
||||
JS_DECLARE_NATIVE_FUNCTION(to_locale_string);
|
||||
JS_DECLARE_NATIVE_FUNCTION(to_json);
|
||||
|
|
|
@ -0,0 +1,19 @@
|
|||
describe("correct behavior", () => {
|
||||
test("length is 1", () => {
|
||||
expect(Temporal.PlainDateTime.prototype.add).toHaveLength(1);
|
||||
});
|
||||
|
||||
test("basic functionality", () => {
|
||||
const plainDateTime = new Temporal.PlainDateTime(1970, 1, 1);
|
||||
const result = plainDateTime.add(new Temporal.Duration(51, 6, 0, 5, 18, 14, 47));
|
||||
expect(result.equals(new Temporal.PlainDateTime(2021, 7, 6, 18, 14, 47))).toBeTrue();
|
||||
});
|
||||
});
|
||||
|
||||
describe("errors", () => {
|
||||
test("this value must be a Temporal.PlainDateTime object", () => {
|
||||
expect(() => {
|
||||
Temporal.PlainDateTime.prototype.add.call("foo");
|
||||
}).toThrowWithMessage(TypeError, "Not an object of type Temporal.PlainDateTime");
|
||||
});
|
||||
});
|
|
@ -0,0 +1,13 @@
|
|||
describe("correct behavior", () => {
|
||||
test("length is 1", () => {
|
||||
expect(Temporal.PlainDateTime.prototype.equals).toHaveLength(1);
|
||||
});
|
||||
|
||||
test("basic functionality", () => {
|
||||
const calendar = "gregory";
|
||||
const firstPlainDateTime = new Temporal.PlainDateTime(1, 1, 1, 1, 1, 1, 1, 1, 1, calendar);
|
||||
const secondPlainDateTime = new Temporal.PlainDateTime(0, 1, 1, 1, 1, 1, 1, 1, 1, calendar);
|
||||
expect(firstPlainDateTime.equals(firstPlainDateTime));
|
||||
expect(!firstPlainDateTime.equals(secondPlainDateTime));
|
||||
});
|
||||
});
|
|
@ -0,0 +1,19 @@
|
|||
describe("correct behavior", () => {
|
||||
test("length is 1", () => {
|
||||
expect(Temporal.PlainDateTime.prototype.subtract).toHaveLength(1);
|
||||
});
|
||||
|
||||
test("basic functionality", () => {
|
||||
const plainDateTime = new Temporal.PlainDateTime(2021, 7, 6, 18, 14, 47);
|
||||
const result = plainDateTime.subtract(new Temporal.Duration(51, 6, 0, 5, 18, 14, 47));
|
||||
expect(result.equals(new Temporal.PlainDateTime(1970, 1, 1)));
|
||||
});
|
||||
});
|
||||
|
||||
describe("errors", () => {
|
||||
test("this value must be a Temporal.PlainDateTime object", () => {
|
||||
expect(() => {
|
||||
Temporal.PlainDateTime.prototype.subtract.call("foo");
|
||||
}).toThrowWithMessage(TypeError, "Not an object of type Temporal.PlainDateTime");
|
||||
});
|
||||
});
|
Loading…
Add table
Reference in a new issue