@@ -52,14 +52,20 @@ class Caches {
5252 static std::shared_ptr<ConcurrentMap<std::string, const Meta*>> Metadata;
5353 static std::shared_ptr<ConcurrentMap<int , std::shared_ptr<Caches::WorkerState>>> Workers;
5454
55+ inline static std::shared_ptr<Caches> Init (v8::Isolate* isolate) {
56+ auto cache = std::make_shared<Caches>(isolate);
57+ // create a new shared_ptr that will live until Remove is called
58+ isolate->SetData (0 , static_cast <void *>(new std::shared_ptr<Caches>(cache)));
59+ return cache;
60+ }
5561 inline static std::shared_ptr<Caches> Get (v8::Isolate* isolate) {
56- std::shared_ptr<Caches> cache = perIsolateCaches_->Get (isolate);
57- if (cache == nullptr ) {
58- cache = std::make_shared<Caches>(isolate);
59- Caches::perIsolateCaches_->Insert (isolate, cache);
62+ auto cache = isolate->GetData (0 );
63+ if (cache != nullptr ) {
64+ return *reinterpret_cast <std::shared_ptr<Caches>*>(cache);
6065 }
61-
62- return cache;
66+ // this should only happen when an isolate is accessed after disposal
67+ // so we return a dummy cache
68+ return std::make_shared<Caches>(isolate);
6369 }
6470 static void Remove (v8::Isolate* isolate);
6571
@@ -96,7 +102,6 @@ class Caches {
96102 std::unique_ptr<v8::Persistent<v8::Function>> FunctionReferenceCtorFunc = std::unique_ptr<v8::Persistent<v8::Function>>(nullptr );
97103 std::unique_ptr<v8::Persistent<v8::Function>> UnmanagedTypeCtorFunc = std::unique_ptr<v8::Persistent<v8::Function>>(nullptr );
98104private:
99- static std::shared_ptr<ConcurrentMap<v8::Isolate*, std::shared_ptr<Caches>>> perIsolateCaches_;
100105 v8::Isolate* isolate_;
101106 std::shared_ptr<v8::Persistent<v8::Context>> context_;
102107};
0 commit comments