| 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 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 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::MaybeLocal<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 auto maybeV8Source = ToV8String(isolate, source); |
| 33 if (maybeV8Source.IsEmpty()) |
| 34 return v8::MaybeLocal<v8::Script>(); |
| 35 const v8::Local<v8::String> v8Source = maybeV8Source.ToLocalChecked(); |
| 33 if (filename.length()) | 36 if (filename.length()) |
| 34 { | 37 { |
| 35 const v8::Local<v8::String> v8Filename = ToV8String(isolate, filename); | 38 auto maybeV8Filename = ToV8String(isolate, filename); |
| 36 v8::ScriptOrigin scriptOrigin(v8Filename); | 39 if (maybeV8Filename.IsEmpty()) |
| 40 return v8::MaybeLocal<v8::Script>(); |
| 41 v8::ScriptOrigin scriptOrigin(maybeV8Filename.ToLocalChecked()); |
| 37 return v8::Script::Compile(isolate->GetCurrentContext(), v8Source, &script
Origin); | 42 return v8::Script::Compile(isolate->GetCurrentContext(), v8Source, &script
Origin); |
| 38 } | 43 } |
| 39 else | 44 else |
| 40 return v8::Script::Compile(isolate->GetCurrentContext(), v8Source); | 45 return v8::Script::Compile(isolate->GetCurrentContext(), v8Source); |
| 41 } | 46 } |
| 42 | 47 |
| 43 class V8Initializer | 48 class V8Initializer |
| 44 { | 49 { |
| 45 V8Initializer() | 50 V8Initializer() |
| 46 : platform{nullptr} | 51 : platform{nullptr} |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 179 JsContext context(*this); | 184 JsContext context(*this); |
| 180 return JsValue(shared_from_this(), context.GetV8Context()->Global()); | 185 return JsValue(shared_from_this(), context.GetV8Context()->Global()); |
| 181 } | 186 } |
| 182 | 187 |
| 183 AdblockPlus::JsValue AdblockPlus::JsEngine::Evaluate(const std::string& source, | 188 AdblockPlus::JsValue AdblockPlus::JsEngine::Evaluate(const std::string& source, |
| 184 const std::string& filename) | 189 const std::string& filename) |
| 185 { | 190 { |
| 186 const JsContext context(*this); | 191 const JsContext context(*this); |
| 187 auto isolate = GetIsolate(); | 192 auto isolate = GetIsolate(); |
| 188 const v8::TryCatch tryCatch(isolate); | 193 const v8::TryCatch tryCatch(isolate); |
| 189 auto script = CHECKED_TO_LOCAL( | 194 auto script = CHECKED_TO_LOCAL_WITH_TRY_CATCH( |
| 190 isolate, CompileScript(isolate, source, filename), tryCatch); | 195 isolate, CompileScript(isolate, source, filename), tryCatch); |
| 191 auto result = CHECKED_TO_LOCAL( | 196 auto result = CHECKED_TO_LOCAL_WITH_TRY_CATCH( |
| 192 isolate, script->Run(isolate->GetCurrentContext()), tryCatch); | 197 isolate, script->Run(isolate->GetCurrentContext()), tryCatch); |
| 193 return JsValue(shared_from_this(), result); | 198 return JsValue(shared_from_this(), result); |
| 194 } | 199 } |
| 195 | 200 |
| 196 void AdblockPlus::JsEngine::SetEventCallback(const std::string& eventName, | 201 void AdblockPlus::JsEngine::SetEventCallback(const std::string& eventName, |
| 197 const AdblockPlus::JsEngine::EventCallback& callback) | 202 const AdblockPlus::JsEngine::EventCallback& callback) |
| 198 { | 203 { |
| 199 if (!callback) | 204 if (!callback) |
| 200 { | 205 { |
| 201 RemoveEventCallback(eventName); | 206 RemoveEventCallback(eventName); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 225 } | 230 } |
| 226 | 231 |
| 227 void AdblockPlus::JsEngine::Gc() | 232 void AdblockPlus::JsEngine::Gc() |
| 228 { | 233 { |
| 229 while (!GetIsolate()->IdleNotificationDeadline(1000)); | 234 while (!GetIsolate()->IdleNotificationDeadline(1000)); |
| 230 } | 235 } |
| 231 | 236 |
| 232 AdblockPlus::JsValue AdblockPlus::JsEngine::NewValue(const std::string& val) | 237 AdblockPlus::JsValue AdblockPlus::JsEngine::NewValue(const std::string& val) |
| 233 { | 238 { |
| 234 const JsContext context(*this); | 239 const JsContext context(*this); |
| 235 return JsValue(shared_from_this(), Utils::ToV8String(GetIsolate(), val)); | 240 auto isolate = GetIsolate(); |
| 241 return JsValue(shared_from_this(), |
| 242 CHECKED_TO_LOCAL(isolate, Utils::ToV8String(isolate, val))); |
| 236 } | 243 } |
| 237 | 244 |
| 238 AdblockPlus::JsValue AdblockPlus::JsEngine::NewValue(int64_t val) | 245 AdblockPlus::JsValue AdblockPlus::JsEngine::NewValue(int64_t val) |
| 239 { | 246 { |
| 240 const JsContext context(*this); | 247 const JsContext context(*this); |
| 241 return JsValue(shared_from_this(), v8::Number::New(GetIsolate(), val)); | 248 return JsValue(shared_from_this(), v8::Number::New(GetIsolate(), val)); |
| 242 } | 249 } |
| 243 | 250 |
| 244 AdblockPlus::JsValue AdblockPlus::JsEngine::NewValue(bool val) | 251 AdblockPlus::JsValue AdblockPlus::JsEngine::NewValue(bool val) |
| 245 { | 252 { |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 334 list.push_back(JsValue(shared_from_this(), arguments[i])); | 341 list.push_back(JsValue(shared_from_this(), arguments[i])); |
| 335 return list; | 342 return list; |
| 336 } | 343 } |
| 337 | 344 |
| 338 void AdblockPlus::JsEngine::SetGlobalProperty(const std::string& name, | 345 void AdblockPlus::JsEngine::SetGlobalProperty(const std::string& name, |
| 339 const AdblockPlus::JsValue& value) | 346 const AdblockPlus::JsValue& value) |
| 340 { | 347 { |
| 341 auto global = GetGlobalObject(); | 348 auto global = GetGlobalObject(); |
| 342 global.SetProperty(name, value); | 349 global.SetProperty(name, value); |
| 343 } | 350 } |
| OLD | NEW |