Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: src/JsEngine.cpp

Issue 29393573: Issue 4692 - Dont' keep a strong reference from timer thread (Closed)
Patch Set: Created March 23, 2017, 5:24 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: src/JsEngine.cpp
diff --git a/src/JsEngine.cpp b/src/JsEngine.cpp
index f874cf97c7486193aa51c1ee55a6b8c45ebf1adf..7bf1fc52ea9d5772111224ba406f0300c93430fd 100644
--- a/src/JsEngine.cpp
+++ b/src/JsEngine.cpp
@@ -64,6 +64,8 @@ namespace
};
}
+using namespace AdblockPlus;
+
AdblockPlus::ScopedV8Isolate::ScopedV8Isolate()
{
V8Initializer::Init();
@@ -76,6 +78,41 @@ AdblockPlus::ScopedV8Isolate::~ScopedV8Isolate()
isolate = nullptr;
}
+JsEngine::TimerTaskInfo::~TimerTaskInfo()
+{
+ for (auto& arg : arguments)
+ arg->Dispose();
+}
+
+JsEngine::TimerTask JsEngine::CreateTimerTask(const v8::Arguments& arguments)
+{
+ if (arguments.Length() < 2)
+ throw std::runtime_error("setTimeout requires at least 2 parameters");
+
+ if (!arguments[0]->IsFunction())
+ throw std::runtime_error("First argument to setTimeout must be a function");
+
+ auto ii_timerTaskInfo = timerTaskInfos.emplace(timerTaskInfos.end());
+ ii_timerTaskInfo->delay = arguments[1]->IntegerValue();
+
+ for (int i = 0; i < arguments.Length(); i++)
+ ii_timerTaskInfo->arguments.emplace_back(new v8::Persistent<v8::Value>(GetIsolate(), arguments[i]));
+ TimerTask retValue = { shared_from_this(), ii_timerTaskInfo };
+ return retValue;
+}
+
+void JsEngine::CallTimerTask(TimerTaskInfos::const_iterator ii_taskInfo)
+{
+ const JsContext context(shared_from_this());
+ JsValue callback(shared_from_this(), v8::Local<v8::Value>::New(GetIsolate(), *ii_taskInfo->arguments[0]));
+ JsValueList callbackArgs;
+ for (int i = 2; i < ii_taskInfo->arguments.size(); i++)
+ callbackArgs.emplace_back(new JsValue(shared_from_this(),
+ v8::Local<v8::Value>::New(GetIsolate(), *ii_taskInfo->arguments[i])));
+ callback.Call(callbackArgs);
+ timerTaskInfos.erase(ii_taskInfo);
+}
+
AdblockPlus::JsEngine::JsEngine(const ScopedV8IsolatePtr& isolate)
: isolate(isolate)
{
« src/GlobalJsObject.cpp ('K') | « src/GlobalJsObject.cpp ('k') | test/GlobalJsObject.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld