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 /* | |
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) | |
50 { | |
51 if (value.IsEmpty()) | |
52 throw AdblockPlus::JsError("Empty value at ", filename, line); | |
53 } | |
54 | |
55 #define CHECK_MAYBE(isolate, value) \ | |
56 CheckMaybe(isolate, value, __FILE__, __LINE__); | |
57 | |
58 class V8Initializer | 43 class V8Initializer |
59 { | 44 { |
60 V8Initializer() | 45 V8Initializer() |
61 : platform{nullptr} | 46 : platform{nullptr} |
62 { | 47 { |
63 std::string cmd = "--use_strict"; | 48 std::string cmd = "--use_strict"; |
64 v8::V8::SetFlagsFromString(cmd.c_str(), cmd.length()); | 49 v8::V8::SetFlagsFromString(cmd.c_str(), cmd.length()); |
65 platform = v8::platform::CreateDefaultPlatform(); | 50 platform = v8::platform::CreateDefaultPlatform(); |
66 v8::V8::InitializePlatform(platform); | 51 v8::V8::InitializePlatform(platform); |
67 v8::V8::Initialize(); | 52 v8::V8::Initialize(); |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
193 AdblockPlus::JsValue AdblockPlus::JsEngine::GetGlobalObject() | 178 AdblockPlus::JsValue AdblockPlus::JsEngine::GetGlobalObject() |
194 { | 179 { |
195 JsContext context(*this); | 180 JsContext context(*this); |
196 return JsValue(shared_from_this(), context.GetV8Context()->Global()); | 181 return JsValue(shared_from_this(), context.GetV8Context()->Global()); |
197 } | 182 } |
198 | 183 |
199 AdblockPlus::JsValue AdblockPlus::JsEngine::Evaluate(const std::string& source, | 184 AdblockPlus::JsValue AdblockPlus::JsEngine::Evaluate(const std::string& source, |
200 const std::string& filename) | 185 const std::string& filename) |
201 { | 186 { |
202 const JsContext context(*this); | 187 const JsContext context(*this); |
203 const v8::TryCatch tryCatch(GetIsolate()); | 188 auto isolate = GetIsolate(); |
204 v8::MaybeLocal<v8::Script> script = CompileScript(GetIsolate(), source, | 189 const v8::TryCatch tryCatch(isolate); |
205 filename); | 190 auto script = CHECKED_TO_LOCAL( |
206 CHECK_MAYBE(GetIsolate(), script); | 191 isolate, CompileScript(isolate, source, filename), tryCatch); |
207 v8::MaybeLocal<v8::Value> result = script.ToLocalChecked()->Run( | 192 auto result = CHECKED_TO_LOCAL( |
208 GetIsolate()->GetCurrentContext()); | 193 isolate, script->Run(isolate->GetCurrentContext()), tryCatch); |
209 CHECK_MAYBE(GetIsolate(), result); | 194 return JsValue(shared_from_this(), result); |
210 return JsValue(shared_from_this(), result.ToLocalChecked()); | |
211 } | 195 } |
212 | 196 |
213 void AdblockPlus::JsEngine::SetEventCallback(const std::string& eventName, | 197 void AdblockPlus::JsEngine::SetEventCallback(const std::string& eventName, |
214 const AdblockPlus::JsEngine::EventCallback& callback) | 198 const AdblockPlus::JsEngine::EventCallback& callback) |
215 { | 199 { |
216 if (!callback) | 200 if (!callback) |
217 { | 201 { |
218 RemoveEventCallback(eventName); | 202 RemoveEventCallback(eventName); |
219 return; | 203 return; |
220 } | 204 } |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
351 list.push_back(JsValue(shared_from_this(), arguments[i])); | 335 list.push_back(JsValue(shared_from_this(), arguments[i])); |
352 return list; | 336 return list; |
353 } | 337 } |
354 | 338 |
355 void AdblockPlus::JsEngine::SetGlobalProperty(const std::string& name, | 339 void AdblockPlus::JsEngine::SetGlobalProperty(const std::string& name, |
356 const AdblockPlus::JsValue& value) | 340 const AdblockPlus::JsValue& value) |
357 { | 341 { |
358 auto global = GetGlobalObject(); | 342 auto global = GetGlobalObject(); |
359 global.SetProperty(name, value); | 343 global.SetProperty(name, value); |
360 } | 344 } |
LEFT | RIGHT |