Skip to content

Commit

Permalink
refactor: put private data logic into its own cc file
Browse files Browse the repository at this point in the history
Signed-off-by: Tony Gorez <gorez.tony@gmail.com>
  • Loading branch information
tony-go committed Mar 8, 2024
1 parent df71304 commit 8d6cfb6
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 19 deletions.
1 change: 1 addition & 0 deletions src/engine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
25 changes: 6 additions & 19 deletions src/engine/include/includejs/engine_private_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,20 @@ namespace includejs {
// Inspired by https://github.com/sourcemeta/jsontoolkit
/// @ingroup engine
struct INCLUDEJS_ENGINE_EXPORT PrivateObjectData {
std::unique_ptr<std::map<void *, Value::Function>> functions;
std::unique_ptr<std::map<void *, Value::Function>> functions =
std::make_unique<std::map<void *, Value::Function>>();

PrivateObjectData() {
functions = std::make_unique<std::map<void *, Value::Function>>();
}

auto data() -> void * { return data_; }
~PrivateObjectData();

auto data() -> void *;
auto set_data(void *new_data, std::function<void(void *)> new_deleter)
-> void {
clear();
data_ = new_data;
deleter_ = new_deleter;
}

~PrivateObjectData() { clear(); }
-> void;

private:
void *data_ = nullptr;
std::function<void(void *)> deleter_;

auto clear() -> void {
if (data_ != nullptr && deleter_ != nullptr) {
deleter_(data_);
data_ = nullptr;
}
}
auto clear() -> void;
};

} // namespace includejs
Expand Down
24 changes: 24 additions & 0 deletions src/engine/javascript_core/engine_private_data.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <includejs/engine_private_data.h>

namespace includejs {

PrivateObjectData::~PrivateObjectData() { clear(); }

auto PrivateObjectData::data() -> void * { return data_; }

auto PrivateObjectData::set_data(void *new_data,
std::function<void(void *)> 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

0 comments on commit 8d6cfb6

Please sign in to comment.