Index: src/JsValue.cpp |
=================================================================== |
--- a/src/JsValue.cpp |
+++ b/src/JsValue.cpp |
@@ -32,25 +32,43 @@ |
} |
AdblockPlus::JsValue::JsValue(AdblockPlus::JsValue&& src) |
: jsEngine(src.jsEngine), |
value(std::move(src.value)) |
{ |
} |
+AdblockPlus::JsValue::JsValue(const JsValue& src) |
+ : jsEngine(src.jsEngine) |
+{ |
+ const JsContext context(src.jsEngine); |
+ value.reset(new v8::Persistent<v8::Value>(src.jsEngine->GetIsolate(), *src.value)); |
hub
2017/04/19 17:57:22
Here I wonder if we shouldn't change JsValue::valu
sergei
2017/04/19 18:56:52
I would not like to change it to std::shared_ptr,
hub
2017/04/19 21:56:49
ok.
|
+} |
+ |
AdblockPlus::JsValue::~JsValue() |
{ |
if (value) |
{ |
value->Dispose(); |
value.reset(); |
} |
} |
+JsValue& AdblockPlus::JsValue::operator=(const JsValue& src) |
+{ |
+ if (value) |
+ value->Dispose(); |
sergei
2017/04/19 18:56:52
actually, the lock (JsContext) should be acquired
hub
2017/04/19 21:56:49
ah ok. Will fix this and the destructor.
|
+ jsEngine = src.jsEngine; |
+ const JsContext context(src.jsEngine); |
+ value.reset(new v8::Persistent<v8::Value>(src.jsEngine->GetIsolate(), *src.value)); |
+ |
+ return *this; |
+} |
+ |
bool AdblockPlus::JsValue::IsUndefined() const |
{ |
const JsContext context(jsEngine); |
return UnwrapValue()->IsUndefined(); |
} |
bool AdblockPlus::JsValue::IsNull() const |
{ |
@@ -122,32 +140,32 @@ |
const JsContext context(jsEngine); |
JsValueList result; |
v8::Local<v8::Array> array = v8::Local<v8::Array>::Cast(UnwrapValue()); |
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(JsValue(jsEngine, item)); |
} |
return result; |
} |
std::vector<std::string> AdblockPlus::JsValue::GetOwnPropertyNames() const |
{ |
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(); |
+ JsValueList properties = JsValue(jsEngine, object->GetOwnPropertyNames()).AsList(); |
std::vector<std::string> result; |
for (const auto& property : properties) |
- result.push_back(property->AsString()); |
+ result.push_back(property.AsString()); |
return result; |
} |
AdblockPlus::JsValue AdblockPlus::JsValue::GetProperty(const std::string& name) const |
{ |
if (!IsObject()) |
throw new std::runtime_error("Attempting to get property of a non-object"); |
@@ -168,21 +186,16 @@ |
obj->Set(property, val); |
} |
v8::Local<v8::Value> AdblockPlus::JsValue::UnwrapValue() const |
{ |
return v8::Local<v8::Value>::New(jsEngine->GetIsolate(), *value); |
} |
-JsValue AdblockPlus::JsValue::Clone() const |
-{ |
- return JsValue(jsEngine, UnwrapValue()); |
-} |
- |
void AdblockPlus::JsValue::SetProperty(const std::string& name, const std::string& val) |
{ |
const JsContext context(jsEngine); |
SetProperty(name, Utils::ToV8String(jsEngine->GetIsolate(), val)); |
} |
void AdblockPlus::JsValue::SetProperty(const std::string& name, int64_t val) |
{ |
@@ -207,26 +220,26 @@ |
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()); |
return Utils::FromV8String(obj->GetConstructorName()); |
} |
-JsValue JsValue::Call(const JsConstValueList& params, const JsValuePtr& thisPtr) const |
+JsValue JsValue::Call(const JsValueList& params, const JsValuePtr& thisPtr) const |
{ |
const JsContext context(jsEngine); |
v8::Local<v8::Object> thisObj = thisPtr ? |
v8::Local<v8::Object>::Cast(thisPtr->UnwrapValue()) : |
context.GetV8Context()->Global(); |
std::vector<v8::Handle<v8::Value>> argv; |
for (const auto& param : params) |
- argv.push_back(param->UnwrapValue()); |
+ argv.push_back(param.UnwrapValue()); |
return Call(argv, thisObj); |
} |
JsValue JsValue::Call(const JsValue& arg) const |
{ |
const JsContext context(jsEngine); |