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

Delta Between Two Patch Sets: src/JsEngine.cpp

Issue 29810586: Issue 6526 - Use the maybe version of Compile() and Run() (Closed) Base URL: https://hg.adblockplus.org/libadblockplus/
Left Patch Set: Move CheckedToLocal() to Utils. Created June 20, 2018, 6:07 p.m.
Right 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:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | src/JsError.h » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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 22 matching lines...) Expand all
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 v8::ScriptOrigin scriptOrigin(v8Filename); 36 v8::ScriptOrigin scriptOrigin(v8Filename);
37 return v8::Script::Compile(isolate->GetCurrentContext(), v8Source, &script Origin); 37 return v8::Script::Compile(isolate->GetCurrentContext(), v8Source, &script Origin);
38 } 38 }
39 else 39 else
40 return v8::Script::Compile(isolate->GetCurrentContext(), v8Source); 40 return v8::Script::Compile(isolate->GetCurrentContext(), v8Source);
41 } 41 }
42 42
43 void CheckTryCatch(v8::Isolate* isolate, const v8::TryCatch& tryCatch)
44 {
45 if (tryCatch.HasCaught())
46 throw AdblockPlus::JsError(isolate, tryCatch.Exception(), tryCatch.Message ());
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
67 class V8Initializer 43 class V8Initializer
68 { 44 {
69 V8Initializer() 45 V8Initializer()
70 : platform{nullptr} 46 : platform{nullptr}
71 { 47 {
72 std::string cmd = "--use_strict"; 48 std::string cmd = "--use_strict";
73 v8::V8::SetFlagsFromString(cmd.c_str(), cmd.length()); 49 v8::V8::SetFlagsFromString(cmd.c_str(), cmd.length());
74 platform = v8::platform::CreateDefaultPlatform(); 50 platform = v8::platform::CreateDefaultPlatform();
75 v8::V8::InitializePlatform(platform); 51 v8::V8::InitializePlatform(platform);
76 v8::V8::Initialize(); 52 v8::V8::Initialize();
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after
204 JsContext context(*this); 180 JsContext context(*this);
205 return JsValue(shared_from_this(), context.GetV8Context()->Global()); 181 return JsValue(shared_from_this(), context.GetV8Context()->Global());
206 } 182 }
207 183
208 AdblockPlus::JsValue AdblockPlus::JsEngine::Evaluate(const std::string& source, 184 AdblockPlus::JsValue AdblockPlus::JsEngine::Evaluate(const std::string& source,
209 const std::string& filename) 185 const std::string& filename)
210 { 186 {
211 const JsContext context(*this); 187 const JsContext context(*this);
212 auto isolate = GetIsolate(); 188 auto isolate = GetIsolate();
213 const v8::TryCatch tryCatch(isolate); 189 const v8::TryCatch tryCatch(isolate);
214 v8::MaybeLocal<v8::Script> script = CompileScript(isolate, source, filename); 190 auto script = CHECKED_TO_LOCAL(
215 v8::MaybeLocal<v8::Value> result = CHECKED_TO_LOCAL( 191 isolate, CompileScript(isolate, source, filename), tryCatch);
216 isolate, script, tryCatch)->Run(isolate->GetCurrentContext()); 192 auto result = CHECKED_TO_LOCAL(
217 return JsValue(shared_from_this(), CHECKED_TO_LOCAL(isolate, result, tryCatch) ); 193 isolate, script->Run(isolate->GetCurrentContext()), tryCatch);
194 return JsValue(shared_from_this(), result);
218 } 195 }
219 196
220 void AdblockPlus::JsEngine::SetEventCallback(const std::string& eventName, 197 void AdblockPlus::JsEngine::SetEventCallback(const std::string& eventName,
221 const AdblockPlus::JsEngine::EventCallback& callback) 198 const AdblockPlus::JsEngine::EventCallback& callback)
222 { 199 {
223 if (!callback) 200 if (!callback)
224 { 201 {
225 RemoveEventCallback(eventName); 202 RemoveEventCallback(eventName);
226 return; 203 return;
227 } 204 }
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
358 list.push_back(JsValue(shared_from_this(), arguments[i])); 335 list.push_back(JsValue(shared_from_this(), arguments[i]));
359 return list; 336 return list;
360 } 337 }
361 338
362 void AdblockPlus::JsEngine::SetGlobalProperty(const std::string& name, 339 void AdblockPlus::JsEngine::SetGlobalProperty(const std::string& name,
363 const AdblockPlus::JsValue& value) 340 const AdblockPlus::JsValue& value)
364 { 341 {
365 auto global = GetGlobalObject(); 342 auto global = GetGlobalObject();
366 global.SetProperty(name, value); 343 global.SetProperty(name, value);
367 } 344 }
LEFTRIGHT
« no previous file | src/JsError.h » ('j') | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

Powered by Google App Engine
This is Rietveld