diff --git a/include/AdblockPlus/JsEngine.h b/include/AdblockPlus/JsEngine.h index 833e0d32..b401231f 100644 --- a/include/AdblockPlus/JsEngine.h +++ b/include/AdblockPlus/JsEngine.h @@ -283,6 +283,11 @@ namespace AdblockPlus JsValue GetGlobalObject(); + JsEnginePtr GetSharedPtr() + { + return shared_from_this(); + } + Platform& platform; /// Isolate must be disposed only after disposing of all objects which are /// using it. diff --git a/src/JsEngine.cpp b/src/JsEngine.cpp index 7e434f7c..743cc1d4 100644 --- a/src/JsEngine.cpp +++ b/src/JsEngine.cpp @@ -82,8 +82,9 @@ namespace ScopedV8Isolate() { V8Initializer::Init(); + allocator.reset(v8::ArrayBuffer::Allocator::NewDefaultAllocator()); v8::Isolate::CreateParams isolateParams; - isolateParams.array_buffer_allocator = v8::ArrayBuffer::Allocator::NewDefaultAllocator(); + isolateParams.array_buffer_allocator = allocator.get(); isolate = v8::Isolate::New(isolateParams); } @@ -101,6 +102,7 @@ namespace ScopedV8Isolate(const ScopedV8Isolate&); ScopedV8Isolate& operator=(const ScopedV8Isolate&); + std::unique_ptr allocator; v8::Isolate* isolate; }; } @@ -268,12 +270,10 @@ AdblockPlus::JsValue AdblockPlus::JsEngine::NewCallback( { const JsContext context(*this); auto isolate = GetIsolate(); - // Note: we are leaking this weak pointer, no obvious way to destroy it when - // it's no longer used - std::weak_ptr* data = - new std::weak_ptr(shared_from_this()); + // The callback may not outlive us since it lives out of our isolate. + // It's safe to bind a bare pointer to self. v8::Local templ = v8::FunctionTemplate::New(isolate, callback, - v8::External::New(isolate, data)); + v8::External::New(isolate, this)); return JsValue(shared_from_this(), CHECKED_TO_LOCAL(isolate, templ->GetFunction(isolate->GetCurrentContext()))); } @@ -282,12 +282,8 @@ AdblockPlus::JsEnginePtr AdblockPlus::JsEngine::FromArguments(const v8::Function { const v8::Local external = v8::Local::Cast(arguments.Data()); - std::weak_ptr* data = - static_cast*>(external->Value()); - JsEnginePtr result = data->lock(); - if (!result) - throw std::runtime_error("Oops, our JsEngine is gone, how did that happen?"); - return result; + JsEngine* engine = static_cast(external->Value()); + return engine->GetSharedPtr(); } JsEngine::JsWeakValuesID JsEngine::StoreJsValues(const JsValueList& values)