| OLD | NEW | 
|---|
| 1 /* | 1 /* | 
| 2  * This file is part of Adblock Plus <http://adblockplus.org/>, | 2  * This file is part of Adblock Plus <http://adblockplus.org/>, | 
| 3  * Copyright (C) 2006-2014 Eyeo GmbH | 3  * Copyright (C) 2006-2014 Eyeo GmbH | 
| 4  * | 4  * | 
| 5  * Adblock Plus is free software: you can redistribute it and/or modify | 5  * Adblock Plus is free software: you can redistribute it and/or modify | 
| 6  * it under the terms of the GNU General Public License version 3 as | 6  * it under the terms of the GNU General Public License version 3 as | 
| 7  * published by the Free Software Foundation. | 7  * published by the Free Software Foundation. | 
| 8  * | 8  * | 
| 9  * Adblock Plus is distributed in the hope that it will be useful, | 9  * Adblock Plus is distributed in the hope that it will be useful, | 
| 10  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
| (...skipping 24 matching lines...) Expand all  Loading... | 
| 35 { | 35 { | 
| 36 } | 36 } | 
| 37 | 37 | 
| 38 AdblockPlus::JsValue::~JsValue() | 38 AdblockPlus::JsValue::~JsValue() | 
| 39 { | 39 { | 
| 40 } | 40 } | 
| 41 | 41 | 
| 42 bool AdblockPlus::JsValue::IsUndefined() const | 42 bool AdblockPlus::JsValue::IsUndefined() const | 
| 43 { | 43 { | 
| 44   const JsContext context(jsEngine); | 44   const JsContext context(jsEngine); | 
| 45   return value->IsUndefined(); | 45   return UnwrapValue()->IsUndefined(); | 
| 46 } | 46 } | 
| 47 | 47 | 
| 48 bool AdblockPlus::JsValue::IsNull() const | 48 bool AdblockPlus::JsValue::IsNull() const | 
| 49 { | 49 { | 
| 50   const JsContext context(jsEngine); | 50   const JsContext context(jsEngine); | 
| 51   return value->IsNull(); | 51   return UnwrapValue()->IsNull(); | 
| 52 } | 52 } | 
| 53 | 53 | 
| 54 bool AdblockPlus::JsValue::IsString() const | 54 bool AdblockPlus::JsValue::IsString() const | 
| 55 { | 55 { | 
| 56   const JsContext context(jsEngine); | 56   const JsContext context(jsEngine); | 
|  | 57   v8::Local<v8::Value> value = UnwrapValue(); | 
| 57   return value->IsString() || value->IsStringObject(); | 58   return value->IsString() || value->IsStringObject(); | 
| 58 } | 59 } | 
| 59 | 60 | 
| 60 bool AdblockPlus::JsValue::IsNumber() const | 61 bool AdblockPlus::JsValue::IsNumber() const | 
| 61 { | 62 { | 
| 62   const JsContext context(jsEngine); | 63   const JsContext context(jsEngine); | 
|  | 64   v8::Local<v8::Value> value = UnwrapValue(); | 
| 63   return value->IsNumber() || value->IsNumberObject(); | 65   return value->IsNumber() || value->IsNumberObject(); | 
| 64 } | 66 } | 
| 65 | 67 | 
| 66 bool AdblockPlus::JsValue::IsBool() const | 68 bool AdblockPlus::JsValue::IsBool() const | 
| 67 { | 69 { | 
| 68   const JsContext context(jsEngine); | 70   const JsContext context(jsEngine); | 
|  | 71   v8::Local<v8::Value> value = UnwrapValue(); | 
| 69   return value->IsBoolean() || value->IsBooleanObject(); | 72   return value->IsBoolean() || value->IsBooleanObject(); | 
| 70 } | 73 } | 
| 71 | 74 | 
| 72 bool AdblockPlus::JsValue::IsObject() const | 75 bool AdblockPlus::JsValue::IsObject() const | 
| 73 { | 76 { | 
| 74   const JsContext context(jsEngine); | 77   const JsContext context(jsEngine); | 
| 75   return value->IsObject(); | 78   return UnwrapValue()->IsObject(); | 
| 76 } | 79 } | 
| 77 | 80 | 
| 78 bool AdblockPlus::JsValue::IsArray() const | 81 bool AdblockPlus::JsValue::IsArray() const | 
| 79 { | 82 { | 
| 80   const JsContext context(jsEngine); | 83   const JsContext context(jsEngine); | 
| 81   return value->IsArray(); | 84   return UnwrapValue()->IsArray(); | 
| 82 } | 85 } | 
| 83 | 86 | 
| 84 bool AdblockPlus::JsValue::IsFunction() const | 87 bool AdblockPlus::JsValue::IsFunction() const | 
| 85 { | 88 { | 
| 86   const JsContext context(jsEngine); | 89   const JsContext context(jsEngine); | 
| 87   return value->IsFunction(); | 90   return UnwrapValue()->IsFunction(); | 
| 88 } | 91 } | 
| 89 | 92 | 
| 90 std::string AdblockPlus::JsValue::AsString() const | 93 std::string AdblockPlus::JsValue::AsString() const | 
| 91 { | 94 { | 
| 92   const JsContext context(jsEngine); | 95   const JsContext context(jsEngine); | 
| 93   return Utils::FromV8String(value); | 96   return Utils::FromV8String(UnwrapValue()); | 
| 94 } | 97 } | 
| 95 | 98 | 
| 96 int64_t AdblockPlus::JsValue::AsInt() const | 99 int64_t AdblockPlus::JsValue::AsInt() const | 
| 97 { | 100 { | 
| 98   const JsContext context(jsEngine); | 101   const JsContext context(jsEngine); | 
| 99   return value->IntegerValue(); | 102   return UnwrapValue()->IntegerValue(); | 
| 100 } | 103 } | 
| 101 | 104 | 
| 102 bool AdblockPlus::JsValue::AsBool() const | 105 bool AdblockPlus::JsValue::AsBool() const | 
| 103 { | 106 { | 
| 104   const JsContext context(jsEngine); | 107   const JsContext context(jsEngine); | 
| 105   return value->BooleanValue(); | 108   return UnwrapValue()->BooleanValue(); | 
| 106 } | 109 } | 
| 107 | 110 | 
| 108 AdblockPlus::JsValueList AdblockPlus::JsValue::AsList() const | 111 AdblockPlus::JsValueList AdblockPlus::JsValue::AsList() const | 
| 109 { | 112 { | 
| 110   if (!IsArray()) | 113   if (!IsArray()) | 
| 111     throw std::runtime_error("Cannot convert a non-array to list"); | 114     throw std::runtime_error("Cannot convert a non-array to list"); | 
| 112 | 115 | 
| 113   const JsContext context(jsEngine); | 116   const JsContext context(jsEngine); | 
| 114   JsValueList result; | 117   JsValueList result; | 
| 115   v8::Persistent<v8::Array> array = v8::Persistent<v8::Array>::Cast<v8::Value>(v
     alue); | 118   v8::Local<v8::Array> array = v8::Local<v8::Array>::Cast(UnwrapValue()); | 
| 116   uint32_t length = array->Length(); | 119   uint32_t length = array->Length(); | 
| 117   for (uint32_t i = 0; i < length; i++) | 120   for (uint32_t i = 0; i < length; i++) | 
| 118   { | 121   { | 
| 119     v8::Local<v8::Value> item = array->Get(i); | 122     v8::Local<v8::Value> item = array->Get(i); | 
| 120     result.push_back(JsValuePtr(new JsValue(jsEngine, item))); | 123     result.push_back(JsValuePtr(new JsValue(jsEngine, item))); | 
| 121   } | 124   } | 
| 122   return result; | 125   return result; | 
| 123 } | 126 } | 
| 124 | 127 | 
| 125 std::vector<std::string> AdblockPlus::JsValue::GetOwnPropertyNames() const | 128 std::vector<std::string> AdblockPlus::JsValue::GetOwnPropertyNames() const | 
| 126 { | 129 { | 
| 127   if (!IsObject()) | 130   if (!IsObject()) | 
| 128     throw new std::runtime_error("Attempting to get propert list for a non-objec
     t"); | 131     throw new std::runtime_error("Attempting to get propert list for a non-objec
     t"); | 
| 129 | 132 | 
| 130   const JsContext context(jsEngine); | 133   const JsContext context(jsEngine); | 
| 131   const v8::Persistent<v8::Object> object = v8::Persistent<v8::Object>::Cast<v8:
     :Value>(value); | 134   v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(UnwrapValue()); | 
| 132   JsValueList properties = JsValuePtr(new JsValue(jsEngine, object->GetOwnProper
     tyNames()))->AsList(); | 135   JsValueList properties = JsValuePtr(new JsValue(jsEngine, object->GetOwnProper
     tyNames()))->AsList(); | 
| 133   std::vector<std::string> result; | 136   std::vector<std::string> result; | 
| 134   for (JsValueList::iterator it = properties.begin(); it != properties.end(); ++
     it) | 137   for (JsValueList::iterator it = properties.begin(); it != properties.end(); ++
     it) | 
| 135     result.push_back((*it)->AsString()); | 138     result.push_back((*it)->AsString()); | 
| 136   return result; | 139   return result; | 
| 137 } | 140 } | 
| 138 | 141 | 
| 139 | 142 | 
| 140 AdblockPlus::JsValuePtr AdblockPlus::JsValue::GetProperty(const std::string& nam
     e) const | 143 AdblockPlus::JsValuePtr AdblockPlus::JsValue::GetProperty(const std::string& nam
     e) const | 
| 141 { | 144 { | 
| 142   if (!IsObject()) | 145   if (!IsObject()) | 
| 143     throw new std::runtime_error("Attempting to get property of a non-object"); | 146     throw new std::runtime_error("Attempting to get property of a non-object"); | 
| 144 | 147 | 
| 145   const JsContext context(jsEngine); | 148   const JsContext context(jsEngine); | 
| 146   v8::Local<v8::String> property = Utils::ToV8String(name); | 149   v8::Local<v8::String> property = Utils::ToV8String(name); | 
| 147   v8::Persistent<v8::Object> obj = v8::Persistent<v8::Object>::Cast<v8::Value>(v
     alue); | 150   v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(UnwrapValue()); | 
| 148   return JsValuePtr(new JsValue(jsEngine, obj->Get(property))); | 151   return JsValuePtr(new JsValue(jsEngine, obj->Get(property))); | 
| 149 } | 152 } | 
| 150 | 153 | 
| 151 void AdblockPlus::JsValue::SetProperty(const std::string& name, v8::Handle<v8::V
     alue> val) | 154 void AdblockPlus::JsValue::SetProperty(const std::string& name, v8::Handle<v8::V
     alue> val) | 
| 152 { | 155 { | 
| 153   if (!IsObject()) | 156   if (!IsObject()) | 
| 154     throw new std::runtime_error("Attempting to set property on a non-object"); | 157     throw new std::runtime_error("Attempting to set property on a non-object"); | 
| 155 | 158 | 
| 156   v8::Local<v8::String> property = Utils::ToV8String(name); | 159   v8::Local<v8::String> property = Utils::ToV8String(name); | 
| 157   v8::Persistent<v8::Object> obj = v8::Persistent<v8::Object>::Cast<v8::Value>(v
     alue); | 160   v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(UnwrapValue()); | 
| 158   obj->Set(property, val); | 161   obj->Set(property, val); | 
| 159 } | 162 } | 
| 160 | 163 | 
|  | 164 v8::Local<v8::Value> AdblockPlus::JsValue::UnwrapValue() const | 
|  | 165 { | 
|  | 166   return v8::Local<v8::Value>::New(jsEngine->isolate, value); | 
|  | 167 } | 
|  | 168 | 
| 161 void AdblockPlus::JsValue::SetProperty(const std::string& name, const std::strin
     g& val) | 169 void AdblockPlus::JsValue::SetProperty(const std::string& name, const std::strin
     g& val) | 
| 162 { | 170 { | 
| 163   const JsContext context(jsEngine); | 171   const JsContext context(jsEngine); | 
| 164   SetProperty(name, Utils::ToV8String(val)); | 172   SetProperty(name, Utils::ToV8String(val)); | 
| 165 } | 173 } | 
| 166 | 174 | 
| 167 void AdblockPlus::JsValue::SetProperty(const std::string& name, int64_t val) | 175 void AdblockPlus::JsValue::SetProperty(const std::string& name, int64_t val) | 
| 168 { | 176 { | 
| 169   const JsContext context(jsEngine); | 177   const JsContext context(jsEngine); | 
| 170   SetProperty(name, v8::Number::New(val)); | 178   SetProperty(name, v8::Number::New(val)); | 
| 171 } | 179 } | 
| 172 | 180 | 
| 173 void AdblockPlus::JsValue::SetProperty(const std::string& name, JsValuePtr val) | 181 void AdblockPlus::JsValue::SetProperty(const std::string& name, const JsValuePtr
     & val) | 
| 174 { | 182 { | 
| 175   const JsContext context(jsEngine); | 183   const JsContext context(jsEngine); | 
| 176   SetProperty(name, val->value); | 184   SetProperty(name, val->UnwrapValue()); | 
| 177 } | 185 } | 
| 178 | 186 | 
| 179 void AdblockPlus::JsValue::SetProperty(const std::string& name, bool val) | 187 void AdblockPlus::JsValue::SetProperty(const std::string& name, bool val) | 
| 180 { | 188 { | 
| 181   const JsContext context(jsEngine); | 189   const JsContext context(jsEngine); | 
| 182   SetProperty(name, v8::Boolean::New(val)); | 190   SetProperty(name, v8::Boolean::New(val)); | 
| 183 } | 191 } | 
| 184 | 192 | 
| 185 std::string AdblockPlus::JsValue::GetClass() const | 193 std::string AdblockPlus::JsValue::GetClass() const | 
| 186 { | 194 { | 
| 187   if (!IsObject()) | 195   if (!IsObject()) | 
| 188     throw new std::runtime_error("Cannot get constructor of a non-object"); | 196     throw new std::runtime_error("Cannot get constructor of a non-object"); | 
| 189 | 197 | 
| 190   const JsContext context(jsEngine); | 198   const JsContext context(jsEngine); | 
| 191   v8::Persistent<v8::Object> obj = v8::Persistent<v8::Object>::Cast<v8::Value>(v
     alue); | 199   v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(UnwrapValue()); | 
| 192   return Utils::FromV8String(obj->GetConstructorName()); | 200   return Utils::FromV8String(obj->GetConstructorName()); | 
| 193 } | 201 } | 
| 194 | 202 | 
| 195 AdblockPlus::JsValuePtr AdblockPlus::JsValue::Call( | 203 AdblockPlus::JsValuePtr AdblockPlus::JsValue::Call(const JsValueList& params, Js
     ValuePtr thisPtr) const | 
| 196     const JsValueList& params, |  | 
| 197     AdblockPlus::JsValuePtr thisPtr) const |  | 
| 198 { | 204 { | 
| 199   if (!IsFunction()) | 205   if (!IsFunction()) | 
| 200     throw new std::runtime_error("Attempting to call a non-function"); | 206     throw new std::runtime_error("Attempting to call a non-function"); | 
| 201 | 207 | 
| 202   const JsContext context(jsEngine); | 208   const JsContext context(jsEngine); | 
| 203 |  | 
| 204   if (!thisPtr) | 209   if (!thisPtr) | 
| 205     thisPtr = JsValuePtr(new JsValue(jsEngine, jsEngine->context->Global())); | 210   { | 
|  | 211     v8::Local<v8::Context> localContext = v8::Local<v8::Context>::New(jsEngine->
     isolate, jsEngine->context); | 
|  | 212     thisPtr = JsValuePtr(new JsValue(jsEngine, localContext->Global())); | 
|  | 213   } | 
| 206   if (!thisPtr->IsObject()) | 214   if (!thisPtr->IsObject()) | 
| 207     throw new std::runtime_error("`this` pointer has to be an object"); | 215     throw new std::runtime_error("`this` pointer has to be an object"); | 
| 208   v8::Persistent<v8::Object> thisObj = v8::Persistent<v8::Object>::Cast<v8::Valu
     e>(thisPtr->value); | 216   v8::Local<v8::Object> thisObj = v8::Local<v8::Object>::Cast(thisPtr->UnwrapVal
     ue()); | 
| 209 | 217 | 
| 210   std::vector<v8::Handle<v8::Value> > argv; | 218   std::vector<v8::Handle<v8::Value>> argv; | 
| 211   for (JsValueList::const_iterator it = params.begin(); it != params.end(); ++it
     ) | 219   for (JsValueList::const_iterator it = params.begin(); it != params.end(); ++it
     ) | 
| 212     argv.push_back((*it)->value); | 220     argv.push_back((*it)->UnwrapValue()); | 
| 213 | 221 | 
| 214   const v8::TryCatch tryCatch; | 222   const v8::TryCatch tryCatch; | 
| 215   v8::Persistent<v8::Function> func = v8::Persistent<v8::Function>::Cast<v8::Val
     ue>(value); | 223   v8::Local<v8::Function> func = v8::Local<v8::Function>::Cast(UnwrapValue()); | 
| 216   v8::Local<v8::Value> result = func->Call(thisObj, argv.size(), | 224   v8::Local<v8::Value> result = func->Call(thisObj, argv.size(), | 
| 217       argv.size() ? &argv.front() : 0); | 225       argv.size() ? &argv.front() : 0); | 
| 218 | 226 | 
| 219   if (tryCatch.HasCaught()) | 227   if (tryCatch.HasCaught()) | 
| 220     throw AdblockPlus::JsError(tryCatch.Exception(), tryCatch.Message()); | 228     throw JsError(tryCatch.Exception(), tryCatch.Message()); | 
| 221 | 229 | 
| 222   return JsValuePtr(new JsValue(jsEngine, result)); | 230   return JsValuePtr(new JsValue(jsEngine, result)); | 
| 223 } | 231 } | 
| OLD | NEW | 
|---|