From b76456f0ed8b9dba63ec2746d8f5f244b3db4e30 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Wed, 8 Sep 2021 22:58:36 +0200 Subject: [PATCH] LibJS: Add a way to attach custom data to a JS::VM instance This will be used by LibWeb to attach web engine specific stuff that LibJS doesn't need to know about. --- Userland/Libraries/LibJS/Runtime/VM.cpp | 11 ++++++++--- Userland/Libraries/LibJS/Runtime/VM.h | 12 ++++++++++-- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/Userland/Libraries/LibJS/Runtime/VM.cpp b/Userland/Libraries/LibJS/Runtime/VM.cpp index a0e0acceb8..1f4eb9b1bb 100644 --- a/Userland/Libraries/LibJS/Runtime/VM.cpp +++ b/Userland/Libraries/LibJS/Runtime/VM.cpp @@ -28,13 +28,14 @@ namespace JS { -NonnullRefPtr VM::create() +NonnullRefPtr VM::create(OwnPtr custom_data) { - return adopt_ref(*new VM); + return adopt_ref(*new VM(move(custom_data))); } -VM::VM() +VM::VM(OwnPtr custom_data) : m_heap(*this) + , m_custom_data(move(custom_data)) { m_empty_string = m_heap.allocate_without_global_object(String::empty()); for (size_t i = 0; i < 128; ++i) { @@ -773,4 +774,8 @@ void VM::dump_environment_chain() const } } +VM::CustomData::~CustomData() +{ +} + } diff --git a/Userland/Libraries/LibJS/Runtime/VM.h b/Userland/Libraries/LibJS/Runtime/VM.h index 5f8c4e1a72..c3ea351d9a 100644 --- a/Userland/Libraries/LibJS/Runtime/VM.h +++ b/Userland/Libraries/LibJS/Runtime/VM.h @@ -61,7 +61,11 @@ struct ExecutionContext { class VM : public RefCounted { public: - static NonnullRefPtr create(); + struct CustomData { + virtual ~CustomData(); + }; + + static NonnullRefPtr create(OwnPtr = {}); ~VM(); Heap& heap() { return m_heap; } @@ -272,8 +276,10 @@ public: void initialize_instance_elements(Object& object, FunctionObject& constructor); + CustomData* custom_data() { return m_custom_data; } + private: - VM(); + explicit VM(OwnPtr); void ordinary_call_bind_this(FunctionObject&, ExecutionContext&, Value this_argument); @@ -310,6 +316,8 @@ private: bool m_underscore_is_last_value { false }; u32 m_execution_generation { 0 }; + + OwnPtr m_custom_data; }; template<>