mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-08-01 13:49:16 +00:00
LibJS: Use HeapFunction for IteratorHelper
This commit is contained in:
parent
315a666e53
commit
87d964b2f5
Notes:
github-actions[bot]
2024-08-18 09:16:36 +00:00
Author: https://github.com/shannonbooth
Commit: 87d964b2f5
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/1108
Reviewed-by: https://github.com/awesomekling
3 changed files with 29 additions and 27 deletions
|
@ -13,16 +13,16 @@ namespace JS {
|
|||
|
||||
JS_DEFINE_ALLOCATOR(IteratorHelper);
|
||||
|
||||
ThrowCompletionOr<NonnullGCPtr<IteratorHelper>> IteratorHelper::create(Realm& realm, NonnullGCPtr<IteratorRecord> underlying_iterator, Closure closure, Optional<AbruptClosure> abrupt_closure)
|
||||
ThrowCompletionOr<NonnullGCPtr<IteratorHelper>> IteratorHelper::create(Realm& realm, NonnullGCPtr<IteratorRecord> underlying_iterator, NonnullGCPtr<Closure> closure, GCPtr<AbruptClosure> abrupt_closure)
|
||||
{
|
||||
return realm.heap().allocate<IteratorHelper>(realm, realm, realm.intrinsics().iterator_helper_prototype(), move(underlying_iterator), move(closure), move(abrupt_closure));
|
||||
return realm.heap().allocate<IteratorHelper>(realm, realm, realm.intrinsics().iterator_helper_prototype(), move(underlying_iterator), closure, abrupt_closure);
|
||||
}
|
||||
|
||||
IteratorHelper::IteratorHelper(Realm& realm, Object& prototype, NonnullGCPtr<IteratorRecord> underlying_iterator, Closure closure, Optional<AbruptClosure> abrupt_closure)
|
||||
IteratorHelper::IteratorHelper(Realm& realm, Object& prototype, NonnullGCPtr<IteratorRecord> underlying_iterator, NonnullGCPtr<Closure> closure, GCPtr<AbruptClosure> abrupt_closure)
|
||||
: GeneratorObject(realm, prototype, realm.vm().running_execution_context().copy(), "Iterator Helper"sv)
|
||||
, m_underlying_iterator(move(underlying_iterator))
|
||||
, m_closure(move(closure))
|
||||
, m_abrupt_closure(move(abrupt_closure))
|
||||
, m_closure(closure)
|
||||
, m_abrupt_closure(abrupt_closure)
|
||||
{
|
||||
}
|
||||
|
||||
|
@ -30,6 +30,8 @@ void IteratorHelper::visit_edges(Visitor& visitor)
|
|||
{
|
||||
Base::visit_edges(visitor);
|
||||
visitor.visit(m_underlying_iterator);
|
||||
visitor.visit(m_closure);
|
||||
visitor.visit(m_abrupt_closure);
|
||||
}
|
||||
|
||||
Value IteratorHelper::result(Value value)
|
||||
|
@ -49,12 +51,12 @@ ThrowCompletionOr<Value> IteratorHelper::execute(VM& vm, JS::Completion const& c
|
|||
ScopeGuard guard { [&] { vm.pop_execution_context(); } };
|
||||
|
||||
if (completion.is_abrupt()) {
|
||||
if (m_abrupt_closure.has_value())
|
||||
return (*m_abrupt_closure)(vm, *this, completion);
|
||||
if (m_abrupt_closure)
|
||||
return m_abrupt_closure->function()(vm, *this, completion);
|
||||
return close_result(vm, completion);
|
||||
}
|
||||
|
||||
auto result_value = m_closure(vm, *this);
|
||||
auto result_value = m_closure->function()(vm, *this);
|
||||
|
||||
if (result_value.is_throw_completion()) {
|
||||
set_generator_state(GeneratorState::Completed);
|
||||
|
|
|
@ -6,11 +6,11 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <LibJS/Heap/HeapFunction.h>
|
||||
#include <LibJS/Runtime/Completion.h>
|
||||
#include <LibJS/Runtime/GeneratorObject.h>
|
||||
#include <LibJS/Runtime/Iterator.h>
|
||||
#include <LibJS/Runtime/Object.h>
|
||||
#include <LibJS/SafeFunction.h>
|
||||
|
||||
namespace JS {
|
||||
|
||||
|
@ -19,10 +19,10 @@ class IteratorHelper final : public GeneratorObject {
|
|||
JS_DECLARE_ALLOCATOR(IteratorHelper);
|
||||
|
||||
public:
|
||||
using Closure = JS::SafeFunction<ThrowCompletionOr<Value>(VM&, IteratorHelper&)>;
|
||||
using AbruptClosure = JS::SafeFunction<ThrowCompletionOr<Value>(VM&, IteratorHelper&, Completion const&)>;
|
||||
using Closure = JS::HeapFunction<ThrowCompletionOr<Value>(VM&, IteratorHelper&)>;
|
||||
using AbruptClosure = JS::HeapFunction<ThrowCompletionOr<Value>(VM&, IteratorHelper&, Completion const&)>;
|
||||
|
||||
static ThrowCompletionOr<NonnullGCPtr<IteratorHelper>> create(Realm&, NonnullGCPtr<IteratorRecord>, Closure, Optional<AbruptClosure> = {});
|
||||
static ThrowCompletionOr<NonnullGCPtr<IteratorHelper>> create(Realm&, NonnullGCPtr<IteratorRecord>, NonnullGCPtr<Closure>, GCPtr<AbruptClosure> = {});
|
||||
|
||||
IteratorRecord const& underlying_iterator() const { return m_underlying_iterator; }
|
||||
|
||||
|
@ -33,14 +33,14 @@ public:
|
|||
ThrowCompletionOr<Value> close_result(VM&, Completion);
|
||||
|
||||
private:
|
||||
IteratorHelper(Realm&, Object& prototype, NonnullGCPtr<IteratorRecord>, Closure, Optional<AbruptClosure>);
|
||||
IteratorHelper(Realm&, Object& prototype, NonnullGCPtr<IteratorRecord>, NonnullGCPtr<Closure>, GCPtr<AbruptClosure>);
|
||||
|
||||
virtual void visit_edges(Visitor&) override;
|
||||
virtual ThrowCompletionOr<Value> execute(VM&, JS::Completion const& completion) override;
|
||||
|
||||
NonnullGCPtr<IteratorRecord> m_underlying_iterator; // [[UnderlyingIterator]]
|
||||
Closure m_closure;
|
||||
Optional<AbruptClosure> m_abrupt_closure;
|
||||
NonnullGCPtr<Closure> m_closure;
|
||||
GCPtr<AbruptClosure> m_abrupt_closure;
|
||||
|
||||
size_t m_counter { 0 };
|
||||
bool m_done { false };
|
||||
|
|
|
@ -98,7 +98,7 @@ JS_DEFINE_NATIVE_FUNCTION(IteratorPrototype::map)
|
|||
auto iterated = TRY(get_iterator_direct(vm, object));
|
||||
|
||||
// 5. Let closure be a new Abstract Closure with no parameters that captures iterated and mapper and performs the following steps when called:
|
||||
IteratorHelper::Closure closure = [mapper = NonnullGCPtr { mapper.as_function() }](auto& vm, auto& iterator) -> ThrowCompletionOr<Value> {
|
||||
auto closure = JS::create_heap_function(realm.heap(), [mapper = NonnullGCPtr { mapper.as_function() }](VM& vm, IteratorHelper& iterator) -> ThrowCompletionOr<Value> {
|
||||
auto const& iterated = iterator.underlying_iterator();
|
||||
|
||||
// a. Let counter be 0.
|
||||
|
@ -128,7 +128,7 @@ JS_DEFINE_NATIVE_FUNCTION(IteratorPrototype::map)
|
|||
// vi. Let completion be Completion(Yield(mapped)).
|
||||
// vii. IfAbruptCloseIterator(completion, iterated).
|
||||
return iterator.result(mapped.release_value());
|
||||
};
|
||||
});
|
||||
|
||||
// 6. Let result be CreateIteratorFromClosure(closure, "Iterator Helper", %IteratorHelperPrototype%, « [[UnderlyingIterator]] »).
|
||||
// 7. Set result.[[UnderlyingIterator]] to iterated.
|
||||
|
@ -157,7 +157,7 @@ JS_DEFINE_NATIVE_FUNCTION(IteratorPrototype::filter)
|
|||
auto iterated = TRY(get_iterator_direct(vm, object));
|
||||
|
||||
// 5. Let closure be a new Abstract Closure with no parameters that captures iterated and predicate and performs the following steps when called:
|
||||
IteratorHelper::Closure closure = [predicate = NonnullGCPtr { predicate.as_function() }](auto& vm, auto& iterator) -> ThrowCompletionOr<Value> {
|
||||
auto closure = JS::create_heap_function(realm.heap(), [predicate = NonnullGCPtr { predicate.as_function() }](VM& vm, IteratorHelper& iterator) -> ThrowCompletionOr<Value> {
|
||||
auto const& iterated = iterator.underlying_iterator();
|
||||
|
||||
// a. Let counter be 0.
|
||||
|
@ -192,7 +192,7 @@ JS_DEFINE_NATIVE_FUNCTION(IteratorPrototype::filter)
|
|||
return iterator.result(value);
|
||||
}
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
// 6. Let result be CreateIteratorFromClosure(closure, "Iterator Helper", %IteratorHelperPrototype%, « [[UnderlyingIterator]] »).
|
||||
// 7. Set result.[[UnderlyingIterator]] to iterated.
|
||||
|
@ -231,7 +231,7 @@ JS_DEFINE_NATIVE_FUNCTION(IteratorPrototype::take)
|
|||
auto iterated = TRY(get_iterator_direct(vm, object));
|
||||
|
||||
// 8. Let closure be a new Abstract Closure with no parameters that captures iterated and integerLimit and performs the following steps when called:
|
||||
IteratorHelper::Closure closure = [integer_limit](auto& vm, auto& iterator) -> ThrowCompletionOr<Value> {
|
||||
auto closure = JS::create_heap_function(realm.heap(), [integer_limit](VM& vm, IteratorHelper& iterator) -> ThrowCompletionOr<Value> {
|
||||
auto const& iterated = iterator.underlying_iterator();
|
||||
|
||||
// a. Let remaining be integerLimit.
|
||||
|
@ -257,7 +257,7 @@ JS_DEFINE_NATIVE_FUNCTION(IteratorPrototype::take)
|
|||
// v. Let completion be Completion(Yield(? IteratorValue(next))).
|
||||
// vi. IfAbruptCloseIterator(completion, iterated).
|
||||
return iterator.result(TRY(iterator_value(vm, *next)));
|
||||
};
|
||||
});
|
||||
|
||||
// 9. Let result be CreateIteratorFromClosure(closure, "Iterator Helper", %IteratorHelperPrototype%, « [[UnderlyingIterator]] »).
|
||||
// 10. Set result.[[UnderlyingIterator]] to iterated.
|
||||
|
@ -296,7 +296,7 @@ JS_DEFINE_NATIVE_FUNCTION(IteratorPrototype::drop)
|
|||
auto iterated = TRY(get_iterator_direct(vm, object));
|
||||
|
||||
// 8. Let closure be a new Abstract Closure with no parameters that captures iterated and integerLimit and performs the following steps when called:
|
||||
IteratorHelper::Closure closure = [integer_limit](auto& vm, auto& iterator) -> ThrowCompletionOr<Value> {
|
||||
auto closure = JS::create_heap_function(realm.heap(), [integer_limit](VM& vm, IteratorHelper& iterator) -> ThrowCompletionOr<Value> {
|
||||
auto const& iterated = iterator.underlying_iterator();
|
||||
|
||||
// a. Let remaining be integerLimit.
|
||||
|
@ -326,7 +326,7 @@ JS_DEFINE_NATIVE_FUNCTION(IteratorPrototype::drop)
|
|||
// iii. Let completion be Completion(Yield(? IteratorValue(next))).
|
||||
// iv. IfAbruptCloseIterator(completion, iterated).
|
||||
return iterator.result(TRY(iterator_value(vm, *next)));
|
||||
};
|
||||
});
|
||||
|
||||
// 9. Let result be CreateIteratorFromClosure(closure, "Iterator Helper", %IteratorHelperPrototype%, « [[UnderlyingIterator]] »).
|
||||
// 10. Set result.[[UnderlyingIterator]] to iterated.
|
||||
|
@ -471,14 +471,14 @@ JS_DEFINE_NATIVE_FUNCTION(IteratorPrototype::flat_map)
|
|||
auto flat_map_iterator = vm.heap().allocate<FlatMapIterator>(realm);
|
||||
|
||||
// 5. Let closure be a new Abstract Closure with no parameters that captures iterated and mapper and performs the following steps when called:
|
||||
IteratorHelper::Closure closure = [flat_map_iterator, mapper = NonnullGCPtr { mapper.as_function() }](auto& vm, auto& iterator) mutable -> ThrowCompletionOr<Value> {
|
||||
auto closure = JS::create_heap_function(realm.heap(), [flat_map_iterator, mapper = NonnullGCPtr { mapper.as_function() }](VM& vm, IteratorHelper& iterator) mutable -> ThrowCompletionOr<Value> {
|
||||
auto const& iterated = iterator.underlying_iterator();
|
||||
return flat_map_iterator->next(vm, iterated, iterator, *mapper);
|
||||
};
|
||||
});
|
||||
|
||||
IteratorHelper::AbruptClosure abrupt_closure = [flat_map_iterator](auto& vm, auto& iterator, auto const& completion) -> ThrowCompletionOr<Value> {
|
||||
auto abrupt_closure = JS::create_heap_function(realm.heap(), [flat_map_iterator](VM& vm, IteratorHelper& iterator, Completion const& completion) -> ThrowCompletionOr<Value> {
|
||||
return flat_map_iterator->on_abrupt_completion(vm, iterator, completion);
|
||||
};
|
||||
});
|
||||
|
||||
// 6. Let result be CreateIteratorFromClosure(closure, "Iterator Helper", %IteratorHelperPrototype%, « [[UnderlyingIterator]] »).
|
||||
// 7. Set result.[[UnderlyingIterator]] to iterated.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue