mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 04:27:43 +00:00
LibJS: Implement Iterator.prototype.map
This uses a new Iterator type called IteratorHelper. This does not implement IteratorHelper.prototype.return as that relies on generator objects (i.e. the internal slots of JS::GeneratorObject), which are not hooked up here.
This commit is contained in:
parent
7ff6b472c0
commit
3eb2e4e08a
10 changed files with 401 additions and 4 deletions
45
Userland/Libraries/LibJS/Runtime/IteratorHelper.cpp
Normal file
45
Userland/Libraries/LibJS/Runtime/IteratorHelper.cpp
Normal file
|
@ -0,0 +1,45 @@
|
|||
/*
|
||||
* Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <LibJS/Runtime/GlobalObject.h>
|
||||
#include <LibJS/Runtime/IteratorHelper.h>
|
||||
#include <LibJS/Runtime/IteratorOperations.h>
|
||||
#include <LibJS/Runtime/Realm.h>
|
||||
|
||||
namespace JS {
|
||||
|
||||
ThrowCompletionOr<NonnullGCPtr<IteratorHelper>> IteratorHelper::create(Realm& realm, IteratorRecord underlying_iterator, Closure closure)
|
||||
{
|
||||
return TRY(realm.heap().allocate<IteratorHelper>(realm, realm.intrinsics().iterator_helper_prototype(), move(underlying_iterator), move(closure)));
|
||||
}
|
||||
|
||||
IteratorHelper::IteratorHelper(Object& prototype, IteratorRecord underlying_iterator, Closure closure)
|
||||
: Object(ConstructWithPrototypeTag::Tag, prototype)
|
||||
, m_underlying_iterator(move(underlying_iterator))
|
||||
, m_closure(move(closure))
|
||||
{
|
||||
}
|
||||
|
||||
void IteratorHelper::visit_edges(Visitor& visitor)
|
||||
{
|
||||
Base::visit_edges(visitor);
|
||||
visitor.visit(m_underlying_iterator.iterator);
|
||||
}
|
||||
|
||||
Value IteratorHelper::result(Value value)
|
||||
{
|
||||
if (value.is_undefined())
|
||||
m_done = true;
|
||||
return value;
|
||||
}
|
||||
|
||||
ThrowCompletionOr<Value> IteratorHelper::close_result(Completion completion)
|
||||
{
|
||||
m_done = true;
|
||||
return *TRY(iterator_close(vm(), underlying_iterator(), move(completion)));
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue