OLD | NEW |
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 /* |
| 44 * Check that a Maybe<> isn't empty, and throw a JsValueError if it is. |
| 45 * Call using the macro %CHECK_MAYBE to get the location. |
| 46 */ |
| 47 template<class T> |
| 48 void CheckMaybe(v8::Isolate* isolate, const v8::MaybeLocal<T>& value, |
| 49 const char* filename, int line) |
43 { | 50 { |
44 if (tryCatch.HasCaught()) | 51 if (value.IsEmpty()) |
45 throw AdblockPlus::JsError(isolate, | 52 throw AdblockPlus::JsValueError(isolate, filename, line); |
46 tryCatch.Exception(), tryCatch.Message()); | |
47 } | 53 } |
48 | 54 |
| 55 #define CHECK_MAYBE(isolate, value) \ |
| 56 CheckMaybe(isolate, value, __FILE__, __LINE__); |
| 57 |
49 class V8Initializer | 58 class V8Initializer |
50 { | 59 { |
51 V8Initializer() | 60 V8Initializer() |
52 : platform{nullptr} | 61 : platform{nullptr} |
53 { | 62 { |
54 std::string cmd = "--use_strict"; | 63 std::string cmd = "--use_strict"; |
55 v8::V8::SetFlagsFromString(cmd.c_str(), cmd.length()); | 64 v8::V8::SetFlagsFromString(cmd.c_str(), cmd.length()); |
56 platform = v8::platform::CreateDefaultPlatform(); | 65 platform = v8::platform::CreateDefaultPlatform(); |
57 v8::V8::InitializePlatform(platform); | 66 v8::V8::InitializePlatform(platform); |
58 v8::V8::Initialize(); | 67 v8::V8::Initialize(); |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
185 { | 194 { |
186 JsContext context(*this); | 195 JsContext context(*this); |
187 return JsValue(shared_from_this(), context.GetV8Context()->Global()); | 196 return JsValue(shared_from_this(), context.GetV8Context()->Global()); |
188 } | 197 } |
189 | 198 |
190 AdblockPlus::JsValue AdblockPlus::JsEngine::Evaluate(const std::string& source, | 199 AdblockPlus::JsValue AdblockPlus::JsEngine::Evaluate(const std::string& source, |
191 const std::string& filename) | 200 const std::string& filename) |
192 { | 201 { |
193 const JsContext context(*this); | 202 const JsContext context(*this); |
194 const v8::TryCatch tryCatch(GetIsolate()); | 203 const v8::TryCatch tryCatch(GetIsolate()); |
195 const v8::Local<v8::Script> script = CompileScript(GetIsolate(), source, | 204 v8::MaybeLocal<v8::Script> script = CompileScript(GetIsolate(), source, |
196 filename); | 205 filename); |
197 CheckTryCatch(GetIsolate(), tryCatch); | 206 CHECK_MAYBE(GetIsolate(), script); |
198 v8::Local<v8::Value> result = script->Run(); | 207 v8::MaybeLocal<v8::Value> result = script.ToLocalChecked()->Run( |
199 CheckTryCatch(GetIsolate(), tryCatch); | 208 GetIsolate()->GetCurrentContext()); |
200 return JsValue(shared_from_this(), result); | 209 CHECK_MAYBE(GetIsolate(), result); |
| 210 return JsValue(shared_from_this(), result.ToLocalChecked()); |
201 } | 211 } |
202 | 212 |
203 void AdblockPlus::JsEngine::SetEventCallback(const std::string& eventName, | 213 void AdblockPlus::JsEngine::SetEventCallback(const std::string& eventName, |
204 const AdblockPlus::JsEngine::EventCallback& callback) | 214 const AdblockPlus::JsEngine::EventCallback& callback) |
205 { | 215 { |
206 if (!callback) | 216 if (!callback) |
207 { | 217 { |
208 RemoveEventCallback(eventName); | 218 RemoveEventCallback(eventName); |
209 return; | 219 return; |
210 } | 220 } |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
341 list.push_back(JsValue(shared_from_this(), arguments[i])); | 351 list.push_back(JsValue(shared_from_this(), arguments[i])); |
342 return list; | 352 return list; |
343 } | 353 } |
344 | 354 |
345 void AdblockPlus::JsEngine::SetGlobalProperty(const std::string& name, | 355 void AdblockPlus::JsEngine::SetGlobalProperty(const std::string& name, |
346 const AdblockPlus::JsValue& value) | 356 const AdblockPlus::JsValue& value) |
347 { | 357 { |
348 auto global = GetGlobalObject(); | 358 auto global = GetGlobalObject(); |
349 global.SetProperty(name, value); | 359 global.SetProperty(name, value); |
350 } | 360 } |
OLD | NEW |