Index: src/JsValue.cpp |
=================================================================== |
--- a/src/JsValue.cpp |
+++ b/src/JsValue.cpp |
@@ -32,25 +32,44 @@ |
} |
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)); |
+} |
+ |
AdblockPlus::JsValue::~JsValue() |
{ |
if (value) |
{ |
+ const JsContext context(jsEngine); |
sergei
2017/04/20 07:42:52
I think it deserves a separate commit. Do you mind
hub
2017/04/20 12:41:01
will do.
|
value->Dispose(); |
value.reset(); |
} |
} |
+JsValue& AdblockPlus::JsValue::operator=(const JsValue& src) |
+{ |
+ const JsContext context(src.jsEngine); |
+ if (value) |
+ value->Dispose(); |
+ jsEngine = 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 +141,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 +187,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 +221,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); |