mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 13:28:11 +00:00
LibJS: Convert get_iterator_values helper to ThrowCompletionOr
This one is a bit unusual, so to clarify: Previously, callers of get_iterator_values() would supply a callback that would return an IterationDecision, and an enum to indicate whether iterator_close() should be invoked upon IterationDecision::Break. Now, use of both those enums is removed, and callers must return an Optional<Completion>. If a Completion is provided, the iterator will be closed, and that completion will be returned from get_iterator_values. Otherwise, once the iterator is exhausted, a default-initialized Completion will be returned.
This commit is contained in:
parent
7b4814f74c
commit
04b4307b3d
8 changed files with 74 additions and 135 deletions
|
@ -232,31 +232,19 @@ JS_DEFINE_OLD_NATIVE_FUNCTION(ObjectConstructor::from_entries)
|
|||
|
||||
auto* object = Object::create(global_object, global_object.object_prototype());
|
||||
|
||||
get_iterator_values(global_object, iterable, [&](Value iterator_value) {
|
||||
if (vm.exception())
|
||||
return IterationDecision::Break;
|
||||
if (!iterator_value.is_object()) {
|
||||
vm.throw_exception<TypeError>(global_object, ErrorType::NotAnObject, String::formatted("Iterator value {}", iterator_value.to_string_without_side_effects()));
|
||||
return IterationDecision::Break;
|
||||
}
|
||||
auto key_or_error = iterator_value.as_object().get(0);
|
||||
if (key_or_error.is_error())
|
||||
return IterationDecision::Break;
|
||||
auto key = key_or_error.release_value();
|
||||
auto value_or_error = iterator_value.as_object().get(1);
|
||||
if (value_or_error.is_error())
|
||||
return IterationDecision::Break;
|
||||
auto value = value_or_error.release_value();
|
||||
auto property_key_or_error = key.to_property_key(global_object);
|
||||
if (property_key_or_error.is_error())
|
||||
return IterationDecision::Break;
|
||||
auto result_or_error = object->create_data_property_or_throw(property_key_or_error.release_value(), value);
|
||||
if (result_or_error.is_error())
|
||||
return IterationDecision::Break;
|
||||
return IterationDecision::Continue;
|
||||
});
|
||||
if (vm.exception())
|
||||
TRY_OR_DISCARD(get_iterator_values(global_object, iterable, [&](Value iterator_value) -> Optional<Completion> {
|
||||
if (!iterator_value.is_object())
|
||||
return vm.throw_completion<TypeError>(global_object, ErrorType::NotAnObject, String::formatted("Iterator value {}", iterator_value.to_string_without_side_effects()));
|
||||
|
||||
auto key = TRY(iterator_value.as_object().get(0));
|
||||
auto value = TRY(iterator_value.as_object().get(1));
|
||||
|
||||
auto property_key = TRY(key.to_property_key(global_object));
|
||||
MUST(object->create_data_property_or_throw(property_key, value));
|
||||
|
||||
return {};
|
||||
}));
|
||||
|
||||
return object;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue