| 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-2013 Eyeo GmbH | 3 * Copyright (C) 2006-2013 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 |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
| 13 * | 13 * |
| 14 * You should have received a copy of the GNU General Public License | 14 * You should have received a copy of the GNU General Public License |
| 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 16 */ | 16 */ |
| 17 | 17 |
| 18 #include <vector> |
| 18 #include <AdblockPlus.h> | 19 #include <AdblockPlus.h> |
| 19 #include <vector> | 20 |
| 20 | 21 #include "JsContext.h" |
| 21 namespace | 22 #include "JsError.h" |
| 22 { | 23 #include "Utils.h" |
| 23 std::string fromV8String(v8::Handle<v8::Value> value) | |
| 24 { | |
| 25 v8::String::Utf8Value stringValue(value); | |
| 26 if (stringValue.length()) | |
| 27 return std::string(*stringValue, stringValue.length()); | |
| 28 else | |
| 29 return std::string(); | |
| 30 } | |
| 31 | |
| 32 v8::Local<v8::String> toV8String(const std::string& str) | |
| 33 { | |
| 34 return v8::String::New(str.c_str(), str.length()); | |
| 35 } | |
| 36 } | |
| 37 | 24 |
| 38 AdblockPlus::JsValue::JsValue(AdblockPlus::JsEnginePtr jsEngine, | 25 AdblockPlus::JsValue::JsValue(AdblockPlus::JsEnginePtr jsEngine, |
| 39 v8::Handle<v8::Value> value) | 26 v8::Handle<v8::Value> value) |
| 40 : jsEngine(jsEngine), | 27 : jsEngine(jsEngine), |
| 41 value(v8::Persistent<v8::Value>::New(jsEngine->isolate, value)) | 28 value(jsEngine->isolate, value) |
| 42 { | 29 { |
| 43 } | 30 } |
| 44 | 31 |
| 45 AdblockPlus::JsValue::JsValue(AdblockPlus::JsValuePtr value) | 32 AdblockPlus::JsValue::JsValue(AdblockPlus::JsValuePtr value) |
| 46 : jsEngine(value->jsEngine), | 33 : jsEngine(value->jsEngine), |
| 47 value(v8::Persistent<v8::Value>::New(jsEngine->isolate, value->value)) | 34 value(value->value) |
| 48 { | 35 { |
| 49 } | 36 } |
| 50 | 37 |
| 51 AdblockPlus::JsValue::~JsValue() | 38 AdblockPlus::JsValue::~JsValue() |
| 52 { | 39 { |
| 53 value.Dispose(jsEngine->isolate); | |
| 54 } | 40 } |
| 55 | 41 |
| 56 bool AdblockPlus::JsValue::IsUndefined() const | 42 bool AdblockPlus::JsValue::IsUndefined() const |
| 57 { | 43 { |
| 58 const JsEngine::Context context(jsEngine); | 44 const JsContext context(jsEngine); |
| 59 return value->IsUndefined(); | 45 return value->IsUndefined(); |
| 60 } | 46 } |
| 61 | 47 |
| 62 bool AdblockPlus::JsValue::IsNull() const | 48 bool AdblockPlus::JsValue::IsNull() const |
| 63 { | 49 { |
| 64 const JsEngine::Context context(jsEngine); | 50 const JsContext context(jsEngine); |
| 65 return value->IsNull(); | 51 return value->IsNull(); |
| 66 } | 52 } |
| 67 | 53 |
| 68 bool AdblockPlus::JsValue::IsString() const | 54 bool AdblockPlus::JsValue::IsString() const |
| 69 { | 55 { |
| 70 const JsEngine::Context context(jsEngine); | 56 const JsContext context(jsEngine); |
| 71 return value->IsString() || value->IsStringObject(); | 57 return value->IsString() || value->IsStringObject(); |
| 72 } | 58 } |
| 73 | 59 |
| 74 bool AdblockPlus::JsValue::IsNumber() const | 60 bool AdblockPlus::JsValue::IsNumber() const |
| 75 { | 61 { |
| 76 const JsEngine::Context context(jsEngine); | 62 const JsContext context(jsEngine); |
| 77 return value->IsNumber() || value->IsNumberObject(); | 63 return value->IsNumber() || value->IsNumberObject(); |
| 78 } | 64 } |
| 79 | 65 |
| 80 bool AdblockPlus::JsValue::IsBool() const | 66 bool AdblockPlus::JsValue::IsBool() const |
| 81 { | 67 { |
| 82 const JsEngine::Context context(jsEngine); | 68 const JsContext context(jsEngine); |
| 83 return value->IsBoolean() || value->IsBooleanObject(); | 69 return value->IsBoolean() || value->IsBooleanObject(); |
| 84 } | 70 } |
| 85 | 71 |
| 86 bool AdblockPlus::JsValue::IsObject() const | 72 bool AdblockPlus::JsValue::IsObject() const |
| 87 { | 73 { |
| 88 const JsEngine::Context context(jsEngine); | 74 const JsContext context(jsEngine); |
| 89 return value->IsObject(); | 75 return value->IsObject(); |
| 90 } | 76 } |
| 91 | 77 |
| 92 bool AdblockPlus::JsValue::IsArray() const | 78 bool AdblockPlus::JsValue::IsArray() const |
| 93 { | 79 { |
| 94 const JsEngine::Context context(jsEngine); | 80 const JsContext context(jsEngine); |
| 95 return value->IsArray(); | 81 return value->IsArray(); |
| 96 } | 82 } |
| 97 | 83 |
| 98 bool AdblockPlus::JsValue::IsFunction() const | 84 bool AdblockPlus::JsValue::IsFunction() const |
| 99 { | 85 { |
| 100 const JsEngine::Context context(jsEngine); | 86 const JsContext context(jsEngine); |
| 101 return value->IsFunction(); | 87 return value->IsFunction(); |
| 102 } | 88 } |
| 103 | 89 |
| 104 std::string AdblockPlus::JsValue::AsString() const | 90 std::string AdblockPlus::JsValue::AsString() const |
| 105 { | 91 { |
| 106 const JsEngine::Context context(jsEngine); | 92 const JsContext context(jsEngine); |
| 107 return fromV8String(value); | 93 return Utils::FromV8String(value); |
| 108 } | 94 } |
| 109 | 95 |
| 110 int64_t AdblockPlus::JsValue::AsInt() const | 96 int64_t AdblockPlus::JsValue::AsInt() const |
| 111 { | 97 { |
| 112 const JsEngine::Context context(jsEngine); | 98 const JsContext context(jsEngine); |
| 113 return value->IntegerValue(); | 99 return value->IntegerValue(); |
| 114 } | 100 } |
| 115 | 101 |
| 116 bool AdblockPlus::JsValue::AsBool() const | 102 bool AdblockPlus::JsValue::AsBool() const |
| 117 { | 103 { |
| 118 const JsEngine::Context context(jsEngine); | 104 const JsContext context(jsEngine); |
| 119 return value->BooleanValue(); | 105 return value->BooleanValue(); |
| 120 } | 106 } |
| 121 | 107 |
| 122 AdblockPlus::JsValueList AdblockPlus::JsValue::AsList() const | 108 AdblockPlus::JsValueList AdblockPlus::JsValue::AsList() const |
| 123 { | 109 { |
| 124 if (!IsArray()) | 110 if (!IsArray()) |
| 125 throw std::runtime_error("Cannot convert a non-array to list"); | 111 throw std::runtime_error("Cannot convert a non-array to list"); |
| 126 | 112 |
| 127 const JsEngine::Context context(jsEngine); | 113 const JsContext context(jsEngine); |
| 128 JsValueList result; | 114 JsValueList result; |
| 129 v8::Persistent<v8::Array> array = v8::Persistent<v8::Array>::Cast(value); | 115 v8::Persistent<v8::Array> array = v8::Persistent<v8::Array>::Cast<v8::Value>(v
alue); |
| 130 uint32_t length = array->Length(); | 116 uint32_t length = array->Length(); |
| 131 for (uint32_t i = 0; i < length; i++) | 117 for (uint32_t i = 0; i < length; i++) |
| 132 { | 118 { |
| 133 v8::Local<v8::Value> item = array->Get(i); | 119 v8::Local<v8::Value> item = array->Get(i); |
| 134 result.push_back(JsValuePtr(new JsValue(jsEngine, item))); | 120 result.push_back(JsValuePtr(new JsValue(jsEngine, item))); |
| 135 } | 121 } |
| 136 return result; | 122 return result; |
| 137 } | 123 } |
| 138 | 124 |
| 139 std::vector<std::string> AdblockPlus::JsValue::GetOwnPropertyNames() const | 125 std::vector<std::string> AdblockPlus::JsValue::GetOwnPropertyNames() const |
| 140 { | 126 { |
| 141 if (!IsObject()) | 127 if (!IsObject()) |
| 142 throw new std::runtime_error("Attempting to get propert list for a non-objec
t"); | 128 throw new std::runtime_error("Attempting to get propert list for a non-objec
t"); |
| 143 | 129 |
| 144 const JsEngine::Context context(jsEngine); | 130 const JsContext context(jsEngine); |
| 145 const v8::Persistent<v8::Object> object = v8::Persistent<v8::Object>::Cast(val
ue); | 131 const v8::Persistent<v8::Object> object = v8::Persistent<v8::Object>::Cast<v8:
:Value>(value); |
| 146 JsValueList properties = JsValuePtr(new JsValue(jsEngine, object->GetOwnProper
tyNames()))->AsList(); | 132 JsValueList properties = JsValuePtr(new JsValue(jsEngine, object->GetOwnProper
tyNames()))->AsList(); |
| 147 std::vector<std::string> result; | 133 std::vector<std::string> result; |
| 148 for (JsValueList::iterator it = properties.begin(); it != properties.end(); ++
it) | 134 for (JsValueList::iterator it = properties.begin(); it != properties.end(); ++
it) |
| 149 result.push_back((*it)->AsString()); | 135 result.push_back((*it)->AsString()); |
| 150 return result; | 136 return result; |
| 151 } | 137 } |
| 152 | 138 |
| 153 | 139 |
| 154 AdblockPlus::JsValuePtr AdblockPlus::JsValue::GetProperty(const std::string& nam
e) const | 140 AdblockPlus::JsValuePtr AdblockPlus::JsValue::GetProperty(const std::string& nam
e) const |
| 155 { | 141 { |
| 156 if (!IsObject()) | 142 if (!IsObject()) |
| 157 throw new std::runtime_error("Attempting to get property of a non-object"); | 143 throw new std::runtime_error("Attempting to get property of a non-object"); |
| 158 | 144 |
| 159 const JsEngine::Context context(jsEngine); | 145 const JsContext context(jsEngine); |
| 160 v8::Local<v8::String> property = toV8String(name); | 146 v8::Local<v8::String> property = Utils::ToV8String(name); |
| 161 v8::Persistent<v8::Object> obj = v8::Persistent<v8::Object>::Cast(value); | 147 v8::Persistent<v8::Object> obj = v8::Persistent<v8::Object>::Cast<v8::Value>(v
alue); |
| 162 return JsValuePtr(new JsValue(jsEngine, obj->Get(property))); | 148 return JsValuePtr(new JsValue(jsEngine, obj->Get(property))); |
| 163 } | 149 } |
| 164 | 150 |
| 165 void AdblockPlus::JsValue::SetProperty(const std::string& name, v8::Handle<v8::V
alue> val) | 151 void AdblockPlus::JsValue::SetProperty(const std::string& name, v8::Handle<v8::V
alue> val) |
| 166 { | 152 { |
| 167 if (!IsObject()) | 153 if (!IsObject()) |
| 168 throw new std::runtime_error("Attempting to set property on a non-object"); | 154 throw new std::runtime_error("Attempting to set property on a non-object"); |
| 169 | 155 |
| 170 v8::Local<v8::String> property = toV8String(name); | 156 v8::Local<v8::String> property = Utils::ToV8String(name); |
| 171 v8::Persistent<v8::Object> obj = v8::Persistent<v8::Object>::Cast(value); | 157 v8::Persistent<v8::Object> obj = v8::Persistent<v8::Object>::Cast<v8::Value>(v
alue); |
| 172 obj->Set(property, val); | 158 obj->Set(property, val); |
| 173 } | 159 } |
| 174 | 160 |
| 175 void AdblockPlus::JsValue::SetProperty(const std::string& name, const std::strin
g& val) | 161 void AdblockPlus::JsValue::SetProperty(const std::string& name, const std::strin
g& val) |
| 176 { | 162 { |
| 177 const JsEngine::Context context(jsEngine); | 163 const JsContext context(jsEngine); |
| 178 SetProperty(name, toV8String(val)); | 164 SetProperty(name, Utils::ToV8String(val)); |
| 179 } | 165 } |
| 180 | 166 |
| 181 void AdblockPlus::JsValue::SetProperty(const std::string& name, int64_t val) | 167 void AdblockPlus::JsValue::SetProperty(const std::string& name, int64_t val) |
| 182 { | 168 { |
| 183 const JsEngine::Context context(jsEngine); | 169 const JsContext context(jsEngine); |
| 184 SetProperty(name, v8::Number::New(val)); | 170 SetProperty(name, v8::Number::New(val)); |
| 185 } | 171 } |
| 186 | 172 |
| 187 void AdblockPlus::JsValue::SetProperty(const std::string& name, JsValuePtr val) | 173 void AdblockPlus::JsValue::SetProperty(const std::string& name, JsValuePtr val) |
| 188 { | 174 { |
| 189 const JsEngine::Context context(jsEngine); | 175 const JsContext context(jsEngine); |
| 190 SetProperty(name, val->value); | 176 SetProperty(name, val->value); |
| 191 } | 177 } |
| 192 | 178 |
| 193 void AdblockPlus::JsValue::SetProperty(const std::string& name, bool val) | 179 void AdblockPlus::JsValue::SetProperty(const std::string& name, bool val) |
| 194 { | 180 { |
| 195 const JsEngine::Context context(jsEngine); | 181 const JsContext context(jsEngine); |
| 196 SetProperty(name, v8::Boolean::New(val)); | 182 SetProperty(name, v8::Boolean::New(val)); |
| 197 } | 183 } |
| 198 | 184 |
| 199 std::string AdblockPlus::JsValue::GetClass() const | 185 std::string AdblockPlus::JsValue::GetClass() const |
| 200 { | 186 { |
| 201 if (!IsObject()) | 187 if (!IsObject()) |
| 202 throw new std::runtime_error("Cannot get constructor of a non-object"); | 188 throw new std::runtime_error("Cannot get constructor of a non-object"); |
| 203 | 189 |
| 204 const JsEngine::Context context(jsEngine); | 190 const JsContext context(jsEngine); |
| 205 v8::Persistent<v8::Object> obj = v8::Persistent<v8::Object>::Cast(value); | 191 v8::Persistent<v8::Object> obj = v8::Persistent<v8::Object>::Cast<v8::Value>(v
alue); |
| 206 return fromV8String(obj->GetConstructorName()); | 192 return Utils::FromV8String(obj->GetConstructorName()); |
| 207 } | 193 } |
| 208 | 194 |
| 209 AdblockPlus::JsValuePtr AdblockPlus::JsValue::Call( | 195 AdblockPlus::JsValuePtr AdblockPlus::JsValue::Call( |
| 210 const JsValueList& params, | 196 const JsValueList& params, |
| 211 AdblockPlus::JsValuePtr thisPtr) const | 197 AdblockPlus::JsValuePtr thisPtr) const |
| 212 { | 198 { |
| 213 if (!IsFunction()) | 199 if (!IsFunction()) |
| 214 throw new std::runtime_error("Attempting to call a non-function"); | 200 throw new std::runtime_error("Attempting to call a non-function"); |
| 215 | 201 |
| 216 const JsEngine::Context context(jsEngine); | 202 const JsContext context(jsEngine); |
| 217 | 203 |
| 218 if (!thisPtr) | 204 if (!thisPtr) |
| 219 thisPtr = JsValuePtr(new JsValue(jsEngine, jsEngine->context->Global())); | 205 thisPtr = JsValuePtr(new JsValue(jsEngine, jsEngine->context->Global())); |
| 220 if (!thisPtr->IsObject()) | 206 if (!thisPtr->IsObject()) |
| 221 throw new std::runtime_error("`this` pointer has to be an object"); | 207 throw new std::runtime_error("`this` pointer has to be an object"); |
| 222 v8::Persistent<v8::Object> thisObj = v8::Persistent<v8::Object>::Cast(thisPtr-
>value); | 208 v8::Persistent<v8::Object> thisObj = v8::Persistent<v8::Object>::Cast<v8::Valu
e>(thisPtr->value); |
| 223 | 209 |
| 224 std::vector<v8::Handle<v8::Value> > argv; | 210 std::vector<v8::Handle<v8::Value> > argv; |
| 225 for (JsValueList::const_iterator it = params.begin(); it != params.end(); ++it
) | 211 for (JsValueList::const_iterator it = params.begin(); it != params.end(); ++it
) |
| 226 argv.push_back((*it)->value); | 212 argv.push_back((*it)->value); |
| 227 | 213 |
| 228 const v8::TryCatch tryCatch; | 214 const v8::TryCatch tryCatch; |
| 229 v8::Persistent<v8::Function> func = v8::Persistent<v8::Function>::Cast(value); | 215 v8::Persistent<v8::Function> func = v8::Persistent<v8::Function>::Cast<v8::Val
ue>(value); |
| 230 v8::Local<v8::Value> result = func->Call(thisObj, argv.size(), | 216 v8::Local<v8::Value> result = func->Call(thisObj, argv.size(), |
| 231 argv.size() ? &argv.front() : 0); | 217 argv.size() ? &argv.front() : 0); |
| 232 | 218 |
| 233 if (tryCatch.HasCaught()) | 219 if (tryCatch.HasCaught()) |
| 234 throw AdblockPlus::JsError(tryCatch.Exception(), tryCatch.Message()); | 220 throw AdblockPlus::JsError(tryCatch.Exception(), tryCatch.Message()); |
| 235 | 221 |
| 236 return JsValuePtr(new JsValue(jsEngine, result)); | 222 return JsValuePtr(new JsValue(jsEngine, result)); |
| 237 } | 223 } |
| OLD | NEW |