diff --git a/native_mate/wrappable.h b/native_mate/wrappable.h index 29c2844fbfe6..561f9d2a9888 100644 --- a/native_mate/wrappable.h +++ b/native_mate/wrappable.h @@ -6,6 +6,8 @@ #define NATIVE_MATE_WRAPPABLE_H_ #include "base/bind.h" +#include "base/lazy_instance.h" +#include "base/threading/thread_local.h" #include "native_mate/compat.h" #include "native_mate/converter.h" #include "native_mate/constructor.h" @@ -30,18 +32,18 @@ class Wrappable : public WrappableBase { isolate, base::Bind(&internal::InvokeNew, constructor)); templ->InstanceTemplate()->SetInternalFieldCount(1); T::BuildPrototype(isolate, templ); - templ_ = new v8::Global(isolate, templ); + templ_.Get().Set(new v8::Global(isolate, templ)); } static v8::Local GetConstructor(v8::Isolate* isolate) { // Fill the object template. - if (!templ_) { + if (!templ_.Get().Get()) { v8::Local templ = v8::FunctionTemplate::New(isolate); templ->InstanceTemplate()->SetInternalFieldCount(1); T::BuildPrototype(isolate, templ); - templ_ = new v8::Global(isolate, templ); + templ_.Get().Set(new v8::Global(isolate, templ)); } - return v8::Local::New(isolate, *templ_); + return v8::Local::New(isolate, *templ_.Get().Get()); } protected: @@ -63,14 +65,16 @@ class Wrappable : public WrappableBase { } private: - static v8::Global* templ_; // Leaked on purpose + static base::LazyInstance>> templ_; DISALLOW_COPY_AND_ASSIGN(Wrappable); }; // static template -v8::Global* Wrappable::templ_ = nullptr; +base::LazyInstance>> +Wrappable::templ_ = LAZY_INSTANCE_INITIALIZER; // This converter handles any subclass of Wrappable. template