Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: src/JsValue.cpp

Issue 29418664: Issue 5162 - JsContext() takes a JsEngine& (Closed) Base URL: https://hg.adblockplus.org/libadblockplus/
Patch Set: Created April 20, 2017, 7:53 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/JsEngine.cpp ('k') | src/WebRequestJsObject.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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-2017 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
(...skipping 22 matching lines...) Expand all
33 33
34 AdblockPlus::JsValue::JsValue(AdblockPlus::JsValue&& src) 34 AdblockPlus::JsValue::JsValue(AdblockPlus::JsValue&& src)
35 : jsEngine(src.jsEngine), 35 : jsEngine(src.jsEngine),
36 value(std::move(src.value)) 36 value(std::move(src.value))
37 { 37 {
38 } 38 }
39 39
40 AdblockPlus::JsValue::JsValue(const JsValue& src) 40 AdblockPlus::JsValue::JsValue(const JsValue& src)
41 : jsEngine(src.jsEngine) 41 : jsEngine(src.jsEngine)
42 { 42 {
43 const JsContext context(src.jsEngine); 43 const JsContext context(*src.jsEngine);
44 value.reset(new v8::Persistent<v8::Value>(src.jsEngine->GetIsolate(), *src.val ue)); 44 value.reset(new v8::Persistent<v8::Value>(src.jsEngine->GetIsolate(), *src.val ue));
45 } 45 }
46 46
47 AdblockPlus::JsValue::~JsValue() 47 AdblockPlus::JsValue::~JsValue()
48 { 48 {
49 if (value) 49 if (value)
50 { 50 {
51 const JsContext context(jsEngine); 51 const JsContext context(*jsEngine);
52 value->Dispose(); 52 value->Dispose();
53 value.reset(); 53 value.reset();
54 } 54 }
55 } 55 }
56 56
57 JsValue& AdblockPlus::JsValue::operator=(const JsValue& src) 57 JsValue& AdblockPlus::JsValue::operator=(const JsValue& src)
58 { 58 {
59 const JsContext context(src.jsEngine); 59 const JsContext context(*src.jsEngine);
60 if (value) 60 if (value)
61 value->Dispose(); 61 value->Dispose();
62 jsEngine = src.jsEngine; 62 jsEngine = src.jsEngine;
63 value.reset(new v8::Persistent<v8::Value>(src.jsEngine->GetIsolate(), *src.val ue)); 63 value.reset(new v8::Persistent<v8::Value>(src.jsEngine->GetIsolate(), *src.val ue));
64 64
65 return *this; 65 return *this;
66 } 66 }
67 67
68 bool AdblockPlus::JsValue::IsUndefined() const 68 bool AdblockPlus::JsValue::IsUndefined() const
69 { 69 {
70 const JsContext context(jsEngine); 70 const JsContext context(*jsEngine);
71 return UnwrapValue()->IsUndefined(); 71 return UnwrapValue()->IsUndefined();
72 } 72 }
73 73
74 bool AdblockPlus::JsValue::IsNull() const 74 bool AdblockPlus::JsValue::IsNull() const
75 { 75 {
76 const JsContext context(jsEngine); 76 const JsContext context(*jsEngine);
77 return UnwrapValue()->IsNull(); 77 return UnwrapValue()->IsNull();
78 } 78 }
79 79
80 bool AdblockPlus::JsValue::IsString() const 80 bool AdblockPlus::JsValue::IsString() const
81 { 81 {
82 const JsContext context(jsEngine); 82 const JsContext context(*jsEngine);
83 v8::Local<v8::Value> value = UnwrapValue(); 83 v8::Local<v8::Value> value = UnwrapValue();
84 return value->IsString() || value->IsStringObject(); 84 return value->IsString() || value->IsStringObject();
85 } 85 }
86 86
87 bool AdblockPlus::JsValue::IsNumber() const 87 bool AdblockPlus::JsValue::IsNumber() const
88 { 88 {
89 const JsContext context(jsEngine); 89 const JsContext context(*jsEngine);
90 v8::Local<v8::Value> value = UnwrapValue(); 90 v8::Local<v8::Value> value = UnwrapValue();
91 return value->IsNumber() || value->IsNumberObject(); 91 return value->IsNumber() || value->IsNumberObject();
92 } 92 }
93 93
94 bool AdblockPlus::JsValue::IsBool() const 94 bool AdblockPlus::JsValue::IsBool() const
95 { 95 {
96 const JsContext context(jsEngine); 96 const JsContext context(*jsEngine);
97 v8::Local<v8::Value> value = UnwrapValue(); 97 v8::Local<v8::Value> value = UnwrapValue();
98 return value->IsBoolean() || value->IsBooleanObject(); 98 return value->IsBoolean() || value->IsBooleanObject();
99 } 99 }
100 100
101 bool AdblockPlus::JsValue::IsObject() const 101 bool AdblockPlus::JsValue::IsObject() const
102 { 102 {
103 const JsContext context(jsEngine); 103 const JsContext context(*jsEngine);
104 return UnwrapValue()->IsObject(); 104 return UnwrapValue()->IsObject();
105 } 105 }
106 106
107 bool AdblockPlus::JsValue::IsArray() const 107 bool AdblockPlus::JsValue::IsArray() const
108 { 108 {
109 const JsContext context(jsEngine); 109 const JsContext context(*jsEngine);
110 return UnwrapValue()->IsArray(); 110 return UnwrapValue()->IsArray();
111 } 111 }
112 112
113 bool AdblockPlus::JsValue::IsFunction() const 113 bool AdblockPlus::JsValue::IsFunction() const
114 { 114 {
115 const JsContext context(jsEngine); 115 const JsContext context(*jsEngine);
116 return UnwrapValue()->IsFunction(); 116 return UnwrapValue()->IsFunction();
117 } 117 }
118 118
119 std::string AdblockPlus::JsValue::AsString() const 119 std::string AdblockPlus::JsValue::AsString() const
120 { 120 {
121 const JsContext context(jsEngine); 121 const JsContext context(*jsEngine);
122 return Utils::FromV8String(UnwrapValue()); 122 return Utils::FromV8String(UnwrapValue());
123 } 123 }
124 124
125 int64_t AdblockPlus::JsValue::AsInt() const 125 int64_t AdblockPlus::JsValue::AsInt() const
126 { 126 {
127 const JsContext context(jsEngine); 127 const JsContext context(*jsEngine);
128 return UnwrapValue()->IntegerValue(); 128 return UnwrapValue()->IntegerValue();
129 } 129 }
130 130
131 bool AdblockPlus::JsValue::AsBool() const 131 bool AdblockPlus::JsValue::AsBool() const
132 { 132 {
133 const JsContext context(jsEngine); 133 const JsContext context(*jsEngine);
134 return UnwrapValue()->BooleanValue(); 134 return UnwrapValue()->BooleanValue();
135 } 135 }
136 136
137 AdblockPlus::JsValueList AdblockPlus::JsValue::AsList() const 137 AdblockPlus::JsValueList AdblockPlus::JsValue::AsList() const
138 { 138 {
139 if (!IsArray()) 139 if (!IsArray())
140 throw std::runtime_error("Cannot convert a non-array to list"); 140 throw std::runtime_error("Cannot convert a non-array to list");
141 141
142 const JsContext context(jsEngine); 142 const JsContext context(*jsEngine);
143 JsValueList result; 143 JsValueList result;
144 v8::Local<v8::Array> array = v8::Local<v8::Array>::Cast(UnwrapValue()); 144 v8::Local<v8::Array> array = v8::Local<v8::Array>::Cast(UnwrapValue());
145 uint32_t length = array->Length(); 145 uint32_t length = array->Length();
146 for (uint32_t i = 0; i < length; i++) 146 for (uint32_t i = 0; i < length; i++)
147 { 147 {
148 v8::Local<v8::Value> item = array->Get(i); 148 v8::Local<v8::Value> item = array->Get(i);
149 result.push_back(JsValue(jsEngine, item)); 149 result.push_back(JsValue(jsEngine, item));
150 } 150 }
151 return result; 151 return result;
152 } 152 }
153 153
154 std::vector<std::string> AdblockPlus::JsValue::GetOwnPropertyNames() const 154 std::vector<std::string> AdblockPlus::JsValue::GetOwnPropertyNames() const
155 { 155 {
156 if (!IsObject()) 156 if (!IsObject())
157 throw new std::runtime_error("Attempting to get propert list for a non-objec t"); 157 throw new std::runtime_error("Attempting to get propert list for a non-objec t");
158 158
159 const JsContext context(jsEngine); 159 const JsContext context(*jsEngine);
160 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(UnwrapValue()); 160 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(UnwrapValue());
161 JsValueList properties = JsValue(jsEngine, object->GetOwnPropertyNames()).AsLi st(); 161 JsValueList properties = JsValue(jsEngine, object->GetOwnPropertyNames()).AsLi st();
162 std::vector<std::string> result; 162 std::vector<std::string> result;
163 for (const auto& property : properties) 163 for (const auto& property : properties)
164 result.push_back(property.AsString()); 164 result.push_back(property.AsString());
165 return result; 165 return result;
166 } 166 }
167 167
168 168
169 AdblockPlus::JsValue AdblockPlus::JsValue::GetProperty(const std::string& name) const 169 AdblockPlus::JsValue AdblockPlus::JsValue::GetProperty(const std::string& name) const
170 { 170 {
171 if (!IsObject()) 171 if (!IsObject())
172 throw new std::runtime_error("Attempting to get property of a non-object"); 172 throw new std::runtime_error("Attempting to get property of a non-object");
173 173
174 const JsContext context(jsEngine); 174 const JsContext context(*jsEngine);
175 v8::Local<v8::String> property = Utils::ToV8String(jsEngine->GetIsolate(), nam e); 175 v8::Local<v8::String> property = Utils::ToV8String(jsEngine->GetIsolate(), nam e);
176 v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(UnwrapValue()); 176 v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(UnwrapValue());
177 return JsValue(jsEngine, obj->Get(property)); 177 return JsValue(jsEngine, obj->Get(property));
178 } 178 }
179 179
180 void AdblockPlus::JsValue::SetProperty(const std::string& name, v8::Handle<v8::V alue> val) 180 void AdblockPlus::JsValue::SetProperty(const std::string& name, v8::Handle<v8::V alue> val)
181 { 181 {
182 if (!IsObject()) 182 if (!IsObject())
183 throw new std::runtime_error("Attempting to set property on a non-object"); 183 throw new std::runtime_error("Attempting to set property on a non-object");
184 184
185 v8::Local<v8::String> property = Utils::ToV8String(jsEngine->GetIsolate(), nam e); 185 v8::Local<v8::String> property = Utils::ToV8String(jsEngine->GetIsolate(), nam e);
186 v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(UnwrapValue()); 186 v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(UnwrapValue());
187 obj->Set(property, val); 187 obj->Set(property, val);
188 } 188 }
189 189
190 v8::Local<v8::Value> AdblockPlus::JsValue::UnwrapValue() const 190 v8::Local<v8::Value> AdblockPlus::JsValue::UnwrapValue() const
191 { 191 {
192 return v8::Local<v8::Value>::New(jsEngine->GetIsolate(), *value); 192 return v8::Local<v8::Value>::New(jsEngine->GetIsolate(), *value);
193 } 193 }
194 194
195 void AdblockPlus::JsValue::SetProperty(const std::string& name, const std::strin g& val) 195 void AdblockPlus::JsValue::SetProperty(const std::string& name, const std::strin g& val)
196 { 196 {
197 const JsContext context(jsEngine); 197 const JsContext context(*jsEngine);
198 SetProperty(name, Utils::ToV8String(jsEngine->GetIsolate(), val)); 198 SetProperty(name, Utils::ToV8String(jsEngine->GetIsolate(), val));
199 } 199 }
200 200
201 void AdblockPlus::JsValue::SetProperty(const std::string& name, int64_t val) 201 void AdblockPlus::JsValue::SetProperty(const std::string& name, int64_t val)
202 { 202 {
203 const JsContext context(jsEngine); 203 const JsContext context(*jsEngine);
204 SetProperty(name, v8::Number::New(jsEngine->GetIsolate(), val)); 204 SetProperty(name, v8::Number::New(jsEngine->GetIsolate(), val));
205 } 205 }
206 206
207 void AdblockPlus::JsValue::SetProperty(const std::string& name, const JsValue& v al) 207 void AdblockPlus::JsValue::SetProperty(const std::string& name, const JsValue& v al)
208 { 208 {
209 const JsContext context(jsEngine); 209 const JsContext context(*jsEngine);
210 SetProperty(name, val.UnwrapValue()); 210 SetProperty(name, val.UnwrapValue());
211 } 211 }
212 212
213 void AdblockPlus::JsValue::SetProperty(const std::string& name, bool val) 213 void AdblockPlus::JsValue::SetProperty(const std::string& name, bool val)
214 { 214 {
215 const JsContext context(jsEngine); 215 const JsContext context(*jsEngine);
216 SetProperty(name, v8::Boolean::New(val)); 216 SetProperty(name, v8::Boolean::New(val));
217 } 217 }
218 218
219 std::string AdblockPlus::JsValue::GetClass() const 219 std::string AdblockPlus::JsValue::GetClass() const
220 { 220 {
221 if (!IsObject()) 221 if (!IsObject())
222 throw new std::runtime_error("Cannot get constructor of a non-object"); 222 throw new std::runtime_error("Cannot get constructor of a non-object");
223 223
224 const JsContext context(jsEngine); 224 const JsContext context(*jsEngine);
225 v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(UnwrapValue()); 225 v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(UnwrapValue());
226 return Utils::FromV8String(obj->GetConstructorName()); 226 return Utils::FromV8String(obj->GetConstructorName());
227 } 227 }
228 228
229 JsValue JsValue::Call(const JsValueList& params) const 229 JsValue JsValue::Call(const JsValueList& params) const
230 { 230 {
231 const JsContext context(jsEngine); 231 const JsContext context(*jsEngine);
232 std::vector<v8::Handle<v8::Value>> argv; 232 std::vector<v8::Handle<v8::Value>> argv;
233 for (const auto& param : params) 233 for (const auto& param : params)
234 argv.push_back(param.UnwrapValue()); 234 argv.push_back(param.UnwrapValue());
235 235
236 return Call(argv, context.GetV8Context()->Global()); 236 return Call(argv, context.GetV8Context()->Global());
237 } 237 }
238 238
239 JsValue JsValue::Call(const JsValueList& params, const JsValue& thisValue) const 239 JsValue JsValue::Call(const JsValueList& params, const JsValue& thisValue) const
240 { 240 {
241 const JsContext context(jsEngine); 241 const JsContext context(*jsEngine);
242 v8::Local<v8::Object> thisObj = v8::Local<v8::Object>::Cast(thisValue.UnwrapVa lue()); 242 v8::Local<v8::Object> thisObj = v8::Local<v8::Object>::Cast(thisValue.UnwrapVa lue());
243 243
244 std::vector<v8::Handle<v8::Value>> argv; 244 std::vector<v8::Handle<v8::Value>> argv;
245 for (const auto& param : params) 245 for (const auto& param : params)
246 argv.push_back(param.UnwrapValue()); 246 argv.push_back(param.UnwrapValue());
247 247
248 return Call(argv, thisObj); 248 return Call(argv, thisObj);
249 } 249 }
250 250
251 JsValue JsValue::Call(const JsValue& arg) const 251 JsValue JsValue::Call(const JsValue& arg) const
252 { 252 {
253 const JsContext context(jsEngine); 253 const JsContext context(*jsEngine);
254 254
255 std::vector<v8::Handle<v8::Value>> argv; 255 std::vector<v8::Handle<v8::Value>> argv;
256 argv.push_back(arg.UnwrapValue()); 256 argv.push_back(arg.UnwrapValue());
257 257
258 return Call(argv, context.GetV8Context()->Global()); 258 return Call(argv, context.GetV8Context()->Global());
259 } 259 }
260 260
261 JsValue JsValue::Call(std::vector<v8::Handle<v8::Value>>& args, v8::Local<v8::Ob ject> thisObj) const 261 JsValue JsValue::Call(std::vector<v8::Handle<v8::Value>>& args, v8::Local<v8::Ob ject> thisObj) const
262 { 262 {
263 if (!IsFunction()) 263 if (!IsFunction())
264 throw new std::runtime_error("Attempting to call a non-function"); 264 throw new std::runtime_error("Attempting to call a non-function");
265 if (!thisObj->IsObject()) 265 if (!thisObj->IsObject())
266 throw new std::runtime_error("`this` pointer has to be an object"); 266 throw new std::runtime_error("`this` pointer has to be an object");
267 267
268 const JsContext context(jsEngine); 268 const JsContext context(*jsEngine);
269 269
270 const v8::TryCatch tryCatch; 270 const v8::TryCatch tryCatch;
271 v8::Local<v8::Function> func = v8::Local<v8::Function>::Cast(UnwrapValue()); 271 v8::Local<v8::Function> func = v8::Local<v8::Function>::Cast(UnwrapValue());
272 v8::Local<v8::Value> result = func->Call(thisObj, args.size(), 272 v8::Local<v8::Value> result = func->Call(thisObj, args.size(),
273 args.size() ? &args[0] : nullptr); 273 args.size() ? &args[0] : nullptr);
274 274
275 if (tryCatch.HasCaught()) 275 if (tryCatch.HasCaught())
276 throw JsError(tryCatch.Exception(), tryCatch.Message()); 276 throw JsError(tryCatch.Exception(), tryCatch.Message());
277 277
278 return JsValue(jsEngine, result); 278 return JsValue(jsEngine, result);
279 } 279 }
OLDNEW
« no previous file with comments | « src/JsEngine.cpp ('k') | src/WebRequestJsObject.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld