LibJS+LibWeb: Move IteratorOperations.h AOs to Iterator.h

Rather than splitting the Iterator type and its AOs into two files,
let's combine them into one file to match every other JS runtime object
that we have.
This commit is contained in:
Timothy Flynn 2023-07-19 06:54:48 -04:00 committed by Linus Groh
parent 7d0f70bfa0
commit 9d7215c636
Notes: sideshowbarker 2024-07-17 03:00:02 +09:00
46 changed files with 307 additions and 340 deletions

View file

@ -3264,7 +3264,7 @@ void generate_constructor_implementation(IDL::Interface const& interface, String
#include <LibJS/Runtime/ArrayBuffer.h>
#include <LibJS/Runtime/DataView.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/IteratorOperations.h>
#include <LibJS/Runtime/Iterator.h>
#include <LibJS/Runtime/TypedArray.h>
#include <LibWeb/Bindings/@constructor_class@.h>
#include <LibWeb/Bindings/@prototype_class@.h>
@ -3651,7 +3651,7 @@ void generate_prototype_implementation(IDL::Interface const& interface, StringBu
#include <LibJS/Runtime/Error.h>
#include <LibJS/Runtime/FunctionObject.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/IteratorOperations.h>
#include <LibJS/Runtime/Iterator.h>
#include <LibJS/Runtime/TypedArray.h>
#include <LibJS/Runtime/Value.h>
#include <LibWeb/Bindings/@prototype_class@.h>
@ -3954,7 +3954,7 @@ void generate_global_mixin_implementation(IDL::Interface const& interface, Strin
#include <LibJS/Runtime/Error.h>
#include <LibJS/Runtime/FunctionObject.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/IteratorOperations.h>
#include <LibJS/Runtime/Iterator.h>
#include <LibJS/Runtime/TypedArray.h>
#include <LibJS/Runtime/Value.h>
#include <LibWeb/Bindings/@class_name@.h>

View file

@ -25,7 +25,7 @@
#include <LibJS/Runtime/Error.h>
#include <LibJS/Runtime/FunctionEnvironment.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/IteratorOperations.h>
#include <LibJS/Runtime/Iterator.h>
#include <LibJS/Runtime/NativeFunction.h>
#include <LibJS/Runtime/ObjectEnvironment.h>
#include <LibJS/Runtime/PrimitiveString.h>

View file

@ -20,7 +20,6 @@
#include <LibJS/Runtime/GlobalEnvironment.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/Iterator.h>
#include <LibJS/Runtime/IteratorOperations.h>
#include <LibJS/Runtime/NativeFunction.h>
#include <LibJS/Runtime/ObjectEnvironment.h>
#include <LibJS/Runtime/Reference.h>

View file

@ -19,7 +19,7 @@
#include <LibJS/Heap/Cell.h>
#include <LibJS/Runtime/Environment.h>
#include <LibJS/Runtime/EnvironmentCoordinate.h>
#include <LibJS/Runtime/IteratorOperations.h>
#include <LibJS/Runtime/Iterator.h>
#include <LibJS/Runtime/Value.h>
#include <LibJS/Runtime/ValueTraits.h>

View file

@ -147,7 +147,6 @@ set(SOURCES
Runtime/IteratorConstructor.cpp
Runtime/IteratorHelper.cpp
Runtime/IteratorHelperPrototype.cpp
Runtime/IteratorOperations.cpp
Runtime/IteratorPrototype.cpp
Runtime/JSONObject.cpp
Runtime/JobCallback.cpp

View file

@ -14,7 +14,7 @@
#include <LibJS/Runtime/CanonicalIndex.h>
#include <LibJS/Runtime/FunctionObject.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/IteratorOperations.h>
#include <LibJS/Runtime/Iterator.h>
#include <LibJS/Runtime/PrivateEnvironment.h>
#include <LibJS/Runtime/Value.h>

View file

@ -10,7 +10,7 @@
#include <LibJS/Runtime/Array.h>
#include <LibJS/Runtime/ErrorConstructor.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/IteratorOperations.h>
#include <LibJS/Runtime/Iterator.h>
namespace JS {

View file

@ -15,7 +15,7 @@
#include <LibJS/Runtime/ECMAScriptFunctionObject.h>
#include <LibJS/Runtime/Error.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/IteratorOperations.h>
#include <LibJS/Runtime/Iterator.h>
#include <LibJS/Runtime/PromiseCapability.h>
#include <LibJS/Runtime/PromiseConstructor.h>
#include <LibJS/Runtime/Shape.h>

View file

@ -9,7 +9,7 @@
#include <LibJS/Runtime/ArrayIteratorPrototype.h>
#include <LibJS/Runtime/Error.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/IteratorOperations.h>
#include <LibJS/Runtime/Iterator.h>
#include <LibJS/Runtime/TypedArray.h>
namespace JS {

View file

@ -7,7 +7,7 @@
#include <LibJS/Runtime/AsyncFromSyncIteratorPrototype.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/IteratorOperations.h>
#include <LibJS/Runtime/Iterator.h>
#include <LibJS/Runtime/Promise.h>
#include <LibJS/Runtime/PromiseCapability.h>
#include <LibJS/Runtime/PromiseConstructor.h>

View file

@ -6,7 +6,7 @@
*/
#include <LibJS/Runtime/AsyncGeneratorPrototype.h>
#include <LibJS/Runtime/IteratorOperations.h>
#include <LibJS/Runtime/Iterator.h>
#include <LibJS/Runtime/PromiseCapability.h>
#include <LibJS/Runtime/PromiseConstructor.h>

View file

@ -10,7 +10,7 @@
#include <LibJS/Runtime/GeneratorObject.h>
#include <LibJS/Runtime/GeneratorPrototype.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/IteratorOperations.h>
#include <LibJS/Runtime/Iterator.h>
namespace JS {

View file

@ -7,7 +7,7 @@
#include <LibJS/Runtime/Array.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/Intl/ListFormat.h>
#include <LibJS/Runtime/IteratorOperations.h>
#include <LibJS/Runtime/Iterator.h>
#include <LibJS/Runtime/ThrowableStringBuilder.h>
namespace JS::Intl {

View file

@ -8,7 +8,7 @@
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/Intl/SegmentIteratorPrototype.h>
#include <LibJS/Runtime/Intl/Segments.h>
#include <LibJS/Runtime/IteratorOperations.h>
#include <LibJS/Runtime/Iterator.h>
namespace JS::Intl {

View file

@ -1,10 +1,15 @@
/*
* Copyright (c) 2020, Matthew Olsson <mattco@serenityos.org>
* Copyright (c) 2022-2023, Linus Groh <linusg@serenityos.org>
* Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibJS/Runtime/AbstractOperations.h>
#include <LibJS/Runtime/AsyncFromSyncIteratorPrototype.h>
#include <LibJS/Runtime/Error.h>
#include <LibJS/Runtime/FunctionObject.h>
#include <LibJS/Runtime/Iterator.h>
#include <LibJS/Runtime/VM.h>
@ -26,6 +31,66 @@ Iterator::Iterator(Object& prototype)
{
}
// 7.4.2 GetIteratorFromMethod ( obj, method ), https://tc39.es/ecma262/#sec-getiteratorfrommethod
ThrowCompletionOr<IteratorRecord> get_iterator_from_method(VM& vm, Value object, NonnullGCPtr<FunctionObject> method)
{
// 1. Let iterator be ? Call(method, obj).
auto iterator = TRY(call(vm, *method, object));
// 2. If iterator is not an Object, throw a TypeError exception.
if (!iterator.is_object())
return vm.throw_completion<TypeError>(ErrorType::NotIterable, TRY_OR_THROW_OOM(vm, object.to_string_without_side_effects()));
// 3. Let nextMethod be ? Get(iterator, "next").
auto next_method = TRY(iterator.get(vm, vm.names.next));
// 4. Let iteratorRecord be the Iterator Record { [[Iterator]]: iterator, [[NextMethod]]: nextMethod, [[Done]]: false }.
auto iterator_record = IteratorRecord { .iterator = &iterator.as_object(), .next_method = next_method, .done = false };
// 5. Return iteratorRecord.
return iterator_record;
}
// 7.4.3 GetIterator ( obj, kind ), https://tc39.es/ecma262/#sec-getiterator
ThrowCompletionOr<IteratorRecord> get_iterator(VM& vm, Value object, IteratorHint kind)
{
JS::GCPtr<FunctionObject> method;
// 1. If kind is async, then
if (kind == IteratorHint::Async) {
// a. Let method be ? GetMethod(obj, @@asyncIterator).
method = TRY(object.get_method(vm, vm.well_known_symbol_async_iterator()));
// b. If method is undefined, then
if (!method) {
// i. Let syncMethod be ? GetMethod(obj, @@iterator).
auto sync_method = TRY(object.get_method(vm, vm.well_known_symbol_iterator()));
// ii. If syncMethod is undefined, throw a TypeError exception.
if (!sync_method)
return vm.throw_completion<TypeError>(ErrorType::NotIterable, TRY_OR_THROW_OOM(vm, object.to_string_without_side_effects()));
// iii. Let syncIteratorRecord be ? GetIteratorFromMethod(obj, syncMethod).
auto sync_iterator_record = TRY(get_iterator_from_method(vm, object, *sync_method));
// iv. Return CreateAsyncFromSyncIterator(syncIteratorRecord).
return create_async_from_sync_iterator(vm, sync_iterator_record);
}
}
// 2. Else,
else {
// a. Let method be ? GetMethod(obj, @@iterator).
method = TRY(object.get_method(vm, vm.well_known_symbol_iterator()));
}
// 3. If method is undefined, throw a TypeError exception.
if (!method)
return vm.throw_completion<TypeError>(ErrorType::NotIterable, TRY_OR_THROW_OOM(vm, object.to_string_without_side_effects()));
// 4. Return ? GetIteratorFromMethod(obj, method).
return TRY(get_iterator_from_method(vm, object, *method));
}
// 2.1.1 GetIteratorDirect ( obj ), https://tc39.es/proposal-iterator-helpers/#sec-getiteratorflattenable
ThrowCompletionOr<IteratorRecord> get_iterator_direct(VM& vm, Object& object)
{
@ -73,4 +138,183 @@ ThrowCompletionOr<IteratorRecord> get_iterator_flattenable(VM& vm, Value object,
return TRY(get_iterator_direct(vm, iterator.as_object()));
}
// 7.4.4 IteratorNext ( iteratorRecord [ , value ] ), https://tc39.es/ecma262/#sec-iteratornext
ThrowCompletionOr<NonnullGCPtr<Object>> iterator_next(VM& vm, IteratorRecord const& iterator_record, Optional<Value> value)
{
Value result;
// 1. If value is not present, then
if (!value.has_value()) {
// a. Let result be ? Call(iteratorRecord.[[NextMethod]], iteratorRecord.[[Iterator]]).
result = TRY(call(vm, iterator_record.next_method, iterator_record.iterator));
} else {
// a. Let result be ? Call(iteratorRecord.[[NextMethod]], iteratorRecord.[[Iterator]], « value »).
result = TRY(call(vm, iterator_record.next_method, iterator_record.iterator, *value));
}
// 3. If Type(result) is not Object, throw a TypeError exception.
if (!result.is_object())
return vm.throw_completion<TypeError>(ErrorType::IterableNextBadReturn);
// 4. Return result.
return result.as_object();
}
// 7.4.5 IteratorComplete ( iterResult ), https://tc39.es/ecma262/#sec-iteratorcomplete
ThrowCompletionOr<bool> iterator_complete(VM& vm, Object& iterator_result)
{
// 1. Return ToBoolean(? Get(iterResult, "done")).
return TRY(iterator_result.get(vm.names.done)).to_boolean();
}
// 7.4.6 IteratorValue ( iterResult ), https://tc39.es/ecma262/#sec-iteratorvalue
ThrowCompletionOr<Value> iterator_value(VM& vm, Object& iterator_result)
{
// 1. Return ? Get(iterResult, "value").
return TRY(iterator_result.get(vm.names.value));
}
// 7.4.7 IteratorStep ( iteratorRecord ), https://tc39.es/ecma262/#sec-iteratorstep
ThrowCompletionOr<GCPtr<Object>> iterator_step(VM& vm, IteratorRecord const& iterator_record)
{
// 1. Let result be ? IteratorNext(iteratorRecord).
auto result = TRY(iterator_next(vm, iterator_record));
// 2. Let done be ? IteratorComplete(result).
auto done = TRY(iterator_complete(vm, result));
// 3. If done is true, return false.
if (done)
return nullptr;
// 4. Return result.
return result;
}
// 7.4.8 IteratorClose ( iteratorRecord, completion ), https://tc39.es/ecma262/#sec-iteratorclose
// 7.4.10 AsyncIteratorClose ( iteratorRecord, completion ), https://tc39.es/ecma262/#sec-asynciteratorclose
// NOTE: These only differ in that async awaits the inner value after the call.
static Completion iterator_close_impl(VM& vm, IteratorRecord const& iterator_record, Completion completion, IteratorHint iterator_hint)
{
// 1. Assert: Type(iteratorRecord.[[Iterator]]) is Object.
// 2. Let iterator be iteratorRecord.[[Iterator]].
auto iterator = iterator_record.iterator;
// 3. Let innerResult be Completion(GetMethod(iterator, "return")).
auto inner_result = ThrowCompletionOr<Value> { js_undefined() };
auto get_method_result = Value(iterator).get_method(vm, vm.names.return_);
if (get_method_result.is_error())
inner_result = get_method_result.release_error();
// 4. If innerResult.[[Type]] is normal, then
if (!inner_result.is_error()) {
// a. Let return be innerResult.[[Value]].
auto return_method = get_method_result.value();
// b. If return is undefined, return ? completion.
if (!return_method)
return completion;
// c. Set innerResult to Completion(Call(return, iterator)).
inner_result = call(vm, return_method, iterator);
// Note: If this is AsyncIteratorClose perform one extra step.
if (iterator_hint == IteratorHint::Async && !inner_result.is_error()) {
// d. If innerResult.[[Type]] is normal, set innerResult to Completion(Await(innerResult.[[Value]])).
inner_result = await(vm, inner_result.value());
}
}
// 5. If completion.[[Type]] is throw, return ? completion.
if (completion.is_error())
return completion;
// 6. If innerResult.[[Type]] is throw, return ? innerResult.
if (inner_result.is_throw_completion())
return inner_result;
// 7. If Type(innerResult.[[Value]]) is not Object, throw a TypeError exception.
if (!inner_result.value().is_object())
return vm.throw_completion<TypeError>(ErrorType::IterableReturnBadReturn);
// 8. Return ? completion.
return completion;
}
// 7.4.8 IteratorClose ( iteratorRecord, completion ), https://tc39.es/ecma262/#sec-iteratorclose
Completion iterator_close(VM& vm, IteratorRecord const& iterator_record, Completion completion)
{
return iterator_close_impl(vm, iterator_record, move(completion), IteratorHint::Sync);
}
// 7.4.10 AsyncIteratorClose ( iteratorRecord, completion ), https://tc39.es/ecma262/#sec-asynciteratorclose
Completion async_iterator_close(VM& vm, IteratorRecord const& iterator_record, Completion completion)
{
return iterator_close_impl(vm, iterator_record, move(completion), IteratorHint::Async);
}
// 7.4.11 CreateIterResultObject ( value, done ), https://tc39.es/ecma262/#sec-createiterresultobject
NonnullGCPtr<Object> create_iterator_result_object(VM& vm, Value value, bool done)
{
auto& realm = *vm.current_realm();
// 1. Let obj be OrdinaryObjectCreate(%Object.prototype%).
auto object = Object::create(realm, realm.intrinsics().object_prototype());
// 2. Perform ! CreateDataPropertyOrThrow(obj, "value", value).
MUST(object->create_data_property_or_throw(vm.names.value, value));
// 3. Perform ! CreateDataPropertyOrThrow(obj, "done", done).
MUST(object->create_data_property_or_throw(vm.names.done, Value(done)));
// 4. Return obj.
return object;
}
// 7.4.13 IteratorToList ( iteratorRecord ), https://tc39.es/ecma262/#sec-iteratortolist
ThrowCompletionOr<MarkedVector<Value>> iterator_to_list(VM& vm, IteratorRecord const& iterator_record)
{
// 1. Let values be a new empty List.
MarkedVector<Value> values(vm.heap());
// 2. Let next be true.
GCPtr<Object> next;
// 3. Repeat, while next is not false,
do {
// a. Set next to ? IteratorStep(iteratorRecord).
next = TRY(iterator_step(vm, iterator_record));
// b. If next is not false, then
if (next) {
// i. Let nextValue be ? IteratorValue(next).
auto next_value = TRY(iterator_value(vm, *next));
// ii. Append nextValue to values.
TRY_OR_THROW_OOM(vm, values.try_append(next_value));
}
} while (next);
// 4. Return values.
return values;
}
// Non-standard
Completion get_iterator_values(VM& vm, Value iterable, IteratorValueCallback callback)
{
auto iterator_record = TRY(get_iterator(vm, iterable, IteratorHint::Sync));
while (true) {
auto next_object = TRY(iterator_step(vm, iterator_record));
if (!next_object)
return {};
auto next_value = TRY(iterator_value(vm, *next_object));
if (auto completion = callback(next_value); completion.has_value())
return iterator_close(vm, iterator_record, completion.release_value());
}
}
}

View file

@ -1,5 +1,6 @@
/*
* Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
* Copyright (c) 2020, Matthew Olsson <mattco@serenityos.org>
* Copyright (c) 2022-2023, Linus Groh <linusg@serenityos.org>
* Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
@ -7,6 +8,8 @@
#pragma once
#include <AK/Function.h>
#include <AK/Optional.h>
#include <LibJS/Runtime/Completion.h>
#include <LibJS/Runtime/Object.h>
#include <LibJS/Runtime/Value.h>
@ -35,12 +38,30 @@ private:
IteratorRecord m_iterated; // [[Iterated]]
};
enum class IteratorHint {
Sync,
Async,
};
enum class StringHandling {
IterateStrings,
RejectStrings,
};
ThrowCompletionOr<IteratorRecord> get_iterator_from_method(VM&, Value, NonnullGCPtr<FunctionObject>);
ThrowCompletionOr<IteratorRecord> get_iterator(VM&, Value, IteratorHint);
ThrowCompletionOr<IteratorRecord> get_iterator_direct(VM&, Object&);
ThrowCompletionOr<IteratorRecord> get_iterator_flattenable(VM&, Value, StringHandling);
ThrowCompletionOr<NonnullGCPtr<Object>> iterator_next(VM&, IteratorRecord const&, Optional<Value> = {});
ThrowCompletionOr<GCPtr<Object>> iterator_step(VM&, IteratorRecord const&);
ThrowCompletionOr<bool> iterator_complete(VM&, Object& iterator_result);
ThrowCompletionOr<Value> iterator_value(VM&, Object& iterator_result);
Completion iterator_close(VM&, IteratorRecord const&, Completion);
Completion async_iterator_close(VM&, IteratorRecord const&, Completion);
NonnullGCPtr<Object> create_iterator_result_object(VM&, Value, bool done);
ThrowCompletionOr<MarkedVector<Value>> iterator_to_list(VM&, IteratorRecord const&);
using IteratorValueCallback = Function<Optional<Completion>(Value)>;
Completion get_iterator_values(VM&, Value iterable, IteratorValueCallback callback);
}

View file

@ -5,8 +5,8 @@
*/
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/Iterator.h>
#include <LibJS/Runtime/IteratorHelper.h>
#include <LibJS/Runtime/IteratorOperations.h>
#include <LibJS/Runtime/Realm.h>
namespace JS {

View file

@ -4,8 +4,8 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibJS/Runtime/Iterator.h>
#include <LibJS/Runtime/IteratorHelperPrototype.h>
#include <LibJS/Runtime/IteratorOperations.h>
#include <LibJS/Runtime/Realm.h>
namespace JS {

View file

@ -1,256 +0,0 @@
/*
* Copyright (c) 2020, Matthew Olsson <mattco@serenityos.org>
* Copyright (c) 2022-2023, Linus Groh <linusg@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibJS/Runtime/AbstractOperations.h>
#include <LibJS/Runtime/AsyncFromSyncIteratorPrototype.h>
#include <LibJS/Runtime/Error.h>
#include <LibJS/Runtime/FunctionObject.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/IteratorOperations.h>
namespace JS {
// 7.4.2 GetIteratorFromMethod ( obj, method ), https://tc39.es/ecma262/#sec-getiteratorfrommethod
ThrowCompletionOr<IteratorRecord> get_iterator_from_method(VM& vm, Value object, NonnullGCPtr<FunctionObject> method)
{
// 1. Let iterator be ? Call(method, obj).
auto iterator = TRY(call(vm, *method, object));
// 2. If iterator is not an Object, throw a TypeError exception.
if (!iterator.is_object())
return vm.throw_completion<TypeError>(ErrorType::NotIterable, TRY_OR_THROW_OOM(vm, object.to_string_without_side_effects()));
// 3. Let nextMethod be ? Get(iterator, "next").
auto next_method = TRY(iterator.get(vm, vm.names.next));
// 4. Let iteratorRecord be the Iterator Record { [[Iterator]]: iterator, [[NextMethod]]: nextMethod, [[Done]]: false }.
auto iterator_record = IteratorRecord { .iterator = &iterator.as_object(), .next_method = next_method, .done = false };
// 5. Return iteratorRecord.
return iterator_record;
}
// 7.4.3 GetIterator ( obj, kind ), https://tc39.es/ecma262/#sec-getiterator
ThrowCompletionOr<IteratorRecord> get_iterator(VM& vm, Value object, IteratorHint kind)
{
JS::GCPtr<FunctionObject> method;
// 1. If kind is async, then
if (kind == IteratorHint::Async) {
// a. Let method be ? GetMethod(obj, @@asyncIterator).
method = TRY(object.get_method(vm, vm.well_known_symbol_async_iterator()));
// b. If method is undefined, then
if (!method) {
// i. Let syncMethod be ? GetMethod(obj, @@iterator).
auto sync_method = TRY(object.get_method(vm, vm.well_known_symbol_iterator()));
// ii. If syncMethod is undefined, throw a TypeError exception.
if (!sync_method)
return vm.throw_completion<TypeError>(ErrorType::NotIterable, TRY_OR_THROW_OOM(vm, object.to_string_without_side_effects()));
// iii. Let syncIteratorRecord be ? GetIteratorFromMethod(obj, syncMethod).
auto sync_iterator_record = TRY(get_iterator_from_method(vm, object, *sync_method));
// iv. Return CreateAsyncFromSyncIterator(syncIteratorRecord).
return create_async_from_sync_iterator(vm, sync_iterator_record);
}
}
// 2. Else,
else {
// a. Let method be ? GetMethod(obj, @@iterator).
method = TRY(object.get_method(vm, vm.well_known_symbol_iterator()));
}
// 3. If method is undefined, throw a TypeError exception.
if (!method)
return vm.throw_completion<TypeError>(ErrorType::NotIterable, TRY_OR_THROW_OOM(vm, object.to_string_without_side_effects()));
// 4. Return ? GetIteratorFromMethod(obj, method).
return TRY(get_iterator_from_method(vm, object, *method));
}
// 7.4.4 IteratorNext ( iteratorRecord [ , value ] ), https://tc39.es/ecma262/#sec-iteratornext
ThrowCompletionOr<NonnullGCPtr<Object>> iterator_next(VM& vm, IteratorRecord const& iterator_record, Optional<Value> value)
{
Value result;
// 1. If value is not present, then
if (!value.has_value()) {
// a. Let result be ? Call(iteratorRecord.[[NextMethod]], iteratorRecord.[[Iterator]]).
result = TRY(call(vm, iterator_record.next_method, iterator_record.iterator));
} else {
// a. Let result be ? Call(iteratorRecord.[[NextMethod]], iteratorRecord.[[Iterator]], « value »).
result = TRY(call(vm, iterator_record.next_method, iterator_record.iterator, *value));
}
// 3. If Type(result) is not Object, throw a TypeError exception.
if (!result.is_object())
return vm.throw_completion<TypeError>(ErrorType::IterableNextBadReturn);
// 4. Return result.
return result.as_object();
}
// 7.4.5 IteratorComplete ( iterResult ), https://tc39.es/ecma262/#sec-iteratorcomplete
ThrowCompletionOr<bool> iterator_complete(VM& vm, Object& iterator_result)
{
// 1. Return ToBoolean(? Get(iterResult, "done")).
return TRY(iterator_result.get(vm.names.done)).to_boolean();
}
// 7.4.6 IteratorValue ( iterResult ), https://tc39.es/ecma262/#sec-iteratorvalue
ThrowCompletionOr<Value> iterator_value(VM& vm, Object& iterator_result)
{
// 1. Return ? Get(iterResult, "value").
return TRY(iterator_result.get(vm.names.value));
}
// 7.4.7 IteratorStep ( iteratorRecord ), https://tc39.es/ecma262/#sec-iteratorstep
ThrowCompletionOr<GCPtr<Object>> iterator_step(VM& vm, IteratorRecord const& iterator_record)
{
// 1. Let result be ? IteratorNext(iteratorRecord).
auto result = TRY(iterator_next(vm, iterator_record));
// 2. Let done be ? IteratorComplete(result).
auto done = TRY(iterator_complete(vm, result));
// 3. If done is true, return false.
if (done)
return nullptr;
// 4. Return result.
return result;
}
// 7.4.8 IteratorClose ( iteratorRecord, completion ), https://tc39.es/ecma262/#sec-iteratorclose
// 7.4.10 AsyncIteratorClose ( iteratorRecord, completion ), https://tc39.es/ecma262/#sec-asynciteratorclose
// NOTE: These only differ in that async awaits the inner value after the call.
static Completion iterator_close_impl(VM& vm, IteratorRecord const& iterator_record, Completion completion, IteratorHint iterator_hint)
{
// 1. Assert: Type(iteratorRecord.[[Iterator]]) is Object.
// 2. Let iterator be iteratorRecord.[[Iterator]].
auto iterator = iterator_record.iterator;
// 3. Let innerResult be Completion(GetMethod(iterator, "return")).
auto inner_result = ThrowCompletionOr<Value> { js_undefined() };
auto get_method_result = Value(iterator).get_method(vm, vm.names.return_);
if (get_method_result.is_error())
inner_result = get_method_result.release_error();
// 4. If innerResult.[[Type]] is normal, then
if (!inner_result.is_error()) {
// a. Let return be innerResult.[[Value]].
auto return_method = get_method_result.value();
// b. If return is undefined, return ? completion.
if (!return_method)
return completion;
// c. Set innerResult to Completion(Call(return, iterator)).
inner_result = call(vm, return_method, iterator);
// Note: If this is AsyncIteratorClose perform one extra step.
if (iterator_hint == IteratorHint::Async && !inner_result.is_error()) {
// d. If innerResult.[[Type]] is normal, set innerResult to Completion(Await(innerResult.[[Value]])).
inner_result = await(vm, inner_result.value());
}
}
// 5. If completion.[[Type]] is throw, return ? completion.
if (completion.is_error())
return completion;
// 6. If innerResult.[[Type]] is throw, return ? innerResult.
if (inner_result.is_throw_completion())
return inner_result;
// 7. If Type(innerResult.[[Value]]) is not Object, throw a TypeError exception.
if (!inner_result.value().is_object())
return vm.throw_completion<TypeError>(ErrorType::IterableReturnBadReturn);
// 8. Return ? completion.
return completion;
}
// 7.4.8 IteratorClose ( iteratorRecord, completion ), https://tc39.es/ecma262/#sec-iteratorclose
Completion iterator_close(VM& vm, IteratorRecord const& iterator_record, Completion completion)
{
return iterator_close_impl(vm, iterator_record, move(completion), IteratorHint::Sync);
}
// 7.4.10 AsyncIteratorClose ( iteratorRecord, completion ), https://tc39.es/ecma262/#sec-asynciteratorclose
Completion async_iterator_close(VM& vm, IteratorRecord const& iterator_record, Completion completion)
{
return iterator_close_impl(vm, iterator_record, move(completion), IteratorHint::Async);
}
// 7.4.11 CreateIterResultObject ( value, done ), https://tc39.es/ecma262/#sec-createiterresultobject
NonnullGCPtr<Object> create_iterator_result_object(VM& vm, Value value, bool done)
{
auto& realm = *vm.current_realm();
// 1. Let obj be OrdinaryObjectCreate(%Object.prototype%).
auto object = Object::create(realm, realm.intrinsics().object_prototype());
// 2. Perform ! CreateDataPropertyOrThrow(obj, "value", value).
MUST(object->create_data_property_or_throw(vm.names.value, value));
// 3. Perform ! CreateDataPropertyOrThrow(obj, "done", done).
MUST(object->create_data_property_or_throw(vm.names.done, Value(done)));
// 4. Return obj.
return object;
}
// 7.4.13 IteratorToList ( iteratorRecord ), https://tc39.es/ecma262/#sec-iteratortolist
ThrowCompletionOr<MarkedVector<Value>> iterator_to_list(VM& vm, IteratorRecord const& iterator_record)
{
// 1. Let values be a new empty List.
MarkedVector<Value> values(vm.heap());
// 2. Let next be true.
GCPtr<Object> next;
// 3. Repeat, while next is not false,
do {
// a. Set next to ? IteratorStep(iteratorRecord).
next = TRY(iterator_step(vm, iterator_record));
// b. If next is not false, then
if (next) {
// i. Let nextValue be ? IteratorValue(next).
auto next_value = TRY(iterator_value(vm, *next));
// ii. Append nextValue to values.
TRY_OR_THROW_OOM(vm, values.try_append(next_value));
}
} while (next);
// 4. Return values.
return values;
}
// Non-standard
Completion get_iterator_values(VM& vm, Value iterable, IteratorValueCallback callback)
{
auto iterator_record = TRY(get_iterator(vm, iterable, IteratorHint::Sync));
while (true) {
auto next_object = TRY(iterator_step(vm, iterator_record));
if (!next_object)
return {};
auto next_value = TRY(iterator_value(vm, *next_object));
if (auto completion = callback(next_value); completion.has_value())
return iterator_close(vm, iterator_record, completion.release_value());
}
}
}

View file

@ -1,39 +0,0 @@
/*
* Copyright (c) 2020, Matthew Olsson <mattco@serenityos.org>
* Copyright (c) 2022-2023, Linus Groh <linusg@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Function.h>
#include <AK/Optional.h>
#include <LibJS/Runtime/Completion.h>
#include <LibJS/Runtime/Iterator.h>
#include <LibJS/Runtime/Object.h>
namespace JS {
// 7.4 Operations on Iterator Objects, https://tc39.es/ecma262/#sec-operations-on-iterator-objects
enum class IteratorHint {
Sync,
Async,
};
ThrowCompletionOr<IteratorRecord> get_iterator_from_method(VM&, Value, NonnullGCPtr<FunctionObject>);
ThrowCompletionOr<IteratorRecord> get_iterator(VM&, Value, IteratorHint);
ThrowCompletionOr<NonnullGCPtr<Object>> iterator_next(VM&, IteratorRecord const&, Optional<Value> = {});
ThrowCompletionOr<GCPtr<Object>> iterator_step(VM&, IteratorRecord const&);
ThrowCompletionOr<bool> iterator_complete(VM&, Object& iterator_result);
ThrowCompletionOr<Value> iterator_value(VM&, Object& iterator_result);
Completion iterator_close(VM&, IteratorRecord const&, Completion);
Completion async_iterator_close(VM&, IteratorRecord const&, Completion);
NonnullGCPtr<Object> create_iterator_result_object(VM&, Value, bool done);
ThrowCompletionOr<MarkedVector<Value>> iterator_to_list(VM&, IteratorRecord const&);
using IteratorValueCallback = Function<Optional<Completion>(Value)>;
Completion get_iterator_values(VM&, Value iterable, IteratorValueCallback callback);
}

View file

@ -9,8 +9,8 @@
#include <LibJS/Runtime/Array.h>
#include <LibJS/Runtime/FunctionObject.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/Iterator.h>
#include <LibJS/Runtime/IteratorHelper.h>
#include <LibJS/Runtime/IteratorOperations.h>
#include <LibJS/Runtime/IteratorPrototype.h>
namespace JS {

View file

@ -8,7 +8,7 @@
#include <LibJS/Runtime/AbstractOperations.h>
#include <LibJS/Runtime/Array.h>
#include <LibJS/Runtime/Error.h>
#include <LibJS/Runtime/IteratorOperations.h>
#include <LibJS/Runtime/Iterator.h>
#include <LibJS/Runtime/Map.h>
#include <LibJS/Runtime/MapConstructor.h>

View file

@ -8,7 +8,7 @@
#include <LibJS/Runtime/Array.h>
#include <LibJS/Runtime/Error.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/IteratorOperations.h>
#include <LibJS/Runtime/Iterator.h>
#include <LibJS/Runtime/MapIteratorPrototype.h>
namespace JS {

View file

@ -10,7 +10,7 @@
#include <LibJS/Runtime/Array.h>
#include <LibJS/Runtime/Error.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/IteratorOperations.h>
#include <LibJS/Runtime/Iterator.h>
#include <LibJS/Runtime/ObjectConstructor.h>
#include <LibJS/Runtime/ProxyObject.h>
#include <LibJS/Runtime/Shape.h>

View file

@ -12,7 +12,7 @@
#include <LibJS/Runtime/Error.h>
#include <LibJS/Runtime/FunctionObject.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/IteratorOperations.h>
#include <LibJS/Runtime/Iterator.h>
#include <LibJS/Runtime/Promise.h>
#include <LibJS/Runtime/PromiseCapability.h>
#include <LibJS/Runtime/PromiseConstructor.h>

View file

@ -5,7 +5,7 @@
*/
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/IteratorOperations.h>
#include <LibJS/Runtime/Iterator.h>
#include <LibJS/Runtime/RegExpPrototype.h>
#include <LibJS/Runtime/RegExpStringIteratorPrototype.h>
#include <LibJS/Runtime/Utf16String.h>

View file

@ -7,7 +7,7 @@
#include <LibJS/Runtime/AbstractOperations.h>
#include <LibJS/Runtime/Error.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/IteratorOperations.h>
#include <LibJS/Runtime/Iterator.h>
#include <LibJS/Runtime/Set.h>
#include <LibJS/Runtime/SetConstructor.h>

View file

@ -8,7 +8,7 @@
#include <LibJS/Runtime/Array.h>
#include <LibJS/Runtime/Error.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/IteratorOperations.h>
#include <LibJS/Runtime/Iterator.h>
#include <LibJS/Runtime/SetIteratorPrototype.h>
namespace JS {

View file

@ -8,7 +8,7 @@
#include <AK/TypeCasts.h>
#include <LibJS/Runtime/AbstractOperations.h>
#include <LibJS/Runtime/FunctionObject.h>
#include <LibJS/Runtime/IteratorOperations.h>
#include <LibJS/Runtime/Iterator.h>
#include <LibJS/Runtime/SetIterator.h>
#include <LibJS/Runtime/SetPrototype.h>

View file

@ -7,7 +7,7 @@
#include <AK/TypeCasts.h>
#include <LibJS/Runtime/Error.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/IteratorOperations.h>
#include <LibJS/Runtime/Iterator.h>
#include <LibJS/Runtime/StringIteratorPrototype.h>
#include <LibJS/Runtime/ThrowableStringBuilder.h>

View file

@ -8,7 +8,7 @@
#include <LibJS/Runtime/Array.h>
#include <LibJS/Runtime/ErrorConstructor.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/IteratorOperations.h>
#include <LibJS/Runtime/Iterator.h>
#include <LibJS/Runtime/SuppressedError.h>
#include <LibJS/Runtime/SuppressedErrorConstructor.h>

View file

@ -13,7 +13,7 @@
#include <LibJS/Runtime/AbstractOperations.h>
#include <LibJS/Runtime/Completion.h>
#include <LibJS/Runtime/Date.h>
#include <LibJS/Runtime/IteratorOperations.h>
#include <LibJS/Runtime/Iterator.h>
#include <LibJS/Runtime/PropertyKey.h>
#include <LibJS/Runtime/Temporal/AbstractOperations.h>
#include <LibJS/Runtime/Temporal/Calendar.h>

View file

@ -7,7 +7,7 @@
#include <AK/TypeCasts.h>
#include <LibJS/Runtime/Array.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/IteratorOperations.h>
#include <LibJS/Runtime/Iterator.h>
#include <LibJS/Runtime/Temporal/AbstractOperations.h>
#include <LibJS/Runtime/Temporal/Calendar.h>
#include <LibJS/Runtime/Temporal/CalendarPrototype.h>

View file

@ -10,7 +10,7 @@
#include <LibJS/Runtime/AbstractOperations.h>
#include <LibJS/Runtime/Date.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/IteratorOperations.h>
#include <LibJS/Runtime/Iterator.h>
#include <LibJS/Runtime/Temporal/AbstractOperations.h>
#include <LibJS/Runtime/Temporal/Calendar.h>
#include <LibJS/Runtime/Temporal/Instant.h>

View file

@ -12,7 +12,7 @@
#include <LibJS/Runtime/ArrayBufferConstructor.h>
#include <LibJS/Runtime/Completion.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/IteratorOperations.h>
#include <LibJS/Runtime/Iterator.h>
#include <LibJS/Runtime/TypedArray.h>
#include <LibJS/Runtime/TypedArrayConstructor.h>

View file

@ -5,7 +5,7 @@
*/
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/IteratorOperations.h>
#include <LibJS/Runtime/Iterator.h>
#include <LibJS/Runtime/TypedArray.h>
#include <LibJS/Runtime/TypedArrayConstructor.h>

View file

@ -25,7 +25,6 @@
#include <LibJS/Runtime/FinalizationRegistry.h>
#include <LibJS/Runtime/FunctionEnvironment.h>
#include <LibJS/Runtime/Iterator.h>
#include <LibJS/Runtime/IteratorOperations.h>
#include <LibJS/Runtime/NativeFunction.h>
#include <LibJS/Runtime/PromiseCapability.h>
#include <LibJS/Runtime/Reference.h>

View file

@ -7,7 +7,7 @@
#include <LibJS/Runtime/AbstractOperations.h>
#include <LibJS/Runtime/Error.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/IteratorOperations.h>
#include <LibJS/Runtime/Iterator.h>
#include <LibJS/Runtime/WeakMap.h>
#include <LibJS/Runtime/WeakMapConstructor.h>

View file

@ -7,7 +7,7 @@
#include <LibJS/Runtime/AbstractOperations.h>
#include <LibJS/Runtime/Error.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/IteratorOperations.h>
#include <LibJS/Runtime/Iterator.h>
#include <LibJS/Runtime/WeakSet.h>
#include <LibJS/Runtime/WeakSetConstructor.h>

View file

@ -5,7 +5,7 @@
*/
#include <LibJS/Runtime/AbstractOperations.h>
#include <LibJS/Runtime/IteratorOperations.h>
#include <LibJS/Runtime/Iterator.h>
#include <LibJS/Runtime/WrapForValidIteratorPrototype.h>
namespace JS {

View file

@ -5,7 +5,7 @@
*/
#include <LibJS/Runtime/Array.h>
#include <LibJS/Runtime/IteratorOperations.h>
#include <LibJS/Runtime/Iterator.h>
#include <LibWeb/Bindings/HeadersIteratorPrototype.h>
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/Fetch/HeadersIterator.h>

View file

@ -5,7 +5,7 @@
*/
#include <LibJS/Runtime/FunctionObject.h>
#include <LibJS/Runtime/IteratorOperations.h>
#include <LibJS/Runtime/Iterator.h>
#include <LibWeb/Bindings/CustomElementRegistryPrototype.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/DOM/ElementFactory.h>

View file

@ -8,7 +8,7 @@
#include <LibJS/Heap/Heap.h>
#include <LibJS/Runtime/ArrayBuffer.h>
#include <LibJS/Runtime/Error.h>
#include <LibJS/Runtime/IteratorOperations.h>
#include <LibJS/Runtime/Iterator.h>
#include <LibJS/Runtime/PromiseCapability.h>
#include <LibJS/Runtime/Realm.h>
#include <LibJS/Runtime/TypedArray.h>

View file

@ -5,7 +5,7 @@
*/
#include <LibJS/Runtime/Array.h>
#include <LibJS/Runtime/IteratorOperations.h>
#include <LibJS/Runtime/Iterator.h>
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/Bindings/URLSearchParamsIteratorPrototype.h>
#include <LibWeb/URL/URLSearchParamsIterator.h>

View file

@ -11,7 +11,7 @@
#include <LibJS/Runtime/ArrayBuffer.h>
#include <LibJS/Runtime/BigInt.h>
#include <LibJS/Runtime/DataView.h>
#include <LibJS/Runtime/IteratorOperations.h>
#include <LibJS/Runtime/Iterator.h>
#include <LibJS/Runtime/NativeFunction.h>
#include <LibJS/Runtime/Object.h>
#include <LibJS/Runtime/Promise.h>

View file

@ -5,7 +5,7 @@
*/
#include <LibJS/Runtime/Array.h>
#include <LibJS/Runtime/IteratorOperations.h>
#include <LibJS/Runtime/Iterator.h>
#include <LibWeb/Bindings/FormDataIteratorPrototype.h>
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/XHR/FormDataIterator.h>