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 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 std::weak_ptr<JsEngine> weakJsEngine = jsEngine; | 107 std::weak_ptr<JsEngine> weakJsEngine = jsEngine; |
108 jsEngine->timer->SetTimer(std::chrono::milliseconds(arguments[1]->IntegerValue
()), [weakJsEngine, timerTaskIterator] | 108 jsEngine->timer->SetTimer(std::chrono::milliseconds(arguments[1]->IntegerValue
()), [weakJsEngine, timerTaskIterator] |
109 { | 109 { |
110 if (auto jsEngine = weakJsEngine.lock()) | 110 if (auto jsEngine = weakJsEngine.lock()) |
111 jsEngine->CallTimerTask(timerTaskIterator); | 111 jsEngine->CallTimerTask(timerTaskIterator); |
112 }); | 112 }); |
113 } | 113 } |
114 | 114 |
115 void JsEngine::CallTimerTask(const TimerTasks::const_iterator& timerTaskIterator
) | 115 void JsEngine::CallTimerTask(const TimerTasks::const_iterator& timerTaskIterator
) |
116 { | 116 { |
117 const JsContext context(shared_from_this()); | 117 const JsContext context(*this); |
118 JsValue callback(shared_from_this(), v8::Local<v8::Value>::New(GetIsolate(), *
timerTaskIterator->arguments[0])); | 118 JsValue callback(shared_from_this(), v8::Local<v8::Value>::New(GetIsolate(), *
timerTaskIterator->arguments[0])); |
119 JsValueList callbackArgs; | 119 JsValueList callbackArgs; |
120 for (int i = 2; i < timerTaskIterator->arguments.size(); i++) | 120 for (int i = 2; i < timerTaskIterator->arguments.size(); i++) |
121 callbackArgs.emplace_back(JsValue(shared_from_this(), | 121 callbackArgs.emplace_back(JsValue(shared_from_this(), |
122 v8::Local<v8::Value>::New(GetIsolate(), *timerTaskIterator->arguments[i]))
); | 122 v8::Local<v8::Value>::New(GetIsolate(), *timerTaskIterator->arguments[i]))
); |
123 callback.Call(callbackArgs); | 123 callback.Call(callbackArgs); |
124 timerTasks.erase(timerTaskIterator); | 124 timerTasks.erase(timerTaskIterator); |
125 } | 125 } |
126 | 126 |
127 AdblockPlus::JsEngine::JsEngine(const ScopedV8IsolatePtr& isolate, TimerPtr time
r) | 127 AdblockPlus::JsEngine::JsEngine(const ScopedV8IsolatePtr& isolate, TimerPtr time
r) |
(...skipping 17 matching lines...) Expand all Loading... |
145 | 145 |
146 result->context.reset(new v8::Persistent<v8::Context>(result->GetIsolate(), | 146 result->context.reset(new v8::Persistent<v8::Context>(result->GetIsolate(), |
147 v8::Context::New(result->GetIsolate()))); | 147 v8::Context::New(result->GetIsolate()))); |
148 auto global = result->GetGlobalObject(); | 148 auto global = result->GetGlobalObject(); |
149 AdblockPlus::GlobalJsObject::Setup(*result, appInfo, global); | 149 AdblockPlus::GlobalJsObject::Setup(*result, appInfo, global); |
150 return result; | 150 return result; |
151 } | 151 } |
152 | 152 |
153 AdblockPlus::JsValue AdblockPlus::JsEngine::GetGlobalObject() | 153 AdblockPlus::JsValue AdblockPlus::JsEngine::GetGlobalObject() |
154 { | 154 { |
155 JsContext context(shared_from_this()); | 155 JsContext context(*this); |
156 return JsValue(shared_from_this(), context.GetV8Context()->Global()); | 156 return JsValue(shared_from_this(), context.GetV8Context()->Global()); |
157 } | 157 } |
158 | 158 |
159 AdblockPlus::JsValue AdblockPlus::JsEngine::Evaluate(const std::string& source, | 159 AdblockPlus::JsValue AdblockPlus::JsEngine::Evaluate(const std::string& source, |
160 const std::string& filename) | 160 const std::string& filename) |
161 { | 161 { |
162 const JsContext context(shared_from_this()); | 162 const JsContext context(*this); |
163 const v8::TryCatch tryCatch; | 163 const v8::TryCatch tryCatch; |
164 const v8::Handle<v8::Script> script = CompileScript(GetIsolate(), source, | 164 const v8::Handle<v8::Script> script = CompileScript(GetIsolate(), source, |
165 filename); | 165 filename); |
166 CheckTryCatch(tryCatch); | 166 CheckTryCatch(tryCatch); |
167 v8::Local<v8::Value> result = script->Run(); | 167 v8::Local<v8::Value> result = script->Run(); |
168 CheckTryCatch(tryCatch); | 168 CheckTryCatch(tryCatch); |
169 return JsValue(shared_from_this(), result); | 169 return JsValue(shared_from_this(), result); |
170 } | 170 } |
171 | 171 |
172 void AdblockPlus::JsEngine::SetEventCallback(const std::string& eventName, | 172 void AdblockPlus::JsEngine::SetEventCallback(const std::string& eventName, |
(...skipping 27 matching lines...) Expand all Loading... |
200 callback(params); | 200 callback(params); |
201 } | 201 } |
202 | 202 |
203 void AdblockPlus::JsEngine::Gc() | 203 void AdblockPlus::JsEngine::Gc() |
204 { | 204 { |
205 while (!v8::V8::IdleNotification()); | 205 while (!v8::V8::IdleNotification()); |
206 } | 206 } |
207 | 207 |
208 AdblockPlus::JsValue AdblockPlus::JsEngine::NewValue(const std::string& val) | 208 AdblockPlus::JsValue AdblockPlus::JsEngine::NewValue(const std::string& val) |
209 { | 209 { |
210 const JsContext context(shared_from_this()); | 210 const JsContext context(*this); |
211 return JsValue(shared_from_this(), Utils::ToV8String(GetIsolate(), val)); | 211 return JsValue(shared_from_this(), Utils::ToV8String(GetIsolate(), val)); |
212 } | 212 } |
213 | 213 |
214 AdblockPlus::JsValue AdblockPlus::JsEngine::NewValue(int64_t val) | 214 AdblockPlus::JsValue AdblockPlus::JsEngine::NewValue(int64_t val) |
215 { | 215 { |
216 const JsContext context(shared_from_this()); | 216 const JsContext context(*this); |
217 return JsValue(shared_from_this(), v8::Number::New(GetIsolate(), val)); | 217 return JsValue(shared_from_this(), v8::Number::New(GetIsolate(), val)); |
218 } | 218 } |
219 | 219 |
220 AdblockPlus::JsValue AdblockPlus::JsEngine::NewValue(bool val) | 220 AdblockPlus::JsValue AdblockPlus::JsEngine::NewValue(bool val) |
221 { | 221 { |
222 const JsContext context(shared_from_this()); | 222 const JsContext context(*this); |
223 return JsValue(shared_from_this(), v8::Boolean::New(val)); | 223 return JsValue(shared_from_this(), v8::Boolean::New(val)); |
224 } | 224 } |
225 | 225 |
226 AdblockPlus::JsValue AdblockPlus::JsEngine::NewObject() | 226 AdblockPlus::JsValue AdblockPlus::JsEngine::NewObject() |
227 { | 227 { |
228 const JsContext context(shared_from_this()); | 228 const JsContext context(*this); |
229 return JsValue(shared_from_this(), v8::Object::New()); | 229 return JsValue(shared_from_this(), v8::Object::New()); |
230 } | 230 } |
231 | 231 |
232 AdblockPlus::JsValue AdblockPlus::JsEngine::NewCallback( | 232 AdblockPlus::JsValue AdblockPlus::JsEngine::NewCallback( |
233 const v8::InvocationCallback& callback) | 233 const v8::InvocationCallback& callback) |
234 { | 234 { |
235 const JsContext context(shared_from_this()); | 235 const JsContext context(*this); |
236 | 236 |
237 // Note: we are leaking this weak pointer, no obvious way to destroy it when | 237 // Note: we are leaking this weak pointer, no obvious way to destroy it when |
238 // it's no longer used | 238 // it's no longer used |
239 std::weak_ptr<JsEngine>* data = | 239 std::weak_ptr<JsEngine>* data = |
240 new std::weak_ptr<JsEngine>(shared_from_this()); | 240 new std::weak_ptr<JsEngine>(shared_from_this()); |
241 v8::Local<v8::FunctionTemplate> templ = v8::FunctionTemplate::New(callback, | 241 v8::Local<v8::FunctionTemplate> templ = v8::FunctionTemplate::New(callback, |
242 v8::External::New(data)); | 242 v8::External::New(data)); |
243 return JsValue(shared_from_this(), templ->GetFunction()); | 243 return JsValue(shared_from_this(), templ->GetFunction()); |
244 } | 244 } |
245 | 245 |
246 AdblockPlus::JsEnginePtr AdblockPlus::JsEngine::FromArguments(const v8::Argument
s& arguments) | 246 AdblockPlus::JsEnginePtr AdblockPlus::JsEngine::FromArguments(const v8::Argument
s& arguments) |
247 { | 247 { |
248 const v8::Local<const v8::External> external = | 248 const v8::Local<const v8::External> external = |
249 v8::Local<const v8::External>::Cast(arguments.Data()); | 249 v8::Local<const v8::External>::Cast(arguments.Data()); |
250 std::weak_ptr<JsEngine>* data = | 250 std::weak_ptr<JsEngine>* data = |
251 static_cast<std::weak_ptr<JsEngine>*>(external->Value()); | 251 static_cast<std::weak_ptr<JsEngine>*>(external->Value()); |
252 JsEnginePtr result = data->lock(); | 252 JsEnginePtr result = data->lock(); |
253 if (!result) | 253 if (!result) |
254 throw std::runtime_error("Oops, our JsEngine is gone, how did that happen?")
; | 254 throw std::runtime_error("Oops, our JsEngine is gone, how did that happen?")
; |
255 return result; | 255 return result; |
256 } | 256 } |
257 | 257 |
258 AdblockPlus::JsValueList AdblockPlus::JsEngine::ConvertArguments(const v8::Argum
ents& arguments) | 258 AdblockPlus::JsValueList AdblockPlus::JsEngine::ConvertArguments(const v8::Argum
ents& arguments) |
259 { | 259 { |
260 const JsContext context(shared_from_this()); | 260 const JsContext context(*this); |
261 JsValueList list; | 261 JsValueList list; |
262 for (int i = 0; i < arguments.Length(); i++) | 262 for (int i = 0; i < arguments.Length(); i++) |
263 list.push_back(JsValue(shared_from_this(), arguments[i])); | 263 list.push_back(JsValue(shared_from_this(), arguments[i])); |
264 return list; | 264 return list; |
265 } | 265 } |
266 | 266 |
267 AdblockPlus::FileSystemPtr AdblockPlus::JsEngine::GetFileSystem() const | 267 AdblockPlus::FileSystemPtr AdblockPlus::JsEngine::GetFileSystem() const |
268 { | 268 { |
269 return fileSystem; | 269 return fileSystem; |
270 } | 270 } |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
323 logSystem = val; | 323 logSystem = val; |
324 } | 324 } |
325 | 325 |
326 | 326 |
327 void AdblockPlus::JsEngine::SetGlobalProperty(const std::string& name, | 327 void AdblockPlus::JsEngine::SetGlobalProperty(const std::string& name, |
328 const AdblockPlus::JsValue& value) | 328 const AdblockPlus::JsValue& value) |
329 { | 329 { |
330 auto global = GetGlobalObject(); | 330 auto global = GetGlobalObject(); |
331 global.SetProperty(name, value); | 331 global.SetProperty(name, value); |
332 } | 332 } |
OLD | NEW |