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

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

Issue 1125683002: Avoid nearly identical toImplArray() and toImplHeapArray() (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Created 5 years, 7 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 734 matching lines...) Expand 10 before | Expand all | Expand 10 after
745 } else { 745 } else {
746 exceptionState.throwTypeError("Invalid Array element type"); 746 exceptionState.throwTypeError("Invalid Array element type");
747 return HeapVector<Member<T>>(); 747 return HeapVector<Member<T>>();
748 } 748 }
749 } 749 }
750 return result; 750 return result;
751 } 751 }
752 752
753 // Converts a JavaScript value to an array as per the Web IDL specification: 753 // Converts a JavaScript value to an array as per the Web IDL specification:
754 // http://www.w3.org/TR/2012/CR-WebIDL-20120419/#es-array 754 // http://www.w3.org/TR/2012/CR-WebIDL-20120419/#es-array
755 template <typename T> 755 template <typename VectorType>
756 Vector<T> toImplArray(v8::Local<v8::Value> value, int argumentIndex, v8::Isolate * isolate, ExceptionState& exceptionState) 756 VectorType toImplArray(v8::Local<v8::Value> value, int argumentIndex, v8::Isolat e* isolate, ExceptionState& exceptionState)
757 { 757 {
758 typedef typename VectorType::ValueType ValueType;
759 typedef NativeValueTraits<ValueType> TraitsType;
760
758 uint32_t length = 0; 761 uint32_t length = 0;
759 if (value->IsArray()) { 762 if (value->IsArray()) {
760 length = v8::Local<v8::Array>::Cast(value)->Length(); 763 length = v8::Local<v8::Array>::Cast(value)->Length();
761 } else if (!toV8Sequence(value, length, isolate, exceptionState)) { 764 } else if (!toV8Sequence(value, length, isolate, exceptionState)) {
762 if (!exceptionState.hadException()) 765 if (!exceptionState.hadException())
763 exceptionState.throwTypeError(ExceptionMessages::notAnArrayTypeArgum entOrValue(argumentIndex)); 766 exceptionState.throwTypeError(ExceptionMessages::notAnArrayTypeArgum entOrValue(argumentIndex));
764 return Vector<T>(); 767 return VectorType();
765 } 768 }
766 769
767 if (length > WTF::DefaultAllocatorQuantizer::kMaxUnquantizedAllocation / siz eof(T)) { 770 if (length > WTF::DefaultAllocatorQuantizer::kMaxUnquantizedAllocation / siz eof(ValueType)) {
768 exceptionState.throwTypeError("Array length exceeds supported limit."); 771 exceptionState.throwTypeError("Array length exceeds supported limit.");
769 return Vector<T>(); 772 return VectorType();
770 } 773 }
771 774
772 Vector<T> result; 775 VectorType result;
773 result.reserveInitialCapacity(length); 776 result.reserveInitialCapacity(length);
774 typedef NativeValueTraits<T> TraitsType;
775 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(value); 777 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(value);
776 v8::TryCatch block; 778 v8::TryCatch block;
777 for (uint32_t i = 0; i < length; ++i) { 779 for (uint32_t i = 0; i < length; ++i) {
778 v8::Local<v8::Value> element; 780 v8::Local<v8::Value> element;
779 if (!v8Call(object->Get(isolate->GetCurrentContext(), i), element, block )) { 781 if (!v8Call(object->Get(isolate->GetCurrentContext(), i), element, block )) {
780 exceptionState.rethrowV8Exception(block.Exception()); 782 exceptionState.rethrowV8Exception(block.Exception());
781 return Vector<T>(); 783 return VectorType();
782 } 784 }
783 result.uncheckedAppend(TraitsType::nativeValue(isolate, element, excepti onState)); 785 result.uncheckedAppend(TraitsType::nativeValue(isolate, element, excepti onState));
784 if (exceptionState.hadException()) 786 if (exceptionState.hadException())
785 return Vector<T>(); 787 return VectorType();
786 } 788 }
787 return result; 789 return result;
788 } 790 }
789 791
790 template <typename T> 792 template <typename VectorType>
791 HeapVector<T> toImplHeapArray(v8::Local<v8::Value> value, int argumentIndex, v8: :Isolate* isolate, ExceptionState& exceptionState) 793 VectorType toImplArray(const Vector<ScriptValue>& value, v8::Isolate* isolate, E xceptionState& exceptionState)
792 { 794 {
793 uint32_t length = 0; 795 VectorType result;
794 if (value->IsArray()) { 796 typedef typename VectorType::ValueType ValueType;
795 length = v8::Local<v8::Array>::Cast(value)->Length(); 797 typedef NativeValueTraits<ValueType> TraitsType;
796 } else if (!toV8Sequence(value, length, isolate, exceptionState)) { 798 result.reserveInitialCapacity(value.size());
797 if (!exceptionState.hadException()) 799 for (unsigned i = 0; i < value.size(); ++i) {
798 exceptionState.throwTypeError(ExceptionMessages::notAnArrayTypeArgum entOrValue(argumentIndex)); 800 result.uncheckedAppend(TraitsType::nativeValue(isolate, value[i].v8Value (), exceptionState));
799 return HeapVector<T>();
800 }
801
802 if (length > WTF::DefaultAllocatorQuantizer::kMaxUnquantizedAllocation / siz eof(T)) {
803 exceptionState.throwTypeError("Array length exceeds supported limit.");
804 return HeapVector<T>();
805 }
806
807 HeapVector<T> result;
808 result.reserveInitialCapacity(length);
809 typedef NativeValueTraits<T> TraitsType;
810 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(value);
811 v8::TryCatch block;
812 for (uint32_t i = 0; i < length; ++i) {
813 v8::Local<v8::Value> element;
814 if (!v8Call(object->Get(isolate->GetCurrentContext(), i), element, block )) {
815 exceptionState.rethrowV8Exception(block.Exception());
816 return HeapVector<T>();
817 }
818 result.uncheckedAppend(TraitsType::nativeValue(isolate, element, excepti onState));
819 if (exceptionState.hadException()) 801 if (exceptionState.hadException())
820 return HeapVector<T>(); 802 return VectorType();
821 } 803 }
822 return result; 804 return result;
823 } 805 }
824
825 template <typename T>
826 Vector<T> toImplArray(const Vector<ScriptValue>& value, v8::Isolate* isolate, Ex ceptionState& exceptionState)
827 {
828 Vector<T> result;
829 result.reserveInitialCapacity(value.size());
830 for (unsigned i = 0; i < value.size(); ++i) {
831 result.uncheckedAppend(NativeValueTraits<T>::nativeValue(isolate, value[ i].v8Value(), exceptionState));
832 if (exceptionState.hadException())
833 return Vector<T>();
834 }
835 return result;
836 }
837 806
838 template <typename T> 807 template <typename T>
839 Vector<T> toImplArguments(const v8::FunctionCallbackInfo<v8::Value>& info, int s tartIndex, ExceptionState& exceptionState) 808 Vector<T> toImplArguments(const v8::FunctionCallbackInfo<v8::Value>& info, int s tartIndex, ExceptionState& exceptionState)
840 { 809 {
841 Vector<T> result; 810 Vector<T> result;
842 typedef NativeValueTraits<T> TraitsType; 811 typedef NativeValueTraits<T> TraitsType;
843 int length = info.Length(); 812 int length = info.Length();
844 if (startIndex < length) { 813 if (startIndex < length) {
845 result.reserveInitialCapacity(length - startIndex); 814 result.reserveInitialCapacity(length - startIndex);
846 for (int i = startIndex; i < length; ++i) { 815 for (int i = startIndex; i < length; ++i) {
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
951 static inline ScriptValue nativeValue(v8::Isolate* isolate, v8::Local<v8::Va lue> value, ExceptionState& exceptionState) 920 static inline ScriptValue nativeValue(v8::Isolate* isolate, v8::Local<v8::Va lue> value, ExceptionState& exceptionState)
952 { 921 {
953 return ScriptValue(ScriptState::current(isolate), value); 922 return ScriptValue(ScriptState::current(isolate), value);
954 } 923 }
955 }; 924 };
956 925
957 template <typename T> 926 template <typename T>
958 struct NativeValueTraits<Vector<T>> { 927 struct NativeValueTraits<Vector<T>> {
959 static inline Vector<T> nativeValue(v8::Isolate* isolate, v8::Local<v8::Valu e> value, ExceptionState& exceptionState) 928 static inline Vector<T> nativeValue(v8::Isolate* isolate, v8::Local<v8::Valu e> value, ExceptionState& exceptionState)
960 { 929 {
961 return toImplArray<T>(value, 0, isolate, exceptionState); 930 return toImplArray<Vector<T>>(value, 0, isolate, exceptionState);
962 } 931 }
963 }; 932 };
964 933
965 using JSONValuePtr = PassRefPtr<JSONValue>; 934 using JSONValuePtr = PassRefPtr<JSONValue>;
966 template <> 935 template <>
967 struct NativeValueTraits<JSONValuePtr> { 936 struct NativeValueTraits<JSONValuePtr> {
968 CORE_EXPORT static JSONValuePtr nativeValue(v8::Isolate*, v8::Local<v8::Valu e>, ExceptionState&, int maxDepth = JSONValue::maxDepth); 937 CORE_EXPORT static JSONValuePtr nativeValue(v8::Isolate*, v8::Local<v8::Valu e>, ExceptionState&, int maxDepth = JSONValue::maxDepth);
969 }; 938 };
970 939
971 CORE_EXPORT v8::Isolate* toIsolate(ExecutionContext*); 940 CORE_EXPORT v8::Isolate* toIsolate(ExecutionContext*);
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
1095 PassRefPtr<TraceEvent::ConvertableToTraceFormat> devToolsTraceEventData(v8::Isol ate*, ExecutionContext*, v8::Local<v8::Function>); 1064 PassRefPtr<TraceEvent::ConvertableToTraceFormat> devToolsTraceEventData(v8::Isol ate*, ExecutionContext*, v8::Local<v8::Function>);
1096 1065
1097 // Callback functions used by generated code. 1066 // Callback functions used by generated code.
1098 CORE_EXPORT void v8ConstructorAttributeGetter(v8::Local<v8::Name> propertyName, const v8::PropertyCallbackInfo<v8::Value>&); 1067 CORE_EXPORT void v8ConstructorAttributeGetter(v8::Local<v8::Name> propertyName, const v8::PropertyCallbackInfo<v8::Value>&);
1099 1068
1100 typedef void (*InstallTemplateFunction)(v8::Local<v8::FunctionTemplate>, v8::Iso late*); 1069 typedef void (*InstallTemplateFunction)(v8::Local<v8::FunctionTemplate>, v8::Iso late*);
1101 1070
1102 } // namespace blink 1071 } // namespace blink
1103 1072
1104 #endif // V8Binding_h 1073 #endif // V8Binding_h
OLDNEW
« no previous file with comments | « Source/bindings/core/v8/DictionaryHelperForCore.cpp ('k') | Source/bindings/core/v8/V8BindingTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698