2020-03-14 20:31:40 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-03-14 20:31:40 +01:00
|
|
|
*/
|
|
|
|
|
|
2020-03-14 13:15:11 +01:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <AK/WeakPtr.h>
|
2020-03-16 14:20:30 +01:00
|
|
|
#include <LibJS/Heap/Heap.h>
|
2020-06-23 16:57:39 +02:00
|
|
|
#include <LibJS/Runtime/GlobalObject.h>
|
2020-03-14 13:15:11 +01:00
|
|
|
#include <LibWeb/Forward.h>
|
|
|
|
|
|
2020-07-21 16:23:08 +02:00
|
|
|
namespace Web::Bindings {
|
2020-03-14 13:15:11 +01:00
|
|
|
|
|
|
|
|
class Wrappable {
|
|
|
|
|
public:
|
|
|
|
|
virtual ~Wrappable();
|
|
|
|
|
|
|
|
|
|
void set_wrapper(Wrapper&);
|
|
|
|
|
Wrapper* wrapper() { return m_wrapper; }
|
|
|
|
|
const Wrapper* wrapper() const { return m_wrapper; }
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
WeakPtr<Wrapper> m_wrapper;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template<class NativeObject>
|
2020-06-23 16:57:39 +02:00
|
|
|
inline Wrapper* wrap_impl(JS::GlobalObject& global_object, NativeObject& native_object)
|
2020-03-14 13:15:11 +01:00
|
|
|
{
|
2020-06-20 17:28:13 +02:00
|
|
|
if (!native_object.wrapper()) {
|
2020-06-23 16:57:39 +02:00
|
|
|
native_object.set_wrapper(*global_object.heap().allocate<typename NativeObject::WrapperType>(global_object, global_object, native_object));
|
2020-06-20 17:28:13 +02:00
|
|
|
}
|
2020-03-14 13:15:11 +01:00
|
|
|
return native_object.wrapper();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|