LibJS: Use more narrow types in Temporal ISODateTime struct

This commit is contained in:
Linus Groh 2021-07-25 20:45:07 +01:00
parent 96e63415b6
commit 7915994dfc
Notes: sideshowbarker 2024-07-18 08:20:21 +09:00
2 changed files with 24 additions and 22 deletions

View file

@ -383,7 +383,7 @@ Optional<ISODateTime> parse_iso_date_time(GlobalObject& global_object, [[maybe_u
// 5. Set year to ! ToIntegerOrInfinity(year).
i32 year = Value(js_string(vm, normalized_year)).to_integer_or_infinity(global_object);
i32 month;
u8 month;
// 6. If month is undefined, then
if (!month_part.has_value()) {
// a. Set month to 1.
@ -392,10 +392,10 @@ Optional<ISODateTime> parse_iso_date_time(GlobalObject& global_object, [[maybe_u
// 7. Else,
else {
// a. Set month to ! ToIntegerOrInfinity(month).
month = Value(js_string(vm, *month_part)).to_integer_or_infinity(global_object);
month = *month_part->to_uint<u8>();
}
i32 day;
u8 day;
// 8. If day is undefined, then
if (!day_part.has_value()) {
// a. Set day to 1.
@ -404,17 +404,17 @@ Optional<ISODateTime> parse_iso_date_time(GlobalObject& global_object, [[maybe_u
// 9. Else,
else {
// a. Set day to ! ToIntegerOrInfinity(day).
day = Value(js_string(vm, *day_part)).to_integer_or_infinity(global_object);
day = *day_part->to_uint<u8>();
}
// 10. Set hour to ! ToIntegerOrInfinity(hour).
i32 hour = Value(js_string(vm, hour_part.value_or(""sv))).to_integer_or_infinity(global_object);
u8 hour = hour_part->to_uint<u8>().value_or(0);
// 11. Set minute to ! ToIntegerOrInfinity(minute).
i32 minute = Value(js_string(vm, minute_part.value_or(""sv))).to_integer_or_infinity(global_object);
u8 minute = minute_part->to_uint<u8>().value_or(0);
// 12. Set second to ! ToIntegerOrInfinity(second).
i32 second = Value(js_string(vm, second_part.value_or(""sv))).to_integer_or_infinity(global_object);
u8 second = second_part->to_uint<u8>().value_or(0);
// 13. If second is 60, then
if (second == 60) {
@ -422,22 +422,22 @@ Optional<ISODateTime> parse_iso_date_time(GlobalObject& global_object, [[maybe_u
second = 59;
}
i32 millisecond;
i32 microsecond;
i32 nanosecond;
u16 millisecond;
u16 microsecond;
u16 nanosecond;
// 14. If fraction is not undefined, then
if (fraction_part.has_value()) {
// a. Set fraction to the string-concatenation of the previous value of fraction and the string "000000000".
auto fraction = String::formatted("{}000000000", *fraction_part);
// b. Let millisecond be the String value equal to the substring of fraction from 0 to 3.
// c. Set millisecond to ! ToIntegerOrInfinity(millisecond).
millisecond = Value(js_string(vm, fraction.substring(0, 3))).to_integer_or_infinity(global_object);
millisecond = *fraction.substring(0, 3).to_uint<u16>();
// d. Let microsecond be the String value equal to the substring of fraction from 3 to 6.
// e. Set microsecond to ! ToIntegerOrInfinity(microsecond).
microsecond = Value(js_string(vm, fraction.substring(3, 3))).to_integer_or_infinity(global_object);
microsecond = *fraction.substring(3, 3).to_uint<u16>();
// f. Let nanosecond be the String value equal to the substring of fraction from 6 to 9.
// g. Set nanosecond to ! ToIntegerOrInfinity(nanosecond).
nanosecond = Value(js_string(vm, fraction.substring(6, 3))).to_integer_or_infinity(global_object);
nanosecond = *fraction.substring(6, 3).to_uint<u16>();
}
// 15. Else,
else {

View file

@ -22,17 +22,18 @@ enum class OptionType {
struct ISODateTime {
i32 year;
i32 month;
i32 day;
i32 hour;
i32 minute;
i32 second;
i32 millisecond;
i32 microsecond;
i32 nanosecond;
Optional<String> calendar;
u8 month;
u8 day;
u8 hour;
u8 minute;
u8 second;
u16 millisecond;
u16 microsecond;
u16 nanosecond;
Optional<String> calendar = {};
};
// FIXME: Use more narrow types for most of these (u8/u16)
struct TemporalInstant {
i32 year;
i32 month;
@ -46,6 +47,7 @@ struct TemporalInstant {
Optional<String> time_zone_offset;
};
// FIXME: Use more narrow type for month/day (u8)
struct TemporalDate {
i32 year;
i32 month;