mirror of
https://github.com/fergalmoran/ladybird.git
synced 2025-12-22 09:19:03 +00:00
Instead, smuggle it in as a `void*` private data and let Javascript aware code cast out that pointer to a VM&. In order to make this split, rename JS::Cell to JS::CellImpl. Once we have a LibGC, this will become GC::Cell. CellImpl then has no specific knowledge of the VM& and Realm&. That knowledge is instead put into JS::Cell, which inherits from CellImpl. JS::Cell is responsible for JavaScript's realm initialization, as well as converting of the void* private data to what it knows should be the VM&.
51 lines
1.2 KiB
C++
51 lines
1.2 KiB
C++
/*
|
|
* Copyright (c) 2023, Andreas Kling <andreas@ladybird.org>
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <AK/Function.h>
|
|
#include <LibJS/Heap/CellImpl.h>
|
|
#include <LibJS/Heap/Heap.h>
|
|
|
|
namespace JS {
|
|
|
|
template<typename T>
|
|
class HeapFunction final : public CellImpl {
|
|
JS_CELL(HeapFunction, CellImpl);
|
|
|
|
public:
|
|
static NonnullGCPtr<HeapFunction> create(Heap& heap, Function<T> function)
|
|
{
|
|
return heap.allocate<HeapFunction>(move(function));
|
|
}
|
|
|
|
virtual ~HeapFunction() override = default;
|
|
|
|
[[nodiscard]] 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 Callable, typename T = EquivalentFunctionType<Callable>>
|
|
static NonnullGCPtr<HeapFunction<T>> create_heap_function(Heap& heap, Callable&& function)
|
|
{
|
|
return HeapFunction<T>::create(heap, Function<T> { forward<Callable>(function) });
|
|
}
|
|
|
|
}
|