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 344 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
432 template<> | 433 template<> |
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 |
443 // To improve error message reporting, separate the conversion of the array | |
444 // elements from the sequence/array type checking. | |
Mike West
2013/10/24 07:08:54
I don't think we need this comment in the code. I
| |
442 template <class T, class V8T> | 445 template <class T, class V8T> |
443 Vector<RefPtr<T> > toRefPtrNativeArray(v8::Handle<v8::Value> value, v8::Isol ate* isolate, bool* success = 0) | 446 Vector<RefPtr<T> > toRefPtrNativeArrayUnchecked(v8::Local<v8::Value> v8Value , uint32_t length, v8::Isolate* isolate, bool* success = 0) |
444 { | 447 { |
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; | 448 Vector<RefPtr<T> > result; |
456 result.reserveInitialCapacity(length); | 449 result.reserveInitialCapacity(length); |
457 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(v8Value); | 450 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(v8Value); |
458 for (uint32_t i = 0; i < length; ++i) { | 451 for (uint32_t i = 0; i < length; ++i) { |
459 v8::Handle<v8::Value> element = object->Get(i); | 452 v8::Handle<v8::Value> element = object->Get(i); |
460 | 453 |
461 if (V8T::HasInstance(element, isolate, worldType(isolate))) { | 454 if (V8T::HasInstance(element, isolate, worldType(isolate))) { |
462 v8::Handle<v8::Object> elementObject = v8::Handle<v8::Object>::C ast(element); | 455 v8::Handle<v8::Object> elementObject = v8::Handle<v8::Object>::C ast(element); |
463 result.uncheckedAppend(V8T::toNative(elementObject)); | 456 result.uncheckedAppend(V8T::toNative(elementObject)); |
464 } else { | 457 } else { |
465 if (success) | 458 if (success) |
466 *success = false; | 459 *success = false; |
467 throwTypeError("Invalid Array element type", isolate); | 460 throwTypeError("Invalid Array element type", isolate); |
468 return Vector<RefPtr<T> >(); | 461 return Vector<RefPtr<T> >(); |
469 } | 462 } |
470 } | 463 } |
471 return result; | 464 return result; |
472 } | 465 } |
473 | 466 |
467 template <class T, class V8T> | |
468 Vector<RefPtr<T> > toRefPtrNativeArray(v8::Handle<v8::Value> value, int argu mentIndex, v8::Isolate* isolate, bool* success = 0) | |
469 { | |
470 if (success) | |
471 *success = true; | |
472 | |
473 v8::Local<v8::Value> v8Value(v8::Local<v8::Value>::New(isolate, value)); | |
474 uint32_t length = 0; | |
475 bool notASequence = false; | |
476 if (value->IsArray()) { | |
477 length = v8::Local<v8::Array>::Cast(v8Value)->Length(); | |
478 } else if (toV8Sequence(value, length, notASequence, isolate).IsEmpty()) { | |
479 if (notASequence) | |
480 throwTypeError(ExceptionMessages::notASequenceType(argumentIndex ), isolate); | |
481 return Vector<RefPtr<T> >(); | |
482 } | |
483 | |
484 return toRefPtrNativeArrayUnchecked<T, V8T>(v8Value, length, isolate, su ccess); | |
485 } | |
486 | |
487 template <class T, class V8T> | |
488 Vector<RefPtr<T> > toRefPtrNativeArray(v8::Handle<v8::Value> value, const St ring& argumentName, const String& argumentKind, v8::Isolate* isolate, bool* succ ess = 0) | |
489 { | |
490 if (success) | |
491 *success = true; | |
492 | |
493 v8::Local<v8::Value> v8Value(v8::Local<v8::Value>::New(isolate, value)); | |
494 uint32_t length = 0; | |
495 bool notASequence = false; | |
496 if (value->IsArray()) { | |
497 length = v8::Local<v8::Array>::Cast(v8Value)->Length(); | |
498 } else if (toV8Sequence(value, length, notASequence, isolate).IsEmpty()) { | |
499 if (notASequence) | |
500 throwTypeError(ExceptionMessages::notASequenceType(argumentName, argumentKind), isolate); | |
501 return Vector<RefPtr<T> >(); | |
502 } | |
503 | |
504 return toRefPtrNativeArrayUnchecked<T, V8T>(v8Value, length, isolate, su ccess); | |
505 } | |
506 | |
474 // Converts a JavaScript value to an array as per the Web IDL specification: | 507 // 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 | 508 // http://www.w3.org/TR/2012/CR-WebIDL-20120419/#es-array |
476 template <class T> | 509 template <class T> |
477 Vector<T> toNativeArray(v8::Handle<v8::Value> value, v8::Isolate* isolate) | 510 Vector<T> toNativeArray(v8::Handle<v8::Value> value, int argumentIndex, v8:: Isolate* isolate) |
478 { | 511 { |
479 v8::Local<v8::Value> v8Value(v8::Local<v8::Value>::New(isolate, value)); | 512 v8::Local<v8::Value> v8Value(v8::Local<v8::Value>::New(isolate, value)); |
480 uint32_t length = 0; | 513 uint32_t length = 0; |
481 if (value->IsArray()) | 514 bool notASequence = false; |
515 if (value->IsArray()) { | |
482 length = v8::Local<v8::Array>::Cast(v8Value)->Length(); | 516 length = v8::Local<v8::Array>::Cast(v8Value)->Length(); |
483 else if (toV8Sequence(value, length, 0, isolate).IsEmpty()) | 517 } else if (toV8Sequence(value, length, notASequence, isolate).IsEmpty()) { |
518 if (notASequence) | |
519 throwTypeError(ExceptionMessages::notASequenceType(argumentIndex ), isolate); | |
484 return Vector<T>(); | 520 return Vector<T>(); |
521 } | |
485 | 522 |
486 Vector<T> result; | 523 Vector<T> result; |
487 result.reserveInitialCapacity(length); | 524 result.reserveInitialCapacity(length); |
488 typedef NativeValueTraits<T> TraitsType; | 525 typedef NativeValueTraits<T> TraitsType; |
489 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(v8Value); | 526 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(v8Value); |
490 for (uint32_t i = 0; i < length; ++i) | 527 for (uint32_t i = 0; i < length; ++i) |
491 result.uncheckedAppend(TraitsType::nativeValue(object->Get(i))); | 528 result.uncheckedAppend(TraitsType::nativeValue(object->Get(i))); |
492 return result; | 529 return result; |
493 } | 530 } |
494 | 531 |
495 template <class T> | 532 template <class T> |
496 Vector<T> toNativeArguments(const v8::FunctionCallbackInfo<v8::Value>& args, int startIndex) | 533 Vector<T> toNativeArguments(const v8::FunctionCallbackInfo<v8::Value>& args, int startIndex) |
497 { | 534 { |
498 ASSERT(startIndex <= args.Length()); | 535 ASSERT(startIndex <= args.Length()); |
499 Vector<T> result; | 536 Vector<T> result; |
500 typedef NativeValueTraits<T> TraitsType; | 537 typedef NativeValueTraits<T> TraitsType; |
501 int length = args.Length(); | 538 int length = args.Length(); |
502 result.reserveInitialCapacity(length); | 539 result.reserveInitialCapacity(length); |
503 for (int i = startIndex; i < length; ++i) | 540 for (int i = startIndex; i < length; ++i) |
504 result.uncheckedAppend(TraitsType::nativeValue(args[i])); | 541 result.uncheckedAppend(TraitsType::nativeValue(args[i])); |
505 return result; | 542 return result; |
506 } | 543 } |
507 | 544 |
508 Vector<v8::Handle<v8::Value> > toVectorOfArguments(const v8::FunctionCallbac kInfo<v8::Value>& args); | 545 Vector<v8::Handle<v8::Value> > toVectorOfArguments(const v8::FunctionCallbac kInfo<v8::Value>& args); |
509 | 546 |
510 // Validates that the passed object is a sequence type per WebIDL spec | 547 // Validates that the passed object is a sequence type per WebIDL spec |
511 // http://www.w3.org/TR/2012/CR-WebIDL-20120419/#es-sequence | 548 // 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) | 549 inline v8::Handle<v8::Value> toV8Sequence(v8::Handle<v8::Value> value, uint3 2_t& length, bool& notASequence, v8::Isolate* isolate) |
513 { | 550 { |
514 // Attempt converting to a sequence if the value is not already an array but is | 551 // 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. | 552 // any kind of object except for a native Date object or a native RegExp object. |
516 ASSERT(!value->IsArray()); | 553 ASSERT(!value->IsArray()); |
517 // FIXME: Do we really need to special case Date and RegExp object? | 554 // FIXME: Do we really need to special case Date and RegExp object? |
518 // https://www.w3.org/Bugs/Public/show_bug.cgi?id=22806 | 555 // https://www.w3.org/Bugs/Public/show_bug.cgi?id=22806 |
519 if (!value->IsObject() || value->IsDate() || value->IsRegExp()) { | 556 if (!value->IsObject() || value->IsDate() || value->IsRegExp()) { |
520 // Signal that the caller must handle the type error. | 557 // Signal that the caller must handle the type error. |
521 if (notASequence) | 558 notASequence = true; |
522 *notASequence = true; | |
523 else | |
524 throwUninformativeAndGenericTypeError(isolate); | |
525 return v8Undefined(); | 559 return v8Undefined(); |
526 } | 560 } |
527 | 561 |
528 v8::Local<v8::Value> v8Value(v8::Local<v8::Value>::New(isolate, value)); | 562 v8::Local<v8::Value> v8Value(v8::Local<v8::Value>::New(isolate, value)); |
529 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(v8Value); | 563 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(v8Value); |
530 | 564 |
531 // FIXME: The specification states that the length property should be us ed as fallback, if value | 565 // 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, | 566 // 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. | 567 // 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"))); | 568 V8TRYCATCH(v8::Local<v8::Value>, lengthValue, object->Get(v8::String::Ne wSymbol("length"))); |
535 | 569 |
536 if (lengthValue->IsUndefined() || lengthValue->IsNull()) { | 570 if (lengthValue->IsUndefined() || lengthValue->IsNull()) { |
537 // Signal that the caller must handle the type error. | 571 notASequence = true; |
538 if (notASequence) | |
539 *notASequence = true; | |
540 else | |
541 throwUninformativeAndGenericTypeError(isolate); | |
542 return v8Undefined(); | 572 return v8Undefined(); |
543 } | 573 } |
544 | 574 |
545 V8TRYCATCH(uint32_t, sequenceLength, lengthValue->Int32Value()); | 575 V8TRYCATCH(uint32_t, sequenceLength, lengthValue->Int32Value()); |
546 length = sequenceLength; | 576 length = sequenceLength; |
547 | 577 |
548 return v8Value; | 578 return v8Value; |
549 } | 579 } |
550 | 580 |
551 PassRefPtr<NodeFilter> toNodeFilter(v8::Handle<v8::Value>, v8::Isolate*); | 581 PassRefPtr<NodeFilter> toNodeFilter(v8::Handle<v8::Value>, v8::Isolate*); |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
663 | 693 |
664 v8::Isolate* mainThreadIsolate(); | 694 v8::Isolate* mainThreadIsolate(); |
665 v8::Isolate* toIsolate(ExecutionContext*); | 695 v8::Isolate* toIsolate(ExecutionContext*); |
666 v8::Isolate* toIsolate(Frame*); | 696 v8::Isolate* toIsolate(Frame*); |
667 | 697 |
668 // Can only be called by WebKit::initialize | 698 // Can only be called by WebKit::initialize |
669 void setMainThreadIsolate(v8::Isolate*); | 699 void setMainThreadIsolate(v8::Isolate*); |
670 } // namespace WebCore | 700 } // namespace WebCore |
671 | 701 |
672 #endif // V8Binding_h | 702 #endif // V8Binding_h |
OLD | NEW |