Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: src/JsValue.cpp

Issue 6584950149087232: Issue 1280 - Update v8 (Closed)
Patch Set: get rid of auto and fix friends of JsValue Created Oct. 31, 2014, 5:06 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/JsEngine.cpp ('k') | src/Utils.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/JsValue.cpp
===================================================================
--- a/src/JsValue.cpp
+++ b/src/JsValue.cpp
@@ -16,103 +16,108 @@
*/
#include <vector>
+#include <v8.h>
#include <AdblockPlus.h>
#include "JsContext.h"
#include "JsError.h"
#include "Utils.h"
-AdblockPlus::JsValue::JsValue(AdblockPlus::JsEnginePtr jsEngine,
- v8::Handle<v8::Value> value)
+using namespace AdblockPlus;
+
+JsValue::JsValue(const JsEnginePtr& jsEngine, v8::Handle<v8::Value> value)
: jsEngine(jsEngine),
- value(jsEngine->isolate, value)
+ value(jsEngine->isolate, value)
{
}
-AdblockPlus::JsValue::JsValue(AdblockPlus::JsValuePtr value)
- : jsEngine(value->jsEngine),
- value(value->value)
+JsValue::JsValue(const JsValuePtr& value)
+ : jsEngine(value->jsEngine),
+ value(value->value)
{
}
-AdblockPlus::JsValue::~JsValue()
+JsValue::~JsValue()
{
}
-bool AdblockPlus::JsValue::IsUndefined() const
+bool JsValue::IsUndefined() const
{
const JsContext context(jsEngine);
- return value->IsUndefined();
+ return UnwrapValue()->IsUndefined();
}
-bool AdblockPlus::JsValue::IsNull() const
+bool JsValue::IsNull() const
{
const JsContext context(jsEngine);
- return value->IsNull();
+ return UnwrapValue()->IsNull();
}
-bool AdblockPlus::JsValue::IsString() const
+bool JsValue::IsString() const
{
const JsContext context(jsEngine);
+ v8::Local<v8::Value> value = UnwrapValue();
return value->IsString() || value->IsStringObject();
}
-bool AdblockPlus::JsValue::IsNumber() const
+bool JsValue::IsNumber() const
{
const JsContext context(jsEngine);
+ v8::Local<v8::Value> value = UnwrapValue();
return value->IsNumber() || value->IsNumberObject();
}
-bool AdblockPlus::JsValue::IsBool() const
+bool JsValue::IsBool() const
{
const JsContext context(jsEngine);
+ v8::Local<v8::Value> value = UnwrapValue();
return value->IsBoolean() || value->IsBooleanObject();
}
-bool AdblockPlus::JsValue::IsObject() const
+bool JsValue::IsObject() const
{
const JsContext context(jsEngine);
- return value->IsObject();
+ return UnwrapValue()->IsObject();
}
-bool AdblockPlus::JsValue::IsArray() const
+bool JsValue::IsArray() const
{
const JsContext context(jsEngine);
- return value->IsArray();
+ return UnwrapValue()->IsArray();
}
-bool AdblockPlus::JsValue::IsFunction() const
+bool JsValue::IsFunction() const
{
const JsContext context(jsEngine);
- return value->IsFunction();
+ return UnwrapValue()->IsFunction();
}
-std::string AdblockPlus::JsValue::AsString() const
+std::string JsValue::AsString() const
{
const JsContext context(jsEngine);
- return Utils::FromV8String(value);
+ return Utils::FromV8String(UnwrapValue());
}
-int64_t AdblockPlus::JsValue::AsInt() const
+int64_t JsValue::AsInt() const
{
const JsContext context(jsEngine);
- return value->IntegerValue();
+ return UnwrapValue()->IntegerValue();
}
-bool AdblockPlus::JsValue::AsBool() const
+bool JsValue::AsBool() const
{
const JsContext context(jsEngine);
- return value->BooleanValue();
+ return UnwrapValue()->BooleanValue();
}
-AdblockPlus::JsValueList AdblockPlus::JsValue::AsList() const
+JsValueList JsValue::AsList() const
{
if (!IsArray())
throw std::runtime_error("Cannot convert a non-array to list");
const JsContext context(jsEngine);
JsValueList result;
- v8::Persistent<v8::Array> array = v8::Persistent<v8::Array>::Cast<v8::Value>(value);
+ v8::Local<v8::Array> array = v8::Local<v8::Array>::Cast(UnwrapValue());
uint32_t length = array->Length();
for (uint32_t i = 0; i < length; i++)
{
@@ -122,102 +127,108 @@
return result;
}
-std::vector<std::string> AdblockPlus::JsValue::GetOwnPropertyNames() const
+std::vector<std::string> JsValue::GetOwnPropertyNames() const
{
if (!IsObject())
throw new std::runtime_error("Attempting to get propert list for a non-object");
const JsContext context(jsEngine);
- const v8::Persistent<v8::Object> object = v8::Persistent<v8::Object>::Cast<v8::Value>(value);
+ std::vector<std::string> result;
+ v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(UnwrapValue());
JsValueList properties = JsValuePtr(new JsValue(jsEngine, object->GetOwnPropertyNames()))->AsList();
- std::vector<std::string> result;
for (JsValueList::iterator it = properties.begin(); it != properties.end(); ++it)
result.push_back((*it)->AsString());
return result;
}
-AdblockPlus::JsValuePtr AdblockPlus::JsValue::GetProperty(const std::string& name) const
+JsValuePtr JsValue::GetProperty(const std::string& name) const
{
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(name);
- v8::Persistent<v8::Object> obj = v8::Persistent<v8::Object>::Cast<v8::Value>(value);
+ v8::Local<v8::String> property = Utils::ToV8String(jsEngine->isolate, name);
+ v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(UnwrapValue());
return JsValuePtr(new JsValue(jsEngine, obj->Get(property)));
}
-void AdblockPlus::JsValue::SetProperty(const std::string& name, v8::Handle<v8::Value> val)
+void JsValue::SetProperty(const std::string& name, v8::Handle<v8::Value> val)
{
if (!IsObject())
throw new std::runtime_error("Attempting to set property on a non-object");
- v8::Local<v8::String> property = Utils::ToV8String(name);
- v8::Persistent<v8::Object> obj = v8::Persistent<v8::Object>::Cast<v8::Value>(value);
+ v8::Local<v8::String> property = Utils::ToV8String(jsEngine->isolate, name);
+ v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(UnwrapValue());
obj->Set(property, val);
}
-void AdblockPlus::JsValue::SetProperty(const std::string& name, const std::string& val)
+v8::Local<v8::Value> JsValue::UnwrapValue() const
+{
+ return v8::Local<v8::Value>::New(jsEngine->isolate, value);
+}
+
+void JsValue::SetProperty(const std::string& name, const std::string& val)
{
const JsContext context(jsEngine);
- SetProperty(name, Utils::ToV8String(val));
+ SetProperty(name, Utils::ToV8String(jsEngine->isolate, val));
}
-void AdblockPlus::JsValue::SetProperty(const std::string& name, int64_t val)
+void JsValue::SetProperty(const std::string& name, int64_t val)
{
const JsContext context(jsEngine);
- SetProperty(name, v8::Number::New(val));
+ SetProperty(name, v8::Number::New(jsEngine->isolate, val));
}
-void AdblockPlus::JsValue::SetProperty(const std::string& name, JsValuePtr val)
+void JsValue::SetProperty(const std::string& name, const JsValuePtr& val)
{
const JsContext context(jsEngine);
- SetProperty(name, val->value);
+ SetProperty(name, val->UnwrapValue());
}
-void AdblockPlus::JsValue::SetProperty(const std::string& name, bool val)
+void JsValue::SetProperty(const std::string& name, bool val)
{
const JsContext context(jsEngine);
SetProperty(name, v8::Boolean::New(val));
}
-std::string AdblockPlus::JsValue::GetClass() const
+std::string JsValue::GetClass() const
{
if (!IsObject())
throw new std::runtime_error("Cannot get constructor of a non-object");
const JsContext context(jsEngine);
- v8::Persistent<v8::Object> obj = v8::Persistent<v8::Object>::Cast<v8::Value>(value);
+ v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(UnwrapValue());
return Utils::FromV8String(obj->GetConstructorName());
}
-AdblockPlus::JsValuePtr AdblockPlus::JsValue::Call(
- const JsValueList& params,
- AdblockPlus::JsValuePtr thisPtr) const
+JsValuePtr JsValue::Call(const JsValueList& params, JsValuePtr thisPtr) const
{
+ const JsContext context(jsEngine);
+
if (!IsFunction())
throw new std::runtime_error("Attempting to call a non-function");
- const JsContext context(jsEngine);
-
if (!thisPtr)
- thisPtr = JsValuePtr(new JsValue(jsEngine, jsEngine->context->Global()));
+ {
+ v8::Local<v8::Context> localContext = v8::Local<v8::Context>::New(jsEngine->isolate, jsEngine->context);
+ thisPtr = JsValuePtr(new JsValue(jsEngine, localContext->Global()));
+ }
if (!thisPtr->IsObject())
throw new std::runtime_error("`this` pointer has to be an object");
- v8::Persistent<v8::Object> thisObj = v8::Persistent<v8::Object>::Cast<v8::Value>(thisPtr->value);
+ v8::Local<v8::Object> thisObj = v8::Local<v8::Object>::Cast(thisPtr->UnwrapValue());
- std::vector<v8::Handle<v8::Value> > argv;
+ std::vector<v8::Handle<v8::Value>> argv;
for (JsValueList::const_iterator it = params.begin(); it != params.end(); ++it)
- argv.push_back((*it)->value);
+ argv.push_back((*it)->UnwrapValue());
const v8::TryCatch tryCatch;
- v8::Persistent<v8::Function> func = v8::Persistent<v8::Function>::Cast<v8::Value>(value);
+ v8::Local<v8::Function> func = v8::Local<v8::Function>::Cast(UnwrapValue());
v8::Local<v8::Value> result = func->Call(thisObj, argv.size(),
argv.size() ? &argv.front() : 0);
if (tryCatch.HasCaught())
- throw AdblockPlus::JsError(tryCatch.Exception(), tryCatch.Message());
+ throw JsError(tryCatch.Exception(), tryCatch.Message());
return JsValuePtr(new JsValue(jsEngine, result));
}
« no previous file with comments | « src/JsEngine.cpp ('k') | src/Utils.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld