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 |
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 "JsError.h" | 22 #include "JsError.h" |
23 #include "Utils.h" | 23 #include "Utils.h" |
24 | 24 |
| 25 using namespace AdblockPlus; |
| 26 |
25 AdblockPlus::JsValue::JsValue(AdblockPlus::JsEnginePtr jsEngine, | 27 AdblockPlus::JsValue::JsValue(AdblockPlus::JsEnginePtr jsEngine, |
26 v8::Handle<v8::Value> value) | 28 v8::Handle<v8::Value> value) |
27 : jsEngine(jsEngine), | 29 : jsEngine(jsEngine), |
28 value(new v8::Persistent<v8::Value>(jsEngine->GetIsolate(), value)) | 30 value(new v8::Persistent<v8::Value>(jsEngine->GetIsolate(), value)) |
29 { | 31 { |
30 } | 32 } |
31 | 33 |
32 AdblockPlus::JsValue::JsValue(AdblockPlus::JsValue&& src) | 34 AdblockPlus::JsValue::JsValue(AdblockPlus::JsValue&& src) |
33 : jsEngine(src.jsEngine), | 35 : jsEngine(src.jsEngine), |
34 value(std::move(src.value)) | 36 value(std::move(src.value)) |
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
198 std::string AdblockPlus::JsValue::GetClass() const | 200 std::string AdblockPlus::JsValue::GetClass() const |
199 { | 201 { |
200 if (!IsObject()) | 202 if (!IsObject()) |
201 throw new std::runtime_error("Cannot get constructor of a non-object"); | 203 throw new std::runtime_error("Cannot get constructor of a non-object"); |
202 | 204 |
203 const JsContext context(jsEngine); | 205 const JsContext context(jsEngine); |
204 v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(UnwrapValue()); | 206 v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(UnwrapValue()); |
205 return Utils::FromV8String(obj->GetConstructorName()); | 207 return Utils::FromV8String(obj->GetConstructorName()); |
206 } | 208 } |
207 | 209 |
208 AdblockPlus::JsValuePtr AdblockPlus::JsValue::Call(const JsConstValueList& param
s, JsValuePtr thisPtr) const | 210 JsValue JsValue::Call(const JsConstValueList& params, JsValuePtr thisPtr) const |
209 { | 211 { |
210 if (!IsFunction()) | |
211 throw new std::runtime_error("Attempting to call a non-function"); | |
212 | |
213 const JsContext context(jsEngine); | 212 const JsContext context(jsEngine); |
214 if (!thisPtr) | 213 v8::Local<v8::Object> thisObj = thisPtr ? |
215 { | 214 v8::Local<v8::Object>::Cast(thisPtr->UnwrapValue()) : |
216 v8::Local<v8::Context> localContext = v8::Local<v8::Context>::New( | 215 context.GetV8Context()->Global(); |
217 jsEngine->GetIsolate(), *jsEngine->context); | |
218 thisPtr = JsValuePtr(new JsValue(jsEngine, localContext->Global())); | |
219 } | |
220 if (!thisPtr->IsObject()) | |
221 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()); | |
223 | 216 |
224 std::vector<v8::Handle<v8::Value>> argv; | 217 std::vector<v8::Handle<v8::Value>> argv; |
225 for (const auto& param : params) | 218 for (const auto& param : params) |
226 argv.push_back(param->UnwrapValue()); | 219 argv.push_back(param->UnwrapValue()); |
227 | 220 |
| 221 return Call(argv, thisObj); |
| 222 } |
| 223 |
| 224 JsValue JsValue::Call(const JsValue& arg) const |
| 225 { |
| 226 const JsContext context(jsEngine); |
| 227 |
| 228 std::vector<v8::Handle<v8::Value>> argv; |
| 229 argv.push_back(arg.UnwrapValue()); |
| 230 |
| 231 return Call(argv, context.GetV8Context()->Global()); |
| 232 } |
| 233 |
| 234 JsValue JsValue::Call(std::vector<v8::Handle<v8::Value>>& args, v8::Local<v8::Ob
ject> thisObj) const |
| 235 { |
| 236 if (!IsFunction()) |
| 237 throw new std::runtime_error("Attempting to call a non-function"); |
| 238 if (!thisObj->IsObject()) |
| 239 throw new std::runtime_error("`this` pointer has to be an object"); |
| 240 |
| 241 const JsContext context(jsEngine); |
| 242 |
228 const v8::TryCatch tryCatch; | 243 const v8::TryCatch tryCatch; |
229 v8::Local<v8::Function> func = v8::Local<v8::Function>::Cast(UnwrapValue()); | 244 v8::Local<v8::Function> func = v8::Local<v8::Function>::Cast(UnwrapValue()); |
230 v8::Local<v8::Value> result = func->Call(thisObj, argv.size(), | 245 v8::Local<v8::Value> result = func->Call(thisObj, args.size(), |
231 argv.size() ? &argv.front() : 0); | 246 args.size() ? &args[0] : nullptr); |
232 | 247 |
233 if (tryCatch.HasCaught()) | 248 if (tryCatch.HasCaught()) |
234 throw JsError(tryCatch.Exception(), tryCatch.Message()); | 249 throw JsError(tryCatch.Exception(), tryCatch.Message()); |
235 | 250 |
236 return JsValuePtr(new JsValue(jsEngine, result)); | 251 return JsValue(jsEngine, result); |
237 } | 252 } |
238 | |
239 AdblockPlus::JsValuePtr AdblockPlus::JsValue::Call(const JsConstValuePtr& arg) c
onst | |
240 { | |
241 JsConstValueList params; | |
242 params.push_back(arg); | |
243 return Call(params); | |
244 } | |
OLD | NEW |