Index: src/JsValue.cpp |
diff --git a/src/JsValue.cpp b/src/JsValue.cpp |
index d2dda5a5df3981132a8b6956f228d12f0cd2916a..187beecf7a98d99bf206ae51e730da27a3edb239 100644 |
--- a/src/JsValue.cpp |
+++ b/src/JsValue.cpp |
@@ -22,122 +22,129 @@ |
#include "JsError.h" |
#include "Utils.h" |
-AdblockPlus::JsValue::JsValue(AdblockPlus::JsEnginePtr jsEngine, |
- v8::Handle<v8::Value> value) |
- : jsEngine(jsEngine), |
- value(new v8::Persistent<v8::Value>(jsEngine->GetIsolate(), value)) |
+AdblockPlus::JsValue::JsValue(const std::weak_ptr<AdblockPlus::JsEngine>& jsEngine, |
+ v8::Handle<v8::Value> value) |
+ : m_jsEngine(jsEngine) |
+ , m_value(new v8::Persistent<v8::Value>(Utils::lockJsEngine(jsEngine)->GetIsolate(), value)) |
{ |
} |
AdblockPlus::JsValue::JsValue(AdblockPlus::JsValue&& src) |
- : jsEngine(src.jsEngine), |
- value(std::move(src.value)) |
+ : m_jsEngine(move(src.m_jsEngine)), |
+ m_value(std::move(src.m_value)) |
{ |
} |
AdblockPlus::JsValue::~JsValue() |
{ |
- if (value) |
+ try |
+ { |
+ JsContext context(m_jsEngine); |
+ if (m_value) |
+ { |
+ m_value->Dispose(); |
+ m_value.reset(); |
+ } |
+ } |
+ catch (const JsEngine::JsEngineNotAvailableException&) |
{ |
- value->Dispose(); |
- value.reset(); |
} |
} |
bool AdblockPlus::JsValue::IsUndefined() const |
{ |
- const JsContext context(jsEngine); |
- return UnwrapValue()->IsUndefined(); |
+ JsContext context(m_jsEngine); |
+ return UnwrapValue(context.jsEngine())->IsUndefined(); |
} |
bool AdblockPlus::JsValue::IsNull() const |
{ |
- const JsContext context(jsEngine); |
- return UnwrapValue()->IsNull(); |
+ JsContext context(m_jsEngine); |
+ return UnwrapValue(context.jsEngine())->IsNull(); |
} |
bool AdblockPlus::JsValue::IsString() const |
{ |
- const JsContext context(jsEngine); |
- v8::Local<v8::Value> value = UnwrapValue(); |
+ JsContext context(m_jsEngine); |
+ v8::Local<v8::Value> value = UnwrapValue(context.jsEngine()); |
return value->IsString() || value->IsStringObject(); |
} |
bool AdblockPlus::JsValue::IsNumber() const |
{ |
- const JsContext context(jsEngine); |
- v8::Local<v8::Value> value = UnwrapValue(); |
+ JsContext context(m_jsEngine); |
+ v8::Local<v8::Value> value = UnwrapValue(context.jsEngine()); |
return value->IsNumber() || value->IsNumberObject(); |
} |
bool AdblockPlus::JsValue::IsBool() const |
{ |
- const JsContext context(jsEngine); |
- v8::Local<v8::Value> value = UnwrapValue(); |
+ JsContext context(m_jsEngine); |
+ v8::Local<v8::Value> value = UnwrapValue(context.jsEngine()); |
return value->IsBoolean() || value->IsBooleanObject(); |
} |
bool AdblockPlus::JsValue::IsObject() const |
{ |
- const JsContext context(jsEngine); |
- return UnwrapValue()->IsObject(); |
+ JsContext context(m_jsEngine); |
+ return UnwrapValue(context.jsEngine())->IsObject(); |
} |
bool AdblockPlus::JsValue::IsArray() const |
{ |
- const JsContext context(jsEngine); |
- return UnwrapValue()->IsArray(); |
+ JsContext context(m_jsEngine); |
+ return UnwrapValue(context.jsEngine())->IsArray(); |
} |
bool AdblockPlus::JsValue::IsFunction() const |
{ |
- const JsContext context(jsEngine); |
- return UnwrapValue()->IsFunction(); |
+ JsContext context(m_jsEngine); |
+ return UnwrapValue(context.jsEngine())->IsFunction(); |
} |
std::string AdblockPlus::JsValue::AsString() const |
{ |
- const JsContext context(jsEngine); |
- return Utils::FromV8String(UnwrapValue()); |
+ JsContext context(m_jsEngine); |
+ return Utils::FromV8String(UnwrapValue(context.jsEngine())); |
} |
int64_t AdblockPlus::JsValue::AsInt() const |
{ |
- const JsContext context(jsEngine); |
- return UnwrapValue()->IntegerValue(); |
+ JsContext context(m_jsEngine); |
+ return UnwrapValue(context.jsEngine())->IntegerValue(); |
} |
bool AdblockPlus::JsValue::AsBool() const |
{ |
- const JsContext context(jsEngine); |
- return UnwrapValue()->BooleanValue(); |
+ JsContext context(m_jsEngine); |
+ return UnwrapValue(context.jsEngine())->BooleanValue(); |
} |
AdblockPlus::JsValueList AdblockPlus::JsValue::AsList() const |
{ |
+ JsContext context(m_jsEngine); |
if (!IsArray()) |
throw std::runtime_error("Cannot convert a non-array to list"); |
- const JsContext context(jsEngine); |
JsValueList result; |
- v8::Local<v8::Array> array = v8::Local<v8::Array>::Cast(UnwrapValue()); |
+ v8::Local<v8::Array> array = v8::Local<v8::Array>::Cast(UnwrapValue(context.jsEngine())); |
uint32_t length = array->Length(); |
for (uint32_t i = 0; i < length; i++) |
{ |
v8::Local<v8::Value> item = array->Get(i); |
- result.push_back(JsValuePtr(new JsValue(jsEngine, item))); |
+ result.push_back(JsValuePtr(new JsValue(m_jsEngine, item))); |
} |
return result; |
} |
std::vector<std::string> AdblockPlus::JsValue::GetOwnPropertyNames() const |
{ |
+ JsContext context(m_jsEngine); |
if (!IsObject()) |
throw new std::runtime_error("Attempting to get propert list for a non-object"); |
- const JsContext context(jsEngine); |
- v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(UnwrapValue()); |
- JsValueList properties = JsValuePtr(new JsValue(jsEngine, object->GetOwnPropertyNames()))->AsList(); |
+ v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(UnwrapValue(context.jsEngine())); |
+ JsValueList properties = JsValuePtr(new JsValue(m_jsEngine, object->GetOwnPropertyNames()))->AsList(); |
std::vector<std::string> result; |
for (JsValueList::iterator it = properties.begin(); it != properties.end(); ++it) |
result.push_back((*it)->AsString()); |
@@ -147,99 +154,100 @@ std::vector<std::string> AdblockPlus::JsValue::GetOwnPropertyNames() const |
AdblockPlus::JsValuePtr AdblockPlus::JsValue::GetProperty(const std::string& name) const |
{ |
+ JsContext context(m_jsEngine); |
if (!IsObject()) |
throw new std::runtime_error("Attempting to get property of a non-object"); |
- const JsContext context(jsEngine); |
- v8::Local<v8::String> property = Utils::ToV8String(jsEngine->GetIsolate(), name); |
- v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(UnwrapValue()); |
- return JsValuePtr(new JsValue(jsEngine, obj->Get(property))); |
+ v8::Local<v8::String> property = Utils::ToV8String(context.jsEngine().GetIsolate(), name); |
+ v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(UnwrapValue(context.jsEngine())); |
+ return JsValuePtr(new JsValue(m_jsEngine, obj->Get(property))); |
} |
void AdblockPlus::JsValue::SetProperty(const std::string& name, v8::Handle<v8::Value> val) |
{ |
+ JsContext context(m_jsEngine); |
if (!IsObject()) |
throw new std::runtime_error("Attempting to set property on a non-object"); |
- v8::Local<v8::String> property = Utils::ToV8String(jsEngine->GetIsolate(), name); |
- v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(UnwrapValue()); |
+ v8::Local<v8::String> property = Utils::ToV8String(context.jsEngine().GetIsolate(), name); |
+ v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(UnwrapValue(context.jsEngine())); |
obj->Set(property, val); |
} |
-v8::Local<v8::Value> AdblockPlus::JsValue::UnwrapValue() const |
+v8::Local<v8::Value> AdblockPlus::JsValue::UnwrapValue(JsEngine& jsEngine) const |
{ |
- return v8::Local<v8::Value>::New(jsEngine->GetIsolate(), *value); |
+ return v8::Local<v8::Value>::New(jsEngine.GetIsolate(), *m_value); |
} |
void AdblockPlus::JsValue::SetProperty(const std::string& name, const std::string& val) |
{ |
- const JsContext context(jsEngine); |
- SetProperty(name, Utils::ToV8String(jsEngine->GetIsolate(), val)); |
+ JsContext context(m_jsEngine); |
+ SetProperty(name, Utils::ToV8String(context.jsEngine().GetIsolate(), val)); |
} |
void AdblockPlus::JsValue::SetProperty(const std::string& name, int64_t val) |
{ |
- const JsContext context(jsEngine); |
- SetProperty(name, v8::Number::New(jsEngine->GetIsolate(), val)); |
+ JsContext context(m_jsEngine); |
+ SetProperty(name, v8::Number::New(context.jsEngine().GetIsolate(), val)); |
} |
void AdblockPlus::JsValue::SetProperty(const std::string& name, const JsValuePtr& val) |
{ |
- const JsContext context(jsEngine); |
- SetProperty(name, val->UnwrapValue()); |
+ JsContext context(m_jsEngine); |
+ SetProperty(name, val->UnwrapValue(context.jsEngine())); |
} |
void AdblockPlus::JsValue::SetProperty(const std::string& name, bool val) |
{ |
- const JsContext context(jsEngine); |
+ JsContext context(m_jsEngine); |
SetProperty(name, v8::Boolean::New(val)); |
} |
std::string AdblockPlus::JsValue::GetClass() const |
{ |
+ JsContext context(m_jsEngine); |
if (!IsObject()) |
throw new std::runtime_error("Cannot get constructor of a non-object"); |
- const JsContext context(jsEngine); |
- v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(UnwrapValue()); |
+ v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(UnwrapValue(context.jsEngine())); |
return Utils::FromV8String(obj->GetConstructorName()); |
} |
AdblockPlus::JsValuePtr AdblockPlus::JsValue::Call(const JsValueList& params, JsValuePtr thisPtr) const |
{ |
+ JsContext context(m_jsEngine); |
if (!IsFunction()) |
throw new std::runtime_error("Attempting to call a non-function"); |
- const JsContext context(jsEngine); |
if (!thisPtr) |
{ |
v8::Local<v8::Context> localContext = v8::Local<v8::Context>::New( |
- jsEngine->GetIsolate(), *jsEngine->context); |
- thisPtr = JsValuePtr(new JsValue(jsEngine, localContext->Global())); |
+ context.jsEngine().GetIsolate(), *context.jsEngine().context); |
+ thisPtr = JsValuePtr(new JsValue(m_jsEngine, localContext->Global())); |
} |
if (!thisPtr->IsObject()) |
throw new std::runtime_error("`this` pointer has to be an object"); |
- v8::Local<v8::Object> thisObj = v8::Local<v8::Object>::Cast(thisPtr->UnwrapValue()); |
+ v8::Local<v8::Object> thisObj = v8::Local<v8::Object>::Cast(thisPtr->UnwrapValue(context.jsEngine())); |
std::vector<v8::Handle<v8::Value>> argv; |
for (JsValueList::const_iterator it = params.begin(); it != params.end(); ++it) |
- argv.push_back((*it)->UnwrapValue()); |
+ argv.push_back((*it)->UnwrapValue(context.jsEngine())); |
const v8::TryCatch tryCatch; |
- v8::Local<v8::Function> func = v8::Local<v8::Function>::Cast(UnwrapValue()); |
+ v8::Local<v8::Function> func = v8::Local<v8::Function>::Cast(UnwrapValue(context.jsEngine())); |
v8::Local<v8::Value> result = func->Call(thisObj, argv.size(), |
argv.size() ? &argv.front() : 0); |
if (tryCatch.HasCaught()) |
throw JsError(tryCatch.Exception(), tryCatch.Message()); |
- return JsValuePtr(new JsValue(jsEngine, result)); |
+ return JsValuePtr(new JsValue(m_jsEngine, result)); |
} |
AdblockPlus::JsValuePtr AdblockPlus::JsValue::Call(const JsValue& arg) const |
{ |
- const JsContext context(jsEngine); |
+ JsContext context(m_jsEngine); |
JsValueList params; |
- params.push_back(JsValuePtr(new JsValue(arg.jsEngine, arg.UnwrapValue()))); |
+ params.push_back(JsValuePtr(new JsValue(arg.m_jsEngine, arg.UnwrapValue(context.jsEngine())))); |
return Call(params); |
} |