Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(19)

Side by Side Diff: third_party/WebKit/Source/bindings/core/v8/V8Binding.h

Issue 2810843002: bindings: Make the sequence conversion code more complaint with WebIDL. (Closed)
Patch Set: Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 641 matching lines...) Expand 10 before | Expand all | Expand 10 after
652 NodeFilter* ToNodeFilter(v8::Local<v8::Value>, 652 NodeFilter* ToNodeFilter(v8::Local<v8::Value>,
653 v8::Local<v8::Object>, 653 v8::Local<v8::Object>,
654 ScriptState*); 654 ScriptState*);
655 XPathNSResolver* ToXPathNSResolver(ScriptState*, v8::Local<v8::Value>); 655 XPathNSResolver* ToXPathNSResolver(ScriptState*, v8::Local<v8::Value>);
656 656
657 bool ToV8Sequence(v8::Local<v8::Value>, 657 bool ToV8Sequence(v8::Local<v8::Value>,
658 uint32_t& length, 658 uint32_t& length,
659 v8::Isolate*, 659 v8::Isolate*,
660 ExceptionState&); 660 ExceptionState&);
661 661
662 template <typename T>
663 HeapVector<Member<T>> ToMemberNativeArray(v8::Local<v8::Value> value,
664 int argument_index,
665 v8::Isolate* isolate,
666 ExceptionState& exception_state) {
667 v8::Local<v8::Value> v8_value(v8::Local<v8::Value>::New(isolate, value));
668 uint32_t length = 0;
669 if (value->IsArray()) {
670 length = v8::Local<v8::Array>::Cast(v8_value)->Length();
671 } else if (!ToV8Sequence(value, length, isolate, exception_state)) {
672 if (!exception_state.HadException())
673 exception_state.ThrowTypeError(
674 ExceptionMessages::NotAnArrayTypeArgumentOrValue(argument_index));
675 return HeapVector<Member<T>>();
676 }
677
678 using VectorType = HeapVector<Member<T>>;
679 if (length > VectorType::MaxCapacity()) {
680 exception_state.ThrowRangeError("Array length exceeds supported limit.");
681 return VectorType();
682 }
683
684 VectorType result;
685 result.ReserveInitialCapacity(length);
686 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(v8_value);
687 v8::TryCatch block(isolate);
688 for (uint32_t i = 0; i < length; ++i) {
689 v8::Local<v8::Value> element;
690 if (!V8Call(object->Get(isolate->GetCurrentContext(), i), element, block)) {
691 exception_state.RethrowV8Exception(block.Exception());
692 return VectorType();
693 }
694 if (V8TypeOf<T>::Type::hasInstance(element, isolate)) {
695 v8::Local<v8::Object> element_object =
696 v8::Local<v8::Object>::Cast(element);
697 result.UncheckedAppend(V8TypeOf<T>::Type::toImpl(element_object));
698 } else {
699 exception_state.ThrowTypeError("Invalid Array element type");
700 return VectorType();
701 }
702 }
703 return result;
704 }
705
706 template <typename T>
707 HeapVector<Member<T>> ToMemberNativeArray(v8::Local<v8::Value> value,
708 const String& property_name,
709 v8::Isolate* isolate,
710 ExceptionState& exception_state) {
711 v8::Local<v8::Value> v8_value(v8::Local<v8::Value>::New(isolate, value));
712 uint32_t length = 0;
713 if (value->IsArray()) {
714 length = v8::Local<v8::Array>::Cast(v8_value)->Length();
715 } else if (!ToV8Sequence(value, length, isolate, exception_state)) {
716 if (!exception_state.HadException())
717 exception_state.ThrowTypeError(
718 ExceptionMessages::NotASequenceTypeProperty(property_name));
719 return HeapVector<Member<T>>();
720 }
721
722 using VectorType = HeapVector<Member<T>>;
723 if (length > VectorType::MaxCapacity()) {
724 exception_state.ThrowRangeError("Array length exceeds supported limit.");
725 return VectorType();
726 }
727
728 VectorType result;
729 result.ReserveInitialCapacity(length);
730 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(v8_value);
731 v8::TryCatch block(isolate);
732 for (uint32_t i = 0; i < length; ++i) {
733 v8::Local<v8::Value> element;
734 if (!V8Call(object->Get(isolate->GetCurrentContext(), i), element, block)) {
735 exception_state.RethrowV8Exception(block.Exception());
736 return VectorType();
737 }
738 if (V8TypeOf<T>::Type::hasInstance(element, isolate)) {
739 v8::Local<v8::Object> element_object =
740 v8::Local<v8::Object>::Cast(element);
741 result.UncheckedAppend(V8TypeOf<T>::Type::toImpl(element_object));
742 } else {
743 exception_state.ThrowTypeError("Invalid Array element type");
744 return VectorType();
745 }
746 }
747 return result;
748 }
749
750 // Converts a JavaScript value to an array as per the Web IDL specification:
751 // http://www.w3.org/TR/2012/CR-WebIDL-20120419/#es-array
752 template <typename VectorType,
753 typename ValueType = typename VectorType::ValueType>
754 VectorType ToImplArray(v8::Local<v8::Value> value,
755 int argument_index,
756 v8::Isolate* isolate,
757 ExceptionState& exception_state) {
758 typedef NativeValueTraits<ValueType> TraitsType;
759
760 uint32_t length = 0;
761 if (value->IsArray()) {
762 length = v8::Local<v8::Array>::Cast(value)->Length();
763 } else if (!ToV8Sequence(value, length, isolate, exception_state)) {
764 if (!exception_state.HadException())
765 exception_state.ThrowTypeError(
766 ExceptionMessages::NotAnArrayTypeArgumentOrValue(argument_index));
767 return VectorType();
768 }
769
770 if (length > VectorType::MaxCapacity()) {
771 exception_state.ThrowRangeError("Array length exceeds supported limit.");
772 return VectorType();
773 }
774
775 VectorType result;
776 result.ReserveInitialCapacity(length);
777 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(value);
778 v8::TryCatch block(isolate);
779 for (uint32_t i = 0; i < length; ++i) {
780 v8::Local<v8::Value> element;
781 if (!V8Call(object->Get(isolate->GetCurrentContext(), i), element, block)) {
782 exception_state.RethrowV8Exception(block.Exception());
783 return VectorType();
784 }
785 result.UncheckedAppend(
786 TraitsType::NativeValue(isolate, element, exception_state));
787 if (exception_state.HadException())
788 return VectorType();
789 }
790 return result;
791 }
792
793 template <typename VectorType>
794 VectorType ToImplArray(const Vector<ScriptValue>& value,
795 v8::Isolate* isolate,
796 ExceptionState& exception_state) {
797 using ValueType = typename VectorType::ValueType;
798 using TraitsType = NativeValueTraits<ValueType>;
799
800 if (value.size() > VectorType::MaxCapacity()) {
801 exception_state.ThrowRangeError("Array length exceeds supported limit.");
802 return VectorType();
803 }
804
805 VectorType result;
806 result.ReserveInitialCapacity(value.size());
807 for (unsigned i = 0; i < value.size(); ++i) {
808 result.UncheckedAppend(
809 TraitsType::NativeValue(isolate, value[i].V8Value(), exception_state));
810 if (exception_state.HadException())
811 return VectorType();
812 }
813 return result;
814 }
815
816 template <typename VectorType> 662 template <typename VectorType>
817 VectorType ToImplArguments(const v8::FunctionCallbackInfo<v8::Value>& info, 663 VectorType ToImplArguments(const v8::FunctionCallbackInfo<v8::Value>& info,
818 int start_index, 664 int start_index,
819 ExceptionState& exception_state) { 665 ExceptionState& exception_state) {
820 using ValueType = typename VectorType::ValueType; 666 using ValueType = typename VectorType::ValueType;
821 using TraitsType = NativeValueTraits<ValueType>; 667 using TraitsType = NativeValueTraits<ValueType>;
822 668
823 int length = info.Length(); 669 int length = info.Length();
824 VectorType result; 670 VectorType result;
825 if (start_index < length) { 671 if (start_index < length) {
(...skipping 10 matching lines...) Expand all
836 } 682 }
837 } 683 }
838 return result; 684 return result;
839 } 685 }
840 686
841 // Gets an iterator from an Object. 687 // Gets an iterator from an Object.
842 CORE_EXPORT v8::Local<v8::Object> GetEsIterator(v8::Isolate*, 688 CORE_EXPORT v8::Local<v8::Object> GetEsIterator(v8::Isolate*,
843 v8::Local<v8::Object>, 689 v8::Local<v8::Object>,
844 ExceptionState&); 690 ExceptionState&);
845 691
846 // Validates that the passed object is a sequence type per WebIDL spec 692 // Validates that the passed object is a sequence type per the WebIDL spec: it
847 // http://www.w3.org/TR/2012/CR-WebIDL-20120419/#es-sequence 693 // has a callable @iterator.
848 inline bool ToV8Sequence(v8::Local<v8::Value> value, 694 // https://heycam.github.io/webidl/#es-sequence
849 uint32_t& length, 695 CORE_EXPORT bool IsV8Sequence(v8::Local<v8::Value>,
Yuki 2017/04/11 07:17:11 nit: If possible, could you make v8::Isolate* the
850 v8::Isolate* isolate, 696 v8::Isolate*,
851 ExceptionState& exception_state) { 697 ExceptionState&);
852 // Attempt converting to a sequence if the value is not already an array but
853 // is any kind of object except for a native Date object or a native RegExp
854 // object.
855 ASSERT(!value->IsArray());
856 // FIXME: Do we really need to special case Date and RegExp object?
857 // https://www.w3.org/Bugs/Public/show_bug.cgi?id=22806
858 if (!value->IsObject() || value->IsDate() || value->IsRegExp()) {
859 // The caller is responsible for reporting a TypeError.
860 return false;
861 }
862
863 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(value);
864 v8::Local<v8::String> length_symbol = V8AtomicString(isolate, "length");
865
866 // FIXME: The specification states that the length property should be used as
867 // fallback, if value is not a platform object that supports indexed
868 // properties. If it supports indexed properties, length should actually be
869 // one greater than value's maximum indexed property index.
870 v8::TryCatch block(isolate);
871 v8::Local<v8::Value> length_value;
872 if (!V8Call(object->Get(isolate->GetCurrentContext(), length_symbol),
873 length_value, block)) {
874 exception_state.RethrowV8Exception(block.Exception());
875 return false;
876 }
877
878 if (length_value->IsUndefined() || length_value->IsNull()) {
879 // The caller is responsible for reporting a TypeError.
880 return false;
881 }
882
883 uint32_t sequence_length;
884 if (!V8Call(length_value->Uint32Value(isolate->GetCurrentContext()),
885 sequence_length, block)) {
886 exception_state.RethrowV8Exception(block.Exception());
887 return false;
888 }
889
890 length = sequence_length;
891 return true;
892 }
893 698
894 // TODO(rakuco): remove the specializations below (and consequently the 699 // TODO(rakuco): remove the specializations below (and consequently the
895 // non-IDLBase version of NativeValueTraitsBase) once we manage to convert all 700 // non-IDLBase version of NativeValueTraitsBase) once we manage to convert all
896 // uses of NativeValueTraits to types that derive from IDLBase or for generated 701 // uses of NativeValueTraits to types that derive from IDLBase or for generated
897 // IDL interfaces/dictionaries/unions. 702 // IDL interfaces/dictionaries/unions.
898 template <> 703 template <>
899 struct NativeValueTraits<String> { 704 struct NativeValueTraits<String> {
900 static inline String NativeValue(v8::Isolate* isolate, 705 static inline String NativeValue(v8::Isolate* isolate,
901 v8::Local<v8::Value> value, 706 v8::Local<v8::Value> value,
902 ExceptionState& exception_state) { 707 ExceptionState& exception_state) {
903 V8StringResource<> string_value(value); 708 V8StringResource<> string_value(value);
904 if (!string_value.Prepare(exception_state)) 709 if (!string_value.Prepare(exception_state))
905 return String(); 710 return String();
906 return string_value; 711 return string_value;
907 } 712 }
908 }; 713 };
909 714
910 template <> 715 template <>
911 struct NativeValueTraits<AtomicString> { 716 struct NativeValueTraits<v8::Local<v8::Value>>
912 static inline AtomicString NativeValue(v8::Isolate* isolate, 717 : public NativeValueTraitsBase<v8::Local<v8::Value>> {
913 v8::Local<v8::Value> value,
914 ExceptionState& exception_state) {
915 V8StringResource<> string_value(value);
916 if (!string_value.Prepare(exception_state))
917 return AtomicString();
918 return string_value;
919 }
920 };
921
922 template <>
923 struct NativeValueTraits<int> {
924 static inline int NativeValue(v8::Isolate* isolate,
925 v8::Local<v8::Value> value,
926 ExceptionState& exception_state) {
927 return ToInt32(isolate, value, kNormalConversion, exception_state);
928 }
929 };
930
931 template <>
932 struct NativeValueTraits<unsigned> {
933 static inline unsigned NativeValue(v8::Isolate* isolate,
934 v8::Local<v8::Value> value,
935 ExceptionState& exception_state) {
936 return ToUInt32(isolate, value, kNormalConversion, exception_state);
937 }
938 };
939
940 template <>
941 struct NativeValueTraits<float> {
942 static inline float NativeValue(v8::Isolate* isolate,
943 v8::Local<v8::Value> value,
944 ExceptionState& exception_state) {
945 return ToFloat(isolate, value, exception_state);
946 }
947 };
948
949 template <>
950 struct NativeValueTraits<double> {
951 static inline double NativeValue(v8::Isolate* isolate,
952 v8::Local<v8::Value> value,
953 ExceptionState& exception_state) {
954 return ToDouble(isolate, value, exception_state);
955 }
956 };
957
958 template <>
959 struct NativeValueTraits<v8::Local<v8::Value>> {
960 static inline v8::Local<v8::Value> NativeValue( 718 static inline v8::Local<v8::Value> NativeValue(
961 v8::Isolate* isolate, 719 v8::Isolate* isolate,
962 v8::Local<v8::Value> value, 720 v8::Local<v8::Value> value,
963 ExceptionState& exception_state) { 721 ExceptionState& exception_state) {
964 return value; 722 return value;
965 } 723 }
966 }; 724 };
967 725
968 template <>
969 struct NativeValueTraits<ScriptValue> {
970 static inline ScriptValue NativeValue(v8::Isolate* isolate,
971 v8::Local<v8::Value> value,
972 ExceptionState& exception_state) {
973 return ScriptValue(ScriptState::Current(isolate), value);
974 }
975 };
976
977 template <typename T>
978 struct NativeValueTraits<Vector<T>> {
979 static inline Vector<T> NativeValue(v8::Isolate* isolate,
980 v8::Local<v8::Value> value,
981 ExceptionState& exception_state) {
982 return ToImplArray<Vector<T>>(value, 0, isolate, exception_state);
983 }
984 };
985
986 CORE_EXPORT v8::Isolate* ToIsolate(ExecutionContext*); 726 CORE_EXPORT v8::Isolate* ToIsolate(ExecutionContext*);
987 CORE_EXPORT v8::Isolate* ToIsolate(LocalFrame*); 727 CORE_EXPORT v8::Isolate* ToIsolate(LocalFrame*);
988 728
989 CORE_EXPORT DOMWindow* ToDOMWindow(v8::Isolate*, v8::Local<v8::Value>); 729 CORE_EXPORT DOMWindow* ToDOMWindow(v8::Isolate*, v8::Local<v8::Value>);
990 LocalDOMWindow* ToLocalDOMWindow(v8::Local<v8::Context>); 730 LocalDOMWindow* ToLocalDOMWindow(v8::Local<v8::Context>);
991 LocalDOMWindow* EnteredDOMWindow(v8::Isolate*); 731 LocalDOMWindow* EnteredDOMWindow(v8::Isolate*);
992 CORE_EXPORT LocalDOMWindow* CurrentDOMWindow(v8::Isolate*); 732 CORE_EXPORT LocalDOMWindow* CurrentDOMWindow(v8::Isolate*);
993 CORE_EXPORT ExecutionContext* ToExecutionContext(v8::Local<v8::Context>); 733 CORE_EXPORT ExecutionContext* ToExecutionContext(v8::Local<v8::Context>);
994 CORE_EXPORT void RegisterToExecutionContextForModules(ExecutionContext* ( 734 CORE_EXPORT void RegisterToExecutionContextForModules(ExecutionContext* (
995 *to_execution_context_for_modules)(v8::Local<v8::Context>)); 735 *to_execution_context_for_modules)(v8::Local<v8::Context>));
(...skipping 24 matching lines...) Expand all
1020 CORE_EXPORT LocalFrame* ToLocalFrameIfNotDetached(v8::Local<v8::Context>); 760 CORE_EXPORT LocalFrame* ToLocalFrameIfNotDetached(v8::Local<v8::Context>);
1021 761
1022 // If 'storage' is non-null, it must be large enough to copy all bytes in the 762 // If 'storage' is non-null, it must be large enough to copy all bytes in the
1023 // array buffer view into it. Use allocateFlexibleArrayBufferStorage(v8Value) 763 // array buffer view into it. Use allocateFlexibleArrayBufferStorage(v8Value)
1024 // to allocate it using alloca() in the callers stack frame. 764 // to allocate it using alloca() in the callers stack frame.
1025 CORE_EXPORT void ToFlexibleArrayBufferView(v8::Isolate*, 765 CORE_EXPORT void ToFlexibleArrayBufferView(v8::Isolate*,
1026 v8::Local<v8::Value>, 766 v8::Local<v8::Value>,
1027 FlexibleArrayBufferView&, 767 FlexibleArrayBufferView&,
1028 void* storage = nullptr); 768 void* storage = nullptr);
1029 769
1030 // Converts a V8 value to an array (an IDL sequence) as per the WebIDL
1031 // specification: http://heycam.github.io/webidl/#es-sequence
1032 template <typename VectorType>
1033 VectorType ToImplSequence(v8::Isolate* isolate,
1034 v8::Local<v8::Value> value,
1035 ExceptionState& exception_state) {
1036 using ValueType = typename VectorType::ValueType;
1037
1038 if (!value->IsObject() || value->IsRegExp()) {
1039 exception_state.ThrowTypeError(
1040 "The provided value cannot be converted to a sequence.");
1041 return VectorType();
1042 }
1043
1044 v8::TryCatch block(isolate);
1045 v8::Local<v8::Object> iterator =
1046 GetEsIterator(isolate, value.As<v8::Object>(), exception_state);
1047 if (exception_state.HadException())
1048 return VectorType();
1049
1050 v8::Local<v8::String> next_key = V8String(isolate, "next");
1051 v8::Local<v8::String> value_key = V8String(isolate, "value");
1052 v8::Local<v8::String> done_key = V8String(isolate, "done");
1053 v8::Local<v8::Context> context = isolate->GetCurrentContext();
1054 VectorType result;
1055 while (true) {
1056 v8::Local<v8::Value> next;
1057 if (!iterator->Get(context, next_key).ToLocal(&next)) {
1058 exception_state.RethrowV8Exception(block.Exception());
1059 return VectorType();
1060 }
1061 // TODO(bashi): Support callable objects.
1062 if (!next->IsObject() || !next.As<v8::Object>()->IsFunction()) {
1063 exception_state.ThrowTypeError("Iterator.next should be callable.");
1064 return VectorType();
1065 }
1066 v8::Local<v8::Value> next_result;
1067 if (!V8ScriptRunner::CallFunction(next.As<v8::Function>(),
1068 ToExecutionContext(context), iterator, 0,
1069 nullptr, isolate)
1070 .ToLocal(&next_result)) {
1071 exception_state.RethrowV8Exception(block.Exception());
1072 return VectorType();
1073 }
1074 if (!next_result->IsObject()) {
1075 exception_state.ThrowTypeError(
1076 "Iterator.next() did not return an object.");
1077 return VectorType();
1078 }
1079 v8::Local<v8::Object> result_object = next_result.As<v8::Object>();
1080 v8::Local<v8::Value> element;
1081 v8::Local<v8::Value> done;
1082 if (!result_object->Get(context, value_key).ToLocal(&element) ||
1083 !result_object->Get(context, done_key).ToLocal(&done)) {
1084 exception_state.RethrowV8Exception(block.Exception());
1085 return VectorType();
1086 }
1087 v8::Local<v8::Boolean> done_boolean;
1088 if (!done->ToBoolean(context).ToLocal(&done_boolean)) {
1089 exception_state.RethrowV8Exception(block.Exception());
1090 return VectorType();
1091 }
1092 if (done_boolean->Value())
1093 break;
1094 result.push_back(NativeValueTraits<ValueType>::NativeValue(
1095 isolate, element, exception_state));
1096 }
1097 return result;
1098 }
1099
1100 // If the current context causes out of memory, JavaScript setting 770 // If the current context causes out of memory, JavaScript setting
1101 // is disabled and it returns true. 771 // is disabled and it returns true.
1102 bool HandleOutOfMemory(); 772 bool HandleOutOfMemory();
1103 773
1104 inline bool IsUndefinedOrNull(v8::Local<v8::Value> value) { 774 inline bool IsUndefinedOrNull(v8::Local<v8::Value> value) {
1105 return value.IsEmpty() || value->IsNull() || value->IsUndefined(); 775 return value.IsEmpty() || value->IsNull() || value->IsUndefined();
1106 } 776 }
1107 v8::Local<v8::Function> GetBoundFunction(v8::Local<v8::Function>); 777 v8::Local<v8::Function> GetBoundFunction(v8::Local<v8::Function>);
1108 778
1109 // FIXME: This will be soon embedded in the generated code. 779 // FIXME: This will be soon embedded in the generated code.
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
1159 // If the argument isn't an object, this will crash. 829 // If the argument isn't an object, this will crash.
1160 CORE_EXPORT v8::Local<v8::Value> FreezeV8Object(v8::Local<v8::Value>, 830 CORE_EXPORT v8::Local<v8::Value> FreezeV8Object(v8::Local<v8::Value>,
1161 v8::Isolate*); 831 v8::Isolate*);
1162 832
1163 CORE_EXPORT v8::Local<v8::Value> FromJSONString(v8::Isolate*, 833 CORE_EXPORT v8::Local<v8::Value> FromJSONString(v8::Isolate*,
1164 const String& stringified_json, 834 const String& stringified_json,
1165 ExceptionState&); 835 ExceptionState&);
1166 } // namespace blink 836 } // namespace blink
1167 837
1168 #endif // V8Binding_h 838 #endif // V8Binding_h
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698