Left: | ||
Right: |
OLD | NEW |
---|---|
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-present eyeo GmbH | 3 * Copyright (C) 2006-present 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 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
129 | 129 |
130 StringBuffer AdblockPlus::JsValue::AsStringBuffer() const | 130 StringBuffer AdblockPlus::JsValue::AsStringBuffer() const |
131 { | 131 { |
132 const JsContext context(*jsEngine); | 132 const JsContext context(*jsEngine); |
133 return Utils::StringBufferFromV8String(jsEngine->GetIsolate(), UnwrapValue()); | 133 return Utils::StringBufferFromV8String(jsEngine->GetIsolate(), UnwrapValue()); |
134 } | 134 } |
135 | 135 |
136 int64_t AdblockPlus::JsValue::AsInt() const | 136 int64_t AdblockPlus::JsValue::AsInt() const |
137 { | 137 { |
138 const JsContext context(*jsEngine); | 138 const JsContext context(*jsEngine); |
139 return UnwrapValue()->IntegerValue(); | 139 auto currentContext = jsEngine->GetIsolate()->GetCurrentContext(); |
140 auto value = UnwrapValue()->IntegerValue(currentContext); | |
141 return value.IsJust() ? value.FromJust() : 0; | |
sergei
2018/08/07 13:14:52
What about using of CheckedToValue here?
Returning
hub
2018/08/07 14:36:48
I wanted to avoid throwing an exception. But it se
| |
140 } | 142 } |
141 | 143 |
142 bool AdblockPlus::JsValue::AsBool() const | 144 bool AdblockPlus::JsValue::AsBool() const |
143 { | 145 { |
144 const JsContext context(*jsEngine); | 146 const JsContext context(*jsEngine); |
145 return UnwrapValue()->BooleanValue(); | 147 auto currentContext = jsEngine->GetIsolate()->GetCurrentContext(); |
148 auto value = UnwrapValue()->BooleanValue(currentContext); | |
149 return value.IsJust() ? value.FromJust() : false; | |
146 } | 150 } |
147 | 151 |
148 AdblockPlus::JsValueList AdblockPlus::JsValue::AsList() const | 152 AdblockPlus::JsValueList AdblockPlus::JsValue::AsList() const |
149 { | 153 { |
150 if (!IsArray()) | 154 if (!IsArray()) |
151 throw std::runtime_error("Cannot convert a non-array to list"); | 155 throw std::runtime_error("Cannot convert a non-array to list"); |
152 | 156 |
153 const JsContext context(*jsEngine); | 157 const JsContext context(*jsEngine); |
158 auto isolate = jsEngine->GetIsolate(); | |
159 auto currentContext = isolate->GetCurrentContext(); | |
154 JsValueList result; | 160 JsValueList result; |
155 v8::Local<v8::Array> array = v8::Local<v8::Array>::Cast(UnwrapValue()); | 161 v8::Local<v8::Array> array = v8::Local<v8::Array>::Cast(UnwrapValue()); |
156 uint32_t length = array->Length(); | 162 uint32_t length = array->Length(); |
157 for (uint32_t i = 0; i < length; i++) | 163 for (uint32_t i = 0; i < length; i++) |
158 { | 164 { |
159 v8::Local<v8::Value> item = array->Get(i); | 165 v8::Local<v8::Value> item = CHECKED_TO_LOCAL( |
166 isolate, array->Get(currentContext, i)); | |
160 result.push_back(JsValue(jsEngine, item)); | 167 result.push_back(JsValue(jsEngine, item)); |
161 } | 168 } |
162 return result; | 169 return result; |
163 } | 170 } |
164 | 171 |
165 std::vector<std::string> AdblockPlus::JsValue::GetOwnPropertyNames() const | 172 std::vector<std::string> AdblockPlus::JsValue::GetOwnPropertyNames() const |
166 { | 173 { |
167 if (!IsObject()) | 174 if (!IsObject()) |
168 throw std::runtime_error("Attempting to get propert list for a non-object"); | 175 throw std::runtime_error("Attempting to get propert list for a non-object"); |
169 | 176 |
170 const JsContext context(*jsEngine); | 177 const JsContext context(*jsEngine); |
178 auto isolate = jsEngine->GetIsolate(); | |
171 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(UnwrapValue()); | 179 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(UnwrapValue()); |
172 JsValueList properties = JsValue(jsEngine, object->GetOwnPropertyNames()).AsLi st(); | 180 auto propertyNames = CHECKED_TO_LOCAL(isolate, |
181 object->GetOwnPropertyNames(isolate->GetCurrentContext())); | |
182 JsValueList properties = JsValue(jsEngine, propertyNames).AsList(); | |
173 std::vector<std::string> result; | 183 std::vector<std::string> result; |
174 for (const auto& property : properties) | 184 for (const auto& property : properties) |
175 result.push_back(property.AsString()); | 185 result.push_back(property.AsString()); |
176 return result; | 186 return result; |
177 } | 187 } |
178 | 188 |
179 | 189 |
180 AdblockPlus::JsValue AdblockPlus::JsValue::GetProperty(const std::string& name) const | 190 AdblockPlus::JsValue AdblockPlus::JsValue::GetProperty(const std::string& name) const |
181 { | 191 { |
182 if (!IsObject()) | 192 if (!IsObject()) |
(...skipping 10 matching lines...) Expand all Loading... | |
193 | 203 |
194 void AdblockPlus::JsValue::SetProperty(const std::string& name, v8::Local<v8::Va lue> val) | 204 void AdblockPlus::JsValue::SetProperty(const std::string& name, v8::Local<v8::Va lue> val) |
195 { | 205 { |
196 if (!IsObject()) | 206 if (!IsObject()) |
197 throw std::runtime_error("Attempting to set property on a non-object"); | 207 throw std::runtime_error("Attempting to set property on a non-object"); |
198 auto isolate = jsEngine->GetIsolate(); | 208 auto isolate = jsEngine->GetIsolate(); |
199 | 209 |
200 v8::Local<v8::String> property = CHECKED_TO_LOCAL( | 210 v8::Local<v8::String> property = CHECKED_TO_LOCAL( |
201 isolate, Utils::ToV8String(isolate, name)); | 211 isolate, Utils::ToV8String(isolate, name)); |
202 v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(UnwrapValue()); | 212 v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(UnwrapValue()); |
203 obj->Set(property, val); | 213 CHECKED_TO_VALUE(obj->Set(isolate->GetCurrentContext(), property, val)); |
204 } | 214 } |
205 | 215 |
206 v8::Local<v8::Value> AdblockPlus::JsValue::UnwrapValue() const | 216 v8::Local<v8::Value> AdblockPlus::JsValue::UnwrapValue() const |
207 { | 217 { |
208 return v8::Local<v8::Value>::New(jsEngine->GetIsolate(), *value); | 218 return v8::Local<v8::Value>::New(jsEngine->GetIsolate(), *value); |
209 } | 219 } |
210 | 220 |
211 void AdblockPlus::JsValue::SetProperty(const std::string& name, const std::strin g& val) | 221 void AdblockPlus::JsValue::SetProperty(const std::string& name, const std::strin g& val) |
212 { | 222 { |
213 const JsContext context(*jsEngine); | 223 const JsContext context(*jsEngine); |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
297 auto isolate = jsEngine->GetIsolate(); | 307 auto isolate = jsEngine->GetIsolate(); |
298 | 308 |
299 const v8::TryCatch tryCatch(isolate); | 309 const v8::TryCatch tryCatch(isolate); |
300 v8::Local<v8::Function> func = v8::Local<v8::Function>::Cast(UnwrapValue()); | 310 v8::Local<v8::Function> func = v8::Local<v8::Function>::Cast(UnwrapValue()); |
301 auto result = CHECKED_TO_LOCAL_WITH_TRY_CATCH( | 311 auto result = CHECKED_TO_LOCAL_WITH_TRY_CATCH( |
302 isolate, func->Call(isolate->GetCurrentContext(), | 312 isolate, func->Call(isolate->GetCurrentContext(), |
303 thisObj, args.size(), args.size() ? &args[0] : nullptr), tryCatch); | 313 thisObj, args.size(), args.size() ? &args[0] : nullptr), tryCatch); |
304 | 314 |
305 return JsValue(jsEngine, result); | 315 return JsValue(jsEngine, result); |
306 } | 316 } |
OLD | NEW |