| 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-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 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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 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 auto isolate = jsEngine->GetIsolate(); |
| 187 v8::MaybeLocal<v8::String> property = Utils::ToV8String(isolate, name); | |
| 188 if (property.IsEmpty()) | |
| 189 throw std::runtime_error("Invalid property name"); | |
|
hub
2018/06/22 00:34:31
Since we already can throw, I thought it was OK to
sergei
2018/06/22 06:57:46
I think here we should use CHECKED_TO_LOCAL.
hub
2018/06/22 15:50:56
Done.
| |
| 187 v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(UnwrapValue()); | 190 v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(UnwrapValue()); |
| 188 return JsValue(jsEngine, obj->Get(property)); | 191 auto maybe = obj->Get(isolate->GetCurrentContext(), property.ToLocalChecked()) ; |
| 192 if (maybe.IsEmpty()) | |
| 193 return JsValue(jsEngine, v8::Undefined(isolate)); | |
|
hub
2018/06/22 00:34:31
If the property isn't found we return `undefined`.
sergei
2018/06/22 06:57:46
If the property is not found then the result of ob
hub
2018/06/22 15:50:56
Done.
| |
| 194 return JsValue(jsEngine, maybe.ToLocalChecked()); | |
| 189 } | 195 } |
| 190 | 196 |
| 191 void AdblockPlus::JsValue::SetProperty(const std::string& name, v8::Local<v8::Va lue> val) | 197 void AdblockPlus::JsValue::SetProperty(const std::string& name, v8::Local<v8::Va lue> val) |
| 192 { | 198 { |
| 193 if (!IsObject()) | 199 if (!IsObject()) |
| 194 throw std::runtime_error("Attempting to set property on a non-object"); | 200 throw std::runtime_error("Attempting to set property on a non-object"); |
| 195 | 201 auto isolate = jsEngine->GetIsolate(); |
|
sergei
2018/06/22 06:57:46
I think here and below we should use CHECKED_TO_LO
hub
2018/06/22 15:50:56
Done.
| |
| 196 v8::Local<v8::String> property = Utils::ToV8String(jsEngine->GetIsolate(), nam e); | 202 v8::MaybeLocal<v8::String> property = Utils::ToV8String(isolate, name); |
| 203 if (property.IsEmpty()) | |
| 204 throw std::runtime_error("Invalid property name"); | |
| 197 v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(UnwrapValue()); | 205 v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(UnwrapValue()); |
| 198 obj->Set(property, val); | 206 obj->Set(property.ToLocalChecked(), val); |
| 199 } | 207 } |
| 200 | 208 |
| 201 v8::Local<v8::Value> AdblockPlus::JsValue::UnwrapValue() const | 209 v8::Local<v8::Value> AdblockPlus::JsValue::UnwrapValue() const |
| 202 { | 210 { |
| 203 return v8::Local<v8::Value>::New(jsEngine->GetIsolate(), *value); | 211 return v8::Local<v8::Value>::New(jsEngine->GetIsolate(), *value); |
| 204 } | 212 } |
| 205 | 213 |
| 206 void AdblockPlus::JsValue::SetProperty(const std::string& name, const std::strin g& val) | 214 void AdblockPlus::JsValue::SetProperty(const std::string& name, const std::strin g& val) |
| 207 { | 215 { |
| 208 const JsContext context(*jsEngine); | 216 const JsContext context(*jsEngine); |
| 209 SetProperty(name, Utils::ToV8String(jsEngine->GetIsolate(), val)); | 217 auto maybe = Utils::ToV8String(jsEngine->GetIsolate(), val); |
| 218 if (maybe.IsEmpty()) | |
| 219 return; | |
| 220 SetProperty(name, maybe.ToLocalChecked()); | |
| 210 } | 221 } |
| 211 | 222 |
| 212 void AdblockPlus::JsValue::SetStringBufferProperty(const std::string& name, cons t StringBuffer& val) | 223 void AdblockPlus::JsValue::SetStringBufferProperty(const std::string& name, cons t StringBuffer& val) |
| 213 { | 224 { |
| 214 const JsContext context(*jsEngine); | 225 const JsContext context(*jsEngine); |
| 215 SetProperty(name, Utils::StringBufferToV8String(jsEngine->GetIsolate(), val)); | 226 auto maybe = Utils::StringBufferToV8String(jsEngine->GetIsolate(), val); |
| 227 if (maybe.IsEmpty()) | |
| 228 return; | |
| 229 SetProperty(name, maybe.ToLocalChecked()); | |
| 216 } | 230 } |
| 217 | 231 |
| 218 void AdblockPlus::JsValue::SetProperty(const std::string& name, int64_t val) | 232 void AdblockPlus::JsValue::SetProperty(const std::string& name, int64_t val) |
| 219 { | 233 { |
| 220 const JsContext context(*jsEngine); | 234 const JsContext context(*jsEngine); |
| 221 SetProperty(name, v8::Number::New(jsEngine->GetIsolate(), val)); | 235 SetProperty(name, v8::Number::New(jsEngine->GetIsolate(), val)); |
| 222 } | 236 } |
| 223 | 237 |
| 224 void AdblockPlus::JsValue::SetProperty(const std::string& name, const JsValue& v al) | 238 void AdblockPlus::JsValue::SetProperty(const std::string& name, const JsValue& v al) |
| 225 { | 239 { |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 275 return Call(argv, context.GetV8Context()->Global()); | 289 return Call(argv, context.GetV8Context()->Global()); |
| 276 } | 290 } |
| 277 | 291 |
| 278 JsValue JsValue::Call(std::vector<v8::Local<v8::Value>>& args, v8::Local<v8::Obj ect> thisObj) const | 292 JsValue JsValue::Call(std::vector<v8::Local<v8::Value>>& args, v8::Local<v8::Obj ect> thisObj) const |
| 279 { | 293 { |
| 280 if (!IsFunction()) | 294 if (!IsFunction()) |
| 281 throw std::runtime_error("Attempting to call a non-function"); | 295 throw std::runtime_error("Attempting to call a non-function"); |
| 282 if (!thisObj->IsObject()) | 296 if (!thisObj->IsObject()) |
| 283 throw std::runtime_error("`this` pointer has to be an object"); | 297 throw std::runtime_error("`this` pointer has to be an object"); |
| 284 | 298 |
| 299 auto isolate = jsEngine->GetIsolate(); | |
| 285 const JsContext context(*jsEngine); | 300 const JsContext context(*jsEngine); |
| 286 | 301 |
| 287 const v8::TryCatch tryCatch(jsEngine->GetIsolate()); | 302 const v8::TryCatch tryCatch(isolate); |
| 288 v8::Local<v8::Function> func = v8::Local<v8::Function>::Cast(UnwrapValue()); | 303 v8::Local<v8::Function> func = v8::Local<v8::Function>::Cast(UnwrapValue()); |
| 289 v8::Local<v8::Value> result = func->Call(thisObj, args.size(), | 304 auto result = CHECKED_TO_LOCAL( |
| 290 args.size() ? &args[0] : nullptr); | 305 isolate, func->Call( |
| 291 if (tryCatch.HasCaught()) | 306 isolate->GetCurrentContext(), |
| 292 throw JsError(jsEngine->GetIsolate(), tryCatch.Exception(), tryCatch.Message ()); | 307 thisObj, args.size(), args.size() ? &args[0] : nullptr), tryCatch); |
| 293 | 308 |
| 294 return JsValue(jsEngine, result); | 309 return JsValue(jsEngine, result); |
| 295 } | 310 } |
| OLD | NEW |