@@ -52,14 +52,20 @@ class Caches {
52
52
static std::shared_ptr<ConcurrentMap<std::string, const Meta*>> Metadata;
53
53
static std::shared_ptr<ConcurrentMap<int , std::shared_ptr<Caches::WorkerState>>> Workers;
54
54
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
+ }
55
61
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);
60
65
}
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);
63
69
}
64
70
static void Remove (v8::Isolate* isolate);
65
71
@@ -96,7 +102,6 @@ class Caches {
96
102
std::unique_ptr<v8::Persistent<v8::Function>> FunctionReferenceCtorFunc = std::unique_ptr<v8::Persistent<v8::Function>>(nullptr );
97
103
std::unique_ptr<v8::Persistent<v8::Function>> UnmanagedTypeCtorFunc = std::unique_ptr<v8::Persistent<v8::Function>>(nullptr );
98
104
private:
99
- static std::shared_ptr<ConcurrentMap<v8::Isolate*, std::shared_ptr<Caches>>> perIsolateCaches_;
100
105
v8::Isolate* isolate_;
101
106
std::shared_ptr<v8::Persistent<v8::Context>> context_;
102
107
};
0 commit comments