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 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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 auto isolate = jsEngine->GetIsolate(); | 186 auto isolate = jsEngine->GetIsolate(); |
187 v8::MaybeLocal<v8::String> property = Utils::ToV8String(isolate, name); | 187 v8::Local<v8::String> property = CHECKED_TO_LOCAL( |
188 if (property.IsEmpty()) | 188 isolate, Utils::ToV8String(isolate, name)); |
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.
| |
190 v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(UnwrapValue()); | 189 v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(UnwrapValue()); |
191 auto maybe = obj->Get(isolate->GetCurrentContext(), property.ToLocalChecked()) ; | 190 return JsValue(jsEngine, CHECKED_TO_LOCAL( |
192 if (maybe.IsEmpty()) | 191 isolate, obj->Get(isolate->GetCurrentContext(), property))); |
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()); | |
195 } | 192 } |
196 | 193 |
197 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) |
198 { | 195 { |
199 if (!IsObject()) | 196 if (!IsObject()) |
200 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"); |
201 auto isolate = jsEngine->GetIsolate(); | 198 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.
| |
202 v8::MaybeLocal<v8::String> property = Utils::ToV8String(isolate, name); | 199 |
203 if (property.IsEmpty()) | 200 v8::Local<v8::String> property = CHECKED_TO_LOCAL( |
204 throw std::runtime_error("Invalid property name"); | 201 isolate, Utils::ToV8String(isolate, name)); |
205 v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(UnwrapValue()); | 202 v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(UnwrapValue()); |
206 obj->Set(property.ToLocalChecked(), val); | 203 obj->Set(property, val); |
207 } | 204 } |
208 | 205 |
209 v8::Local<v8::Value> AdblockPlus::JsValue::UnwrapValue() const | 206 v8::Local<v8::Value> AdblockPlus::JsValue::UnwrapValue() const |
210 { | 207 { |
211 return v8::Local<v8::Value>::New(jsEngine->GetIsolate(), *value); | 208 return v8::Local<v8::Value>::New(jsEngine->GetIsolate(), *value); |
212 } | 209 } |
213 | 210 |
214 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) |
215 { | 212 { |
216 const JsContext context(*jsEngine); | 213 const JsContext context(*jsEngine); |
217 auto maybe = Utils::ToV8String(jsEngine->GetIsolate(), val); | 214 auto isolate = jsEngine->GetIsolate(); |
218 if (maybe.IsEmpty()) | 215 |
219 return; | 216 SetProperty(name, CHECKED_TO_LOCAL( |
220 SetProperty(name, maybe.ToLocalChecked()); | 217 isolate, Utils::ToV8String(jsEngine->GetIsolate(), val))); |
221 } | 218 } |
222 | 219 |
223 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) |
224 { | 221 { |
225 const JsContext context(*jsEngine); | 222 const JsContext context(*jsEngine); |
226 auto maybe = Utils::StringBufferToV8String(jsEngine->GetIsolate(), val); | 223 auto isolate = jsEngine->GetIsolate(); |
227 if (maybe.IsEmpty()) | 224 |
228 return; | 225 SetProperty(name, CHECKED_TO_LOCAL( |
229 SetProperty(name, maybe.ToLocalChecked()); | 226 isolate, Utils::StringBufferToV8String(isolate, val))); |
230 } | 227 } |
231 | 228 |
232 void AdblockPlus::JsValue::SetProperty(const std::string& name, int64_t val) | 229 void AdblockPlus::JsValue::SetProperty(const std::string& name, int64_t val) |
233 { | 230 { |
234 const JsContext context(*jsEngine); | 231 const JsContext context(*jsEngine); |
235 SetProperty(name, v8::Number::New(jsEngine->GetIsolate(), val)); | 232 SetProperty(name, v8::Number::New(jsEngine->GetIsolate(), val)); |
236 } | 233 } |
237 | 234 |
238 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) |
239 { | 236 { |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
289 return Call(argv, context.GetV8Context()->Global()); | 286 return Call(argv, context.GetV8Context()->Global()); |
290 } | 287 } |
291 | 288 |
292 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 |
293 { | 290 { |
294 if (!IsFunction()) | 291 if (!IsFunction()) |
295 throw std::runtime_error("Attempting to call a non-function"); | 292 throw std::runtime_error("Attempting to call a non-function"); |
296 if (!thisObj->IsObject()) | 293 if (!thisObj->IsObject()) |
297 throw std::runtime_error("`this` pointer has to be an object"); | 294 throw std::runtime_error("`this` pointer has to be an object"); |
298 | 295 |
299 auto isolate = jsEngine->GetIsolate(); | 296 const JsContext context(*jsEngine); |
300 const JsContext context(*jsEngine); | 297 auto isolate = jsEngine->GetIsolate(); |
301 | 298 |
302 const v8::TryCatch tryCatch(isolate); | 299 const v8::TryCatch tryCatch(isolate); |
303 v8::Local<v8::Function> func = v8::Local<v8::Function>::Cast(UnwrapValue()); | 300 v8::Local<v8::Function> func = v8::Local<v8::Function>::Cast(UnwrapValue()); |
304 auto result = CHECKED_TO_LOCAL( | 301 auto result = CHECKED_TO_LOCAL_WITH_TRY_CATCH( |
305 isolate, func->Call( | 302 isolate, func->Call(isolate->GetCurrentContext(), |
306 isolate->GetCurrentContext(), | |
307 thisObj, args.size(), args.size() ? &args[0] : nullptr), tryCatch); | 303 thisObj, args.size(), args.size() ? &args[0] : nullptr), tryCatch); |
308 | 304 |
309 return JsValue(jsEngine, result); | 305 return JsValue(jsEngine, result); |
310 } | 306 } |
LEFT | RIGHT |