LEFT | RIGHT |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 * Copyright (C) 2006-2016 Eyeo GmbH | 3 * Copyright (C) 2006-2017 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 <vector> |
19 #include <AdblockPlus.h> | 19 #include <AdblockPlus.h> |
20 | 20 |
21 #include "JsContext.h" | 21 #include "JsContext.h" |
22 #include "JsError.h" | 22 #include "JsError.h" |
23 #include "Utils.h" | 23 #include "Utils.h" |
24 | 24 |
| 25 using namespace AdblockPlus; |
| 26 |
25 AdblockPlus::JsValue::JsValue(AdblockPlus::JsEnginePtr jsEngine, | 27 AdblockPlus::JsValue::JsValue(AdblockPlus::JsEnginePtr jsEngine, |
26 v8::Handle<v8::Value> value) | 28 v8::Handle<v8::Value> value) |
27 : jsEngine(jsEngine), | 29 : jsEngine(jsEngine), |
28 value(new v8::Persistent<v8::Value>(jsEngine->isolate, value)) | 30 value(new v8::Persistent<v8::Value>(jsEngine->GetIsolate(), value)) |
29 { | 31 { |
30 } | 32 } |
31 | 33 |
32 AdblockPlus::JsValue::JsValue(AdblockPlus::JsValue&& src) | 34 AdblockPlus::JsValue::JsValue(AdblockPlus::JsValue&& src) |
33 : jsEngine(src.jsEngine), | 35 : jsEngine(src.jsEngine), |
34 value(std::move(src.value)) | 36 value(std::move(src.value)) |
35 { | 37 { |
36 } | 38 } |
37 | 39 |
38 AdblockPlus::JsValue::~JsValue() | 40 AdblockPlus::JsValue::~JsValue() |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 | 134 |
133 std::vector<std::string> AdblockPlus::JsValue::GetOwnPropertyNames() const | 135 std::vector<std::string> AdblockPlus::JsValue::GetOwnPropertyNames() const |
134 { | 136 { |
135 if (!IsObject()) | 137 if (!IsObject()) |
136 throw new std::runtime_error("Attempting to get propert list for a non-objec
t"); | 138 throw new std::runtime_error("Attempting to get propert list for a non-objec
t"); |
137 | 139 |
138 const JsContext context(jsEngine); | 140 const JsContext context(jsEngine); |
139 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(UnwrapValue()); | 141 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(UnwrapValue()); |
140 JsValueList properties = JsValuePtr(new JsValue(jsEngine, object->GetOwnProper
tyNames()))->AsList(); | 142 JsValueList properties = JsValuePtr(new JsValue(jsEngine, object->GetOwnProper
tyNames()))->AsList(); |
141 std::vector<std::string> result; | 143 std::vector<std::string> result; |
142 for (JsValueList::iterator it = properties.begin(); it != properties.end(); ++
it) | 144 for (const auto& property : properties) |
143 result.push_back((*it)->AsString()); | 145 result.push_back(property->AsString()); |
144 return result; | 146 return result; |
145 } | 147 } |
146 | 148 |
147 | 149 |
148 AdblockPlus::JsValuePtr AdblockPlus::JsValue::GetProperty(const std::string& nam
e) const | 150 AdblockPlus::JsValuePtr AdblockPlus::JsValue::GetProperty(const std::string& nam
e) const |
149 { | 151 { |
150 if (!IsObject()) | 152 if (!IsObject()) |
151 throw new std::runtime_error("Attempting to get property of a non-object"); | 153 throw new std::runtime_error("Attempting to get property of a non-object"); |
152 | 154 |
153 const JsContext context(jsEngine); | 155 const JsContext context(jsEngine); |
154 v8::Local<v8::String> property = Utils::ToV8String(jsEngine->isolate, name); | 156 v8::Local<v8::String> property = Utils::ToV8String(jsEngine->GetIsolate(), nam
e); |
155 v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(UnwrapValue()); | 157 v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(UnwrapValue()); |
156 return JsValuePtr(new JsValue(jsEngine, obj->Get(property))); | 158 return JsValuePtr(new JsValue(jsEngine, obj->Get(property))); |
157 } | 159 } |
158 | 160 |
159 void AdblockPlus::JsValue::SetProperty(const std::string& name, v8::Handle<v8::V
alue> val) | 161 void AdblockPlus::JsValue::SetProperty(const std::string& name, v8::Handle<v8::V
alue> val) |
160 { | 162 { |
161 if (!IsObject()) | 163 if (!IsObject()) |
162 throw new std::runtime_error("Attempting to set property on a non-object"); | 164 throw new std::runtime_error("Attempting to set property on a non-object"); |
163 | 165 |
164 v8::Local<v8::String> property = Utils::ToV8String(jsEngine->isolate, name); | 166 v8::Local<v8::String> property = Utils::ToV8String(jsEngine->GetIsolate(), nam
e); |
165 v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(UnwrapValue()); | 167 v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(UnwrapValue()); |
166 obj->Set(property, val); | 168 obj->Set(property, val); |
167 } | 169 } |
168 | 170 |
169 v8::Local<v8::Value> AdblockPlus::JsValue::UnwrapValue() const | 171 v8::Local<v8::Value> AdblockPlus::JsValue::UnwrapValue() const |
170 { | 172 { |
171 return v8::Local<v8::Value>::New(jsEngine->isolate, *value); | 173 return v8::Local<v8::Value>::New(jsEngine->GetIsolate(), *value); |
172 } | 174 } |
173 | 175 |
174 void AdblockPlus::JsValue::SetProperty(const std::string& name, const std::strin
g& val) | 176 void AdblockPlus::JsValue::SetProperty(const std::string& name, const std::strin
g& val) |
175 { | 177 { |
176 const JsContext context(jsEngine); | 178 const JsContext context(jsEngine); |
177 SetProperty(name, Utils::ToV8String(jsEngine->isolate, val)); | 179 SetProperty(name, Utils::ToV8String(jsEngine->GetIsolate(), val)); |
178 } | 180 } |
179 | 181 |
180 void AdblockPlus::JsValue::SetProperty(const std::string& name, int64_t val) | 182 void AdblockPlus::JsValue::SetProperty(const std::string& name, int64_t val) |
181 { | 183 { |
182 const JsContext context(jsEngine); | 184 const JsContext context(jsEngine); |
183 SetProperty(name, v8::Number::New(jsEngine->isolate, val)); | 185 SetProperty(name, v8::Number::New(jsEngine->GetIsolate(), val)); |
184 } | 186 } |
185 | 187 |
186 void AdblockPlus::JsValue::SetProperty(const std::string& name, const JsValuePtr
& val) | 188 void AdblockPlus::JsValue::SetProperty(const std::string& name, const JsValuePtr
& val) |
187 { | 189 { |
188 const JsContext context(jsEngine); | 190 const JsContext context(jsEngine); |
189 SetProperty(name, val->UnwrapValue()); | 191 SetProperty(name, val->UnwrapValue()); |
190 } | 192 } |
191 | 193 |
192 void AdblockPlus::JsValue::SetProperty(const std::string& name, bool val) | 194 void AdblockPlus::JsValue::SetProperty(const std::string& name, bool val) |
193 { | 195 { |
194 const JsContext context(jsEngine); | 196 const JsContext context(jsEngine); |
195 SetProperty(name, v8::Boolean::New(val)); | 197 SetProperty(name, v8::Boolean::New(val)); |
196 } | 198 } |
197 | 199 |
198 std::string AdblockPlus::JsValue::GetClass() const | 200 std::string AdblockPlus::JsValue::GetClass() const |
199 { | 201 { |
200 if (!IsObject()) | 202 if (!IsObject()) |
201 throw new std::runtime_error("Cannot get constructor of a non-object"); | 203 throw new std::runtime_error("Cannot get constructor of a non-object"); |
202 | 204 |
203 const JsContext context(jsEngine); | 205 const JsContext context(jsEngine); |
204 v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(UnwrapValue()); | 206 v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(UnwrapValue()); |
205 return Utils::FromV8String(obj->GetConstructorName()); | 207 return Utils::FromV8String(obj->GetConstructorName()); |
206 } | 208 } |
207 | 209 |
208 AdblockPlus::JsValue AdblockPlus::JsValue::Call(const JsValueList& params, JsVal
uePtr thisPtr) const | 210 JsValue JsValue::Call(const JsConstValueList& params, JsValuePtr thisPtr) const |
| 211 { |
| 212 const JsContext context(jsEngine); |
| 213 v8::Local<v8::Object> thisObj = thisPtr ? |
| 214 v8::Local<v8::Object>::Cast(thisPtr->UnwrapValue()) : |
| 215 context.GetV8Context()->Global(); |
| 216 |
| 217 std::vector<v8::Handle<v8::Value>> argv; |
| 218 for (const auto& param : params) |
| 219 argv.push_back(param->UnwrapValue()); |
| 220 |
| 221 return Call(argv, thisObj); |
| 222 } |
| 223 |
| 224 JsValue JsValue::Call(const JsValue& arg) const |
| 225 { |
| 226 const JsContext context(jsEngine); |
| 227 |
| 228 std::vector<v8::Handle<v8::Value>> argv; |
| 229 argv.push_back(arg.UnwrapValue()); |
| 230 |
| 231 return Call(argv, context.GetV8Context()->Global()); |
| 232 } |
| 233 |
| 234 JsValue JsValue::Call(std::vector<v8::Handle<v8::Value>>& args, v8::Local<v8::Ob
ject> thisObj) const |
209 { | 235 { |
210 if (!IsFunction()) | 236 if (!IsFunction()) |
211 throw new std::runtime_error("Attempting to call a non-function"); | 237 throw new std::runtime_error("Attempting to call a non-function"); |
212 | 238 if (!thisObj->IsObject()) |
213 const JsContext context(jsEngine); | |
214 if (!thisPtr) | |
215 { | |
216 v8::Local<v8::Context> localContext = v8::Local<v8::Context>::New( | |
217 jsEngine->isolate, *jsEngine->context); | |
218 thisPtr = JsValuePtr(new JsValue(jsEngine, localContext->Global())); | |
219 } | |
220 if (!thisPtr->IsObject()) | |
221 throw new std::runtime_error("`this` pointer has to be an object"); | 239 throw new std::runtime_error("`this` pointer has to be an object"); |
222 v8::Local<v8::Object> thisObj = v8::Local<v8::Object>::Cast(thisPtr->UnwrapVal
ue()); | 240 |
223 | 241 const JsContext context(jsEngine); |
224 std::vector<v8::Handle<v8::Value>> argv; | |
225 for (JsValueList::const_iterator it = params.begin(); it != params.end(); ++it
) | |
226 argv.push_back((*it)->UnwrapValue()); | |
227 | 242 |
228 const v8::TryCatch tryCatch; | 243 const v8::TryCatch tryCatch; |
229 v8::Local<v8::Function> func = v8::Local<v8::Function>::Cast(UnwrapValue()); | 244 v8::Local<v8::Function> func = v8::Local<v8::Function>::Cast(UnwrapValue()); |
230 v8::Local<v8::Value> result = func->Call(thisObj, argv.size(), | 245 v8::Local<v8::Value> result = func->Call(thisObj, args.size(), |
231 argv.size() ? &argv.front() : 0); | 246 args.size() ? &args[0] : nullptr); |
232 | 247 |
233 if (tryCatch.HasCaught()) | 248 if (tryCatch.HasCaught()) |
234 throw JsError(tryCatch.Exception(), tryCatch.Message()); | 249 throw JsError(tryCatch.Exception(), tryCatch.Message()); |
235 | 250 |
236 return JsValue(jsEngine, result); | 251 return JsValue(jsEngine, result); |
237 } | 252 } |
LEFT | RIGHT |