mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 02:17:35 +00:00
LibJS: Convert 'possible instants' AOs to MarkedVector<Instant*>
Resolve TODOs suggesting the use of a strongly typed MarkedValueList (a.k.a. MarkedVector<T>) in the following AOs: - get_possible_instants_for() - disambiguate_possible_instants()
This commit is contained in:
parent
a863363b06
commit
af7003ebd2
3 changed files with 20 additions and 29 deletions
|
@ -529,10 +529,8 @@ ThrowCompletionOr<Instant*> builtin_time_zone_get_instant_for(GlobalObject& glob
|
|||
}
|
||||
|
||||
// 11.6.15 DisambiguatePossibleInstants ( possibleInstants, timeZone, dateTime, disambiguation ), https://tc39.es/proposal-temporal/#sec-temporal-disambiguatepossibleinstants
|
||||
ThrowCompletionOr<Instant*> disambiguate_possible_instants(GlobalObject& global_object, Vector<Value> const& possible_instants, Value time_zone, PlainDateTime& date_time, StringView disambiguation)
|
||||
ThrowCompletionOr<Instant*> disambiguate_possible_instants(GlobalObject& global_object, MarkedVector<Instant*> const& possible_instants, Value time_zone, PlainDateTime& date_time, StringView disambiguation)
|
||||
{
|
||||
// TODO: MarkedValueList<T> would be nice, then we could pass a Vector<Instant*> here and wouldn't need the casts...
|
||||
|
||||
auto& vm = global_object.vm();
|
||||
|
||||
// 1. Assert: dateTime has an [[InitializedTemporalDateTime]] internal slot.
|
||||
|
@ -543,8 +541,7 @@ ThrowCompletionOr<Instant*> disambiguate_possible_instants(GlobalObject& global_
|
|||
// 3. If n = 1, then
|
||||
if (n == 1) {
|
||||
// a. Return possibleInstants[0].
|
||||
auto& instant = possible_instants[0];
|
||||
return &static_cast<Instant&>(const_cast<Object&>(instant.as_object()));
|
||||
return possible_instants[0];
|
||||
}
|
||||
|
||||
// 4. If n ≠ 0, then
|
||||
|
@ -552,15 +549,13 @@ ThrowCompletionOr<Instant*> disambiguate_possible_instants(GlobalObject& global_
|
|||
// a. If disambiguation is "earlier" or "compatible", then
|
||||
if (disambiguation.is_one_of("earlier"sv, "compatible"sv)) {
|
||||
// i. Return possibleInstants[0].
|
||||
auto& instant = possible_instants[0];
|
||||
return &static_cast<Instant&>(const_cast<Object&>(instant.as_object()));
|
||||
return possible_instants[0];
|
||||
}
|
||||
|
||||
// b. If disambiguation is "later", then
|
||||
if (disambiguation == "later"sv) {
|
||||
// i. Return possibleInstants[n − 1].
|
||||
auto& instant = possible_instants[n - 1];
|
||||
return &static_cast<Instant&>(const_cast<Object&>(instant.as_object()));
|
||||
return possible_instants[n - 1];
|
||||
}
|
||||
|
||||
// c. Assert: disambiguation is "reject".
|
||||
|
@ -606,15 +601,14 @@ ThrowCompletionOr<Instant*> disambiguate_possible_instants(GlobalObject& global_
|
|||
auto* earlier_date_time = MUST(create_temporal_date_time(global_object, earlier.year, earlier.month, earlier.day, earlier.hour, earlier.minute, earlier.second, earlier.millisecond, earlier.microsecond, earlier.nanosecond, date_time.calendar()));
|
||||
|
||||
// c. Set possibleInstants to ? GetPossibleInstantsFor(timeZone, earlierDateTime).
|
||||
auto possible_instants_mvl = TRY(get_possible_instants_for(global_object, time_zone, *earlier_date_time));
|
||||
auto possible_instants_ = TRY(get_possible_instants_for(global_object, time_zone, *earlier_date_time));
|
||||
|
||||
// d. If possibleInstants is empty, throw a RangeError exception.
|
||||
if (possible_instants_mvl.is_empty())
|
||||
if (possible_instants_.is_empty())
|
||||
return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalDisambiguatePossibleInstantsEarlierZero);
|
||||
|
||||
// e. Return possibleInstants[0].
|
||||
auto& instant = possible_instants_mvl[0];
|
||||
return &static_cast<Instant&>(const_cast<Object&>(instant.as_object()));
|
||||
return possible_instants_[0];
|
||||
}
|
||||
|
||||
// 14. Assert: disambiguation is "compatible" or "later".
|
||||
|
@ -627,22 +621,21 @@ ThrowCompletionOr<Instant*> disambiguate_possible_instants(GlobalObject& global_
|
|||
auto* later_date_time = MUST(create_temporal_date_time(global_object, later.year, later.month, later.day, later.hour, later.minute, later.second, later.millisecond, later.microsecond, later.nanosecond, date_time.calendar()));
|
||||
|
||||
// 17. Set possibleInstants to ? GetPossibleInstantsFor(timeZone, laterDateTime).
|
||||
auto possible_instants_mvl = TRY(get_possible_instants_for(global_object, time_zone, *later_date_time));
|
||||
auto possible_instants_ = TRY(get_possible_instants_for(global_object, time_zone, *later_date_time));
|
||||
|
||||
// 18. Set n to possibleInstants's length.
|
||||
n = possible_instants_mvl.size();
|
||||
n = possible_instants_.size();
|
||||
|
||||
// 19. If n = 0, throw a RangeError exception.
|
||||
if (n == 0)
|
||||
return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalDisambiguatePossibleInstantsZero);
|
||||
|
||||
// 20. Return possibleInstants[n − 1].
|
||||
auto& instant = possible_instants_mvl[n - 1];
|
||||
return &static_cast<Instant&>(const_cast<Object&>(instant.as_object()));
|
||||
return possible_instants_[n - 1];
|
||||
}
|
||||
|
||||
// 11.6.16 GetPossibleInstantsFor ( timeZone, dateTime ), https://tc39.es/proposal-temporal/#sec-temporal-getpossibleinstantsfor
|
||||
ThrowCompletionOr<MarkedValueList> get_possible_instants_for(GlobalObject& global_object, Value time_zone, PlainDateTime& date_time)
|
||||
ThrowCompletionOr<MarkedVector<Instant*>> get_possible_instants_for(GlobalObject& global_object, Value time_zone, PlainDateTime& date_time)
|
||||
{
|
||||
auto& vm = global_object.vm();
|
||||
|
||||
|
@ -655,7 +648,7 @@ ThrowCompletionOr<MarkedValueList> get_possible_instants_for(GlobalObject& globa
|
|||
auto iterator = TRY(get_iterator(global_object, possible_instants, IteratorHint::Sync));
|
||||
|
||||
// 4. Let list be a new empty List.
|
||||
auto list = MarkedValueList { vm.heap() };
|
||||
auto list = MarkedVector<Instant*> { vm.heap() };
|
||||
|
||||
// 5. Let next be true.
|
||||
Object* next = nullptr;
|
||||
|
@ -680,7 +673,7 @@ ThrowCompletionOr<MarkedValueList> get_possible_instants_for(GlobalObject& globa
|
|||
}
|
||||
|
||||
// iii. Append nextValue to the end of the List list.
|
||||
list.append(next_value);
|
||||
list.append(static_cast<Instant*>(&next_value.as_object()));
|
||||
}
|
||||
} while (next != nullptr);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue