#ifndef Caches_h #define Caches_h #include #include #include "ConcurrentMap.h" #include "robin_hood.h" #include "Common.h" #include "Metadata.h" namespace tns { struct StructInfo; struct pair_hash { template std::size_t operator() (const std::pair &pair) const { return std::hash()(pair.first) ^ std::hash()(pair.second); } }; class Caches { public: class WorkerState { public: WorkerState(v8::Isolate* isolate, std::shared_ptr> poWorker, void* userData) : isolate_(isolate), poWorker_(poWorker), userData_(userData) { } v8::Isolate* GetIsolate() { return this->isolate_; } std::shared_ptr> GetWorker() { return this->poWorker_; } void* UserData() { return this->userData_; } private: v8::Isolate* isolate_; std::shared_ptr> poWorker_; void* userData_; }; Caches(v8::Isolate* isolate, const int& isolateId_ = -1); ~Caches(); bool isWorker = false; static std::shared_ptr> Metadata; static std::shared_ptr>> Workers; inline static std::shared_ptr Init(v8::Isolate* isolate, const int& isolateId) { auto cache = std::make_shared(isolate, isolateId); // create a new shared_ptr that will live until Remove is called isolate->SetData(0, static_cast(new std::shared_ptr(cache))); return cache; } inline static std::shared_ptr Get(v8::Isolate* isolate) { auto cache = isolate->GetData(0); if (cache != nullptr) { return *reinterpret_cast*>(cache); } // this should only happen when an isolate is accessed after disposal // so we return a dummy cache return std::make_shared(isolate); } static void Remove(v8::Isolate* isolate); inline int getIsolateId() { return isolateId_; } inline void InvalidateIsolate() { isolateId_ = -1; } inline bool IsValid() { return isolateId_ != -1; } void SetContext(v8::Local context); v8::Local GetContext(); robin_hood::unordered_map>> Prototypes; robin_hood::unordered_map>> ClassPrototypes; robin_hood::unordered_map>> CtorFuncTemplates; robin_hood::unordered_map>> CtorFuncs; robin_hood::unordered_map>> ProtocolCtorFuncs; robin_hood::unordered_map>> StructConstructorFunctions; robin_hood::unordered_map>> PrimitiveInteropTypes; robin_hood::unordered_map>> CFunctions; robin_hood::unordered_map>> Instances; robin_hood::unordered_map, std::shared_ptr>, pair_hash> StructInstances; robin_hood::unordered_map>> PointerInstances; std::function(v8::Local, const BaseClassMeta*, KnownUnknownClassPair, const std::vector&)> ObjectCtorInitializer; std::function(v8::Local, StructInfo)> StructCtorInitializer; robin_hood::unordered_map Timers; robin_hood::unordered_map> Initializers; std::unique_ptr> EmptyObjCtorFunc = std::unique_ptr>(nullptr); std::unique_ptr> EmptyStructCtorFunc = std::unique_ptr>(nullptr); std::unique_ptr> SliceFunc = std::unique_ptr>(nullptr); std::unique_ptr> OriginalExtendsFunc = std::unique_ptr>(nullptr); std::unique_ptr> WeakRefGetterFunc = std::unique_ptr>(nullptr); std::unique_ptr> WeakRefClearFunc = std::unique_ptr>(nullptr); std::unique_ptr> SmartJSONStringifyFunc = std::unique_ptr>(nullptr); std::unique_ptr> InteropReferenceCtorFunc = std::unique_ptr>(nullptr); std::unique_ptr> PointerCtorFunc = std::unique_ptr>(nullptr); std::unique_ptr> FunctionReferenceCtorFunc = std::unique_ptr>(nullptr); std::unique_ptr> UnmanagedTypeCtorFunc = std::unique_ptr>(nullptr); using unique_void_ptr = std::unique_ptr; template auto unique_void(T * ptr) -> unique_void_ptr { return unique_void_ptr(ptr, [](void const * data) { T const * p = static_cast(data); delete p; }); } std::vector cacheBoundObjects_; template void registerCacheBoundObject(T *ptr) { this->cacheBoundObjects_.push_back(unique_void(ptr)); } private: v8::Isolate* isolate_; std::shared_ptr> context_; int isolateId_; }; } #endif /* Caches_h */