[V8Binding] Merging v8NumberArray()/v8NumberArrayToVector() to v8Array()/toNativeArra...
authorcommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 2 Jul 2012 08:42:29 +0000 (08:42 +0000)
committercommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 2 Jul 2012 08:42:29 +0000 (08:42 +0000)
https://bugs.webkit.org/show_bug.cgi?id=90338

Patch by Vineet Chaudhary <rgf748@motorola.com> on 2012-07-02
Reviewed by Kentaro Hara.

We can remove v8NumberArray() and v8NumberArrayToVector() implementaion
merging them to current v8Array() and toNativeArray() traits.

Tests: TestObj.idl
Shouldn't cause any behavioural changes.

* bindings/scripts/CodeGeneratorV8.pm: Removed float[]/double[] specific binding code.
(IsRefPtrType):
(GetNativeType):
(JSValueToNative):
(NativeToJSValue):
* bindings/scripts/test/V8/V8TestObj.cpp: Rebased binding test.
(WebCore::TestObjV8Internal::floatArrayAttrGetter):
(WebCore::TestObjV8Internal::floatArrayAttrSetter):
(WebCore::TestObjV8Internal::doubleArrayAttrGetter):
(WebCore::TestObjV8Internal::doubleArrayAttrSetter):
* bindings/v8/V8Binding.h: Added templates for float and double.
(WebCore::v8Array):
(WebCore::toNativeArray):

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@121663 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebCore/ChangeLog
Source/WebCore/bindings/scripts/CodeGeneratorV8.pm
Source/WebCore/bindings/scripts/test/V8/V8TestObj.cpp
Source/WebCore/bindings/v8/V8Binding.h

index 8a96ff4..91219b6 100644 (file)
@@ -1,3 +1,30 @@
+2012-07-02  Vineet Chaudhary  <rgf748@motorola.com>
+
+        [V8Binding] Merging v8NumberArray()/v8NumberArrayToVector() to v8Array()/toNativeArray() respectively.
+        https://bugs.webkit.org/show_bug.cgi?id=90338
+
+        Reviewed by Kentaro Hara.
+
+        We can remove v8NumberArray() and v8NumberArrayToVector() implementaion
+        merging them to current v8Array() and toNativeArray() traits.
+
+        Tests: TestObj.idl
+        Shouldn't cause any behavioural changes.
+
+        * bindings/scripts/CodeGeneratorV8.pm: Removed float[]/double[] specific binding code.
+        (IsRefPtrType):
+        (GetNativeType):
+        (JSValueToNative):
+        (NativeToJSValue):
+        * bindings/scripts/test/V8/V8TestObj.cpp: Rebased binding test.
+        (WebCore::TestObjV8Internal::floatArrayAttrGetter):
+        (WebCore::TestObjV8Internal::floatArrayAttrSetter):
+        (WebCore::TestObjV8Internal::doubleArrayAttrGetter):
+        (WebCore::TestObjV8Internal::doubleArrayAttrSetter):
+        * bindings/v8/V8Binding.h: Added templates for float and double.
+        (WebCore::v8Array):
+        (WebCore::toNativeArray):
+
 2012-07-02  Konrad Piascik  <kpiascik@rim.com>
 
         [EFL] [GTK] [QT] fast/viewport/viewport-91.html is failing after r121555
index 90ef218..6d4f552 100644 (file)
@@ -3511,8 +3511,6 @@ sub IsRefPtrType
     return 0 if $type eq "unsigned";
     return 0 if $type eq "unsigned long";
     return 0 if $type eq "unsigned short";
-    return 0 if $type eq "float[]";
-    return 0 if $type eq "double[]";
 
     return 1;
 }
@@ -3573,8 +3571,6 @@ sub GetNativeType
     return "RefPtr<MediaQueryListListener>" if $type eq "MediaQueryListListener";
 
     # FIXME: Support T[], T[]?, sequence<T> generically
-    return "Vector<float>" if $type eq "float[]";
-    return "Vector<double>" if $type eq "double[]";
     return "RefPtr<DOMStringList>" if $type eq "DOMStringList";
 
     # Default, assume native type is a pointer with same type name as idl type
@@ -3633,14 +3629,6 @@ sub JSValueToNative
     return "v8ValueToWebCoreDOMStringList($value)" if $type eq "DOMStringList";
     # FIXME: Add proper support for T[], T[]? and sequence<T>.
     return "v8ValueToWebCoreDOMStringList($value)" if $type eq "DOMString[]";
-    if ($type eq "float[]") {
-        AddToImplIncludes("wtf/Vector.h");
-        return "v8NumberArrayToVector<float>($value)";
-    }
-    if ($type eq "double[]") {
-        AddToImplIncludes("wtf/Vector.h");
-        return "v8NumberArrayToVector<double>($value)";
-    }
 
     if ($type eq "DOMString" or $type eq "DOMUserData") {
         return $value;
@@ -3820,9 +3808,7 @@ my %non_wrapper_types = (
     'SerializedScriptValue' => 1,
     'boolean' => 1,
     'double' => 1,
-    'double[]' => 1,
     'float' => 1,
-    'float[]' => 1,
     'int' => 1,
     'long long' => 1,
     'long' => 1,
@@ -3909,9 +3895,6 @@ sub NativeToJSValue
     return "v8::Number::New($value)" if $codeGenerator->IsPrimitiveType($type);
     return "$value.v8Value()" if $nativeType eq "ScriptValue";
 
-    return "v8NumberArray($value)" if $type eq "float[]";
-    return "v8NumberArray($value)" if $type eq "double[]";
-
     if ($codeGenerator->IsStringType($type)) {
         my $conv = $signature->extendedAttributes->{"TreatReturnedNullStringAs"};
         if (defined $conv) {
index 372eadf..a396dbb 100644 (file)
@@ -60,7 +60,6 @@
 #include <wtf/RefCounted.h>
 #include <wtf/RefPtr.h>
 #include <wtf/UnusedParam.h>
-#include <wtf/Vector.h>
 
 #if ENABLE(Condition1)
 #include "V8TestObjectA.h"
@@ -879,14 +878,14 @@ static v8::Handle<v8::Value> floatArrayAttrGetter(v8::Local<v8::String> name, co
 {
     INC_STATS("DOM.TestObj.floatArray._get");
     TestObj* imp = V8TestObj::toNative(info.Holder());
-    return v8NumberArray(imp->floatArray());
+    return v8Array(imp->floatArray(), info.GetIsolate());
 }
 
 static void floatArrayAttrSetter(v8::Local<v8::String> name, v8::Local<v8::Value> value, const v8::AccessorInfo& info)
 {
     INC_STATS("DOM.TestObj.floatArray._set");
     TestObj* imp = V8TestObj::toNative(info.Holder());
-    Vector<float> v = v8NumberArrayToVector<float>(value);
+    Vector<float> v = toNativeArray<float>(value);
     imp->setFloatArray(v);
     return;
 }
@@ -895,14 +894,14 @@ static v8::Handle<v8::Value> doubleArrayAttrGetter(v8::Local<v8::String> name, c
 {
     INC_STATS("DOM.TestObj.doubleArray._get");
     TestObj* imp = V8TestObj::toNative(info.Holder());
-    return v8NumberArray(imp->doubleArray());
+    return v8Array(imp->doubleArray(), info.GetIsolate());
 }
 
 static void doubleArrayAttrSetter(v8::Local<v8::String> name, v8::Local<v8::Value> value, const v8::AccessorInfo& info)
 {
     INC_STATS("DOM.TestObj.doubleArray._set");
     TestObj* imp = V8TestObj::toNative(info.Holder());
-    Vector<double> v = v8NumberArrayToVector<double>(value);
+    Vector<double> v = toNativeArray<double>(value);
     imp->setDoubleArray(v);
     return;
 }
index 4e45f7b..cb42000 100644 (file)
@@ -343,7 +343,7 @@ namespace WebCore {
     }
 
     template <class T>
-    struct Traits {
+    struct V8ValueTraits {
         static inline v8::Handle<v8::Value> arrayV8Value(const T& value, v8::Isolate* isolate)
         {
             return toV8(WTF::getPtr(value), isolate);
@@ -351,7 +351,7 @@ namespace WebCore {
     };
 
     template<>
-    struct Traits<String> {
+    struct V8ValueTraits<String> {
         static inline v8::Handle<v8::Value> arrayV8Value(const String& value, v8::Isolate* isolate)
         {
             return v8String(value, isolate);
@@ -359,20 +359,36 @@ namespace WebCore {
     };
 
     template<>
-    struct Traits<unsigned long> {
+    struct V8ValueTraits<unsigned long> {
         static inline v8::Handle<v8::Value> arrayV8Value(const unsigned long& value, v8::Isolate* isolate)
         {
             return v8UnsignedInteger(value, isolate);
         }
     };
 
+    template<>
+    struct V8ValueTraits<float> {
+        static inline v8::Handle<v8::Value> arrayV8Value(const float& value, v8::Isolate*)
+        {
+            return v8::Number::New(value);
+        }
+    };
+
+    template<>
+    struct V8ValueTraits<double> {
+        static inline v8::Handle<v8::Value> arrayV8Value(const double& value, v8::Isolate*)
+        {
+            return v8::Number::New(value);
+        }
+    };
+
     template<typename T>
     v8::Handle<v8::Value> v8Array(const Vector<T>& iterator, v8::Isolate* isolate)
     {
         v8::Local<v8::Array> result = v8::Array::New(iterator.size());
         int index = 0;
         typename Vector<T>::const_iterator end = iterator.end();
-        typedef Traits<T> TraitsType;
+        typedef V8ValueTraits<T> TraitsType;
         for (typename Vector<T>::const_iterator iter = iterator.begin(); iter != end; ++iter)
             result->Set(v8Integer(index++, isolate), TraitsType::arrayV8Value(*iter, isolate));
         return result;
@@ -380,6 +396,32 @@ namespace WebCore {
 
     v8::Handle<v8::Value> v8Array(PassRefPtr<DOMStringList>, v8::Isolate*);
 
+    template<class T> struct NativeValueTraits;
+
+    template<>
+    struct NativeValueTraits<String> {
+        static inline String arrayNativeValue(const v8::Local<v8::Array>& array, size_t i)
+        {
+            return v8ValueToWebCoreString(array->Get(i));
+        }
+    };
+
+    template<>
+    struct NativeValueTraits<float> {
+        static inline float arrayNativeValue(const v8::Local<v8::Array>& array, size_t i)
+        {
+            return static_cast<float>(array->Get(v8Integer(i))->NumberValue());
+        }
+    };
+
+    template<>
+    struct NativeValueTraits<double> {
+        static inline double arrayNativeValue(const v8::Local<v8::Array>& array, size_t i)
+        {
+            return static_cast<double>(array->Get(v8Integer(i))->NumberValue());
+        }
+    };
+
     template <class T>
     Vector<T> toNativeArray(v8::Handle<v8::Value> value)
     {
@@ -387,12 +429,12 @@ namespace WebCore {
             return Vector<T>();
 
         Vector<T> result;
+        typedef NativeValueTraits<T> TraitsType;
         v8::Local<v8::Value> v8Value(v8::Local<v8::Value>::New(value));
         v8::Local<v8::Array> array = v8::Local<v8::Array>::Cast(v8Value);
         size_t length = array->Length();
-
         for (size_t i = 0; i < length; ++i) {
-            result.append(v8ValueToWebCoreString(array->Get(i)));
+            result.append(TraitsType::arrayNativeValue(array, i));
         }
         return result;
     }
@@ -522,15 +564,6 @@ namespace WebCore {
         return str.isNull() ? v8::Handle<v8::Value>(v8Boolean(false)) : v8::Handle<v8::Value>(v8String(str, isolate));
     }
 
-    template <class T> v8::Handle<v8::Value> v8NumberArray(const Vector<T>& values)
-    {
-        size_t size = values.size();
-        v8::Local<v8::Array> result = v8::Array::New(size);
-        for (size_t i = 0; i < size; ++i)
-            result->Set(i, v8::Number::New(values[i]));
-        return result;
-    }
-
     inline double toWebCoreDate(v8::Handle<v8::Value> object)
     {
         return (object->IsDate() || object->IsNumber()) ? object->NumberValue() : std::numeric_limits<double>::quiet_NaN();
@@ -560,22 +593,6 @@ namespace WebCore {
 
     String int32ToWebCoreString(int value);
 
-    template <class T> Vector<T> v8NumberArrayToVector(v8::Handle<v8::Value> value)
-    {
-        v8::Local<v8::Value> v8Value(v8::Local<v8::Value>::New(value));
-        if (!v8Value->IsArray())
-            return Vector<T>();
-
-        Vector<T> result;
-        v8::Local<v8::Array> v8Array = v8::Local<v8::Array>::Cast(v8Value);
-        size_t length = v8Array->Length();
-        for (size_t i = 0; i < length; ++i) {
-            v8::Local<v8::Value> indexedValue = v8Array->Get(v8Integer(i));
-            result.append(static_cast<T>(indexedValue->NumberValue()));
-        }
-        return result;
-    }
-
     PassRefPtr<DOMStringList> v8ValueToWebCoreDOMStringList(v8::Handle<v8::Value>);
 
     class V8ParameterBase {