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 | |
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 Loading... |
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 v8::MaybeLocal<v8::Script> script = CompileScript(isolate, source, filename); |
196 filename); | 191 v8::MaybeLocal<v8::Value> result = CHECKED_TO_LOCAL( |
197 CheckTryCatch(isolate, tryCatch); | 192 isolate, script, tryCatch)->Run(isolate->GetCurrentContext()); |
198 v8::Local<v8::Value> result = script->Run(); | 193 return JsValue(shared_from_this(), CHECKED_TO_LOCAL(isolate, result, tryCatch)
); |
199 CheckTryCatch(isolate, tryCatch); | |
200 return JsValue(shared_from_this(), result); | |
201 } | 194 } |
202 | 195 |
203 void AdblockPlus::JsEngine::SetEventCallback(const std::string& eventName, | 196 void AdblockPlus::JsEngine::SetEventCallback(const std::string& eventName, |
204 const AdblockPlus::JsEngine::EventCallback& callback) | 197 const AdblockPlus::JsEngine::EventCallback& callback) |
205 { | 198 { |
206 if (!callback) | 199 if (!callback) |
207 { | 200 { |
208 RemoveEventCallback(eventName); | 201 RemoveEventCallback(eventName); |
209 return; | 202 return; |
210 } | 203 } |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
341 list.push_back(JsValue(shared_from_this(), arguments[i])); | 334 list.push_back(JsValue(shared_from_this(), arguments[i])); |
342 return list; | 335 return list; |
343 } | 336 } |
344 | 337 |
345 void AdblockPlus::JsEngine::SetGlobalProperty(const std::string& name, | 338 void AdblockPlus::JsEngine::SetGlobalProperty(const std::string& name, |
346 const AdblockPlus::JsValue& value) | 339 const AdblockPlus::JsValue& value) |
347 { | 340 { |
348 auto global = GetGlobalObject(); | 341 auto global = GetGlobalObject(); |
349 global.SetProperty(name, value); | 342 global.SetProperty(name, value); |
350 } | 343 } |
OLD | NEW |