| Left: | ||
| Right: | 
| LEFT | RIGHT | 
|---|---|
| 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-present eyeo GmbH | 3 * Copyright (C) 2006-present 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 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 131 { | 131 { | 
| 132 const JsContext context(*jsEngine); | 132 const JsContext context(*jsEngine); | 
| 133 return Utils::StringBufferFromV8String(jsEngine->GetIsolate(), UnwrapValue()); | 133 return Utils::StringBufferFromV8String(jsEngine->GetIsolate(), UnwrapValue()); | 
| 134 } | 134 } | 
| 135 | 135 | 
| 136 int64_t AdblockPlus::JsValue::AsInt() const | 136 int64_t AdblockPlus::JsValue::AsInt() const | 
| 137 { | 137 { | 
| 138 const JsContext context(*jsEngine); | 138 const JsContext context(*jsEngine); | 
| 139 auto currentContext = jsEngine->GetIsolate()->GetCurrentContext(); | 139 auto currentContext = jsEngine->GetIsolate()->GetCurrentContext(); | 
| 140 auto value = UnwrapValue()->IntegerValue(currentContext); | 140 auto value = UnwrapValue()->IntegerValue(currentContext); | 
| 141 return value.IsJust() ? value.FromJust() : 0; | 141 return CHECKED_TO_VALUE(std::move(value)); | 
| 
 
hub
2018/06/22 21:32:39
The test expect that if accessing value throw a JS
 
 | 
 
sergei
2018/08/07 16:47:35
I find the usage of std::move good here too, for t
 
 | 
| 142 } | 142 } | 
| 143 | 143 | 
| 144 bool AdblockPlus::JsValue::AsBool() const | 144 bool AdblockPlus::JsValue::AsBool() const | 
| 145 { | 145 { | 
| 146 const JsContext context(*jsEngine); | 146 const JsContext context(*jsEngine); | 
| 147 auto currentContext = jsEngine->GetIsolate()->GetCurrentContext(); | 147 auto currentContext = jsEngine->GetIsolate()->GetCurrentContext(); | 
| 148 auto value = UnwrapValue()->BooleanValue(currentContext); | 148 auto value = UnwrapValue()->BooleanValue(currentContext); | 
| 149 return value.IsJust() ? value.FromJust() : false; | 149 return CHECKED_TO_VALUE(std::move(value)); | 
| 150 } | 150 } | 
| 151 | 151 | 
| 152 AdblockPlus::JsValueList AdblockPlus::JsValue::AsList() const | 152 AdblockPlus::JsValueList AdblockPlus::JsValue::AsList() const | 
| 153 { | 153 { | 
| 154 if (!IsArray()) | 154 if (!IsArray()) | 
| 155 throw std::runtime_error("Cannot convert a non-array to list"); | 155 throw std::runtime_error("Cannot convert a non-array to list"); | 
| 156 | 156 | 
| 157 const JsContext context(*jsEngine); | 157 const JsContext context(*jsEngine); | 
| 158 auto isolate = jsEngine->GetIsolate(); | 158 auto isolate = jsEngine->GetIsolate(); | 
| 159 auto currentContext = isolate->GetCurrentContext(); | 159 auto currentContext = isolate->GetCurrentContext(); | 
| 160 JsValueList result; | 160 JsValueList result; | 
| 161 v8::Local<v8::Array> array = v8::Local<v8::Array>::Cast(UnwrapValue()); | 161 v8::Local<v8::Array> array = v8::Local<v8::Array>::Cast(UnwrapValue()); | 
| 162 uint32_t length = array->Length(); | 162 uint32_t length = array->Length(); | 
| 163 for (uint32_t i = 0; i < length; i++) | 163 for (uint32_t i = 0; i < length; i++) | 
| 164 { | 164 { | 
| 165 v8::Local<v8::Value> item = CHECKED_TO_LOCAL_NOTHROW( | 165 v8::Local<v8::Value> item = CHECKED_TO_LOCAL( | 
| 166 isolate, array->Get(currentContext, i)); | 166 isolate, array->Get(currentContext, i)); | 
| 167 result.push_back(JsValue(jsEngine, item)); | 167 result.push_back(JsValue(jsEngine, item)); | 
| 168 } | 168 } | 
| 169 return result; | 169 return result; | 
| 170 } | 170 } | 
| 171 | 171 | 
| 172 std::vector<std::string> AdblockPlus::JsValue::GetOwnPropertyNames() const | 172 std::vector<std::string> AdblockPlus::JsValue::GetOwnPropertyNames() const | 
| 173 { | 173 { | 
| 174 if (!IsObject()) | 174 if (!IsObject()) | 
| 175 throw std::runtime_error("Attempting to get propert list for a non-object"); | 175 throw std::runtime_error("Attempting to get propert list for a non-object"); | 
| 176 | 176 | 
| 177 const JsContext context(*jsEngine); | 177 const JsContext context(*jsEngine); | 
| 178 auto isolate = jsEngine->GetIsolate(); | 178 auto isolate = jsEngine->GetIsolate(); | 
| 179 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(UnwrapValue()); | 179 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(UnwrapValue()); | 
| 180 auto propertyNames = CHECKED_TO_LOCAL_NOTHROW(isolate, | 180 auto propertyNames = CHECKED_TO_LOCAL(isolate, | 
| 181 object->GetOwnPropertyNames(isolate->GetCurrentContext())); | 181 object->GetOwnPropertyNames(isolate->GetCurrentContext())); | 
| 182 JsValueList properties = JsValue(jsEngine, propertyNames).AsList(); | 182 JsValueList properties = JsValue(jsEngine, propertyNames).AsList(); | 
| 183 std::vector<std::string> result; | 183 std::vector<std::string> result; | 
| 184 for (const auto& property : properties) | 184 for (const auto& property : properties) | 
| 185 result.push_back(property.AsString()); | 185 result.push_back(property.AsString()); | 
| 186 return result; | 186 return result; | 
| 187 } | 187 } | 
| 188 | 188 | 
| 189 | 189 | 
| 190 AdblockPlus::JsValue AdblockPlus::JsValue::GetProperty(const std::string& name) const | 190 AdblockPlus::JsValue AdblockPlus::JsValue::GetProperty(const std::string& name) const | 
| 191 { | 191 { | 
| 192 if (!IsObject()) | 192 if (!IsObject()) | 
| 193 throw std::runtime_error("Attempting to get property of a non-object"); | 193 throw std::runtime_error("Attempting to get property of a non-object"); | 
| 194 | 194 | 
| 195 const JsContext context(*jsEngine); | 195 const JsContext context(*jsEngine); | 
| 196 auto isolate = jsEngine->GetIsolate(); | 196 auto isolate = jsEngine->GetIsolate(); | 
| 197 v8::Local<v8::String> property = CHECKED_TO_LOCAL_NOTHROW( | 197 v8::Local<v8::String> property = CHECKED_TO_LOCAL( | 
| 198 isolate, Utils::ToV8String(isolate, name)); | 198 isolate, Utils::ToV8String(isolate, name)); | 
| 199 v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(UnwrapValue()); | 199 v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(UnwrapValue()); | 
| 200 return JsValue(jsEngine, CHECKED_TO_LOCAL_NOTHROW( | 200 return JsValue(jsEngine, CHECKED_TO_LOCAL( | 
| 201 isolate, obj->Get(isolate->GetCurrentContext(), property))); | 201 isolate, obj->Get(isolate->GetCurrentContext(), property))); | 
| 202 } | 202 } | 
| 203 | 203 | 
| 204 void AdblockPlus::JsValue::SetProperty(const std::string& name, v8::Local<v8::Va lue> val) | 204 void AdblockPlus::JsValue::SetProperty(const std::string& name, v8::Local<v8::Va lue> val) | 
| 205 { | 205 { | 
| 206 if (!IsObject()) | 206 if (!IsObject()) | 
| 207 throw std::runtime_error("Attempting to set property on a non-object"); | 207 throw std::runtime_error("Attempting to set property on a non-object"); | 
| 208 auto isolate = jsEngine->GetIsolate(); | 208 auto isolate = jsEngine->GetIsolate(); | 
| 209 | 209 | 
| 210 v8::Local<v8::String> property = CHECKED_TO_LOCAL_NOTHROW( | 210 v8::Local<v8::String> property = CHECKED_TO_LOCAL( | 
| 211 isolate, Utils::ToV8String(isolate, name)); | 211 isolate, Utils::ToV8String(isolate, name)); | 
| 212 v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(UnwrapValue()); | 212 v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(UnwrapValue()); | 
| 213 CHECKED_TO_VALUE(obj->Set(isolate->GetCurrentContext(), property, val)); | 213 CHECKED_TO_VALUE(obj->Set(isolate->GetCurrentContext(), property, val)); | 
| 214 } | 214 } | 
| 215 | 215 | 
| 216 v8::Local<v8::Value> AdblockPlus::JsValue::UnwrapValue() const | 216 v8::Local<v8::Value> AdblockPlus::JsValue::UnwrapValue() const | 
| 217 { | 217 { | 
| 218 return v8::Local<v8::Value>::New(jsEngine->GetIsolate(), *value); | 218 return v8::Local<v8::Value>::New(jsEngine->GetIsolate(), *value); | 
| 219 } | 219 } | 
| 220 | 220 | 
| 221 void AdblockPlus::JsValue::SetProperty(const std::string& name, const std::strin g& val) | 221 void AdblockPlus::JsValue::SetProperty(const std::string& name, const std::strin g& val) | 
| 222 { | 222 { | 
| 223 const JsContext context(*jsEngine); | 223 const JsContext context(*jsEngine); | 
| 224 auto isolate = jsEngine->GetIsolate(); | 224 auto isolate = jsEngine->GetIsolate(); | 
| 225 | 225 | 
| 226 SetProperty(name, CHECKED_TO_LOCAL_NOTHROW( | 226 SetProperty(name, CHECKED_TO_LOCAL( | 
| 227 isolate, Utils::ToV8String(jsEngine->GetIsolate(), val))); | 227 isolate, Utils::ToV8String(jsEngine->GetIsolate(), val))); | 
| 228 } | 228 } | 
| 229 | 229 | 
| 230 void AdblockPlus::JsValue::SetStringBufferProperty(const std::string& name, cons t StringBuffer& val) | 230 void AdblockPlus::JsValue::SetStringBufferProperty(const std::string& name, cons t StringBuffer& val) | 
| 231 { | 231 { | 
| 232 const JsContext context(*jsEngine); | 232 const JsContext context(*jsEngine); | 
| 233 auto isolate = jsEngine->GetIsolate(); | 233 auto isolate = jsEngine->GetIsolate(); | 
| 234 | 234 | 
| 235 SetProperty(name, CHECKED_TO_LOCAL_NOTHROW( | 235 SetProperty(name, CHECKED_TO_LOCAL( | 
| 236 isolate, Utils::StringBufferToV8String(isolate, val))); | 236 isolate, Utils::StringBufferToV8String(isolate, val))); | 
| 237 } | 237 } | 
| 238 | 238 | 
| 239 void AdblockPlus::JsValue::SetProperty(const std::string& name, int64_t val) | 239 void AdblockPlus::JsValue::SetProperty(const std::string& name, int64_t val) | 
| 240 { | 240 { | 
| 241 const JsContext context(*jsEngine); | 241 const JsContext context(*jsEngine); | 
| 242 SetProperty(name, v8::Number::New(jsEngine->GetIsolate(), val)); | 242 SetProperty(name, v8::Number::New(jsEngine->GetIsolate(), val)); | 
| 243 } | 243 } | 
| 244 | 244 | 
| 245 void AdblockPlus::JsValue::SetProperty(const std::string& name, const JsValue& v al) | 245 void AdblockPlus::JsValue::SetProperty(const std::string& name, const JsValue& v al) | 
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 301 if (!IsFunction()) | 301 if (!IsFunction()) | 
| 302 throw std::runtime_error("Attempting to call a non-function"); | 302 throw std::runtime_error("Attempting to call a non-function"); | 
| 303 if (!thisObj->IsObject()) | 303 if (!thisObj->IsObject()) | 
| 304 throw std::runtime_error("`this` pointer has to be an object"); | 304 throw std::runtime_error("`this` pointer has to be an object"); | 
| 305 | 305 | 
| 306 const JsContext context(*jsEngine); | 306 const JsContext context(*jsEngine); | 
| 307 auto isolate = jsEngine->GetIsolate(); | 307 auto isolate = jsEngine->GetIsolate(); | 
| 308 | 308 | 
| 309 const v8::TryCatch tryCatch(isolate); | 309 const v8::TryCatch tryCatch(isolate); | 
| 310 v8::Local<v8::Function> func = v8::Local<v8::Function>::Cast(UnwrapValue()); | 310 v8::Local<v8::Function> func = v8::Local<v8::Function>::Cast(UnwrapValue()); | 
| 311 auto result = CHECKED_TO_LOCAL( | 311 auto result = CHECKED_TO_LOCAL_WITH_TRY_CATCH( | 
| 312 isolate, func->Call(isolate->GetCurrentContext(), | 312 isolate, func->Call(isolate->GetCurrentContext(), | 
| 313 thisObj, args.size(), args.size() ? &args[0] : nullptr), tryCatch); | 313 thisObj, args.size(), args.size() ? &args[0] : nullptr), tryCatch); | 
| 314 | 314 | 
| 315 return JsValue(jsEngine, result); | 315 return JsValue(jsEngine, result); | 
| 316 } | 316 } | 
| LEFT | RIGHT |