Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: src/JsValue.cpp

Issue 29812649: Issue 6526 - *ToV8String() return MaybeLocal<> and check Call() return value (Closed) Base URL: https://hg.adblockplus.org/libadblockplus/
Patch Set: Added tests. Renamed CHECKED_TO_LOCAL. Created July 11, 2018, 2:10 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/JsEngine.cpp ('k') | src/Utils.h » ('j') | src/Utils.h » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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::Local<v8::String> property = CHECKED_TO_LOCAL(
188 isolate, Utils::ToV8String(isolate, name));
187 v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(UnwrapValue()); 189 v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(UnwrapValue());
188 return JsValue(jsEngine, obj->Get(property)); 190 return JsValue(jsEngine, CHECKED_TO_LOCAL(
191 isolate, obj->Get(isolate->GetCurrentContext(), property)));
189 } 192 }
190 193
191 void AdblockPlus::JsValue::SetProperty(const std::string& name, v8::Local<v8::Va lue> val) 194 void AdblockPlus::JsValue::SetProperty(const std::string& name, v8::Local<v8::Va lue> val)
192 { 195 {
193 if (!IsObject()) 196 if (!IsObject())
194 throw std::runtime_error("Attempting to set property on a non-object"); 197 throw std::runtime_error("Attempting to set property on a non-object");
198 auto isolate = jsEngine->GetIsolate();
195 199
196 v8::Local<v8::String> property = Utils::ToV8String(jsEngine->GetIsolate(), nam e); 200 v8::Local<v8::String> property = CHECKED_TO_LOCAL(
201 isolate, Utils::ToV8String(isolate, name));
197 v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(UnwrapValue()); 202 v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(UnwrapValue());
198 obj->Set(property, val); 203 obj->Set(property, val);
199 } 204 }
200 205
201 v8::Local<v8::Value> AdblockPlus::JsValue::UnwrapValue() const 206 v8::Local<v8::Value> AdblockPlus::JsValue::UnwrapValue() const
202 { 207 {
203 return v8::Local<v8::Value>::New(jsEngine->GetIsolate(), *value); 208 return v8::Local<v8::Value>::New(jsEngine->GetIsolate(), *value);
204 } 209 }
205 210
206 void AdblockPlus::JsValue::SetProperty(const std::string& name, const std::strin g& val) 211 void AdblockPlus::JsValue::SetProperty(const std::string& name, const std::strin g& val)
207 { 212 {
208 const JsContext context(*jsEngine); 213 const JsContext context(*jsEngine);
209 SetProperty(name, Utils::ToV8String(jsEngine->GetIsolate(), val)); 214 auto isolate = jsEngine->GetIsolate();
215
216 SetProperty(name, CHECKED_TO_LOCAL(
217 isolate, Utils::ToV8String(jsEngine->GetIsolate(), val)));
210 } 218 }
211 219
212 void AdblockPlus::JsValue::SetStringBufferProperty(const std::string& name, cons t StringBuffer& val) 220 void AdblockPlus::JsValue::SetStringBufferProperty(const std::string& name, cons t StringBuffer& val)
213 { 221 {
214 const JsContext context(*jsEngine); 222 const JsContext context(*jsEngine);
215 SetProperty(name, Utils::StringBufferToV8String(jsEngine->GetIsolate(), val)); 223 auto isolate = jsEngine->GetIsolate();
224
225 SetProperty(name, CHECKED_TO_LOCAL(
226 isolate, Utils::StringBufferToV8String(isolate, val)));
216 } 227 }
217 228
218 void AdblockPlus::JsValue::SetProperty(const std::string& name, int64_t val) 229 void AdblockPlus::JsValue::SetProperty(const std::string& name, int64_t val)
219 { 230 {
220 const JsContext context(*jsEngine); 231 const JsContext context(*jsEngine);
221 SetProperty(name, v8::Number::New(jsEngine->GetIsolate(), val)); 232 SetProperty(name, v8::Number::New(jsEngine->GetIsolate(), val));
222 } 233 }
223 234
224 void AdblockPlus::JsValue::SetProperty(const std::string& name, const JsValue& v al) 235 void AdblockPlus::JsValue::SetProperty(const std::string& name, const JsValue& v al)
225 { 236 {
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
276 } 287 }
277 288
278 JsValue JsValue::Call(std::vector<v8::Local<v8::Value>>& args, v8::Local<v8::Obj ect> thisObj) const 289 JsValue JsValue::Call(std::vector<v8::Local<v8::Value>>& args, v8::Local<v8::Obj ect> thisObj) const
279 { 290 {
280 if (!IsFunction()) 291 if (!IsFunction())
281 throw std::runtime_error("Attempting to call a non-function"); 292 throw std::runtime_error("Attempting to call a non-function");
282 if (!thisObj->IsObject()) 293 if (!thisObj->IsObject())
283 throw std::runtime_error("`this` pointer has to be an object"); 294 throw std::runtime_error("`this` pointer has to be an object");
284 295
285 const JsContext context(*jsEngine); 296 const JsContext context(*jsEngine);
297 auto isolate = jsEngine->GetIsolate();
286 298
287 const v8::TryCatch tryCatch(jsEngine->GetIsolate()); 299 const v8::TryCatch tryCatch(isolate);
288 v8::Local<v8::Function> func = v8::Local<v8::Function>::Cast(UnwrapValue()); 300 v8::Local<v8::Function> func = v8::Local<v8::Function>::Cast(UnwrapValue());
289 v8::Local<v8::Value> result = func->Call(thisObj, args.size(), 301 auto result = CHECKED_TO_LOCAL_WITH_TRY_CATCH(
290 args.size() ? &args[0] : nullptr); 302 isolate, func->Call(isolate->GetCurrentContext(),
291 if (tryCatch.HasCaught()) 303 thisObj, args.size(), args.size() ? &args[0] : nullptr), tryCatch);
292 throw JsError(jsEngine->GetIsolate(), tryCatch.Exception(), tryCatch.Message ());
293 304
294 return JsValue(jsEngine, result); 305 return JsValue(jsEngine, result);
295 } 306 }
OLDNEW
« no previous file with comments | « src/JsEngine.cpp ('k') | src/Utils.h » ('j') | src/Utils.h » ('J')

Powered by Google App Engine
This is Rietveld