From 8d6cfb653473eb7952ba6451d6315a4f80be0ea0 Mon Sep 17 00:00:00 2001 From: Tony Gorez Date: Fri, 8 Mar 2024 10:24:13 +0100 Subject: [PATCH] refactor: put private data logic into its own cc file Signed-off-by: Tony Gorez --- src/engine/CMakeLists.txt | 1 + .../include/includejs/engine_private_data.h | 25 +++++-------------- .../javascript_core/engine_private_data.cc | 24 ++++++++++++++++++ 3 files changed, 31 insertions(+), 19 deletions(-) create mode 100644 src/engine/javascript_core/engine_private_data.cc diff --git a/src/engine/CMakeLists.txt b/src/engine/CMakeLists.txt index 39c48b0..cfc5c6d 100644 --- a/src/engine/CMakeLists.txt +++ b/src/engine/CMakeLists.txt @@ -8,6 +8,7 @@ if(INCLUDEJS_BACKEND STREQUAL "JavaScriptCore") list(APPEND INCLUDEJS_ENGINE_SOURCES javascript_core/engine_context.cc) list(APPEND INCLUDEJS_ENGINE_SOURCES javascript_core/engine_promise.cc) list(APPEND INCLUDEJS_ENGINE_SOURCES javascript_core/engine_value.cc) + list(APPEND INCLUDEJS_ENGINE_SOURCES javascript_core/engine_private_data.cc) elseif(INCLUDEJS_BACKEND STREQUAL "V8") list(APPEND INCLUDEJS_ENGINE_SOURCES v8/engine.cc) else() diff --git a/src/engine/include/includejs/engine_private_data.h b/src/engine/include/includejs/engine_private_data.h index ad755d1..5539218 100644 --- a/src/engine/include/includejs/engine_private_data.h +++ b/src/engine/include/includejs/engine_private_data.h @@ -12,33 +12,20 @@ namespace includejs { // Inspired by https://github.com/sourcemeta/jsontoolkit /// @ingroup engine struct INCLUDEJS_ENGINE_EXPORT PrivateObjectData { - std::unique_ptr> functions; + std::unique_ptr> functions = + std::make_unique>(); - PrivateObjectData() { - functions = std::make_unique>(); - } - - auto data() -> void * { return data_; } + ~PrivateObjectData(); + auto data() -> void *; auto set_data(void *new_data, std::function new_deleter) - -> void { - clear(); - data_ = new_data; - deleter_ = new_deleter; - } - - ~PrivateObjectData() { clear(); } + -> void; private: void *data_ = nullptr; std::function deleter_; - auto clear() -> void { - if (data_ != nullptr && deleter_ != nullptr) { - deleter_(data_); - data_ = nullptr; - } - } + auto clear() -> void; }; } // namespace includejs diff --git a/src/engine/javascript_core/engine_private_data.cc b/src/engine/javascript_core/engine_private_data.cc new file mode 100644 index 0000000..5959690 --- /dev/null +++ b/src/engine/javascript_core/engine_private_data.cc @@ -0,0 +1,24 @@ +#include + +namespace includejs { + +PrivateObjectData::~PrivateObjectData() { clear(); } + +auto PrivateObjectData::data() -> void * { return data_; } + +auto PrivateObjectData::set_data(void *new_data, + std::function new_deleter) + -> void { + clear(); + data_ = new_data; + deleter_ = new_deleter; +} + +auto PrivateObjectData::clear() -> void { + if (data_ != nullptr && deleter_ != nullptr) { + deleter_(data_); + data_ = nullptr; + } +} + +} // namespace includejs