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 15 matching lines...) Expand all Loading... |
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
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/V8BindingMacros.h" | 37 #include "bindings/v8/V8BindingMacros.h" |
37 #include "bindings/v8/V8PerIsolateData.h" | 38 #include "bindings/v8/V8PerIsolateData.h" |
38 #include "bindings/v8/V8StringResource.h" | 39 #include "bindings/v8/V8StringResource.h" |
39 #include "bindings/v8/V8ThrowException.h" | 40 #include "bindings/v8/V8ThrowException.h" |
40 #include "bindings/v8/V8ValueCache.h" | 41 #include "bindings/v8/V8ValueCache.h" |
41 #include "wtf/MathExtras.h" | 42 #include "wtf/MathExtras.h" |
42 #include "wtf/text/AtomicString.h" | 43 #include "wtf/text/AtomicString.h" |
43 #include <v8.h> | 44 #include <v8.h> |
44 | 45 |
45 namespace WebCore { | 46 namespace WebCore { |
(...skipping 21 matching lines...) Expand all Loading... |
67 v8::Handle<v8::Value> throwError(v8::Handle<v8::Value>, v8::Isolate*); | 68 v8::Handle<v8::Value> throwError(v8::Handle<v8::Value>, v8::Isolate*); |
68 | 69 |
69 // A helper for throwing JavaScript TypeError. | 70 // A helper for throwing JavaScript TypeError. |
70 v8::Handle<v8::Value> throwTypeError(const String&, v8::Isolate*); | 71 v8::Handle<v8::Value> throwTypeError(const String&, v8::Isolate*); |
71 | 72 |
72 // FIXME: Remove this once we kill its callers. | 73 // FIXME: Remove this once we kill its callers. |
73 v8::Handle<v8::Value> throwUninformativeAndGenericTypeError(v8::Isolate*); | 74 v8::Handle<v8::Value> throwUninformativeAndGenericTypeError(v8::Isolate*); |
74 | 75 |
75 v8::ArrayBuffer::Allocator* v8ArrayBufferAllocator(); | 76 v8::ArrayBuffer::Allocator* v8ArrayBufferAllocator(); |
76 | 77 |
77 v8::Handle<v8::Value> toV8Sequence(v8::Handle<v8::Value>, uint32_t& length,
bool* notASequence, v8::Isolate*); | 78 v8::Handle<v8::Value> toV8Sequence(v8::Handle<v8::Value>, uint32_t& length,
bool& notASequence, v8::Isolate*); |
78 | 79 |
79 inline v8::Handle<v8::Value> argumentOrNull(const v8::FunctionCallbackInfo<v
8::Value>& args, int index) | 80 inline v8::Handle<v8::Value> argumentOrNull(const v8::FunctionCallbackInfo<v
8::Value>& args, int index) |
80 { | 81 { |
81 return index >= args.Length() ? v8::Local<v8::Value>() : args[index]; | 82 return index >= args.Length() ? v8::Local<v8::Value>() : args[index]; |
82 } | 83 } |
83 | 84 |
84 // Since v8::Null(isolate) crashes if we pass a null isolate, | 85 // Since v8::Null(isolate) crashes if we pass a null isolate, |
85 // we need to use v8NullWithCheck(isolate) if an isolate can be null. | 86 // we need to use v8NullWithCheck(isolate) if an isolate can be null. |
86 // | 87 // |
87 // FIXME: Remove all null isolates from V8 bindings, and remove v8NullWithCh
eck(isolate). | 88 // FIXME: Remove all null isolates from V8 bindings, and remove v8NullWithCh
eck(isolate). |
(...skipping 345 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
433 struct NativeValueTraits<double> { | 434 struct NativeValueTraits<double> { |
434 static inline double nativeValue(const v8::Handle<v8::Value>& value) | 435 static inline double nativeValue(const v8::Handle<v8::Value>& value) |
435 { | 436 { |
436 return static_cast<double>(value->NumberValue()); | 437 return static_cast<double>(value->NumberValue()); |
437 } | 438 } |
438 }; | 439 }; |
439 | 440 |
440 // Converts a JavaScript value to an array as per the Web IDL specification: | 441 // Converts a JavaScript value to an array as per the Web IDL specification: |
441 // http://www.w3.org/TR/2012/CR-WebIDL-20120419/#es-array | 442 // http://www.w3.org/TR/2012/CR-WebIDL-20120419/#es-array |
442 template <class T, class V8T> | 443 template <class T, class V8T> |
443 Vector<RefPtr<T> > toRefPtrNativeArray(v8::Handle<v8::Value> value, v8::Isol
ate* isolate, bool* success = 0) | 444 Vector<RefPtr<T> > toRefPtrNativeArrayUnchecked(v8::Local<v8::Value> v8Value
, uint32_t length, v8::Isolate* isolate, bool* success = 0) |
444 { | 445 { |
445 if (success) | |
446 *success = true; | |
447 | |
448 v8::Local<v8::Value> v8Value(v8::Local<v8::Value>::New(isolate, value)); | |
449 uint32_t length = 0; | |
450 if (value->IsArray()) | |
451 length = v8::Local<v8::Array>::Cast(v8Value)->Length(); | |
452 else if (toV8Sequence(value, length, 0, isolate).IsEmpty()) | |
453 return Vector<RefPtr<T> >(); | |
454 | |
455 Vector<RefPtr<T> > result; | 446 Vector<RefPtr<T> > result; |
456 result.reserveInitialCapacity(length); | 447 result.reserveInitialCapacity(length); |
457 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(v8Value); | 448 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(v8Value); |
458 for (uint32_t i = 0; i < length; ++i) { | 449 for (uint32_t i = 0; i < length; ++i) { |
459 v8::Handle<v8::Value> element = object->Get(i); | 450 v8::Handle<v8::Value> element = object->Get(i); |
460 | 451 |
461 if (V8T::HasInstance(element, isolate, worldType(isolate))) { | 452 if (V8T::HasInstance(element, isolate, worldType(isolate))) { |
462 v8::Handle<v8::Object> elementObject = v8::Handle<v8::Object>::C
ast(element); | 453 v8::Handle<v8::Object> elementObject = v8::Handle<v8::Object>::C
ast(element); |
463 result.uncheckedAppend(V8T::toNative(elementObject)); | 454 result.uncheckedAppend(V8T::toNative(elementObject)); |
464 } else { | 455 } else { |
465 if (success) | 456 if (success) |
466 *success = false; | 457 *success = false; |
467 throwTypeError("Invalid Array element type", isolate); | 458 throwTypeError("Invalid Array element type", isolate); |
468 return Vector<RefPtr<T> >(); | 459 return Vector<RefPtr<T> >(); |
469 } | 460 } |
470 } | 461 } |
471 return result; | 462 return result; |
472 } | 463 } |
473 | 464 |
| 465 template <class T, class V8T> |
| 466 Vector<RefPtr<T> > toRefPtrNativeArray(v8::Handle<v8::Value> value, int argu
mentIndex, v8::Isolate* isolate, bool* success = 0) |
| 467 { |
| 468 if (success) |
| 469 *success = true; |
| 470 |
| 471 v8::Local<v8::Value> v8Value(v8::Local<v8::Value>::New(isolate, value)); |
| 472 uint32_t length = 0; |
| 473 bool notASequence = false; |
| 474 if (value->IsArray()) { |
| 475 length = v8::Local<v8::Array>::Cast(v8Value)->Length(); |
| 476 } else if (toV8Sequence(value, length, notASequence, isolate).IsEmpty())
{ |
| 477 if (notASequence) |
| 478 throwTypeError(ExceptionMessages::notASequenceTypeArgumentOrValu
e(argumentIndex), isolate); |
| 479 return Vector<RefPtr<T> >(); |
| 480 } |
| 481 |
| 482 return toRefPtrNativeArrayUnchecked<T, V8T>(v8Value, length, isolate, su
ccess); |
| 483 } |
| 484 |
| 485 template <class T, class V8T> |
| 486 Vector<RefPtr<T> > toRefPtrNativeArray(v8::Handle<v8::Value> value, const St
ring& propertyName, v8::Isolate* isolate, bool* success = 0) |
| 487 { |
| 488 if (success) |
| 489 *success = true; |
| 490 |
| 491 v8::Local<v8::Value> v8Value(v8::Local<v8::Value>::New(isolate, value)); |
| 492 uint32_t length = 0; |
| 493 bool notASequence = false; |
| 494 if (value->IsArray()) { |
| 495 length = v8::Local<v8::Array>::Cast(v8Value)->Length(); |
| 496 } else if (toV8Sequence(value, length, notASequence, isolate).IsEmpty())
{ |
| 497 if (notASequence) |
| 498 throwTypeError(ExceptionMessages::notASequenceTypeProperty(prope
rtyName), isolate); |
| 499 return Vector<RefPtr<T> >(); |
| 500 } |
| 501 |
| 502 return toRefPtrNativeArrayUnchecked<T, V8T>(v8Value, length, isolate, su
ccess); |
| 503 } |
| 504 |
474 // Converts a JavaScript value to an array as per the Web IDL specification: | 505 // Converts a JavaScript value to an array as per the Web IDL specification: |
475 // http://www.w3.org/TR/2012/CR-WebIDL-20120419/#es-array | 506 // http://www.w3.org/TR/2012/CR-WebIDL-20120419/#es-array |
476 template <class T> | 507 template <class T> |
477 Vector<T> toNativeArray(v8::Handle<v8::Value> value, v8::Isolate* isolate) | 508 Vector<T> toNativeArray(v8::Handle<v8::Value> value, int argumentIndex, v8::
Isolate* isolate) |
478 { | 509 { |
479 v8::Local<v8::Value> v8Value(v8::Local<v8::Value>::New(isolate, value)); | 510 v8::Local<v8::Value> v8Value(v8::Local<v8::Value>::New(isolate, value)); |
480 uint32_t length = 0; | 511 uint32_t length = 0; |
481 if (value->IsArray()) | 512 bool notASequence = false; |
| 513 if (value->IsArray()) { |
482 length = v8::Local<v8::Array>::Cast(v8Value)->Length(); | 514 length = v8::Local<v8::Array>::Cast(v8Value)->Length(); |
483 else if (toV8Sequence(value, length, 0, isolate).IsEmpty()) | 515 } else if (toV8Sequence(value, length, notASequence, isolate).IsEmpty())
{ |
| 516 if (notASequence) |
| 517 throwTypeError(ExceptionMessages::notASequenceTypeArgumentOrValu
e(argumentIndex), isolate); |
484 return Vector<T>(); | 518 return Vector<T>(); |
| 519 } |
485 | 520 |
486 Vector<T> result; | 521 Vector<T> result; |
487 result.reserveInitialCapacity(length); | 522 result.reserveInitialCapacity(length); |
488 typedef NativeValueTraits<T> TraitsType; | 523 typedef NativeValueTraits<T> TraitsType; |
489 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(v8Value); | 524 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(v8Value); |
490 for (uint32_t i = 0; i < length; ++i) | 525 for (uint32_t i = 0; i < length; ++i) |
491 result.uncheckedAppend(TraitsType::nativeValue(object->Get(i))); | 526 result.uncheckedAppend(TraitsType::nativeValue(object->Get(i))); |
492 return result; | 527 return result; |
493 } | 528 } |
494 | 529 |
495 template <class T> | 530 template <class T> |
496 Vector<T> toNativeArguments(const v8::FunctionCallbackInfo<v8::Value>& args,
int startIndex) | 531 Vector<T> toNativeArguments(const v8::FunctionCallbackInfo<v8::Value>& args,
int startIndex) |
497 { | 532 { |
498 ASSERT(startIndex <= args.Length()); | 533 ASSERT(startIndex <= args.Length()); |
499 Vector<T> result; | 534 Vector<T> result; |
500 typedef NativeValueTraits<T> TraitsType; | 535 typedef NativeValueTraits<T> TraitsType; |
501 int length = args.Length(); | 536 int length = args.Length(); |
502 result.reserveInitialCapacity(length); | 537 result.reserveInitialCapacity(length); |
503 for (int i = startIndex; i < length; ++i) | 538 for (int i = startIndex; i < length; ++i) |
504 result.uncheckedAppend(TraitsType::nativeValue(args[i])); | 539 result.uncheckedAppend(TraitsType::nativeValue(args[i])); |
505 return result; | 540 return result; |
506 } | 541 } |
507 | 542 |
508 Vector<v8::Handle<v8::Value> > toVectorOfArguments(const v8::FunctionCallbac
kInfo<v8::Value>& args); | 543 Vector<v8::Handle<v8::Value> > toVectorOfArguments(const v8::FunctionCallbac
kInfo<v8::Value>& args); |
509 | 544 |
510 // Validates that the passed object is a sequence type per WebIDL spec | 545 // Validates that the passed object is a sequence type per WebIDL spec |
511 // http://www.w3.org/TR/2012/CR-WebIDL-20120419/#es-sequence | 546 // http://www.w3.org/TR/2012/CR-WebIDL-20120419/#es-sequence |
512 inline v8::Handle<v8::Value> toV8Sequence(v8::Handle<v8::Value> value, uint3
2_t& length, bool* notASequence, v8::Isolate* isolate) | 547 inline v8::Handle<v8::Value> toV8Sequence(v8::Handle<v8::Value> value, uint3
2_t& length, bool& notASequence, v8::Isolate* isolate) |
513 { | 548 { |
514 // Attempt converting to a sequence if the value is not already an array
but is | 549 // Attempt converting to a sequence if the value is not already an array
but is |
515 // any kind of object except for a native Date object or a native RegExp
object. | 550 // any kind of object except for a native Date object or a native RegExp
object. |
516 ASSERT(!value->IsArray()); | 551 ASSERT(!value->IsArray()); |
517 // FIXME: Do we really need to special case Date and RegExp object? | 552 // FIXME: Do we really need to special case Date and RegExp object? |
518 // https://www.w3.org/Bugs/Public/show_bug.cgi?id=22806 | 553 // https://www.w3.org/Bugs/Public/show_bug.cgi?id=22806 |
519 if (!value->IsObject() || value->IsDate() || value->IsRegExp()) { | 554 if (!value->IsObject() || value->IsDate() || value->IsRegExp()) { |
520 // Signal that the caller must handle the type error. | 555 // Signal that the caller must handle the type error. |
521 if (notASequence) | 556 notASequence = true; |
522 *notASequence = true; | |
523 else | |
524 throwUninformativeAndGenericTypeError(isolate); | |
525 return v8Undefined(); | 557 return v8Undefined(); |
526 } | 558 } |
527 | 559 |
528 v8::Local<v8::Value> v8Value(v8::Local<v8::Value>::New(isolate, value)); | 560 v8::Local<v8::Value> v8Value(v8::Local<v8::Value>::New(isolate, value)); |
529 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(v8Value); | 561 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(v8Value); |
530 | 562 |
531 // FIXME: The specification states that the length property should be us
ed as fallback, if value | 563 // FIXME: The specification states that the length property should be us
ed as fallback, if value |
532 // is not a platform object that supports indexed properties. If it supp
orts indexed properties, | 564 // is not a platform object that supports indexed properties. If it supp
orts indexed properties, |
533 // length should actually be one greater than value’s maximum indexed pr
operty index. | 565 // length should actually be one greater than value’s maximum indexed pr
operty index. |
534 V8TRYCATCH(v8::Local<v8::Value>, lengthValue, object->Get(v8::String::Ne
wSymbol("length"))); | 566 V8TRYCATCH(v8::Local<v8::Value>, lengthValue, object->Get(v8::String::Ne
wSymbol("length"))); |
535 | 567 |
536 if (lengthValue->IsUndefined() || lengthValue->IsNull()) { | 568 if (lengthValue->IsUndefined() || lengthValue->IsNull()) { |
537 // Signal that the caller must handle the type error. | 569 notASequence = true; |
538 if (notASequence) | |
539 *notASequence = true; | |
540 else | |
541 throwUninformativeAndGenericTypeError(isolate); | |
542 return v8Undefined(); | 570 return v8Undefined(); |
543 } | 571 } |
544 | 572 |
545 V8TRYCATCH(uint32_t, sequenceLength, lengthValue->Int32Value()); | 573 V8TRYCATCH(uint32_t, sequenceLength, lengthValue->Int32Value()); |
546 length = sequenceLength; | 574 length = sequenceLength; |
547 | 575 |
548 return v8Value; | 576 return v8Value; |
549 } | 577 } |
550 | 578 |
551 PassRefPtr<NodeFilter> toNodeFilter(v8::Handle<v8::Value>, v8::Isolate*); | 579 PassRefPtr<NodeFilter> toNodeFilter(v8::Handle<v8::Value>, v8::Isolate*); |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
663 | 691 |
664 v8::Isolate* mainThreadIsolate(); | 692 v8::Isolate* mainThreadIsolate(); |
665 v8::Isolate* toIsolate(ExecutionContext*); | 693 v8::Isolate* toIsolate(ExecutionContext*); |
666 v8::Isolate* toIsolate(Frame*); | 694 v8::Isolate* toIsolate(Frame*); |
667 | 695 |
668 // Can only be called by WebKit::initialize | 696 // Can only be called by WebKit::initialize |
669 void setMainThreadIsolate(v8::Isolate*); | 697 void setMainThreadIsolate(v8::Isolate*); |
670 } // namespace WebCore | 698 } // namespace WebCore |
671 | 699 |
672 #endif // V8Binding_h | 700 #endif // V8Binding_h |
OLD | NEW |