Index: src/JsValue.cpp |
=================================================================== |
new file mode 100644 |
--- /dev/null |
+++ b/src/JsValue.cpp |
@@ -0,0 +1,135 @@ |
+#include <AdblockPlus.h> |
+ |
+namespace |
+{ |
+ v8::Local<v8::String> toV8String(const std::string& str) |
+ { |
+ return v8::String::New(str.c_str(), str.length()); |
+ } |
+} |
+ |
+AdblockPlus::JsValue::JsValue(v8::Isolate* isolate, |
+ v8::Handle<v8::Context> context, v8::Handle<v8::Value> value) |
+ : isolate(isolate), |
+ context(v8::Persistent<v8::Context>::New(isolate, context)), |
+ value(v8::Persistent<v8::Value>::New(isolate, value)) |
+{ |
+} |
+ |
+AdblockPlus::JsValue::~JsValue() |
+{ |
+ context.Dispose(isolate); |
+ value.Dispose(isolate); |
+} |
+ |
+bool AdblockPlus::JsValue::IsUndefined() const |
+{ |
+ return value->IsUndefined(); |
+} |
+ |
+bool AdblockPlus::JsValue::IsNull() const |
+{ |
+ return value->IsNull(); |
+} |
+ |
+bool AdblockPlus::JsValue::IsString() const |
+{ |
+ return value->IsString() || value->IsStringObject(); |
+} |
+ |
+bool AdblockPlus::JsValue::IsNumber() const |
+{ |
+ return value->IsNumber() || value->IsNumberObject(); |
+} |
+ |
+bool AdblockPlus::JsValue::IsBool() const |
+{ |
+ return value->IsBoolean() || value->IsBooleanObject(); |
+} |
+ |
+bool AdblockPlus::JsValue::IsObject() const |
+{ |
+ return value->IsObject(); |
+} |
+ |
+std::string AdblockPlus::JsValue::AsString() const |
+{ |
+ const v8::Locker locker(isolate); |
+ const v8::HandleScope handleScope; |
+ const v8::Context::Scope contextScope(context); |
+ |
+ v8::String::Utf8Value stringValue(value); |
+ if (stringValue.length()) |
+ return std::string(*stringValue, stringValue.length()); |
+ else |
+ return std::string(); |
+} |
+ |
+int64_t AdblockPlus::JsValue::AsInt() const |
+{ |
+ const v8::Locker locker(isolate); |
+ const v8::HandleScope handleScope; |
+ const v8::Context::Scope contextScope(context); |
+ |
+ return value->IntegerValue(); |
+} |
+ |
+bool AdblockPlus::JsValue::AsBool() const |
+{ |
+ const v8::Locker locker(isolate); |
+ const v8::HandleScope handleScope; |
+ const v8::Context::Scope contextScope(context); |
+ |
+ return value->BooleanValue(); |
+} |
+ |
+AdblockPlus::JsValuePtr AdblockPlus::JsValue::GetProperty(const std::string& name) const |
+{ |
+ if (!IsObject()) |
+ throw new std::runtime_error("Attempting to get property of a non-object"); |
+ |
+ const v8::Locker locker(isolate); |
+ const v8::HandleScope handleScope; |
+ const v8::Context::Scope contextScope(context); |
+ |
+ v8::Local<v8::String> property = toV8String(name); |
+ v8::Persistent<v8::Object> obj = v8::Persistent<v8::Object>::Cast(value); |
+ return JsValuePtr(new JsValue(isolate, context, obj->Get(property))); |
+} |
+ |
+void AdblockPlus::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 = toV8String(name); |
+ v8::Persistent<v8::Object> obj = v8::Persistent<v8::Object>::Cast(value); |
+ obj->Set(property, val); |
+} |
+ |
+void AdblockPlus::JsValue::SetProperty(const std::string& name, const std::string& val) |
+{ |
+ const v8::Locker locker(isolate); |
+ const v8::HandleScope handleScope; |
+ const v8::Context::Scope contextScope(context); |
+ |
+ SetProperty(name, toV8String(val)); |
+} |
+ |
+void AdblockPlus::JsValue::SetProperty(const std::string& name, int64_t val) |
+{ |
+ const v8::Locker locker(isolate); |
+ const v8::HandleScope handleScope; |
+ const v8::Context::Scope contextScope(context); |
+ |
+ SetProperty(name, v8::Integer::New(val)); |
+} |
+ |
+void AdblockPlus::JsValue::SetProperty(const std::string& name, bool val) |
+{ |
+ const v8::Locker locker(isolate); |
+ const v8::HandleScope handleScope; |
+ const v8::Context::Scope contextScope(context); |
+ |
+ SetProperty(name, v8::Boolean::New(val)); |
+} |