OLD | NEW |
(Empty) | |
| 1 #include <AdblockPlus.h> |
| 2 |
| 3 namespace |
| 4 { |
| 5 v8::Local<v8::String> toV8String(const std::string& str) |
| 6 { |
| 7 return v8::String::New(str.c_str(), str.length()); |
| 8 } |
| 9 } |
| 10 |
| 11 AdblockPlus::JsValue::JsValue(v8::Isolate* isolate, |
| 12 v8::Handle<v8::Context> context, v8::Handle<v8::Value> value) |
| 13 : isolate(isolate), |
| 14 context(v8::Persistent<v8::Context>::New(isolate, context)), |
| 15 value(v8::Persistent<v8::Value>::New(isolate, value)) |
| 16 { |
| 17 } |
| 18 |
| 19 AdblockPlus::JsValue::~JsValue() |
| 20 { |
| 21 context.Dispose(isolate); |
| 22 value.Dispose(isolate); |
| 23 } |
| 24 |
| 25 bool AdblockPlus::JsValue::IsUndefined() const |
| 26 { |
| 27 return value->IsUndefined(); |
| 28 } |
| 29 |
| 30 bool AdblockPlus::JsValue::IsNull() const |
| 31 { |
| 32 return value->IsNull(); |
| 33 } |
| 34 |
| 35 bool AdblockPlus::JsValue::IsString() const |
| 36 { |
| 37 return value->IsString() || value->IsStringObject(); |
| 38 } |
| 39 |
| 40 bool AdblockPlus::JsValue::IsNumber() const |
| 41 { |
| 42 return value->IsNumber() || value->IsNumberObject(); |
| 43 } |
| 44 |
| 45 bool AdblockPlus::JsValue::IsBool() const |
| 46 { |
| 47 return value->IsBoolean() || value->IsBooleanObject(); |
| 48 } |
| 49 |
| 50 bool AdblockPlus::JsValue::IsObject() const |
| 51 { |
| 52 return value->IsObject(); |
| 53 } |
| 54 |
| 55 std::string AdblockPlus::JsValue::AsString() const |
| 56 { |
| 57 const v8::Locker locker(isolate); |
| 58 const v8::HandleScope handleScope; |
| 59 const v8::Context::Scope contextScope(context); |
| 60 |
| 61 v8::String::Utf8Value stringValue(value); |
| 62 if (stringValue.length()) |
| 63 return std::string(*stringValue, stringValue.length()); |
| 64 else |
| 65 return std::string(); |
| 66 } |
| 67 |
| 68 int64_t AdblockPlus::JsValue::AsInt() const |
| 69 { |
| 70 const v8::Locker locker(isolate); |
| 71 const v8::HandleScope handleScope; |
| 72 const v8::Context::Scope contextScope(context); |
| 73 |
| 74 return value->IntegerValue(); |
| 75 } |
| 76 |
| 77 bool AdblockPlus::JsValue::AsBool() const |
| 78 { |
| 79 const v8::Locker locker(isolate); |
| 80 const v8::HandleScope handleScope; |
| 81 const v8::Context::Scope contextScope(context); |
| 82 |
| 83 return value->BooleanValue(); |
| 84 } |
| 85 |
| 86 AdblockPlus::JsValuePtr AdblockPlus::JsValue::GetProperty(const std::string& nam
e) const |
| 87 { |
| 88 if (!IsObject()) |
| 89 throw new std::runtime_error("Attempting to get property of a non-object"); |
| 90 |
| 91 const v8::Locker locker(isolate); |
| 92 const v8::HandleScope handleScope; |
| 93 const v8::Context::Scope contextScope(context); |
| 94 |
| 95 v8::Local<v8::String> property = toV8String(name); |
| 96 v8::Persistent<v8::Object> obj = v8::Persistent<v8::Object>::Cast(value); |
| 97 return JsValuePtr(new JsValue(isolate, context, obj->Get(property))); |
| 98 } |
| 99 |
| 100 void AdblockPlus::JsValue::SetProperty(const std::string& name, v8::Handle<v8::V
alue> val) |
| 101 { |
| 102 if (!IsObject()) |
| 103 throw new std::runtime_error("Attempting to set property on a non-object"); |
| 104 |
| 105 v8::Local<v8::String> property = toV8String(name); |
| 106 v8::Persistent<v8::Object> obj = v8::Persistent<v8::Object>::Cast(value); |
| 107 obj->Set(property, val); |
| 108 } |
| 109 |
| 110 void AdblockPlus::JsValue::SetProperty(const std::string& name, const std::strin
g& val) |
| 111 { |
| 112 const v8::Locker locker(isolate); |
| 113 const v8::HandleScope handleScope; |
| 114 const v8::Context::Scope contextScope(context); |
| 115 |
| 116 SetProperty(name, toV8String(val)); |
| 117 } |
| 118 |
| 119 void AdblockPlus::JsValue::SetProperty(const std::string& name, int64_t val) |
| 120 { |
| 121 const v8::Locker locker(isolate); |
| 122 const v8::HandleScope handleScope; |
| 123 const v8::Context::Scope contextScope(context); |
| 124 |
| 125 SetProperty(name, v8::Integer::New(val)); |
| 126 } |
| 127 |
| 128 void AdblockPlus::JsValue::SetProperty(const std::string& name, bool val) |
| 129 { |
| 130 const v8::Locker locker(isolate); |
| 131 const v8::HandleScope handleScope; |
| 132 const v8::Context::Scope contextScope(context); |
| 133 |
| 134 SetProperty(name, v8::Boolean::New(val)); |
| 135 } |
OLD | NEW |