1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 04:17:35 +00:00

AK+LibJS: Introduce JS::HeapFunction

This change introduces HeapFunction, which is intended to be used as a
replacement for SafeFunction. The new type behaves like a regular
GC-allocated object, which means it needs to be visited from
visit_edges, and unlike SafeFunction, it does not create new roots for
captured parameters.

Co-Authored-By: Andreas Kling <kling@serenityos.org>
This commit is contained in:
Aliaksandr Kalenik 2023-08-18 18:21:33 +02:00 committed by Andreas Kling
parent a1ccc4b0cf
commit 469aea5a5b
6 changed files with 183 additions and 42 deletions

View file

@ -0,0 +1,50 @@
/*
* Copyright (c) 2023, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Function.h>
#include <LibJS/Heap/Cell.h>
#include <LibJS/Heap/Heap.h>
namespace JS {
template<typename T>
class HeapFunction final : public JS::Cell {
JS_CELL(HeapFunction, Cell);
public:
static NonnullGCPtr<HeapFunction> create(Heap& heap, Function<T> function)
{
return heap.allocate_without_realm<HeapFunction>(move(function));
}
virtual ~HeapFunction() override = default;
Function<T> const& function() const { return m_function; }
private:
HeapFunction(Function<T> function)
: m_function(move(function))
{
}
virtual void visit_edges(Visitor& visitor) override
{
Base::visit_edges(visitor);
visitor.visit_possible_values(m_function.raw_capture_range());
}
Function<T> m_function;
};
template<typename T>
static NonnullGCPtr<HeapFunction<T>> create_heap_function(Heap& heap, Function<T> function)
{
return HeapFunction<T>::create(heap, move(function));
}
}