OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2009 Google Inc. All rights reserved. | 2 * Copyright (C) 2009 Google Inc. All rights reserved. |
3 * Copyright (C) 2012 Ericsson AB. All rights reserved. | 3 * Copyright (C) 2012 Ericsson AB. All rights reserved. |
4 * | 4 * |
5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
6 * modification, are permitted provided that the following conditions are | 6 * modification, are permitted provided that the following conditions are |
7 * met: | 7 * met: |
8 * | 8 * |
9 * * Redistributions of source code must retain the above copyright | 9 * * Redistributions of source code must retain the above copyright |
10 * notice, this list of conditions and the following disclaimer. | 10 * notice, this list of conditions and the following disclaimer. |
(...skipping 16 matching lines...) Expand all Loading... |
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
30 */ | 30 */ |
31 | 31 |
32 #ifndef V8Binding_h | 32 #ifndef V8Binding_h |
33 #define V8Binding_h | 33 #define V8Binding_h |
34 | 34 |
35 #include "bindings/v8/DOMWrapperWorld.h" | 35 #include "bindings/v8/DOMWrapperWorld.h" |
36 #include "bindings/v8/ExceptionMessages.h" | 36 #include "bindings/v8/ExceptionMessages.h" |
| 37 #include "bindings/v8/ScriptValue.h" |
37 #include "bindings/v8/V8BindingMacros.h" | 38 #include "bindings/v8/V8BindingMacros.h" |
38 #include "bindings/v8/V8PerIsolateData.h" | 39 #include "bindings/v8/V8PerIsolateData.h" |
39 #include "bindings/v8/V8StringResource.h" | 40 #include "bindings/v8/V8StringResource.h" |
40 #include "bindings/v8/V8ThrowException.h" | 41 #include "bindings/v8/V8ThrowException.h" |
41 #include "bindings/v8/V8ValueCache.h" | 42 #include "bindings/v8/V8ValueCache.h" |
42 #include "platform/heap/Heap.h" | 43 #include "platform/heap/Heap.h" |
43 #include "wtf/MathExtras.h" | 44 #include "wtf/MathExtras.h" |
44 #include "wtf/text/AtomicString.h" | 45 #include "wtf/text/AtomicString.h" |
45 #include <v8.h> | 46 #include <v8.h> |
46 | 47 |
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
211 return v8::String::NewFromUtf8(isolate, str, v8::String::kInternalizedString
, length); | 212 return v8::String::NewFromUtf8(isolate, str, v8::String::kInternalizedString
, length); |
212 } | 213 } |
213 | 214 |
214 inline v8::Handle<v8::Value> v8Undefined() | 215 inline v8::Handle<v8::Value> v8Undefined() |
215 { | 216 { |
216 return v8::Handle<v8::Value>(); | 217 return v8::Handle<v8::Value>(); |
217 } | 218 } |
218 | 219 |
219 template <class T> | 220 template <class T> |
220 struct V8ValueTraits { | 221 struct V8ValueTraits { |
221 static inline v8::Handle<v8::Value> arrayV8Value(const T& value, v8::Isolate
* isolate) | 222 // FIXME: This function requires the associated generated header to be |
| 223 // included. Also, this function does not match with other V8ValueTraits |
| 224 // classes. Remove this V8ValueTraits if possible. |
| 225 static inline v8::Handle<v8::Value> toV8Value(const T& value, v8::Isolate* i
solate) |
222 { | 226 { |
223 return toV8(WTF::getPtr(value), v8::Handle<v8::Object>(), isolate); | 227 return toV8(WTF::getPtr(value), v8::Handle<v8::Object>(), isolate); |
224 } | 228 } |
225 }; | 229 }; |
226 | 230 |
227 template<> | 231 template<> |
228 struct V8ValueTraits<String> { | 232 struct V8ValueTraits<String> { |
229 static inline v8::Handle<v8::Value> arrayV8Value(const String& value, v8::Is
olate* isolate) | 233 static inline v8::Handle<v8::Value> toV8Value(const String& value, v8::Isola
te* isolate) |
230 { | 234 { |
231 return v8String(isolate, value); | 235 return v8String(isolate, value); |
232 } | 236 } |
233 }; | 237 }; |
234 | 238 |
235 template<> | 239 template<> |
236 struct V8ValueTraits<AtomicString> { | 240 struct V8ValueTraits<AtomicString> { |
237 static inline v8::Handle<v8::Value> arrayV8Value(const AtomicString& value,
v8::Isolate* isolate) | 241 static inline v8::Handle<v8::Value> toV8Value(const AtomicString& value, v8:
:Isolate* isolate) |
| 242 { |
| 243 return v8String(isolate, value); |
| 244 } |
| 245 }; |
| 246 |
| 247 template<size_t n> |
| 248 struct V8ValueTraits<char[n]> { |
| 249 static inline v8::Handle<v8::Value> toV8Value(char const (&value)[n], v8::Is
olate* isolate) |
238 { | 250 { |
239 return v8String(isolate, value); | 251 return v8String(isolate, value); |
240 } | 252 } |
241 }; | 253 }; |
242 | 254 |
243 template<> | 255 template<> |
| 256 struct V8ValueTraits<const char*> { |
| 257 static inline v8::Handle<v8::Value> toV8Value(const char* const& value, v8::
Isolate* isolate) |
| 258 { |
| 259 return v8String(isolate, value); |
| 260 } |
| 261 }; |
| 262 |
| 263 template<> |
| 264 struct V8ValueTraits<int> { |
| 265 static inline v8::Handle<v8::Value> toV8Value(const int& value, v8::Isolate*
isolate) |
| 266 { |
| 267 return v8::Integer::New(isolate, value); |
| 268 } |
| 269 }; |
| 270 |
| 271 template<> |
| 272 struct V8ValueTraits<long> { |
| 273 static inline v8::Handle<v8::Value> toV8Value(const long& value, v8::Isolate
* isolate) |
| 274 { |
| 275 return v8::Integer::New(isolate, value); |
| 276 } |
| 277 }; |
| 278 |
| 279 template<> |
244 struct V8ValueTraits<unsigned> { | 280 struct V8ValueTraits<unsigned> { |
245 static inline v8::Handle<v8::Value> arrayV8Value(const unsigned& value, v8::
Isolate* isolate) | 281 static inline v8::Handle<v8::Value> toV8Value(const unsigned& value, v8::Iso
late* isolate) |
246 { | 282 { |
247 return v8::Integer::NewFromUnsigned(isolate, value); | 283 return v8::Integer::NewFromUnsigned(isolate, value); |
248 } | 284 } |
249 }; | 285 }; |
250 | 286 |
251 template<> | 287 template<> |
252 struct V8ValueTraits<unsigned long> { | 288 struct V8ValueTraits<unsigned long> { |
253 static inline v8::Handle<v8::Value> arrayV8Value(const unsigned long& value,
v8::Isolate* isolate) | 289 static inline v8::Handle<v8::Value> toV8Value(const unsigned long& value, v8
::Isolate* isolate) |
254 { | 290 { |
255 return v8::Integer::NewFromUnsigned(isolate, value); | 291 return v8::Integer::NewFromUnsigned(isolate, value); |
256 } | 292 } |
257 }; | 293 }; |
258 | 294 |
259 template<> | 295 template<> |
260 struct V8ValueTraits<float> { | 296 struct V8ValueTraits<float> { |
261 static inline v8::Handle<v8::Value> arrayV8Value(const float& value, v8::Iso
late* isolate) | 297 static inline v8::Handle<v8::Value> toV8Value(const float& value, v8::Isolat
e* isolate) |
262 { | 298 { |
263 return v8::Number::New(isolate, value); | 299 return v8::Number::New(isolate, value); |
264 } | 300 } |
265 }; | 301 }; |
266 | 302 |
267 template<> | 303 template<> |
268 struct V8ValueTraits<double> { | 304 struct V8ValueTraits<double> { |
269 static inline v8::Handle<v8::Value> arrayV8Value(const double& value, v8::Is
olate* isolate) | 305 static inline v8::Handle<v8::Value> toV8Value(const double& value, v8::Isola
te* isolate) |
270 { | 306 { |
271 return v8::Number::New(isolate, value); | 307 return v8::Number::New(isolate, value); |
272 } | 308 } |
273 }; | 309 }; |
274 | 310 |
| 311 template<> |
| 312 struct V8ValueTraits<bool> { |
| 313 static inline v8::Handle<v8::Value> toV8Value(const bool& value, v8::Isolate
* isolate) |
| 314 { |
| 315 return v8::Boolean::New(isolate, value); |
| 316 } |
| 317 }; |
| 318 |
| 319 // V8NullType and V8UndefinedType are used only for the value conversion. |
| 320 class V8NullType { }; |
| 321 class V8UndefinedType { }; |
| 322 |
| 323 template<> |
| 324 struct V8ValueTraits<V8NullType> { |
| 325 static inline v8::Handle<v8::Value> toV8Value(const V8NullType&, v8::Isolate
* isolate) |
| 326 { |
| 327 return v8::Null(isolate); |
| 328 } |
| 329 }; |
| 330 |
| 331 template<> |
| 332 struct V8ValueTraits<V8UndefinedType> { |
| 333 static inline v8::Handle<v8::Value> toV8Value(const V8UndefinedType&, v8::Is
olate* isolate) |
| 334 { |
| 335 return v8::Undefined(isolate); |
| 336 } |
| 337 }; |
| 338 |
| 339 template<> |
| 340 struct V8ValueTraits<ScriptValue> { |
| 341 static inline v8::Handle<v8::Value> toV8Value(const ScriptValue& value, v8::
Isolate*) |
| 342 { |
| 343 return value.v8Value(); |
| 344 } |
| 345 }; |
| 346 |
| 347 template<> |
| 348 struct V8ValueTraits<v8::Handle<v8::Value> > { |
| 349 static inline v8::Handle<v8::Value> toV8Value(const v8::Handle<v8::Value>& v
alue, v8::Isolate*) |
| 350 { |
| 351 return value; |
| 352 } |
| 353 }; |
| 354 |
| 355 template<> |
| 356 struct V8ValueTraits<v8::Local<v8::Value> > { |
| 357 static inline v8::Handle<v8::Value> toV8Value(const v8::Local<v8::Value>& va
lue, v8::Isolate*) |
| 358 { |
| 359 return value; |
| 360 } |
| 361 }; |
| 362 |
275 template<typename T, size_t inlineCapacity> | 363 template<typename T, size_t inlineCapacity> |
276 v8::Handle<v8::Value> v8Array(const Vector<T, inlineCapacity>& iterator, v8::Iso
late* isolate) | 364 v8::Handle<v8::Value> v8Array(const Vector<T, inlineCapacity>& iterator, v8::Iso
late* isolate) |
277 { | 365 { |
278 v8::Local<v8::Array> result = v8::Array::New(isolate, iterator.size()); | 366 v8::Local<v8::Array> result = v8::Array::New(isolate, iterator.size()); |
279 int index = 0; | 367 int index = 0; |
280 typename Vector<T, inlineCapacity>::const_iterator end = iterator.end(); | 368 typename Vector<T, inlineCapacity>::const_iterator end = iterator.end(); |
281 typedef V8ValueTraits<T> TraitsType; | 369 typedef V8ValueTraits<T> TraitsType; |
282 for (typename Vector<T, inlineCapacity>::const_iterator iter = iterator.begi
n(); iter != end; ++iter) | 370 for (typename Vector<T, inlineCapacity>::const_iterator iter = iterator.begi
n(); iter != end; ++iter) |
283 result->Set(v8::Integer::New(isolate, index++), TraitsType::arrayV8Value
(*iter, isolate)); | 371 result->Set(v8::Integer::New(isolate, index++), TraitsType::toV8Value(*i
ter, isolate)); |
284 return result; | 372 return result; |
285 } | 373 } |
286 | 374 |
287 template<typename T, size_t inlineCapacity> | 375 template<typename T, size_t inlineCapacity> |
288 v8::Handle<v8::Value> v8Array(const HeapVector<T, inlineCapacity>& iterator, v8:
:Isolate* isolate) | 376 v8::Handle<v8::Value> v8Array(const HeapVector<T, inlineCapacity>& iterator, v8:
:Isolate* isolate) |
289 { | 377 { |
290 v8::Local<v8::Array> result = v8::Array::New(isolate, iterator.size()); | 378 v8::Local<v8::Array> result = v8::Array::New(isolate, iterator.size()); |
291 int index = 0; | 379 int index = 0; |
292 typename HeapVector<T, inlineCapacity>::const_iterator end = iterator.end(); | 380 typename HeapVector<T, inlineCapacity>::const_iterator end = iterator.end(); |
293 typedef V8ValueTraits<T> TraitsType; | 381 typedef V8ValueTraits<T> TraitsType; |
294 for (typename HeapVector<T, inlineCapacity>::const_iterator iter = iterator.
begin(); iter != end; ++iter) | 382 for (typename HeapVector<T, inlineCapacity>::const_iterator iter = iterator.
begin(); iter != end; ++iter) |
295 result->Set(v8::Integer::New(isolate, index++), TraitsType::arrayV8Value
(*iter, isolate)); | 383 result->Set(v8::Integer::New(isolate, index++), TraitsType::toV8Value(*i
ter, isolate)); |
296 return result; | 384 return result; |
297 } | 385 } |
298 | 386 |
299 template<typename T, size_t inlineCapacity> | 387 template<typename T, size_t inlineCapacity> |
300 v8::Handle<v8::Value> v8ArrayNoInline(const Vector<T, inlineCapacity>& iterator,
v8::Isolate* isolate) | 388 v8::Handle<v8::Value> v8ArrayNoInline(const Vector<T, inlineCapacity>& iterator,
v8::Isolate* isolate) |
301 { | 389 { |
302 v8::Local<v8::Array> result = v8::Array::New(isolate, iterator.size()); | 390 v8::Local<v8::Array> result = v8::Array::New(isolate, iterator.size()); |
303 int index = 0; | 391 int index = 0; |
304 typename Vector<T, inlineCapacity>::const_iterator end = iterator.end(); | 392 typename Vector<T, inlineCapacity>::const_iterator end = iterator.end(); |
305 for (typename Vector<T, inlineCapacity>::const_iterator iter = iterator.begi
n(); iter != end; ++iter) | 393 for (typename Vector<T, inlineCapacity>::const_iterator iter = iterator.begi
n(); iter != end; ++iter) |
(...skipping 456 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
762 | 850 |
763 private: | 851 private: |
764 v8::HandleScope m_handleScope; | 852 v8::HandleScope m_handleScope; |
765 v8::Context::Scope m_contextScope; | 853 v8::Context::Scope m_contextScope; |
766 RefPtr<NewScriptState> m_scriptState; | 854 RefPtr<NewScriptState> m_scriptState; |
767 }; | 855 }; |
768 | 856 |
769 } // namespace WebCore | 857 } // namespace WebCore |
770 | 858 |
771 #endif // V8Binding_h | 859 #endif // V8Binding_h |
OLD | NEW |