#ifndef V8SCRIPTWRAPPED_H #define V8SCRIPTWRAPPED_H #include #include #include "scripting/interface/ScriptBindings.h" #include "scripting/v8/V8ScriptUtils.h" template class V8ScriptObject : public IScriptObject { public: V8ScriptObject(T* object, v8::Isolate* isolate, v8::Local handle) : IScriptObject(object), m_isolate(isolate) { m_handle.Reset(isolate, handle); } ~V8ScriptObject() { // clear handle for children for (auto it = m_children.begin(); it != m_children.end(); ++it) { v8::Local child = globalPersistentToLocal(m_isolate, it->second); child->SetAlignedPointerInInternalField(0, nullptr); it->second.Reset(); } // clear handle v8::Local obj = handle(m_isolate); obj->SetAlignedPointerInInternalField(0, nullptr); m_handle.Reset(); } void addChild(const std::string& prop, v8::Local handle) { removeChild(prop); v8::Global persist_child; persist_child.Reset(m_isolate, handle); m_children[prop] = std::move(persist_child); } void removeChild(const std::string& prop) { auto it = m_children.find(prop); if (it != m_children.end()) { v8::Local child = globalPersistentToLocal(m_isolate, it->second); child->SetAlignedPointerInInternalField(0, nullptr); it->second.Reset(); m_children.erase(it); } } v8::Local handle(v8::Isolate* isolate) const { return persistentToLocal(isolate, m_handle); } v8::Persistent& persistent() { return m_handle; } // TODO(joey): This is not implemented just yet. Protect / Unprotect objects from being garbage collected. // May not be used because as of now there is no objects you can create in script so.. //inline void protect() { // m_object.ClearWeak(); //} //inline void unprotect() { // m_object.SetWeak(this, _V8WeakObjectCallback, v8::WeakCallbackType::kParameter); // m_object.MarkIndependent(); //} //inline static void v8WeakObjectCallback(const v8::WeakCallbackInfo& data) { //} private: v8::Isolate* m_isolate; v8::Persistent m_handle; std::unordered_map> m_children; }; #endif