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 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
209 { | 210 { |
210 ASSERT(isolate); | 211 ASSERT(isolate); |
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 |
220 // Converts a DOM object to a v8 value. | |
221 // This is a no-inline version of toV8(). If you want to call toV8() | |
222 // without creating #include cycles, you can use this function instead. | |
223 // Each specialized implementation will be generated. | |
224 template<typename T> | |
225 v8::Handle<v8::Value> toV8NoInline(T* impl, v8::Handle<v8::Object> creationConte xt, v8::Isolate*); | |
226 | |
227 // Every ValueTraits has the following methods and properties. | |
228 // - arrayV8Value: A conversion function that is used to construct an array. | |
229 // This function may use toV8 and including the associated | |
230 // generated header is needed to call this function. | |
231 // | |
232 // - toV8Value: A general conversion function. This function uses toV8NoInline | |
233 // if necessary, instead of toV8. Hence this function can be | |
234 // called without including a generated header. | |
235 // Some V8ValueTraitses have toV8Value whose type is | |
236 // Handle<Value> toV8Value(const T&, Handle<Object>, Isolate*); | |
237 // and the other have toV8Value whose type is | |
238 // Handle<Value> toV8Value(const T&, Isolate*); | |
239 // . | |
240 // - toV8ValueNeedsCreationContext: set true if the class provides toV8 which | |
241 // requires a creationContext as a parameter. | |
242 // set false otherwise. | |
219 template <class T> | 243 template <class T> |
220 struct V8ValueTraits { | 244 struct V8ValueTraits { |
221 static inline v8::Handle<v8::Value> arrayV8Value(const T& value, v8::Isolate * isolate) | 245 static inline v8::Handle<v8::Value> arrayV8Value(const T& value, v8::Isolate * isolate) |
222 { | 246 { |
223 return toV8(WTF::getPtr(value), v8::Handle<v8::Object>(), isolate); | 247 return toV8(WTF::getPtr(value), v8::Handle<v8::Object>(), isolate); |
224 } | 248 } |
249 static inline v8::Handle<v8::Value> toV8Value(const T& value, v8::Handle<v8: :Object> creationContext, v8::Isolate* isolate) | |
250 { | |
251 return toV8NoInline(WTF::getPtr(value), creationContext, isolate); | |
252 } | |
253 static const bool toV8ValueNeedsCreationContext = true; | |
254 }; | |
255 | |
256 template <typename T> | |
257 struct V8ValueTraits<T*> { | |
258 static inline v8::Handle<v8::Value> arrayV8Value(T* const& value, v8::Isolat e* isolate) | |
259 { | |
260 return toV8(value, v8::Handle<v8::Object>(), isolate); | |
261 } | |
262 static inline v8::Handle<v8::Value> toV8Value(T* const& value, v8::Handle<v8 ::Object> creationContext, v8::Isolate* isolate) | |
263 { | |
264 return toV8NoInline(value, creationContext, isolate); | |
265 } | |
266 static const bool toV8ValueNeedsCreationContext = true; | |
267 }; | |
268 | |
269 template <typename T> | |
270 struct V8ValueTraitsToV8ValueIsIdentialToArrayJsValue { | |
271 static inline v8::Handle<v8::Value> toV8Value(const T& value, v8::Isolate* i solate) | |
272 { | |
273 return V8ValueTraits<T>::arrayV8Value(value, isolate); | |
274 } | |
275 static const bool toV8ValueNeedsCreationContext = false; | |
225 }; | 276 }; |
226 | 277 |
227 template<> | 278 template<> |
228 struct V8ValueTraits<String> { | 279 struct V8ValueTraits<String> : public V8ValueTraitsToV8ValueIsIdentialToArrayJsV alue<String> { |
229 static inline v8::Handle<v8::Value> arrayV8Value(const String& value, v8::Is olate* isolate) | 280 static inline v8::Handle<v8::Value> arrayV8Value(const String& value, v8::Is olate* isolate) |
230 { | 281 { |
231 return v8String(isolate, value); | 282 return v8String(isolate, value); |
232 } | 283 } |
233 }; | 284 }; |
234 | 285 |
235 template<> | 286 template<> |
236 struct V8ValueTraits<AtomicString> { | 287 struct V8ValueTraits<AtomicString> : public V8ValueTraitsToV8ValueIsIdentialToAr rayJsValue<AtomicString> { |
237 static inline v8::Handle<v8::Value> arrayV8Value(const AtomicString& value, v8::Isolate* isolate) | 288 static inline v8::Handle<v8::Value> arrayV8Value(const AtomicString& value, v8::Isolate* isolate) |
238 { | 289 { |
239 return v8String(isolate, value); | 290 return v8String(isolate, value); |
240 } | 291 } |
241 }; | 292 }; |
242 | 293 |
243 template<> | 294 template<> |
244 struct V8ValueTraits<unsigned> { | 295 struct V8ValueTraits<const char*> : public V8ValueTraitsToV8ValueIsIdentialToArr ayJsValue<const char*> { |
296 static inline v8::Handle<v8::Value> arrayV8Value(const char* const& value, v 8::Isolate* isolate) | |
297 { | |
298 return v8String(isolate, value); | |
299 } | |
300 }; | |
301 | |
302 template<> | |
303 struct V8ValueTraits<int> : public V8ValueTraitsToV8ValueIsIdentialToArrayJsValu e<int> { | |
304 static inline v8::Handle<v8::Value> arrayV8Value(const int& value, v8::Isola te* isolate) | |
305 { | |
306 return v8::Integer::New(isolate, value); | |
307 } | |
308 }; | |
309 | |
310 template<> | |
311 struct V8ValueTraits<long> : public V8ValueTraitsToV8ValueIsIdentialToArrayJsVal ue<long> { | |
312 static inline v8::Handle<v8::Value> arrayV8Value(const long& value, v8::Isol ate* isolate) | |
313 { | |
314 return v8::Integer::New(isolate, value); | |
315 } | |
316 }; | |
317 | |
318 template<> | |
319 struct V8ValueTraits<unsigned> : public V8ValueTraitsToV8ValueIsIdentialToArrayJ sValue<unsigned> { | |
245 static inline v8::Handle<v8::Value> arrayV8Value(const unsigned& value, v8:: Isolate* isolate) | 320 static inline v8::Handle<v8::Value> arrayV8Value(const unsigned& value, v8:: Isolate* isolate) |
246 { | 321 { |
247 return v8::Integer::NewFromUnsigned(isolate, value); | 322 return v8::Integer::NewFromUnsigned(isolate, value); |
248 } | 323 } |
249 }; | 324 }; |
250 | 325 |
251 template<> | 326 template<> |
252 struct V8ValueTraits<unsigned long> { | 327 struct V8ValueTraits<unsigned long> : public V8ValueTraitsToV8ValueIsIdentialToA rrayJsValue<unsigned long> { |
253 static inline v8::Handle<v8::Value> arrayV8Value(const unsigned long& value, v8::Isolate* isolate) | 328 static inline v8::Handle<v8::Value> arrayV8Value(const unsigned long& value, v8::Isolate* isolate) |
254 { | 329 { |
255 return v8::Integer::NewFromUnsigned(isolate, value); | 330 return v8::Integer::NewFromUnsigned(isolate, value); |
256 } | 331 } |
257 }; | 332 }; |
258 | 333 |
259 template<> | 334 template<> |
260 struct V8ValueTraits<float> { | 335 struct V8ValueTraits<float> : public V8ValueTraitsToV8ValueIsIdentialToArrayJsVa lue<float> { |
261 static inline v8::Handle<v8::Value> arrayV8Value(const float& value, v8::Iso late* isolate) | 336 static inline v8::Handle<v8::Value> arrayV8Value(const float& value, v8::Iso late* isolate) |
262 { | 337 { |
263 return v8::Number::New(isolate, value); | 338 return v8::Number::New(isolate, value); |
264 } | 339 } |
265 }; | 340 }; |
266 | 341 |
267 template<> | 342 template<> |
268 struct V8ValueTraits<double> { | 343 struct V8ValueTraits<double> : public V8ValueTraitsToV8ValueIsIdentialToArrayJsV alue<double> { |
269 static inline v8::Handle<v8::Value> arrayV8Value(const double& value, v8::Is olate* isolate) | 344 static inline v8::Handle<v8::Value> arrayV8Value(const double& value, v8::Is olate* isolate) |
270 { | 345 { |
271 return v8::Number::New(isolate, value); | 346 return v8::Number::New(isolate, value); |
272 } | 347 } |
273 }; | 348 }; |
274 | 349 |
350 template<> | |
351 struct V8ValueTraits<bool> : public V8ValueTraitsToV8ValueIsIdentialToArrayJsVal ue<bool> { | |
352 static inline v8::Handle<v8::Value> arrayV8Value(const bool& value, v8::Isol ate* isolate) | |
353 { | |
354 return v8::Boolean::New(isolate, value); | |
355 } | |
356 }; | |
357 | |
358 // V8NullType and V8UndefinedType are used only for the value conversion. | |
359 class V8NullType { }; | |
360 class V8UndefinedType { }; | |
361 | |
362 template<> | |
363 struct V8ValueTraits<V8NullType> : public V8ValueTraitsToV8ValueIsIdentialToArra yJsValue<V8NullType> { | |
364 static inline v8::Handle<v8::Value> arrayV8Value(const V8NullType&, v8::Isol ate* isolate) | |
365 { | |
366 return v8::Null(isolate); | |
367 } | |
368 }; | |
369 | |
370 template<> | |
371 struct V8ValueTraits<V8UndefinedType> : public V8ValueTraitsToV8ValueIsIdentialT oArrayJsValue<V8UndefinedType> { | |
372 static inline v8::Handle<v8::Value> arrayV8Value(const V8UndefinedType&, v8: :Isolate* isolate) | |
373 { | |
374 return v8::Undefined(isolate); | |
375 } | |
376 }; | |
377 | |
378 template<> | |
379 struct V8ValueTraits<ScriptValue> : public V8ValueTraitsToV8ValueIsIdentialToArr ayJsValue<ScriptValue> { | |
380 static inline v8::Handle<v8::Value> arrayV8Value(const ScriptValue& value, v 8::Isolate*) | |
381 { | |
382 return value.v8Value(); | |
383 } | |
384 }; | |
385 | |
386 template<> | |
387 struct V8ValueTraits<v8::Handle<v8::Value> > : public V8ValueTraitsToV8ValueIsId entialToArrayJsValue<v8::Handle<v8::Value> > { | |
388 static inline v8::Handle<v8::Value> arrayV8Value(const v8::Handle<v8::Value> & value, v8::Isolate*) | |
389 { | |
390 return value; | |
391 } | |
392 }; | |
393 | |
394 template<> | |
395 struct V8ValueTraits<v8::Local<v8::Value> > : public V8ValueTraitsToV8ValueIsIde ntialToArrayJsValue<v8::Local<v8::Value> > { | |
396 static inline v8::Handle<v8::Value> arrayV8Value(const v8::Local<v8::Value>& value, v8::Isolate*) | |
397 { | |
398 return value; | |
399 } | |
400 }; | |
401 | |
402 template<typename Context, typename Helper> | |
403 struct ToV8ValueHelper { | |
404 template<typename T, bool toV8ValueNeedsCreationContext> | |
405 struct Switch; | |
406 | |
407 template<typename T> | |
408 struct Switch<T, true> { | |
409 static v8::Handle<v8::Value> toV8Value(const T& value, const Context& co ntext, v8::Isolate* isolate) | |
410 { | |
411 return Helper::toV8Value(value, context, isolate); | |
412 } | |
413 }; | |
414 | |
415 template<typename T> | |
416 struct Switch<T, false> { | |
417 static v8::Handle<v8::Value> toV8Value(const T& value, const Context& co ntext, v8::Isolate* isolate) | |
418 { | |
419 return Helper::toV8Value(value, isolate); | |
420 } | |
421 }; | |
422 | |
423 template<typename T> | |
424 static v8::Handle<v8::Value> toV8Value(const T& value, const Context& contex t, v8::Isolate* isolate) | |
425 { | |
426 return Switch<T, V8ValueTraits<T>::toV8ValueNeedsCreationContext>::toV8V alue(value, context, isolate); | |
427 } | |
428 }; | |
429 | |
275 template<typename T, size_t inlineCapacity> | 430 template<typename T, size_t inlineCapacity> |
276 v8::Handle<v8::Value> v8Array(const Vector<T, inlineCapacity>& iterator, v8::Iso late* isolate) | 431 v8::Handle<v8::Value> v8Array(const Vector<T, inlineCapacity>& iterator, v8::Iso late* isolate) |
277 { | 432 { |
278 v8::Local<v8::Array> result = v8::Array::New(isolate, iterator.size()); | 433 v8::Local<v8::Array> result = v8::Array::New(isolate, iterator.size()); |
279 int index = 0; | 434 int index = 0; |
280 typename Vector<T, inlineCapacity>::const_iterator end = iterator.end(); | 435 typename Vector<T, inlineCapacity>::const_iterator end = iterator.end(); |
281 typedef V8ValueTraits<T> TraitsType; | 436 typedef V8ValueTraits<T> TraitsType; |
282 for (typename Vector<T, inlineCapacity>::const_iterator iter = iterator.begi n(); iter != end; ++iter) | 437 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)); | 438 result->Set(v8::Integer::New(isolate, index++), TraitsType::arrayV8Value (*iter, isolate)); |
284 return result; | 439 return result; |
(...skipping 15 matching lines...) Expand all Loading... | |
300 v8::Handle<v8::Value> v8ArrayNoInline(const Vector<T, inlineCapacity>& iterator, v8::Isolate* isolate) | 455 v8::Handle<v8::Value> v8ArrayNoInline(const Vector<T, inlineCapacity>& iterator, v8::Isolate* isolate) |
301 { | 456 { |
302 v8::Local<v8::Array> result = v8::Array::New(isolate, iterator.size()); | 457 v8::Local<v8::Array> result = v8::Array::New(isolate, iterator.size()); |
303 int index = 0; | 458 int index = 0; |
304 typename Vector<T, inlineCapacity>::const_iterator end = iterator.end(); | 459 typename Vector<T, inlineCapacity>::const_iterator end = iterator.end(); |
305 for (typename Vector<T, inlineCapacity>::const_iterator iter = iterator.begi n(); iter != end; ++iter) | 460 for (typename Vector<T, inlineCapacity>::const_iterator iter = iterator.begi n(); iter != end; ++iter) |
306 result->Set(v8::Integer::New(isolate, index++), toV8NoInline(WTF::getPtr (*iter), v8::Handle<v8::Object>(), isolate)); | 461 result->Set(v8::Integer::New(isolate, index++), toV8NoInline(WTF::getPtr (*iter), v8::Handle<v8::Object>(), isolate)); |
307 return result; | 462 return result; |
308 } | 463 } |
309 | 464 |
465 template<typename T, size_t inlinCapacity> | |
haraken
2014/04/18 03:59:58
inlineCapacity
| |
466 struct V8ValueTraits<Vector<T, inlinCapacity> > { | |
467 static inline v8::Handle<v8::Value> arrayV8Value(const Vector<T, inlinCapaci ty>& value, v8::Isolate* isolate) | |
468 { | |
469 return v8Array(value, isolate); | |
470 } | |
471 static inline v8::Handle<v8::Value> toV8Value(const Vector<T, inlinCapacity> & value, v8::Isolate* isolate) | |
472 { | |
473 return v8ArrayNoInline(value, isolate); | |
474 } | |
475 static const bool toV8ValueNeedsCreationContext = false; | |
476 }; | |
477 | |
310 // Conversion flags, used in toIntXX/toUIntXX. | 478 // Conversion flags, used in toIntXX/toUIntXX. |
311 enum IntegerConversionConfiguration { | 479 enum IntegerConversionConfiguration { |
312 NormalConversion, | 480 NormalConversion, |
313 EnforceRange, | 481 EnforceRange, |
314 Clamp | 482 Clamp |
315 }; | 483 }; |
316 | 484 |
317 // Convert a value to a 8-bit signed integer. The conversion fails if the | 485 // Convert a value to a 8-bit signed integer. The conversion fails if the |
318 // value cannot be converted to a number or the range violated per WebIDL: | 486 // value cannot be converted to a number or the range violated per WebIDL: |
319 // http://www.w3.org/TR/WebIDL/#es-byte | 487 // http://www.w3.org/TR/WebIDL/#es-byte |
(...skipping 383 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
703 properties->Set(integer, integer); | 871 properties->Set(integer, integer); |
704 } | 872 } |
705 v8SetReturnValue(info, properties); | 873 v8SetReturnValue(info, properties); |
706 } | 874 } |
707 | 875 |
708 // These methods store hidden values into an array that is stored in the interna l field of a DOM wrapper. | 876 // These methods store hidden values into an array that is stored in the interna l field of a DOM wrapper. |
709 void addHiddenValueToArray(v8::Handle<v8::Object>, v8::Local<v8::Value>, int cac heIndex, v8::Isolate*); | 877 void addHiddenValueToArray(v8::Handle<v8::Object>, v8::Local<v8::Value>, int cac heIndex, v8::Isolate*); |
710 void removeHiddenValueFromArray(v8::Handle<v8::Object>, v8::Local<v8::Value>, in t cacheIndex, v8::Isolate*); | 878 void removeHiddenValueFromArray(v8::Handle<v8::Object>, v8::Local<v8::Value>, in t cacheIndex, v8::Isolate*); |
711 void moveEventListenerToNewWrapper(v8::Handle<v8::Object>, EventListener* oldVal ue, v8::Local<v8::Value> newValue, int cacheIndex, v8::Isolate*); | 879 void moveEventListenerToNewWrapper(v8::Handle<v8::Object>, EventListener* oldVal ue, v8::Local<v8::Value> newValue, int cacheIndex, v8::Isolate*); |
712 | 880 |
713 // Converts a DOM object to a v8 value. | |
714 // This is a no-inline version of toV8(). If you want to call toV8() | |
715 // without creating #include cycles, you can use this function instead. | |
716 // Each specialized implementation will be generated. | |
717 template<typename T> | |
718 v8::Handle<v8::Value> toV8NoInline(T* impl, v8::Handle<v8::Object> creationConte xt, v8::Isolate*); | |
719 template<typename T> | 881 template<typename T> |
720 v8::Handle<v8::Value> toV8NoInline(T* impl, ExecutionContext* context) | 882 v8::Handle<v8::Value> toV8NoInline(T* impl, ExecutionContext* context) |
721 { | 883 { |
722 v8::Isolate* isolate = toIsolate(context); | 884 v8::Isolate* isolate = toIsolate(context); |
723 v8::Handle<v8::Context> v8Context = toV8Context(context, DOMWrapperWorld::cu rrent(isolate)); | 885 v8::Handle<v8::Context> v8Context = toV8Context(context, DOMWrapperWorld::cu rrent(isolate)); |
724 return toV8NoInline(impl, v8Context->Global(), isolate); | 886 return toV8NoInline(impl, v8Context->Global(), isolate); |
725 } | 887 } |
726 | 888 |
727 // Result values for platform object 'deleter' methods, | 889 // Result values for platform object 'deleter' methods, |
728 // http://www.w3.org/TR/WebIDL/#delete | 890 // http://www.w3.org/TR/WebIDL/#delete |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
762 | 924 |
763 private: | 925 private: |
764 v8::HandleScope m_handleScope; | 926 v8::HandleScope m_handleScope; |
765 v8::Context::Scope m_contextScope; | 927 v8::Context::Scope m_contextScope; |
766 RefPtr<NewScriptState> m_scriptState; | 928 RefPtr<NewScriptState> m_scriptState; |
767 }; | 929 }; |
768 | 930 |
769 } // namespace WebCore | 931 } // namespace WebCore |
770 | 932 |
771 #endif // V8Binding_h | 933 #endif // V8Binding_h |
OLD | NEW |