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 22 matching lines...) Expand all Loading... | |
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 Loading... | |
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, | 190 auto script = CHECKED_TO_LOCAL( |
sergei
2018/06/20 16:32:59
what do you think about calling CHECKED_TO_LOCAL a
hub
2018/06/20 16:41:47
I can't. CheckedToLocal() 2nd argument is expected
sergei
2018/06/20 19:01:50
Can the argument be r-value, v8::MaybeLocal<T>&& v
hub
2018/06/21 01:13:43
Yes.
But I don't find the result very readable. An
sergei
2018/06/21 08:45:45
If one has to use the variable more than one time
hub
2018/06/21 13:02:17
Done.
| |
215 filename); | 191 isolate, CompileScript(isolate, source, filename), tryCatch); |
216 v8::MaybeLocal<v8::Value> result = CHECKED_TO_LOCAL( | 192 auto result = CHECKED_TO_LOCAL( |
217 isolate, script, tryCatch)->Run(isolate->GetCurrentContext()); | 193 isolate, script->Run(isolate->GetCurrentContext()), tryCatch); |
218 return JsValue(shared_from_this(), CHECKED_TO_LOCAL(isolate, result, tryCatch) ); | 194 return JsValue(shared_from_this(), result); |
219 } | 195 } |
220 | 196 |
221 void AdblockPlus::JsEngine::SetEventCallback(const std::string& eventName, | 197 void AdblockPlus::JsEngine::SetEventCallback(const std::string& eventName, |
222 const AdblockPlus::JsEngine::EventCallback& callback) | 198 const AdblockPlus::JsEngine::EventCallback& callback) |
223 { | 199 { |
224 if (!callback) | 200 if (!callback) |
225 { | 201 { |
226 RemoveEventCallback(eventName); | 202 RemoveEventCallback(eventName); |
227 return; | 203 return; |
228 } | 204 } |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
359 list.push_back(JsValue(shared_from_this(), arguments[i])); | 335 list.push_back(JsValue(shared_from_this(), arguments[i])); |
360 return list; | 336 return list; |
361 } | 337 } |
362 | 338 |
363 void AdblockPlus::JsEngine::SetGlobalProperty(const std::string& name, | 339 void AdblockPlus::JsEngine::SetGlobalProperty(const std::string& name, |
364 const AdblockPlus::JsValue& value) | 340 const AdblockPlus::JsValue& value) |
365 { | 341 { |
366 auto global = GetGlobalObject(); | 342 auto global = GetGlobalObject(); |
367 global.SetProperty(name, value); | 343 global.SetProperty(name, value); |
368 } | 344 } |
LEFT | RIGHT |