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: r-value CheckedToLocal Created June 21, 2018, 1:04 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
42 void CheckTryCatch(v8::Isolate* isolate, const v8::TryCatch& tryCatch)
43 {
44 if (tryCatch.HasCaught())
45 throw AdblockPlus::JsError(isolate, tryCatch.Exception(), tryCatch.Message ());
46 } 41 }
47 42
48 class V8Initializer 43 class V8Initializer
49 { 44 {
50 V8Initializer() 45 V8Initializer()
51 : platform{nullptr} 46 : platform{nullptr}
52 { 47 {
53 std::string cmd = "--use_strict"; 48 std::string cmd = "--use_strict";
54 v8::V8::SetFlagsFromString(cmd.c_str(), cmd.length()); 49 v8::V8::SetFlagsFromString(cmd.c_str(), cmd.length());
55 platform = v8::platform::CreateDefaultPlatform(); 50 platform = v8::platform::CreateDefaultPlatform();
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after
185 JsContext context(*this); 180 JsContext context(*this);
186 return JsValue(shared_from_this(), context.GetV8Context()->Global()); 181 return JsValue(shared_from_this(), context.GetV8Context()->Global());
187 } 182 }
188 183
189 AdblockPlus::JsValue AdblockPlus::JsEngine::Evaluate(const std::string& source, 184 AdblockPlus::JsValue AdblockPlus::JsEngine::Evaluate(const std::string& source,
190 const std::string& filename) 185 const std::string& filename)
191 { 186 {
192 const JsContext context(*this); 187 const JsContext context(*this);
193 auto isolate = GetIsolate(); 188 auto isolate = GetIsolate();
194 const v8::TryCatch tryCatch(isolate); 189 const v8::TryCatch tryCatch(isolate);
195 const v8::Local<v8::Script> script = CompileScript(isolate, source, 190 auto script = CHECKED_TO_LOCAL(
196 filename); 191 isolate, CompileScript(isolate, source, filename), tryCatch);
197 CheckTryCatch(isolate, tryCatch); 192 auto result = CHECKED_TO_LOCAL(
198 v8::Local<v8::Value> result = script->Run(); 193 isolate, script->Run(isolate->GetCurrentContext()), tryCatch);
199 CheckTryCatch(isolate, tryCatch);
200 return JsValue(shared_from_this(), result); 194 return JsValue(shared_from_this(), result);
201 } 195 }
202 196
203 void AdblockPlus::JsEngine::SetEventCallback(const std::string& eventName, 197 void AdblockPlus::JsEngine::SetEventCallback(const std::string& eventName,
204 const AdblockPlus::JsEngine::EventCallback& callback) 198 const AdblockPlus::JsEngine::EventCallback& callback)
205 { 199 {
206 if (!callback) 200 if (!callback)
207 { 201 {
208 RemoveEventCallback(eventName); 202 RemoveEventCallback(eventName);
209 return; 203 return;
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
341 list.push_back(JsValue(shared_from_this(), arguments[i])); 335 list.push_back(JsValue(shared_from_this(), arguments[i]));
342 return list; 336 return list;
343 } 337 }
344 338
345 void AdblockPlus::JsEngine::SetGlobalProperty(const std::string& name, 339 void AdblockPlus::JsEngine::SetGlobalProperty(const std::string& name,
346 const AdblockPlus::JsValue& value) 340 const AdblockPlus::JsValue& value)
347 { 341 {
348 auto global = GetGlobalObject(); 342 auto global = GetGlobalObject();
349 global.SetProperty(name, value); 343 global.SetProperty(name, value);
350 } 344 }
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