| OLD | NEW |
| 1 /* | 1 /* |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
| 3 * Copyright (C) 2006-2017 eyeo GmbH | 3 * Copyright (C) 2006-2017 eyeo GmbH |
| 4 * | 4 * |
| 5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
| 6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
| 7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
| 8 * | 8 * |
| 9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 V8Initializer::Init(); | 77 V8Initializer::Init(); |
| 78 isolate = v8::Isolate::New(); | 78 isolate = v8::Isolate::New(); |
| 79 } | 79 } |
| 80 | 80 |
| 81 AdblockPlus::ScopedV8Isolate::~ScopedV8Isolate() | 81 AdblockPlus::ScopedV8Isolate::~ScopedV8Isolate() |
| 82 { | 82 { |
| 83 isolate->Dispose(); | 83 isolate->Dispose(); |
| 84 isolate = nullptr; | 84 isolate = nullptr; |
| 85 } | 85 } |
| 86 | 86 |
| 87 JsEngine::TimerTask::~TimerTask() | 87 JsEngine::JsWeakValuesList::~JsWeakValuesList() |
| 88 { | 88 { |
| 89 for (auto& arg : arguments) | 89 for (auto& value : values) |
| 90 arg->Dispose(); | 90 value->Dispose(); |
| 91 } | 91 } |
| 92 | 92 |
| 93 void JsEngine::ScheduleTimer(const v8::Arguments& arguments) | 93 void JsEngine::ScheduleTimer(const v8::Arguments& arguments) |
| 94 { | 94 { |
| 95 auto jsEngine = FromArguments(arguments); | 95 auto jsEngine = FromArguments(arguments); |
| 96 if (arguments.Length() < 2) | 96 if (arguments.Length() < 2) |
| 97 throw std::runtime_error("setTimeout requires at least 2 parameters"); | 97 throw std::runtime_error("setTimeout requires at least 2 parameters"); |
| 98 | 98 |
| 99 if (!arguments[0]->IsFunction()) | 99 if (!arguments[0]->IsFunction()) |
| 100 throw std::runtime_error("First argument to setTimeout must be a function"); | 100 throw std::runtime_error("First argument to setTimeout must be a function"); |
| 101 | 101 |
| 102 auto timerTaskIterator = jsEngine->timerTasks.emplace(jsEngine->timerTasks.end
()); | 102 auto jsValueArguments = jsEngine->ConvertArguments(arguments); |
| 103 | 103 auto timerParamsID = jsEngine->StoreJsValues(jsValueArguments); |
| 104 for (int i = 0; i < arguments.Length(); i++) | |
| 105 timerTaskIterator->arguments.emplace_back(new v8::Persistent<v8::Value>(jsEn
gine->GetIsolate(), arguments[i])); | |
| 106 | 104 |
| 107 std::weak_ptr<JsEngine> weakJsEngine = jsEngine; | 105 std::weak_ptr<JsEngine> weakJsEngine = jsEngine; |
| 108 jsEngine->timer->SetTimer(std::chrono::milliseconds(arguments[1]->IntegerValue
()), [weakJsEngine, timerTaskIterator] | 106 jsEngine->timer->SetTimer(std::chrono::milliseconds(arguments[1]->IntegerValue
()), [weakJsEngine, timerParamsID] |
| 109 { | 107 { |
| 110 if (auto jsEngine = weakJsEngine.lock()) | 108 if (auto jsEngine = weakJsEngine.lock()) |
| 111 jsEngine->CallTimerTask(timerTaskIterator); | 109 jsEngine->CallTimerTask(timerParamsID); |
| 112 }); | 110 }); |
| 113 } | 111 } |
| 114 | 112 |
| 115 void JsEngine::CallTimerTask(const TimerTasks::const_iterator& timerTaskIterator
) | 113 void JsEngine::CallTimerTask(const JsWeakValuesID& timerParamsID) |
| 116 { | 114 { |
| 117 const JsContext context(*this); | 115 auto timerParams = TakeJsValues(timerParamsID); |
| 118 JsValue callback(shared_from_this(), v8::Local<v8::Value>::New(GetIsolate(), *
timerTaskIterator->arguments[0])); | 116 JsValue callback = std::move(timerParams[0]); |
| 119 JsValueList callbackArgs; | 117 |
| 120 for (int i = 2; i < timerTaskIterator->arguments.size(); i++) | 118 timerParams.erase(timerParams.begin()); // remove callback placeholder |
| 121 callbackArgs.emplace_back(JsValue(shared_from_this(), | 119 timerParams.erase(timerParams.begin()); // remove timeout param |
| 122 v8::Local<v8::Value>::New(GetIsolate(), *timerTaskIterator->arguments[i]))
); | 120 callback.Call(timerParams); |
| 123 callback.Call(callbackArgs); | |
| 124 timerTasks.erase(timerTaskIterator); | |
| 125 } | 121 } |
| 126 | 122 |
| 127 AdblockPlus::JsEngine::JsEngine(const ScopedV8IsolatePtr& isolate, TimerPtr time
r) | 123 AdblockPlus::JsEngine::JsEngine(const ScopedV8IsolatePtr& isolate, TimerPtr time
r) |
| 128 : isolate(isolate) | 124 : isolate(isolate) |
| 129 , fileSystem(new DefaultFileSystem()) | 125 , fileSystem(new DefaultFileSystem()) |
| 130 , webRequest(new DefaultWebRequest()) | 126 , webRequest(new DefaultWebRequest()) |
| 131 , logSystem(new DefaultLogSystem()) | 127 , logSystem(new DefaultLogSystem()) |
| 132 , timer(std::move(timer)) | 128 , timer(std::move(timer)) |
| 133 { | 129 { |
| 134 } | 130 } |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 248 const v8::Local<const v8::External> external = | 244 const v8::Local<const v8::External> external = |
| 249 v8::Local<const v8::External>::Cast(arguments.Data()); | 245 v8::Local<const v8::External>::Cast(arguments.Data()); |
| 250 std::weak_ptr<JsEngine>* data = | 246 std::weak_ptr<JsEngine>* data = |
| 251 static_cast<std::weak_ptr<JsEngine>*>(external->Value()); | 247 static_cast<std::weak_ptr<JsEngine>*>(external->Value()); |
| 252 JsEnginePtr result = data->lock(); | 248 JsEnginePtr result = data->lock(); |
| 253 if (!result) | 249 if (!result) |
| 254 throw std::runtime_error("Oops, our JsEngine is gone, how did that happen?")
; | 250 throw std::runtime_error("Oops, our JsEngine is gone, how did that happen?")
; |
| 255 return result; | 251 return result; |
| 256 } | 252 } |
| 257 | 253 |
| 254 JsEngine::JsWeakValuesID JsEngine::StoreJsValues(const JsValueList& values) |
| 255 { |
| 256 JsWeakValuesLists::iterator it; |
| 257 { |
| 258 std::lock_guard<std::mutex> lock(jsWeakValuesListsMutex); |
| 259 it = jsWeakValuesLists.emplace(jsWeakValuesLists.end()); |
| 260 } |
| 261 { |
| 262 JsContext context(*this); |
| 263 for (const auto& value : values) |
| 264 { |
| 265 it->values.emplace_back(new v8::Persistent<v8::Value>(GetIsolate(), value.
UnwrapValue())); |
| 266 } |
| 267 } |
| 268 JsWeakValuesID retValue; |
| 269 retValue.iterator = it; |
| 270 return retValue; |
| 271 } |
| 272 |
| 273 JsValueList JsEngine::TakeJsValues(const JsWeakValuesID& id) |
| 274 { |
| 275 JsValueList retValue; |
| 276 { |
| 277 JsContext context(*this); |
| 278 for (const auto& v8Value : id.iterator->values) |
| 279 { |
| 280 retValue.emplace_back(JsValue(shared_from_this(), v8::Local<v8::Value>::Ne
w(GetIsolate(), *v8Value))); |
| 281 } |
| 282 } |
| 283 { |
| 284 std::lock_guard<std::mutex> lock(jsWeakValuesListsMutex); |
| 285 jsWeakValuesLists.erase(id.iterator); |
| 286 } |
| 287 return retValue; |
| 288 } |
| 289 |
| 258 AdblockPlus::JsValueList AdblockPlus::JsEngine::ConvertArguments(const v8::Argum
ents& arguments) | 290 AdblockPlus::JsValueList AdblockPlus::JsEngine::ConvertArguments(const v8::Argum
ents& arguments) |
| 259 { | 291 { |
| 260 const JsContext context(*this); | 292 const JsContext context(*this); |
| 261 JsValueList list; | 293 JsValueList list; |
| 262 for (int i = 0; i < arguments.Length(); i++) | 294 for (int i = 0; i < arguments.Length(); i++) |
| 263 list.push_back(JsValue(shared_from_this(), arguments[i])); | 295 list.push_back(JsValue(shared_from_this(), arguments[i])); |
| 264 return list; | 296 return list; |
| 265 } | 297 } |
| 266 | 298 |
| 267 AdblockPlus::FileSystemPtr AdblockPlus::JsEngine::GetFileSystem() const | 299 AdblockPlus::FileSystemPtr AdblockPlus::JsEngine::GetFileSystem() const |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 323 logSystem = val; | 355 logSystem = val; |
| 324 } | 356 } |
| 325 | 357 |
| 326 | 358 |
| 327 void AdblockPlus::JsEngine::SetGlobalProperty(const std::string& name, | 359 void AdblockPlus::JsEngine::SetGlobalProperty(const std::string& name, |
| 328 const AdblockPlus::JsValue& value) | 360 const AdblockPlus::JsValue& value) |
| 329 { | 361 { |
| 330 auto global = GetGlobalObject(); | 362 auto global = GetGlobalObject(); |
| 331 global.SetProperty(name, value); | 363 global.SetProperty(name, value); |
| 332 } | 364 } |
| OLD | NEW |