| 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 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 158 { | 158 { |
| 159 v8::Local<v8::Value> item = array->Get(i); | 159 v8::Local<v8::Value> item = array->Get(i); |
| 160 result.push_back(JsValue(jsEngine, item)); | 160 result.push_back(JsValue(jsEngine, item)); |
| 161 } | 161 } |
| 162 return result; | 162 return result; |
| 163 } | 163 } |
| 164 | 164 |
| 165 std::vector<std::string> AdblockPlus::JsValue::GetOwnPropertyNames() const | 165 std::vector<std::string> AdblockPlus::JsValue::GetOwnPropertyNames() const |
| 166 { | 166 { |
| 167 if (!IsObject()) | 167 if (!IsObject()) |
| 168 throw new std::runtime_error("Attempting to get propert list for a non-objec
t"); | 168 throw std::runtime_error("Attempting to get propert list for a non-object"); |
| 169 | 169 |
| 170 const JsContext context(*jsEngine); | 170 const JsContext context(*jsEngine); |
| 171 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(UnwrapValue()); | 171 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(UnwrapValue()); |
| 172 JsValueList properties = JsValue(jsEngine, object->GetOwnPropertyNames()).AsLi
st(); | 172 JsValueList properties = JsValue(jsEngine, object->GetOwnPropertyNames()).AsLi
st(); |
| 173 std::vector<std::string> result; | 173 std::vector<std::string> result; |
| 174 for (const auto& property : properties) | 174 for (const auto& property : properties) |
| 175 result.push_back(property.AsString()); | 175 result.push_back(property.AsString()); |
| 176 return result; | 176 return result; |
| 177 } | 177 } |
| 178 | 178 |
| 179 | 179 |
| 180 AdblockPlus::JsValue AdblockPlus::JsValue::GetProperty(const std::string& name)
const | 180 AdblockPlus::JsValue AdblockPlus::JsValue::GetProperty(const std::string& name)
const |
| 181 { | 181 { |
| 182 if (!IsObject()) | 182 if (!IsObject()) |
| 183 throw new std::runtime_error("Attempting to get property of a non-object"); | 183 throw std::runtime_error("Attempting to get property of a non-object"); |
| 184 | 184 |
| 185 const JsContext context(*jsEngine); | 185 const JsContext context(*jsEngine); |
| 186 v8::Local<v8::String> property = Utils::ToV8String(jsEngine->GetIsolate(), nam
e); | 186 v8::Local<v8::String> property = Utils::ToV8String(jsEngine->GetIsolate(), nam
e); |
| 187 v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(UnwrapValue()); | 187 v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(UnwrapValue()); |
| 188 return JsValue(jsEngine, obj->Get(property)); | 188 return JsValue(jsEngine, obj->Get(property)); |
| 189 } | 189 } |
| 190 | 190 |
| 191 void AdblockPlus::JsValue::SetProperty(const std::string& name, v8::Handle<v8::V
alue> val) | 191 void AdblockPlus::JsValue::SetProperty(const std::string& name, v8::Handle<v8::V
alue> val) |
| 192 { | 192 { |
| 193 if (!IsObject()) | 193 if (!IsObject()) |
| 194 throw new std::runtime_error("Attempting to set property on a non-object"); | 194 throw std::runtime_error("Attempting to set property on a non-object"); |
| 195 | 195 |
| 196 v8::Local<v8::String> property = Utils::ToV8String(jsEngine->GetIsolate(), nam
e); | 196 v8::Local<v8::String> property = Utils::ToV8String(jsEngine->GetIsolate(), nam
e); |
| 197 v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(UnwrapValue()); | 197 v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(UnwrapValue()); |
| 198 obj->Set(property, val); | 198 obj->Set(property, val); |
| 199 } | 199 } |
| 200 | 200 |
| 201 v8::Local<v8::Value> AdblockPlus::JsValue::UnwrapValue() const | 201 v8::Local<v8::Value> AdblockPlus::JsValue::UnwrapValue() const |
| 202 { | 202 { |
| 203 return v8::Local<v8::Value>::New(jsEngine->GetIsolate(), *value); | 203 return v8::Local<v8::Value>::New(jsEngine->GetIsolate(), *value); |
| 204 } | 204 } |
| (...skipping 24 matching lines...) Expand all Loading... |
| 229 | 229 |
| 230 void AdblockPlus::JsValue::SetProperty(const std::string& name, bool val) | 230 void AdblockPlus::JsValue::SetProperty(const std::string& name, bool val) |
| 231 { | 231 { |
| 232 const JsContext context(*jsEngine); | 232 const JsContext context(*jsEngine); |
| 233 SetProperty(name, v8::Boolean::New(jsEngine->GetIsolate(), val)); | 233 SetProperty(name, v8::Boolean::New(jsEngine->GetIsolate(), val)); |
| 234 } | 234 } |
| 235 | 235 |
| 236 std::string AdblockPlus::JsValue::GetClass() const | 236 std::string AdblockPlus::JsValue::GetClass() const |
| 237 { | 237 { |
| 238 if (!IsObject()) | 238 if (!IsObject()) |
| 239 throw new std::runtime_error("Cannot get constructor of a non-object"); | 239 throw std::runtime_error("Cannot get constructor of a non-object"); |
| 240 | 240 |
| 241 const JsContext context(*jsEngine); | 241 const JsContext context(*jsEngine); |
| 242 v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(UnwrapValue()); | 242 v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(UnwrapValue()); |
| 243 return Utils::FromV8String(obj->GetConstructorName()); | 243 return Utils::FromV8String(obj->GetConstructorName()); |
| 244 } | 244 } |
| 245 | 245 |
| 246 JsValue JsValue::Call(const JsValueList& params) const | 246 JsValue JsValue::Call(const JsValueList& params) const |
| 247 { | 247 { |
| 248 const JsContext context(*jsEngine); | 248 const JsContext context(*jsEngine); |
| 249 std::vector<v8::Handle<v8::Value>> argv; | 249 std::vector<v8::Handle<v8::Value>> argv; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 271 | 271 |
| 272 std::vector<v8::Handle<v8::Value>> argv; | 272 std::vector<v8::Handle<v8::Value>> argv; |
| 273 argv.push_back(arg.UnwrapValue()); | 273 argv.push_back(arg.UnwrapValue()); |
| 274 | 274 |
| 275 return Call(argv, context.GetV8Context()->Global()); | 275 return Call(argv, context.GetV8Context()->Global()); |
| 276 } | 276 } |
| 277 | 277 |
| 278 JsValue JsValue::Call(std::vector<v8::Handle<v8::Value>>& args, v8::Local<v8::Ob
ject> thisObj) const | 278 JsValue JsValue::Call(std::vector<v8::Handle<v8::Value>>& args, v8::Local<v8::Ob
ject> thisObj) const |
| 279 { | 279 { |
| 280 if (!IsFunction()) | 280 if (!IsFunction()) |
| 281 throw new std::runtime_error("Attempting to call a non-function"); | 281 throw std::runtime_error("Attempting to call a non-function"); |
| 282 if (!thisObj->IsObject()) | 282 if (!thisObj->IsObject()) |
| 283 throw new std::runtime_error("`this` pointer has to be an object"); | 283 throw std::runtime_error("`this` pointer has to be an object"); |
| 284 | 284 |
| 285 const JsContext context(*jsEngine); | 285 const JsContext context(*jsEngine); |
| 286 | 286 |
| 287 const v8::TryCatch tryCatch; | 287 const v8::TryCatch tryCatch; |
| 288 v8::Local<v8::Function> func = v8::Local<v8::Function>::Cast(UnwrapValue()); | 288 v8::Local<v8::Function> func = v8::Local<v8::Function>::Cast(UnwrapValue()); |
| 289 v8::Local<v8::Value> result = func->Call(thisObj, args.size(), | 289 v8::Local<v8::Value> result = func->Call(thisObj, args.size(), |
| 290 args.size() ? &args[0] : nullptr); | 290 args.size() ? &args[0] : nullptr); |
| 291 | 291 |
| 292 if (tryCatch.HasCaught()) | 292 if (tryCatch.HasCaught()) |
| 293 throw JsError(tryCatch.Exception(), tryCatch.Message()); | 293 throw JsError(tryCatch.Exception(), tryCatch.Message()); |
| 294 | 294 |
| 295 return JsValue(jsEngine, result); | 295 return JsValue(jsEngine, result); |
| 296 } | 296 } |
| OLD | NEW |