 Issue 29812649:
  Issue 6526 - *ToV8String() return MaybeLocal<> and check Call() return value  (Closed) 
  Base URL: https://hg.adblockplus.org/libadblockplus/
    
  
    Issue 29812649:
  Issue 6526 - *ToV8String() return MaybeLocal<> and check Call() return value  (Closed) 
  Base URL: https://hg.adblockplus.org/libadblockplus/| Index: src/JsValue.cpp | 
| =================================================================== | 
| --- a/src/JsValue.cpp | 
| +++ b/src/JsValue.cpp | 
| @@ -178,46 +178,60 @@ | 
| AdblockPlus::JsValue AdblockPlus::JsValue::GetProperty(const std::string& name) const | 
| { | 
| if (!IsObject()) | 
| throw std::runtime_error("Attempting to get property of a non-object"); | 
| const JsContext context(*jsEngine); | 
| - v8::Local<v8::String> property = Utils::ToV8String(jsEngine->GetIsolate(), name); | 
| + auto isolate = jsEngine->GetIsolate(); | 
| + v8::MaybeLocal<v8::String> property = Utils::ToV8String(isolate, name); | 
| + if (property.IsEmpty()) | 
| + 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.
 | 
| v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(UnwrapValue()); | 
| - return JsValue(jsEngine, obj->Get(property)); | 
| + auto maybe = obj->Get(isolate->GetCurrentContext(), property.ToLocalChecked()); | 
| + if (maybe.IsEmpty()) | 
| + 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.
 | 
| + return JsValue(jsEngine, maybe.ToLocalChecked()); | 
| } | 
| void AdblockPlus::JsValue::SetProperty(const std::string& name, v8::Local<v8::Value> val) | 
| { | 
| if (!IsObject()) | 
| throw std::runtime_error("Attempting to set property on a non-object"); | 
| - | 
| - v8::Local<v8::String> property = Utils::ToV8String(jsEngine->GetIsolate(), name); | 
| + 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.
 | 
| + v8::MaybeLocal<v8::String> property = Utils::ToV8String(isolate, name); | 
| + if (property.IsEmpty()) | 
| + throw std::runtime_error("Invalid property name"); | 
| v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(UnwrapValue()); | 
| - obj->Set(property, val); | 
| + obj->Set(property.ToLocalChecked(), val); | 
| } | 
| v8::Local<v8::Value> AdblockPlus::JsValue::UnwrapValue() const | 
| { | 
| return v8::Local<v8::Value>::New(jsEngine->GetIsolate(), *value); | 
| } | 
| void AdblockPlus::JsValue::SetProperty(const std::string& name, const std::string& val) | 
| { | 
| const JsContext context(*jsEngine); | 
| - SetProperty(name, Utils::ToV8String(jsEngine->GetIsolate(), val)); | 
| + auto maybe = Utils::ToV8String(jsEngine->GetIsolate(), val); | 
| + if (maybe.IsEmpty()) | 
| + return; | 
| + SetProperty(name, maybe.ToLocalChecked()); | 
| } | 
| void AdblockPlus::JsValue::SetStringBufferProperty(const std::string& name, const StringBuffer& val) | 
| { | 
| const JsContext context(*jsEngine); | 
| - SetProperty(name, Utils::StringBufferToV8String(jsEngine->GetIsolate(), val)); | 
| + auto maybe = Utils::StringBufferToV8String(jsEngine->GetIsolate(), val); | 
| + if (maybe.IsEmpty()) | 
| + return; | 
| + SetProperty(name, maybe.ToLocalChecked()); | 
| } | 
| void AdblockPlus::JsValue::SetProperty(const std::string& name, int64_t val) | 
| { | 
| const JsContext context(*jsEngine); | 
| SetProperty(name, v8::Number::New(jsEngine->GetIsolate(), val)); | 
| } | 
| @@ -277,19 +291,20 @@ | 
| JsValue JsValue::Call(std::vector<v8::Local<v8::Value>>& args, v8::Local<v8::Object> thisObj) const | 
| { | 
| if (!IsFunction()) | 
| throw std::runtime_error("Attempting to call a non-function"); | 
| if (!thisObj->IsObject()) | 
| throw std::runtime_error("`this` pointer has to be an object"); | 
| + auto isolate = jsEngine->GetIsolate(); | 
| const JsContext context(*jsEngine); | 
| - const v8::TryCatch tryCatch(jsEngine->GetIsolate()); | 
| + const v8::TryCatch tryCatch(isolate); | 
| v8::Local<v8::Function> func = v8::Local<v8::Function>::Cast(UnwrapValue()); | 
| - v8::Local<v8::Value> result = func->Call(thisObj, args.size(), | 
| - args.size() ? &args[0] : nullptr); | 
| - if (tryCatch.HasCaught()) | 
| - throw JsError(jsEngine->GetIsolate(), tryCatch.Exception(), tryCatch.Message()); | 
| + auto result = CHECKED_TO_LOCAL( | 
| + isolate, func->Call( | 
| + isolate->GetCurrentContext(), | 
| + thisObj, args.size(), args.size() ? &args[0] : nullptr), tryCatch); | 
| return JsValue(jsEngine, result); | 
| } |