Index: src/JsEngine.cpp |
=================================================================== |
--- a/src/JsEngine.cpp |
+++ b/src/JsEngine.cpp |
@@ -20,36 +20,55 @@ |
#include "JsContext.h" |
#include "JsError.h" |
#include "Utils.h" |
#include <libplatform/libplatform.h> |
#include <AdblockPlus/Platform.h> |
namespace |
{ |
- v8::Local<v8::Script> CompileScript(v8::Isolate* isolate, |
+ v8::MaybeLocal<v8::Script> CompileScript(v8::Isolate* isolate, |
const std::string& source, const std::string& filename) |
{ |
using AdblockPlus::Utils::ToV8String; |
const v8::Local<v8::String> v8Source = ToV8String(isolate, source); |
if (filename.length()) |
{ |
const v8::Local<v8::String> v8Filename = ToV8String(isolate, filename); |
- return v8::Script::Compile(v8Source, v8Filename); |
+ v8::ScriptOrigin scriptOrigin(v8Filename); |
+ return v8::Script::Compile(isolate->GetCurrentContext(), v8Source, &scriptOrigin); |
} |
else |
- return v8::Script::Compile(v8Source); |
+ return v8::Script::Compile(isolate->GetCurrentContext(), v8Source); |
} |
void CheckTryCatch(v8::Isolate* isolate, const v8::TryCatch& tryCatch) |
{ |
if (tryCatch.HasCaught()) |
throw AdblockPlus::JsError(isolate, tryCatch.Exception(), tryCatch.Message()); |
} |
+ /* |
+ * Check for exception and then that a MaybeLocal<> isn't empty, |
+ * and throw a JsError if it is, otherwise return the Local<> |
+ * Call using the macro %CHECKED_MAYBE to get the location. |
+ */ |
+ template<class T> |
+ v8::Local<T> CheckedToLocal(v8::Isolate* isolate, v8::MaybeLocal<T>& value, |
+ const v8::TryCatch& tryCatch, const char* filename, int line) |
+ { |
+ CheckTryCatch(isolate, tryCatch); |
+ if (value.IsEmpty()) |
+ throw AdblockPlus::JsError("Empty value at ", filename, line); |
+ return value.ToLocalChecked(); |
+ } |
+ |
+#define CHECKED_TO_LOCAL(isolate, value, tryCatch) \ |
+ CheckedToLocal(isolate, value, tryCatch, __FILE__, __LINE__) |
+ |
class V8Initializer |
{ |
V8Initializer() |
: platform{nullptr} |
{ |
std::string cmd = "--use_strict"; |
v8::V8::SetFlagsFromString(cmd.c_str(), cmd.length()); |
platform = v8::platform::CreateDefaultPlatform(); |
@@ -187,22 +206,20 @@ |
} |
AdblockPlus::JsValue AdblockPlus::JsEngine::Evaluate(const std::string& source, |
const std::string& filename) |
{ |
const JsContext context(*this); |
auto isolate = GetIsolate(); |
const v8::TryCatch tryCatch(isolate); |
- const v8::Local<v8::Script> script = CompileScript(isolate, source, |
- filename); |
- CheckTryCatch(isolate, tryCatch); |
- v8::Local<v8::Value> result = script->Run(); |
- CheckTryCatch(isolate, tryCatch); |
- return JsValue(shared_from_this(), result); |
+ v8::MaybeLocal<v8::Script> script = CompileScript(isolate, source, filename); |
+ v8::MaybeLocal<v8::Value> result = CHECKED_TO_LOCAL( |
+ isolate, script, tryCatch)->Run(isolate->GetCurrentContext()); |
+ return JsValue(shared_from_this(), CHECKED_TO_LOCAL(isolate, result, tryCatch)); |
} |
void AdblockPlus::JsEngine::SetEventCallback(const std::string& eventName, |
const AdblockPlus::JsEngine::EventCallback& callback) |
{ |
if (!callback) |
{ |
RemoveEventCallback(eventName); |