Index: src/JsEngine.cpp |
=================================================================== |
--- a/src/JsEngine.cpp |
+++ b/src/JsEngine.cpp |
@@ -20,37 +20,46 @@ |
#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) |
+ /* |
+ * Check that a Maybe<> isn't empty, and throw a JsValueError if it is. |
+ * Call using the macro %CHECK_MAYBE to get the location. |
+ */ |
+ template<class T> |
+ void CheckMaybe(v8::Isolate* isolate, const v8::MaybeLocal<T>& value, |
+ const char* filename, int line) |
{ |
- if (tryCatch.HasCaught()) |
- throw AdblockPlus::JsError(isolate, |
- tryCatch.Exception(), tryCatch.Message()); |
+ if (value.IsEmpty()) |
+ throw AdblockPlus::JsError("Empty value at ", filename, line); |
} |
+#define CHECK_MAYBE(isolate, value) \ |
+ CheckMaybe(isolate, value, __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 +196,23 @@ |
return JsValue(shared_from_this(), context.GetV8Context()->Global()); |
} |
AdblockPlus::JsValue AdblockPlus::JsEngine::Evaluate(const std::string& source, |
const std::string& filename) |
{ |
const JsContext context(*this); |
const v8::TryCatch tryCatch(GetIsolate()); |
- const v8::Local<v8::Script> script = CompileScript(GetIsolate(), source, |
+ v8::MaybeLocal<v8::Script> script = CompileScript(GetIsolate(), source, |
filename); |
- CheckTryCatch(GetIsolate(), tryCatch); |
- v8::Local<v8::Value> result = script->Run(); |
- CheckTryCatch(GetIsolate(), tryCatch); |
- return JsValue(shared_from_this(), result); |
+ CHECK_MAYBE(GetIsolate(), script); |
+ v8::MaybeLocal<v8::Value> result = script.ToLocalChecked()->Run( |
+ GetIsolate()->GetCurrentContext()); |
+ CHECK_MAYBE(GetIsolate(), result); |
+ return JsValue(shared_from_this(), result.ToLocalChecked()); |
sergei
2018/06/20 15:09:55
I think it's very important to keep the exception
hub
2018/06/20 16:09:21
Done.
|
} |
void AdblockPlus::JsEngine::SetEventCallback(const std::string& eventName, |
const AdblockPlus::JsEngine::EventCallback& callback) |
{ |
if (!callback) |
{ |
RemoveEventCallback(eventName); |