2023-06-25 13:39:33 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <LibJS/Runtime/GlobalObject.h>
|
2023-07-19 10:54:48 +00:00
|
|
|
#include <LibJS/Runtime/Iterator.h>
|
2023-06-25 13:39:33 +00:00
|
|
|
#include <LibJS/Runtime/IteratorHelper.h>
|
|
|
|
#include <LibJS/Runtime/Realm.h>
|
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
2024-11-14 15:01:23 +00:00
|
|
|
GC_DEFINE_ALLOCATOR(IteratorHelper);
|
2023-11-19 08:45:05 +00:00
|
|
|
|
2024-11-14 15:01:23 +00:00
|
|
|
ThrowCompletionOr<GC::Ref<IteratorHelper>> IteratorHelper::create(Realm& realm, GC::Ref<IteratorRecord> underlying_iterator, GC::Ref<Closure> closure, GC::Ptr<AbruptClosure> abrupt_closure)
|
2023-06-25 13:39:33 +00:00
|
|
|
{
|
2024-11-13 16:50:17 +00:00
|
|
|
return realm.create<IteratorHelper>(realm, realm.intrinsics().iterator_helper_prototype(), move(underlying_iterator), closure, abrupt_closure);
|
2023-06-25 13:39:33 +00:00
|
|
|
}
|
|
|
|
|
2024-11-14 15:01:23 +00:00
|
|
|
IteratorHelper::IteratorHelper(Realm& realm, Object& prototype, GC::Ref<IteratorRecord> underlying_iterator, GC::Ref<Closure> closure, GC::Ptr<AbruptClosure> abrupt_closure)
|
2023-07-16 19:04:59 +00:00
|
|
|
: GeneratorObject(realm, prototype, realm.vm().running_execution_context().copy(), "Iterator Helper"sv)
|
2023-06-25 13:39:33 +00:00
|
|
|
, m_underlying_iterator(move(underlying_iterator))
|
2024-08-18 02:54:48 +00:00
|
|
|
, m_closure(closure)
|
|
|
|
, m_abrupt_closure(abrupt_closure)
|
2023-06-25 13:39:33 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void IteratorHelper::visit_edges(Visitor& visitor)
|
|
|
|
{
|
|
|
|
Base::visit_edges(visitor);
|
2023-12-07 09:44:41 +00:00
|
|
|
visitor.visit(m_underlying_iterator);
|
2024-08-18 02:54:48 +00:00
|
|
|
visitor.visit(m_closure);
|
|
|
|
visitor.visit(m_abrupt_closure);
|
2023-06-25 13:39:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Value IteratorHelper::result(Value value)
|
|
|
|
{
|
2023-07-16 19:04:59 +00:00
|
|
|
set_generator_state(value.is_undefined() ? GeneratorState::Completed : GeneratorState::SuspendedYield);
|
2023-06-25 13:39:33 +00:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2023-07-16 19:04:59 +00:00
|
|
|
ThrowCompletionOr<Value> IteratorHelper::close_result(VM& vm, Completion completion)
|
2023-06-25 13:39:33 +00:00
|
|
|
{
|
2023-07-16 19:04:59 +00:00
|
|
|
set_generator_state(GeneratorState::Completed);
|
|
|
|
return *TRY(iterator_close(vm, underlying_iterator(), move(completion)));
|
|
|
|
}
|
|
|
|
|
2023-07-16 19:50:56 +00:00
|
|
|
ThrowCompletionOr<Value> IteratorHelper::execute(VM& vm, JS::Completion const& completion)
|
2023-07-16 19:04:59 +00:00
|
|
|
{
|
|
|
|
ScopeGuard guard { [&] { vm.pop_execution_context(); } };
|
2023-07-16 19:50:56 +00:00
|
|
|
|
|
|
|
if (completion.is_abrupt()) {
|
2024-08-18 02:54:48 +00:00
|
|
|
if (m_abrupt_closure)
|
|
|
|
return m_abrupt_closure->function()(vm, *this, completion);
|
2023-07-16 19:50:56 +00:00
|
|
|
return close_result(vm, completion);
|
|
|
|
}
|
|
|
|
|
2024-08-18 02:54:48 +00:00
|
|
|
auto result_value = m_closure->function()(vm, *this);
|
2023-07-16 19:04:59 +00:00
|
|
|
|
|
|
|
if (result_value.is_throw_completion()) {
|
|
|
|
set_generator_state(GeneratorState::Completed);
|
|
|
|
return result_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
return create_iterator_result_object(vm, result(result_value.release_value()), generator_state() == GeneratorState::Completed);
|
2023-06-25 13:39:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|