mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 05:07:45 +00:00
LibJS: Adjust order of operations in ISO{Date,MonthDay}FromFields
This is a normative change in the Temporal spec.
See: 7dd90dc
This commit is contained in:
parent
3bc54ac75a
commit
569c2dc1d0
8 changed files with 32 additions and 40 deletions
|
@ -738,33 +738,25 @@ ThrowCompletionOr<ISODateRecord> iso_date_from_fields(GlobalObject& global_objec
|
||||||
// 2. Let overflow be ? ToTemporalOverflow(options).
|
// 2. Let overflow be ? ToTemporalOverflow(options).
|
||||||
auto overflow = TRY(to_temporal_overflow(global_object, &options));
|
auto overflow = TRY(to_temporal_overflow(global_object, &options));
|
||||||
|
|
||||||
// 3. Set fields to ? PrepareTemporalFields(fields, « "day", "month", "monthCode", "year" », «»).
|
// 3. Set fields to ? PrepareTemporalFields(fields, « "day", "month", "monthCode", "year" », « "year", "day" »).
|
||||||
auto* prepared_fields = TRY(prepare_temporal_fields(global_object, fields, { "day", "month", "monthCode", "year" }, Vector<StringView> {}));
|
auto* prepared_fields = TRY(prepare_temporal_fields(global_object, fields, { "day", "month", "monthCode", "year" }, Vector<StringView> { "year"sv, "day"sv }));
|
||||||
|
|
||||||
// 4. Let year be ! Get(fields, "year").
|
// 4. Let year be ! Get(fields, "year").
|
||||||
auto year = MUST(prepared_fields->get(vm.names.year));
|
auto year = MUST(prepared_fields->get(vm.names.year));
|
||||||
|
|
||||||
// 5. If year is undefined, throw a TypeError exception.
|
// 5. Assert: Type(year) is Number.
|
||||||
if (year.is_undefined())
|
|
||||||
return vm.throw_completion<TypeError>(global_object, ErrorType::MissingRequiredProperty, vm.names.year.as_string());
|
|
||||||
|
|
||||||
// 6. Assert: Type(year) is Number.
|
|
||||||
VERIFY(year.is_number());
|
VERIFY(year.is_number());
|
||||||
|
|
||||||
// 7. Let month be ? ResolveISOMonth(fields).
|
// 6. Let month be ? ResolveISOMonth(fields).
|
||||||
auto month = TRY(resolve_iso_month(global_object, *prepared_fields));
|
auto month = TRY(resolve_iso_month(global_object, *prepared_fields));
|
||||||
|
|
||||||
// 8. Let day be ! Get(fields, "day").
|
// 7. Let day be ! Get(fields, "day").
|
||||||
auto day = MUST(prepared_fields->get(vm.names.day));
|
auto day = MUST(prepared_fields->get(vm.names.day));
|
||||||
|
|
||||||
// 9. If day is undefined, throw a TypeError exception.
|
// 8. Assert: Type(day) is Number.
|
||||||
if (day.is_undefined())
|
|
||||||
return vm.throw_completion<TypeError>(global_object, ErrorType::MissingRequiredProperty, vm.names.day.as_string());
|
|
||||||
|
|
||||||
// 10. Assert: Type(day) is Number.
|
|
||||||
VERIFY(day.is_number());
|
VERIFY(day.is_number());
|
||||||
|
|
||||||
// 11. Return ? RegulateISODate(ℝ(year), month, ℝ(day), overflow).
|
// 9. Return ? RegulateISODate(ℝ(year), month, ℝ(day), overflow).
|
||||||
return regulate_iso_date(global_object, year.as_double(), month, day.as_double(), overflow);
|
return regulate_iso_date(global_object, year.as_double(), month, day.as_double(), overflow);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -807,8 +799,8 @@ ThrowCompletionOr<ISOMonthDay> iso_month_day_from_fields(GlobalObject& global_ob
|
||||||
// 2. Let overflow be ? ToTemporalOverflow(options).
|
// 2. Let overflow be ? ToTemporalOverflow(options).
|
||||||
auto overflow = TRY(to_temporal_overflow(global_object, &options));
|
auto overflow = TRY(to_temporal_overflow(global_object, &options));
|
||||||
|
|
||||||
// 3. Set fields to ? PrepareTemporalFields(fields, « "day", "month", "monthCode", "year" », «»).
|
// 3. Set fields to ? PrepareTemporalFields(fields, « "day", "month", "monthCode", "year" », « "day" »).
|
||||||
auto* prepared_fields = TRY(prepare_temporal_fields(global_object, fields, { "day"sv, "month"sv, "monthCode"sv, "year"sv }, Vector<StringView> {}));
|
auto* prepared_fields = TRY(prepare_temporal_fields(global_object, fields, { "day"sv, "month"sv, "monthCode"sv, "year"sv }, Vector<StringView> { "day"sv }));
|
||||||
|
|
||||||
// 4. Let month be ! Get(fields, "month").
|
// 4. Let month be ! Get(fields, "month").
|
||||||
auto month_value = MUST(prepared_fields->get(vm.names.month));
|
auto month_value = MUST(prepared_fields->get(vm.names.month));
|
||||||
|
@ -831,19 +823,15 @@ ThrowCompletionOr<ISOMonthDay> iso_month_day_from_fields(GlobalObject& global_ob
|
||||||
// 9. Let day be ! Get(fields, "day").
|
// 9. Let day be ! Get(fields, "day").
|
||||||
auto day = MUST(prepared_fields->get(vm.names.day));
|
auto day = MUST(prepared_fields->get(vm.names.day));
|
||||||
|
|
||||||
// 10. If day is undefined, throw a TypeError exception.
|
// 10. Assert: Type(day) is Number.
|
||||||
if (day.is_undefined())
|
|
||||||
return vm.throw_completion<TypeError>(global_object, ErrorType::MissingRequiredProperty, vm.names.day.as_string());
|
|
||||||
|
|
||||||
// 11. Assert: Type(day) is Number.
|
|
||||||
VERIFY(day.is_number());
|
VERIFY(day.is_number());
|
||||||
|
|
||||||
// 12. Let referenceISOYear be 1972 (the first leap year after the Unix epoch).
|
// 11. Let referenceISOYear be 1972 (the first leap year after the Unix epoch).
|
||||||
i32 reference_iso_year = 1972;
|
i32 reference_iso_year = 1972;
|
||||||
|
|
||||||
Optional<ISODateRecord> result;
|
Optional<ISODateRecord> result;
|
||||||
|
|
||||||
// 13. If monthCode is undefined, then
|
// 12. If monthCode is undefined, then
|
||||||
if (month_code.is_undefined()) {
|
if (month_code.is_undefined()) {
|
||||||
// a. Assert: Type(year) is Number.
|
// a. Assert: Type(year) is Number.
|
||||||
VERIFY(year.is_number());
|
VERIFY(year.is_number());
|
||||||
|
@ -851,13 +839,13 @@ ThrowCompletionOr<ISOMonthDay> iso_month_day_from_fields(GlobalObject& global_ob
|
||||||
// b. Let result be ? RegulateISODate(ℝ(year), month, ℝ(day), overflow).
|
// b. Let result be ? RegulateISODate(ℝ(year), month, ℝ(day), overflow).
|
||||||
result = TRY(regulate_iso_date(global_object, year.as_double(), month, day.as_double(), overflow));
|
result = TRY(regulate_iso_date(global_object, year.as_double(), month, day.as_double(), overflow));
|
||||||
}
|
}
|
||||||
// 14. Else,
|
// 13. Else,
|
||||||
else {
|
else {
|
||||||
// a. Let result be ? RegulateISODate(referenceISOYear, month, ℝ(day), overflow).
|
// a. Let result be ? RegulateISODate(referenceISOYear, month, ℝ(day), overflow).
|
||||||
result = TRY(regulate_iso_date(global_object, reference_iso_year, month, day.as_double(), overflow));
|
result = TRY(regulate_iso_date(global_object, reference_iso_year, month, day.as_double(), overflow));
|
||||||
}
|
}
|
||||||
|
|
||||||
// 15. Return the Record { [[Month]]: result.[[Month]], [[Day]]: result.[[Day]], [[ReferenceISOYear]]: referenceISOYear }.
|
// 14. Return the Record { [[Month]]: result.[[Month]], [[Day]]: result.[[Day]], [[ReferenceISOYear]]: referenceISOYear }.
|
||||||
return ISOMonthDay { .month = result->month, .day = result->day, .reference_iso_year = reference_iso_year };
|
return ISOMonthDay { .month = result->month, .day = result->day, .reference_iso_year = reference_iso_year };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -21,6 +21,6 @@ describe("errors", () => {
|
||||||
const calendar = new Temporal.Calendar("iso8601");
|
const calendar = new Temporal.Calendar("iso8601");
|
||||||
expect(() => {
|
expect(() => {
|
||||||
calendar.era({});
|
calendar.era({});
|
||||||
}).toThrowWithMessage(TypeError, "Required property year is missing or undefined");
|
}).toThrowWithMessage(TypeError, "Required property day is missing or undefined");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -21,6 +21,6 @@ describe("errors", () => {
|
||||||
const calendar = new Temporal.Calendar("iso8601");
|
const calendar = new Temporal.Calendar("iso8601");
|
||||||
expect(() => {
|
expect(() => {
|
||||||
calendar.eraYear({});
|
calendar.eraYear({});
|
||||||
}).toThrowWithMessage(TypeError, "Required property year is missing or undefined");
|
}).toThrowWithMessage(TypeError, "Required property day is missing or undefined");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -37,7 +37,7 @@ describe("errors", () => {
|
||||||
test("month or monthCode field is required", () => {
|
test("month or monthCode field is required", () => {
|
||||||
const calendar = new Temporal.Calendar("iso8601");
|
const calendar = new Temporal.Calendar("iso8601");
|
||||||
expect(() => {
|
expect(() => {
|
||||||
calendar.monthDayFromFields({ year: 2021 });
|
calendar.monthDayFromFields({ year: 2021, day: 1 });
|
||||||
}).toThrowWithMessage(TypeError, "Required property month is missing or undefined");
|
}).toThrowWithMessage(TypeError, "Required property month is missing or undefined");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -113,13 +113,13 @@ describe("errors", () => {
|
||||||
test("missing fields", () => {
|
test("missing fields", () => {
|
||||||
expect(() => {
|
expect(() => {
|
||||||
Temporal.PlainDateTime.from({});
|
Temporal.PlainDateTime.from({});
|
||||||
}).toThrowWithMessage(TypeError, "Required property year is missing or undefined");
|
}).toThrowWithMessage(TypeError, "Required property day is missing or undefined");
|
||||||
expect(() => {
|
expect(() => {
|
||||||
Temporal.PlainDateTime.from({ year: 0 });
|
Temporal.PlainDateTime.from({ year: 0, day: 1 });
|
||||||
}).toThrowWithMessage(TypeError, "Required property month is missing or undefined");
|
}).toThrowWithMessage(TypeError, "Required property month is missing or undefined");
|
||||||
expect(() => {
|
expect(() => {
|
||||||
Temporal.PlainDateTime.from({ year: 0, month: 1 });
|
Temporal.PlainDateTime.from({ month: 1, day: 1 });
|
||||||
}).toThrowWithMessage(TypeError, "Required property day is missing or undefined");
|
}).toThrowWithMessage(TypeError, "Required property year is missing or undefined");
|
||||||
});
|
});
|
||||||
|
|
||||||
test("with 'reject' overflow option", () => {
|
test("with 'reject' overflow option", () => {
|
||||||
|
|
|
@ -52,7 +52,7 @@ describe("errors", () => {
|
||||||
test("missing fields", () => {
|
test("missing fields", () => {
|
||||||
expect(() => {
|
expect(() => {
|
||||||
Temporal.PlainMonthDay.from({});
|
Temporal.PlainMonthDay.from({});
|
||||||
}).toThrowWithMessage(TypeError, "Required property month is missing or undefined");
|
}).toThrowWithMessage(TypeError, "Required property day is missing or undefined");
|
||||||
expect(() => {
|
expect(() => {
|
||||||
Temporal.PlainMonthDay.from({ month: 1 });
|
Temporal.PlainMonthDay.from({ month: 1 });
|
||||||
}).toThrowWithMessage(TypeError, "Required property day is missing or undefined");
|
}).toThrowWithMessage(TypeError, "Required property day is missing or undefined");
|
||||||
|
|
|
@ -129,12 +129,16 @@ describe("errors", () => {
|
||||||
test("requires year property", () => {
|
test("requires year property", () => {
|
||||||
expect(() => {
|
expect(() => {
|
||||||
Temporal.ZonedDateTime.from({ timeZone: new Temporal.TimeZone("UTC") });
|
Temporal.ZonedDateTime.from({ timeZone: new Temporal.TimeZone("UTC") });
|
||||||
}).toThrowWithMessage(TypeError, "Required property year is missing or undefined");
|
}).toThrowWithMessage(TypeError, "Required property day is missing or undefined");
|
||||||
});
|
});
|
||||||
|
|
||||||
test("requires month property", () => {
|
test("requires month property", () => {
|
||||||
expect(() => {
|
expect(() => {
|
||||||
Temporal.ZonedDateTime.from({ timeZone: new Temporal.TimeZone("UTC"), year: 2021 });
|
Temporal.ZonedDateTime.from({
|
||||||
|
timeZone: new Temporal.TimeZone("UTC"),
|
||||||
|
day: 1,
|
||||||
|
year: 2021,
|
||||||
|
});
|
||||||
}).toThrowWithMessage(TypeError, "Required property month is missing or undefined");
|
}).toThrowWithMessage(TypeError, "Required property month is missing or undefined");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -70,14 +70,14 @@ describe("errors", () => {
|
||||||
test("missing properties", () => {
|
test("missing properties", () => {
|
||||||
expect(() => {
|
expect(() => {
|
||||||
new Temporal.ZonedDateTime(1n, {}).withPlainDate({});
|
new Temporal.ZonedDateTime(1n, {}).withPlainDate({});
|
||||||
}).toThrowWithMessage(TypeError, "Required property year is missing or undefined");
|
}).toThrowWithMessage(TypeError, "Required property day is missing or undefined");
|
||||||
|
|
||||||
expect(() => {
|
expect(() => {
|
||||||
new Temporal.ZonedDateTime(1n, {}).withPlainDate({ year: 1 });
|
new Temporal.ZonedDateTime(1n, {}).withPlainDate({ day: 1, year: 1 });
|
||||||
}).toThrowWithMessage(TypeError, "Required property month is missing or undefined");
|
}).toThrowWithMessage(TypeError, "Required property month is missing or undefined");
|
||||||
|
|
||||||
expect(() => {
|
expect(() => {
|
||||||
new Temporal.ZonedDateTime(1n, {}).withPlainDate({ year: 1, month: 1 });
|
new Temporal.ZonedDateTime(1n, {}).withPlainDate({ day: 1, month: 1 });
|
||||||
}).toThrowWithMessage(TypeError, "Required property day is missing or undefined");
|
}).toThrowWithMessage(TypeError, "Required property year is missing or undefined");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue