| 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; | 25 using namespace AdblockPlus; | 
| 26 | 26 | 
| 27 AdblockPlus::JsValue::JsValue(AdblockPlus::JsEnginePtr jsEngine, | 27 AdblockPlus::JsValue::JsValue(AdblockPlus::JsEnginePtr jsEngine, | 
| 28       v8::Handle<v8::Value> value) | 28       v8::Handle<v8::Value> value) | 
| 29     : jsEngine(jsEngine), | 29     : jsEngine(jsEngine), | 
| 30       value(new v8::Persistent<v8::Value>(jsEngine->GetIsolate(), value)) | 30       value(new v8::Global<v8::Value>(jsEngine->GetIsolate(), value)) | 
| 31 { | 31 { | 
| 32 } | 32 } | 
| 33 | 33 | 
| 34 AdblockPlus::JsValue::JsValue(AdblockPlus::JsValue&& src) | 34 AdblockPlus::JsValue::JsValue(AdblockPlus::JsValue&& src) | 
| 35     : jsEngine(src.jsEngine), | 35     : jsEngine(src.jsEngine), | 
| 36       value(std::move(src.value)) | 36       value(std::move(src.value)) | 
| 37 { | 37 { | 
| 38 } | 38 } | 
| 39 | 39 | 
| 40 AdblockPlus::JsValue::JsValue(const JsValue& src) | 40 AdblockPlus::JsValue::JsValue(const JsValue& src) | 
| 41   : jsEngine(src.jsEngine) | 41   : jsEngine(src.jsEngine) | 
| 42 { | 42 { | 
| 43   const JsContext context(*src.jsEngine); | 43   const JsContext context(*src.jsEngine); | 
| 44   value.reset(new v8::Persistent<v8::Value>(src.jsEngine->GetIsolate(), *src.val
     ue)); | 44   value.reset(new v8::Global<v8::Value>(src.jsEngine->GetIsolate(), *src.value))
     ; | 
| 45 } | 45 } | 
| 46 | 46 | 
| 47 AdblockPlus::JsValue::~JsValue() | 47 AdblockPlus::JsValue::~JsValue() | 
| 48 { | 48 { | 
| 49   if (value) | 49   if (value) | 
| 50   { | 50   { | 
| 51     const JsContext context(*jsEngine); | 51     const JsContext context(*jsEngine); | 
| 52     value->Dispose(); |  | 
| 53     value.reset(); | 52     value.reset(); | 
| 54   } | 53   } | 
| 55 } | 54 } | 
| 56 | 55 | 
| 57 JsValue& AdblockPlus::JsValue::operator=(const JsValue& src) | 56 JsValue& AdblockPlus::JsValue::operator=(const JsValue& src) | 
| 58 { | 57 { | 
| 59   const JsContext context(*src.jsEngine); | 58   const JsContext context(*src.jsEngine); | 
| 60   if (value) |  | 
| 61     value->Dispose(); |  | 
| 62   jsEngine = src.jsEngine; | 59   jsEngine = src.jsEngine; | 
| 63   value.reset(new v8::Persistent<v8::Value>(src.jsEngine->GetIsolate(), *src.val
     ue)); | 60   value.reset(new v8::Global<v8::Value>(src.jsEngine->GetIsolate(), *src.value))
     ; | 
| 64 | 61 | 
| 65   return *this; | 62   return *this; | 
| 66 } | 63 } | 
|  | 64 | 
|  | 65 JsValue& AdblockPlus::JsValue::operator=(JsValue&& src) | 
|  | 66 { | 
|  | 67   jsEngine = std::move(src.jsEngine); | 
|  | 68   value = std::move(src.value); | 
|  | 69 | 
|  | 70   return *this; | 
|  | 71 } | 
| 67 | 72 | 
| 68 bool AdblockPlus::JsValue::IsUndefined() const | 73 bool AdblockPlus::JsValue::IsUndefined() const | 
| 69 { | 74 { | 
| 70   const JsContext context(*jsEngine); | 75   const JsContext context(*jsEngine); | 
| 71   return UnwrapValue()->IsUndefined(); | 76   return UnwrapValue()->IsUndefined(); | 
| 72 } | 77 } | 
| 73 | 78 | 
| 74 bool AdblockPlus::JsValue::IsNull() const | 79 bool AdblockPlus::JsValue::IsNull() const | 
| 75 { | 80 { | 
| 76   const JsContext context(*jsEngine); | 81   const JsContext context(*jsEngine); | 
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 206 | 211 | 
| 207 void AdblockPlus::JsValue::SetProperty(const std::string& name, const JsValue& v
     al) | 212 void AdblockPlus::JsValue::SetProperty(const std::string& name, const JsValue& v
     al) | 
| 208 { | 213 { | 
| 209   const JsContext context(*jsEngine); | 214   const JsContext context(*jsEngine); | 
| 210   SetProperty(name, val.UnwrapValue()); | 215   SetProperty(name, val.UnwrapValue()); | 
| 211 } | 216 } | 
| 212 | 217 | 
| 213 void AdblockPlus::JsValue::SetProperty(const std::string& name, bool val) | 218 void AdblockPlus::JsValue::SetProperty(const std::string& name, bool val) | 
| 214 { | 219 { | 
| 215   const JsContext context(*jsEngine); | 220   const JsContext context(*jsEngine); | 
| 216   SetProperty(name, v8::Boolean::New(val)); | 221   SetProperty(name, v8::Boolean::New(jsEngine->GetIsolate(), val)); | 
| 217 } | 222 } | 
| 218 | 223 | 
| 219 std::string AdblockPlus::JsValue::GetClass() const | 224 std::string AdblockPlus::JsValue::GetClass() const | 
| 220 { | 225 { | 
| 221   if (!IsObject()) | 226   if (!IsObject()) | 
| 222     throw new std::runtime_error("Cannot get constructor of a non-object"); | 227     throw new std::runtime_error("Cannot get constructor of a non-object"); | 
| 223 | 228 | 
| 224   const JsContext context(*jsEngine); | 229   const JsContext context(*jsEngine); | 
| 225   v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(UnwrapValue()); | 230   v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(UnwrapValue()); | 
| 226   return Utils::FromV8String(obj->GetConstructorName()); | 231   return Utils::FromV8String(obj->GetConstructorName()); | 
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 270   const v8::TryCatch tryCatch; | 275   const v8::TryCatch tryCatch; | 
| 271   v8::Local<v8::Function> func = v8::Local<v8::Function>::Cast(UnwrapValue()); | 276   v8::Local<v8::Function> func = v8::Local<v8::Function>::Cast(UnwrapValue()); | 
| 272   v8::Local<v8::Value> result = func->Call(thisObj, args.size(), | 277   v8::Local<v8::Value> result = func->Call(thisObj, args.size(), | 
| 273     args.size() ? &args[0] : nullptr); | 278     args.size() ? &args[0] : nullptr); | 
| 274 | 279 | 
| 275   if (tryCatch.HasCaught()) | 280   if (tryCatch.HasCaught()) | 
| 276     throw JsError(tryCatch.Exception(), tryCatch.Message()); | 281     throw JsError(tryCatch.Exception(), tryCatch.Message()); | 
| 277 | 282 | 
| 278   return JsValue(jsEngine, result); | 283   return JsValue(jsEngine, result); | 
| 279 } | 284 } | 
| OLD | NEW | 
|---|