diff options
author | Linus Groh <mail@linusgroh.de> | 2022-02-09 09:41:08 +0000 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2022-02-09 12:25:27 +0000 |
commit | af7003ebd24cf4f3a4d083f62bc1caff564b6128 (patch) | |
tree | b72459d2b8a2d300abcca17b0632e567b31ae629 /Userland/Libraries/LibJS/Runtime/Temporal | |
parent | a863363b06267a859bdc01e15546fc2aa7884117 (diff) | |
download | serenity-af7003ebd24cf4f3a4d083f62bc1caff564b6128.zip |
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()
Diffstat (limited to 'Userland/Libraries/LibJS/Runtime/Temporal')
3 files changed, 20 insertions, 29 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/TimeZone.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/TimeZone.cpp index 37a68e0224..71da4456bb 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/TimeZone.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/TimeZone.cpp @@ -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); diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/TimeZone.h b/Userland/Libraries/LibJS/Runtime/Temporal/TimeZone.h index cc932facec..2b800e87ca 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/TimeZone.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/TimeZone.h @@ -7,6 +7,7 @@ #pragma once #include <AK/Optional.h> +#include <LibJS/Heap/MarkedVector.h> #include <LibJS/Runtime/MarkedValueList.h> #include <LibJS/Runtime/Object.h> #include <LibJS/Runtime/Temporal/AbstractOperations.h> @@ -53,8 +54,8 @@ ThrowCompletionOr<double> get_offset_nanoseconds_for(GlobalObject&, Value time_z ThrowCompletionOr<String> builtin_time_zone_get_offset_string_for(GlobalObject&, Value time_zone, Instant&); ThrowCompletionOr<PlainDateTime*> builtin_time_zone_get_plain_date_time_for(GlobalObject&, Value time_zone, Instant&, Object& calendar); ThrowCompletionOr<Instant*> builtin_time_zone_get_instant_for(GlobalObject&, Value time_zone, PlainDateTime&, StringView disambiguation); -ThrowCompletionOr<Instant*> disambiguate_possible_instants(GlobalObject&, Vector<Value> const& possible_instants, Value time_zone, PlainDateTime&, StringView disambiguation); -ThrowCompletionOr<MarkedValueList> get_possible_instants_for(GlobalObject&, Value time_zone, PlainDateTime&); +ThrowCompletionOr<Instant*> disambiguate_possible_instants(GlobalObject&, MarkedVector<Instant*> const& possible_instants, Value time_zone, PlainDateTime&, StringView disambiguation); +ThrowCompletionOr<MarkedVector<Instant*>> get_possible_instants_for(GlobalObject&, Value time_zone, PlainDateTime&); ThrowCompletionOr<bool> time_zone_equals(GlobalObject&, Object& one, Object& two); bool is_valid_time_zone_numeric_utc_offset_syntax(String const&); diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTime.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTime.cpp index cc5fd3f0cb..66a9eeef45 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTime.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTime.cpp @@ -79,17 +79,14 @@ ThrowCompletionOr<BigInt const*> interpret_iso_date_time_offset(GlobalObject& gl auto possible_instants = TRY(get_possible_instants_for(global_object, time_zone, *date_time)); // 9. For each element candidate of possibleInstants, do - for (auto& candidate_value : possible_instants) { - // TODO: As per the comment in disambiguate_possible_instants, having a MarkedValueList<T> would allow us to remove this cast. - auto& candidate = static_cast<Instant&>(candidate_value.as_object()); - + for (auto* candidate : possible_instants) { // a. Let candidateNanoseconds be ? GetOffsetNanosecondsFor(timeZone, candidate). - auto candidate_nanoseconds = TRY(get_offset_nanoseconds_for(global_object, time_zone, candidate)); + auto candidate_nanoseconds = TRY(get_offset_nanoseconds_for(global_object, time_zone, *candidate)); // b. If candidateNanoseconds = offsetNanoseconds, then if (candidate_nanoseconds == offset_nanoseconds) { // i. Return candidate.[[Nanoseconds]]. - return &candidate.nanoseconds(); + return &candidate->nanoseconds(); } // c. If matchBehaviour is match minutes, then @@ -100,7 +97,7 @@ ThrowCompletionOr<BigInt const*> interpret_iso_date_time_offset(GlobalObject& gl // ii. If roundedCandidateNanoseconds = offsetNanoseconds, then if (rounded_candidate_nanoseconds == offset_nanoseconds) { // 1. Return candidate.[[Nanoseconds]]. - return &candidate.nanoseconds(); + return &candidate->nanoseconds(); } } } |