 Issue 29417605:
  Issue 5034 - Part 3: Create plain JsValue instead of JsValuePtr  (Closed) 
  Base URL: https://hg.adblockplus.org/libadblockplus/
    
  
    Issue 29417605:
  Issue 5034 - Part 3: Create plain JsValue instead of JsValuePtr  (Closed) 
  Base URL: https://hg.adblockplus.org/libadblockplus/| Left: | ||
| Right: | 
| 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 19 matching lines...) Expand all Loading... | |
| 30 value(new v8::Persistent<v8::Value>(jsEngine->GetIsolate(), value)) | 30 value(new v8::Persistent<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) | |
| 41 : jsEngine(src.jsEngine) | |
| 42 { | |
| 43 const JsContext context(src.jsEngine); | |
| 44 value.reset(new v8::Persistent<v8::Value>(src.jsEngine->GetIsolate(), *src.val ue)); | |
| 45 } | |
| 46 | |
| 40 AdblockPlus::JsValue::~JsValue() | 47 AdblockPlus::JsValue::~JsValue() | 
| 41 { | 48 { | 
| 42 if (value) | 49 if (value) | 
| 43 { | 50 { | 
| 51 const JsContext context(jsEngine); | |
| 
sergei
2017/04/20 07:42:52
I think it deserves a separate commit. Do you mind
 
hub
2017/04/20 12:41:01
will do.
 | |
| 44 value->Dispose(); | 52 value->Dispose(); | 
| 45 value.reset(); | 53 value.reset(); | 
| 46 } | 54 } | 
| 47 } | 55 } | 
| 48 | 56 | 
| 57 JsValue& AdblockPlus::JsValue::operator=(const JsValue& src) | |
| 58 { | |
| 59 const JsContext context(src.jsEngine); | |
| 60 if (value) | |
| 61 value->Dispose(); | |
| 62 jsEngine = src.jsEngine; | |
| 63 value.reset(new v8::Persistent<v8::Value>(src.jsEngine->GetIsolate(), *src.val ue)); | |
| 64 | |
| 65 return *this; | |
| 66 } | |
| 67 | |
| 49 bool AdblockPlus::JsValue::IsUndefined() const | 68 bool AdblockPlus::JsValue::IsUndefined() const | 
| 50 { | 69 { | 
| 51 const JsContext context(jsEngine); | 70 const JsContext context(jsEngine); | 
| 52 return UnwrapValue()->IsUndefined(); | 71 return UnwrapValue()->IsUndefined(); | 
| 53 } | 72 } | 
| 54 | 73 | 
| 55 bool AdblockPlus::JsValue::IsNull() const | 74 bool AdblockPlus::JsValue::IsNull() const | 
| 56 { | 75 { | 
| 57 const JsContext context(jsEngine); | 76 const JsContext context(jsEngine); | 
| 58 return UnwrapValue()->IsNull(); | 77 return UnwrapValue()->IsNull(); | 
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 120 if (!IsArray()) | 139 if (!IsArray()) | 
| 121 throw std::runtime_error("Cannot convert a non-array to list"); | 140 throw std::runtime_error("Cannot convert a non-array to list"); | 
| 122 | 141 | 
| 123 const JsContext context(jsEngine); | 142 const JsContext context(jsEngine); | 
| 124 JsValueList result; | 143 JsValueList result; | 
| 125 v8::Local<v8::Array> array = v8::Local<v8::Array>::Cast(UnwrapValue()); | 144 v8::Local<v8::Array> array = v8::Local<v8::Array>::Cast(UnwrapValue()); | 
| 126 uint32_t length = array->Length(); | 145 uint32_t length = array->Length(); | 
| 127 for (uint32_t i = 0; i < length; i++) | 146 for (uint32_t i = 0; i < length; i++) | 
| 128 { | 147 { | 
| 129 v8::Local<v8::Value> item = array->Get(i); | 148 v8::Local<v8::Value> item = array->Get(i); | 
| 130 result.push_back(JsValuePtr(new JsValue(jsEngine, item))); | 149 result.push_back(JsValue(jsEngine, item)); | 
| 131 } | 150 } | 
| 132 return result; | 151 return result; | 
| 133 } | 152 } | 
| 134 | 153 | 
| 135 std::vector<std::string> AdblockPlus::JsValue::GetOwnPropertyNames() const | 154 std::vector<std::string> AdblockPlus::JsValue::GetOwnPropertyNames() const | 
| 136 { | 155 { | 
| 137 if (!IsObject()) | 156 if (!IsObject()) | 
| 138 throw new std::runtime_error("Attempting to get propert list for a non-objec t"); | 157 throw new std::runtime_error("Attempting to get propert list for a non-objec t"); | 
| 139 | 158 | 
| 140 const JsContext context(jsEngine); | 159 const JsContext context(jsEngine); | 
| 141 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(UnwrapValue()); | 160 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(UnwrapValue()); | 
| 142 JsValueList properties = JsValuePtr(new JsValue(jsEngine, object->GetOwnProper tyNames()))->AsList(); | 161 JsValueList properties = JsValue(jsEngine, object->GetOwnPropertyNames()).AsLi st(); | 
| 143 std::vector<std::string> result; | 162 std::vector<std::string> result; | 
| 144 for (const auto& property : properties) | 163 for (const auto& property : properties) | 
| 145 result.push_back(property->AsString()); | 164 result.push_back(property.AsString()); | 
| 146 return result; | 165 return result; | 
| 147 } | 166 } | 
| 148 | 167 | 
| 149 | 168 | 
| 150 AdblockPlus::JsValue AdblockPlus::JsValue::GetProperty(const std::string& name) const | 169 AdblockPlus::JsValue AdblockPlus::JsValue::GetProperty(const std::string& name) const | 
| 151 { | 170 { | 
| 152 if (!IsObject()) | 171 if (!IsObject()) | 
| 153 throw new std::runtime_error("Attempting to get property of a non-object"); | 172 throw new std::runtime_error("Attempting to get property of a non-object"); | 
| 154 | 173 | 
| 155 const JsContext context(jsEngine); | 174 const JsContext context(jsEngine); | 
| (...skipping 10 matching lines...) Expand all Loading... | |
| 166 v8::Local<v8::String> property = Utils::ToV8String(jsEngine->GetIsolate(), nam e); | 185 v8::Local<v8::String> property = Utils::ToV8String(jsEngine->GetIsolate(), nam e); | 
| 167 v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(UnwrapValue()); | 186 v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(UnwrapValue()); | 
| 168 obj->Set(property, val); | 187 obj->Set(property, val); | 
| 169 } | 188 } | 
| 170 | 189 | 
| 171 v8::Local<v8::Value> AdblockPlus::JsValue::UnwrapValue() const | 190 v8::Local<v8::Value> AdblockPlus::JsValue::UnwrapValue() const | 
| 172 { | 191 { | 
| 173 return v8::Local<v8::Value>::New(jsEngine->GetIsolate(), *value); | 192 return v8::Local<v8::Value>::New(jsEngine->GetIsolate(), *value); | 
| 174 } | 193 } | 
| 175 | 194 | 
| 176 JsValue AdblockPlus::JsValue::Clone() const | |
| 177 { | |
| 178 return JsValue(jsEngine, UnwrapValue()); | |
| 179 } | |
| 180 | |
| 181 void AdblockPlus::JsValue::SetProperty(const std::string& name, const std::strin g& val) | 195 void AdblockPlus::JsValue::SetProperty(const std::string& name, const std::strin g& val) | 
| 182 { | 196 { | 
| 183 const JsContext context(jsEngine); | 197 const JsContext context(jsEngine); | 
| 184 SetProperty(name, Utils::ToV8String(jsEngine->GetIsolate(), val)); | 198 SetProperty(name, Utils::ToV8String(jsEngine->GetIsolate(), val)); | 
| 185 } | 199 } | 
| 186 | 200 | 
| 187 void AdblockPlus::JsValue::SetProperty(const std::string& name, int64_t val) | 201 void AdblockPlus::JsValue::SetProperty(const std::string& name, int64_t val) | 
| 188 { | 202 { | 
| 189 const JsContext context(jsEngine); | 203 const JsContext context(jsEngine); | 
| 190 SetProperty(name, v8::Number::New(jsEngine->GetIsolate(), val)); | 204 SetProperty(name, v8::Number::New(jsEngine->GetIsolate(), val)); | 
| (...skipping 14 matching lines...) Expand all Loading... | |
| 205 std::string AdblockPlus::JsValue::GetClass() const | 219 std::string AdblockPlus::JsValue::GetClass() const | 
| 206 { | 220 { | 
| 207 if (!IsObject()) | 221 if (!IsObject()) | 
| 208 throw new std::runtime_error("Cannot get constructor of a non-object"); | 222 throw new std::runtime_error("Cannot get constructor of a non-object"); | 
| 209 | 223 | 
| 210 const JsContext context(jsEngine); | 224 const JsContext context(jsEngine); | 
| 211 v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(UnwrapValue()); | 225 v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(UnwrapValue()); | 
| 212 return Utils::FromV8String(obj->GetConstructorName()); | 226 return Utils::FromV8String(obj->GetConstructorName()); | 
| 213 } | 227 } | 
| 214 | 228 | 
| 215 JsValue JsValue::Call(const JsConstValueList& params, const JsValuePtr& thisPtr) const | 229 JsValue JsValue::Call(const JsValueList& params, const JsValuePtr& thisPtr) cons t | 
| 216 { | 230 { | 
| 217 const JsContext context(jsEngine); | 231 const JsContext context(jsEngine); | 
| 218 v8::Local<v8::Object> thisObj = thisPtr ? | 232 v8::Local<v8::Object> thisObj = thisPtr ? | 
| 219 v8::Local<v8::Object>::Cast(thisPtr->UnwrapValue()) : | 233 v8::Local<v8::Object>::Cast(thisPtr->UnwrapValue()) : | 
| 220 context.GetV8Context()->Global(); | 234 context.GetV8Context()->Global(); | 
| 221 | 235 | 
| 222 std::vector<v8::Handle<v8::Value>> argv; | 236 std::vector<v8::Handle<v8::Value>> argv; | 
| 223 for (const auto& param : params) | 237 for (const auto& param : params) | 
| 224 argv.push_back(param->UnwrapValue()); | 238 argv.push_back(param.UnwrapValue()); | 
| 225 | 239 | 
| 226 return Call(argv, thisObj); | 240 return Call(argv, thisObj); | 
| 227 } | 241 } | 
| 228 | 242 | 
| 229 JsValue JsValue::Call(const JsValue& arg) const | 243 JsValue JsValue::Call(const JsValue& arg) const | 
| 230 { | 244 { | 
| 231 const JsContext context(jsEngine); | 245 const JsContext context(jsEngine); | 
| 232 | 246 | 
| 233 std::vector<v8::Handle<v8::Value>> argv; | 247 std::vector<v8::Handle<v8::Value>> argv; | 
| 234 argv.push_back(arg.UnwrapValue()); | 248 argv.push_back(arg.UnwrapValue()); | 
| (...skipping 13 matching lines...) Expand all Loading... | |
| 248 const v8::TryCatch tryCatch; | 262 const v8::TryCatch tryCatch; | 
| 249 v8::Local<v8::Function> func = v8::Local<v8::Function>::Cast(UnwrapValue()); | 263 v8::Local<v8::Function> func = v8::Local<v8::Function>::Cast(UnwrapValue()); | 
| 250 v8::Local<v8::Value> result = func->Call(thisObj, args.size(), | 264 v8::Local<v8::Value> result = func->Call(thisObj, args.size(), | 
| 251 args.size() ? &args[0] : nullptr); | 265 args.size() ? &args[0] : nullptr); | 
| 252 | 266 | 
| 253 if (tryCatch.HasCaught()) | 267 if (tryCatch.HasCaught()) | 
| 254 throw JsError(tryCatch.Exception(), tryCatch.Message()); | 268 throw JsError(tryCatch.Exception(), tryCatch.Message()); | 
| 255 | 269 | 
| 256 return JsValue(jsEngine, result); | 270 return JsValue(jsEngine, result); | 
| 257 } | 271 } | 
| OLD | NEW |