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

Side by Side Diff: src/JsEngine.cpp

Issue 29810586: Issue 6526 - Use the maybe version of Compile() and Run() (Closed) Base URL: https://hg.adblockplus.org/libadblockplus/
Patch Set: Move CheckedToLocal() to Utils. Created June 20, 2018, 6:07 p.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 | « no previous file | src/JsError.h » ('j') | no next file with comments »
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
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 #include <AdblockPlus.h> 18 #include <AdblockPlus.h>
19 #include "GlobalJsObject.h" 19 #include "GlobalJsObject.h"
20 #include "JsContext.h" 20 #include "JsContext.h"
21 #include "JsError.h" 21 #include "JsError.h"
22 #include "Utils.h" 22 #include "Utils.h"
23 #include <libplatform/libplatform.h> 23 #include <libplatform/libplatform.h>
24 #include <AdblockPlus/Platform.h> 24 #include <AdblockPlus/Platform.h>
25 25
26 namespace 26 namespace
27 { 27 {
28 v8::Local<v8::Script> CompileScript(v8::Isolate* isolate, 28 v8::MaybeLocal<v8::Script> CompileScript(v8::Isolate* isolate,
29 const std::string& source, const std::string& filename) 29 const std::string& source, const std::string& filename)
30 { 30 {
31 using AdblockPlus::Utils::ToV8String; 31 using AdblockPlus::Utils::ToV8String;
32 const v8::Local<v8::String> v8Source = ToV8String(isolate, source); 32 const v8::Local<v8::String> v8Source = ToV8String(isolate, source);
33 if (filename.length()) 33 if (filename.length())
34 { 34 {
35 const v8::Local<v8::String> v8Filename = ToV8String(isolate, filename); 35 const v8::Local<v8::String> v8Filename = ToV8String(isolate, filename);
36 return v8::Script::Compile(v8Source, v8Filename); 36 v8::ScriptOrigin scriptOrigin(v8Filename);
37 return v8::Script::Compile(isolate->GetCurrentContext(), v8Source, &script Origin);
37 } 38 }
38 else 39 else
39 return v8::Script::Compile(v8Source); 40 return v8::Script::Compile(isolate->GetCurrentContext(), v8Source);
40 } 41 }
41 42
42 void CheckTryCatch(v8::Isolate* isolate, const v8::TryCatch& tryCatch) 43 void CheckTryCatch(v8::Isolate* isolate, const v8::TryCatch& tryCatch)
43 { 44 {
44 if (tryCatch.HasCaught()) 45 if (tryCatch.HasCaught())
45 throw AdblockPlus::JsError(isolate, tryCatch.Exception(), tryCatch.Message ()); 46 throw AdblockPlus::JsError(isolate, tryCatch.Exception(), tryCatch.Message ());
46 } 47 }
47 48
49 /*
50 * Check for exception and then that a MaybeLocal<> isn't empty,
51 * and throw a JsError if it is, otherwise return the Local<>
52 * Call using the macro %CHECKED_MAYBE to get the location.
53 */
54 template<class T>
55 v8::Local<T> CheckedToLocal(v8::Isolate* isolate, v8::MaybeLocal<T>& value,
56 const v8::TryCatch& tryCatch, const char* filename, int line)
57 {
58 CheckTryCatch(isolate, tryCatch);
59 if (value.IsEmpty())
60 throw AdblockPlus::JsError("Empty value at ", filename, line);
61 return value.ToLocalChecked();
62 }
63
64 #define CHECKED_TO_LOCAL(isolate, value, tryCatch) \
65 CheckedToLocal(isolate, value, tryCatch, __FILE__, __LINE__)
66
48 class V8Initializer 67 class V8Initializer
49 { 68 {
50 V8Initializer() 69 V8Initializer()
51 : platform{nullptr} 70 : platform{nullptr}
52 { 71 {
53 std::string cmd = "--use_strict"; 72 std::string cmd = "--use_strict";
54 v8::V8::SetFlagsFromString(cmd.c_str(), cmd.length()); 73 v8::V8::SetFlagsFromString(cmd.c_str(), cmd.length());
55 platform = v8::platform::CreateDefaultPlatform(); 74 platform = v8::platform::CreateDefaultPlatform();
56 v8::V8::InitializePlatform(platform); 75 v8::V8::InitializePlatform(platform);
57 v8::V8::Initialize(); 76 v8::V8::Initialize();
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after
185 JsContext context(*this); 204 JsContext context(*this);
186 return JsValue(shared_from_this(), context.GetV8Context()->Global()); 205 return JsValue(shared_from_this(), context.GetV8Context()->Global());
187 } 206 }
188 207
189 AdblockPlus::JsValue AdblockPlus::JsEngine::Evaluate(const std::string& source, 208 AdblockPlus::JsValue AdblockPlus::JsEngine::Evaluate(const std::string& source,
190 const std::string& filename) 209 const std::string& filename)
191 { 210 {
192 const JsContext context(*this); 211 const JsContext context(*this);
193 auto isolate = GetIsolate(); 212 auto isolate = GetIsolate();
194 const v8::TryCatch tryCatch(isolate); 213 const v8::TryCatch tryCatch(isolate);
195 const v8::Local<v8::Script> script = CompileScript(isolate, source, 214 v8::MaybeLocal<v8::Script> script = CompileScript(isolate, source, filename);
196 filename); 215 v8::MaybeLocal<v8::Value> result = CHECKED_TO_LOCAL(
197 CheckTryCatch(isolate, tryCatch); 216 isolate, script, tryCatch)->Run(isolate->GetCurrentContext());
198 v8::Local<v8::Value> result = script->Run(); 217 return JsValue(shared_from_this(), CHECKED_TO_LOCAL(isolate, result, tryCatch) );
199 CheckTryCatch(isolate, tryCatch);
200 return JsValue(shared_from_this(), result);
201 } 218 }
202 219
203 void AdblockPlus::JsEngine::SetEventCallback(const std::string& eventName, 220 void AdblockPlus::JsEngine::SetEventCallback(const std::string& eventName,
204 const AdblockPlus::JsEngine::EventCallback& callback) 221 const AdblockPlus::JsEngine::EventCallback& callback)
205 { 222 {
206 if (!callback) 223 if (!callback)
207 { 224 {
208 RemoveEventCallback(eventName); 225 RemoveEventCallback(eventName);
209 return; 226 return;
210 } 227 }
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
341 list.push_back(JsValue(shared_from_this(), arguments[i])); 358 list.push_back(JsValue(shared_from_this(), arguments[i]));
342 return list; 359 return list;
343 } 360 }
344 361
345 void AdblockPlus::JsEngine::SetGlobalProperty(const std::string& name, 362 void AdblockPlus::JsEngine::SetGlobalProperty(const std::string& name,
346 const AdblockPlus::JsValue& value) 363 const AdblockPlus::JsValue& value)
347 { 364 {
348 auto global = GetGlobalObject(); 365 auto global = GetGlobalObject();
349 global.SetProperty(name, value); 366 global.SetProperty(name, value);
350 } 367 }
OLDNEW
« no previous file with comments | « no previous file | src/JsError.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld