summaryrefslogtreecommitdiff
path: root/Libraries/LibWeb/Bindings/Wrappable.h
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2020-03-14 13:15:11 +0100
committerAndreas Kling <kling@serenityos.org>2020-03-14 13:25:40 +0100
commit1c406294fc426a2d083e7d5f30f7e4e97ef4a794 (patch)
tree9dbd3c6fce454018e1f0af6c9dd2ecad1b779898 /Libraries/LibWeb/Bindings/Wrappable.h
parent9c9d3f090429746df8a39232eb75467007463a9e (diff)
downloadserenity-1c406294fc426a2d083e7d5f30f7e4e97ef4a794.zip
LibWeb: Start implementing basic JavaScript DOM bindings
This patch introduces the Wrapper and Wrappable classes. Node now inherits from Wrappable, and can be wrapped in a GC-allocated Bindings::NodeWrapper object. The only property we expose right now is the very simple nodeName property. When a Document's JS::Interpreter is first instantiated, we add a "document" property with a DocumentWrapper object to the global object. This is pretty cool! :^)
Diffstat (limited to 'Libraries/LibWeb/Bindings/Wrappable.h')
-rw-r--r--Libraries/LibWeb/Bindings/Wrappable.h31
1 files changed, 31 insertions, 0 deletions
diff --git a/Libraries/LibWeb/Bindings/Wrappable.h b/Libraries/LibWeb/Bindings/Wrappable.h
new file mode 100644
index 0000000000..4531f63935
--- /dev/null
+++ b/Libraries/LibWeb/Bindings/Wrappable.h
@@ -0,0 +1,31 @@
+#pragma once
+
+#include <AK/WeakPtr.h>
+#include <LibJS/Heap.h>
+#include <LibWeb/Forward.h>
+
+namespace Web {
+namespace Bindings {
+
+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>
+inline Wrapper* wrap(JS::Heap& heap, NativeObject& native_object)
+{
+ if (!native_object.wrapper())
+ native_object.set_wrapper(*heap.allocate<typename NativeObject::WrapperType>(native_object));
+ return native_object.wrapper();
+}
+
+}
+}