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-2016 Eyeo GmbH | 3 * Copyright (C) 2006-2016 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 |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
13 * | 13 * |
14 * You should have received a copy of the GNU General Public License | 14 * You should have received a copy of the GNU General Public License |
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
16 */ | 16 */ |
17 | 17 |
18 #include <vector> | 18 #include <vector> |
19 #include <AdblockPlus.h> | 19 #include <AdblockPlus.h> |
20 | 20 |
21 #include "JsContext.h" | 21 #include "JsContext.h" |
| 22 #include "JsEngineTransition.h" |
22 #include "JsError.h" | 23 #include "JsError.h" |
23 #include "Utils.h" | 24 #include "Utils.h" |
24 | 25 |
25 AdblockPlus::JsValue::JsValue(AdblockPlus::JsEnginePtr jsEngine, | 26 AdblockPlus::JsValue::JsValue(AdblockPlus::JsEnginePtr jsEngine, |
26 v8::Handle<v8::Value> value) | 27 v8::Handle<v8::Value> value) |
27 : jsEngine(jsEngine), | 28 : jsEngine(jsEngine), |
28 value(new v8::Persistent<v8::Value>(jsEngine->GetIsolate(), value)) | 29 value(new v8::Persistent<v8::Value>(jsEngine->GetIsolate(), value)) |
29 { | 30 { |
30 } | 31 } |
31 | 32 |
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
206 } | 207 } |
207 | 208 |
208 AdblockPlus::JsValuePtr AdblockPlus::JsValue::Call(const JsValueList& params, Js
ValuePtr thisPtr) const | 209 AdblockPlus::JsValuePtr AdblockPlus::JsValue::Call(const JsValueList& params, Js
ValuePtr thisPtr) const |
209 { | 210 { |
210 if (!IsFunction()) | 211 if (!IsFunction()) |
211 throw new std::runtime_error("Attempting to call a non-function"); | 212 throw new std::runtime_error("Attempting to call a non-function"); |
212 | 213 |
213 const JsContext context(jsEngine); | 214 const JsContext context(jsEngine); |
214 if (!thisPtr) | 215 if (!thisPtr) |
215 { | 216 { |
216 v8::Local<v8::Context> localContext = v8::Local<v8::Context>::New( | 217 thisPtr = JsValuePtr(new JsValue(jsEngine, ToInternal(jsEngine)->GetGlobalOb
ject())); |
217 jsEngine->GetIsolate(), *jsEngine->context); | |
218 thisPtr = JsValuePtr(new JsValue(jsEngine, localContext->Global())); | |
219 } | 218 } |
220 if (!thisPtr->IsObject()) | 219 if (!thisPtr->IsObject()) |
221 throw new std::runtime_error("`this` pointer has to be an object"); | 220 throw new std::runtime_error("`this` pointer has to be an object"); |
222 v8::Local<v8::Object> thisObj = v8::Local<v8::Object>::Cast(thisPtr->UnwrapVal
ue()); | 221 v8::Local<v8::Object> thisObj = v8::Local<v8::Object>::Cast(thisPtr->UnwrapVal
ue()); |
223 | 222 |
224 std::vector<v8::Handle<v8::Value>> argv; | 223 std::vector<v8::Handle<v8::Value>> argv; |
225 for (JsValueList::const_iterator it = params.begin(); it != params.end(); ++it
) | 224 for (JsValueList::const_iterator it = params.begin(); it != params.end(); ++it
) |
226 argv.push_back((*it)->UnwrapValue()); | 225 argv.push_back((*it)->UnwrapValue()); |
227 | 226 |
228 const v8::TryCatch tryCatch; | 227 const v8::TryCatch tryCatch; |
229 v8::Local<v8::Function> func = v8::Local<v8::Function>::Cast(UnwrapValue()); | 228 v8::Local<v8::Function> func = v8::Local<v8::Function>::Cast(UnwrapValue()); |
230 v8::Local<v8::Value> result = func->Call(thisObj, argv.size(), | 229 v8::Local<v8::Value> result = func->Call(thisObj, argv.size(), |
231 argv.size() ? &argv.front() : 0); | 230 argv.size() ? &argv.front() : 0); |
232 | 231 |
233 if (tryCatch.HasCaught()) | 232 if (tryCatch.HasCaught()) |
234 throw JsError(tryCatch.Exception(), tryCatch.Message()); | 233 throw JsError(tryCatch.Exception(), tryCatch.Message()); |
235 | 234 |
236 return JsValuePtr(new JsValue(jsEngine, result)); | 235 return JsValuePtr(new JsValue(jsEngine, result)); |
237 } | 236 } |
238 | 237 |
239 AdblockPlus::JsValuePtr AdblockPlus::JsValue::Call(const JsValue& arg) const | 238 AdblockPlus::JsValuePtr AdblockPlus::JsValue::Call(const JsValue& arg) const |
240 { | 239 { |
241 const JsContext context(jsEngine); | 240 const JsContext context(jsEngine); |
242 JsValueList params; | 241 JsValueList params; |
243 params.push_back(JsValuePtr(new JsValue(arg.jsEngine, arg.UnwrapValue()))); | 242 params.push_back(JsValuePtr(new JsValue(arg.jsEngine, arg.UnwrapValue()))); |
244 return Call(params); | 243 return Call(params); |
245 } | 244 } |
OLD | NEW |