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: address comments Created March 24, 2017, 12:28 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
« no previous file with comments | « src/GlobalJsObject.cpp ('k') | test/GlobalJsObject.cpp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/JsEngine.cpp
diff --git a/src/JsEngine.cpp b/src/JsEngine.cpp
index f874cf97c7486193aa51c1ee55a6b8c45ebf1adf..4bd43551197bd3cdfaa4b9e613e6dff453b3df8e 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 timerTaskInfoIterator = timerTaskInfos.emplace(timerTaskInfos.end());
+ timerTaskInfoIterator->delay = arguments[1]->IntegerValue();
+
+ for (int i = 0; i < arguments.Length(); i++)
+ timerTaskInfoIterator->arguments.emplace_back(new v8::Persistent<v8::Value>(GetIsolate(), arguments[i]));
+ TimerTask retValue = { shared_from_this(), timerTaskInfoIterator };
+ return retValue;
+}
+
+void JsEngine::CallTimerTask(TimerTaskInfos::const_iterator timerTaskInfoIterator)
+{
+ const JsContext context(shared_from_this());
+ JsValue callback(shared_from_this(), v8::Local<v8::Value>::New(GetIsolate(), *timerTaskInfoIterator->arguments[0]));
+ JsValueList callbackArgs;
+ for (int i = 2; i < timerTaskInfoIterator->arguments.size(); i++)
+ callbackArgs.emplace_back(new JsValue(shared_from_this(),
+ v8::Local<v8::Value>::New(GetIsolate(), *timerTaskInfoIterator->arguments[i])));
+ callback.Call(callbackArgs);
+ timerTaskInfos.erase(timerTaskInfoIterator);
+}
+
AdblockPlus::JsEngine::JsEngine(const ScopedV8IsolatePtr& isolate)
: isolate(isolate)
{
« no previous file with comments | « src/GlobalJsObject.cpp ('k') | test/GlobalJsObject.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld