Add support for arrays of numbers to IDL bindings code generator
authorcommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 9 Nov 2011 00:43:07 +0000 (00:43 +0000)
committercommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 9 Nov 2011 00:43:07 +0000 (00:43 +0000)
https://bugs.webkit.org/show_bug.cgi?id=71763

Adds support for float[] and double[] to IDL. Support for other types
appears to be more complicated (or at least more copy-paste) so not
doing that for now for lack of need.

Patch by Scott Graham <scottmg@chromium.org> on 2011-11-08
Reviewed by Adam Barth.

Tests added to TestObj.idl.

* bindings/scripts/CodeGeneratorV8.pm:
(IsRefPtrType):
(GetNativeType):
(JSValueToNative):
(NativeToJSValue):
* bindings/scripts/test/TestObj.idl:
* bindings/scripts/test/V8/V8TestObj.cpp:
(WebCore::TestObjInternal::floatArrayAttrGetter):
(WebCore::TestObjInternal::floatArrayAttrSetter):
(WebCore::TestObjInternal::doubleArrayAttrGetter):
(WebCore::TestObjInternal::doubleArrayAttrSetter):
* bindings/v8/V8Binding.h:
(WebCore::v8NumberArray):
(WebCore::v8NumberArrayToVector):

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

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

index e30f5aa..2557bc6 100644 (file)
@@ -1,3 +1,31 @@
+2011-11-08  Scott Graham  <scottmg@chromium.org>
+
+        Add support for arrays of numbers to IDL bindings code generator
+        https://bugs.webkit.org/show_bug.cgi?id=71763
+
+        Adds support for float[] and double[] to IDL. Support for other types
+        appears to be more complicated (or at least more copy-paste) so not
+        doing that for now for lack of need.
+
+        Reviewed by Adam Barth.
+
+        Tests added to TestObj.idl.
+
+        * bindings/scripts/CodeGeneratorV8.pm:
+        (IsRefPtrType):
+        (GetNativeType):
+        (JSValueToNative):
+        (NativeToJSValue):
+        * bindings/scripts/test/TestObj.idl:
+        * bindings/scripts/test/V8/V8TestObj.cpp:
+        (WebCore::TestObjInternal::floatArrayAttrGetter):
+        (WebCore::TestObjInternal::floatArrayAttrSetter):
+        (WebCore::TestObjInternal::doubleArrayAttrGetter):
+        (WebCore::TestObjInternal::doubleArrayAttrSetter):
+        * bindings/v8/V8Binding.h:
+        (WebCore::v8NumberArray):
+        (WebCore::v8NumberArrayToVector):
+
 2011-11-08  Dan Bernstein  <mitz@apple.com>
 
         REGRESSION (r99613): Incomplete painting of the root element background in flipped blocks writing mode
index 471b94a..74a587d 100644 (file)
@@ -3079,6 +3079,8 @@ 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;
 }
@@ -3134,8 +3136,10 @@ 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";
-    # FIXME: Add proper support for T[], T[]?, sequence<T>.
     return "RefPtr<DOMStringList>" if $type eq "DOMString[]";
 
     # Default, assume native type is a pointer with same type name as idl type
@@ -3192,6 +3196,14 @@ 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;
@@ -3342,6 +3354,8 @@ my %non_wrapper_types = (
     'boolean' => 1,
     'long long' => 1,
     'unsigned long long' => 1,
+    'float[]' => 1,
+    'double[]' => 1,
     'DOMString' => 1,
     'CompareHow' => 1,
     'SerializedScriptValue' => 1,
@@ -3432,6 +3446,9 @@ 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->{"ConvertNullStringTo"};
         if (defined $conv) {
index 3624465..428239f 100644 (file)
@@ -172,6 +172,12 @@ module test {
         attribute [EnabledAtRuntime=FeatureName] long enabledAtRuntimeAttr2;
 #endif
 
+
+#if defined(TESTING_V8)
+        attribute float[]                   floatArray;
+        attribute double[]                  doubleArray;
+#endif
+
         // ObjectiveC reserved words.
         readonly attribute long      description;
         attribute long               id;
index c8e55d3..ba3ec47 100644 (file)
@@ -46,6 +46,7 @@
 #include <wtf/RefCounted.h>
 #include <wtf/RefPtr.h>
 #include <wtf/UnusedParam.h>
+#include <wtf/Vector.h>
 
 #if ENABLE(Condition1)
 #include "V8TestObjectA.h"
@@ -642,6 +643,38 @@ static void enabledAtRuntimeAttr2AttrSetter(v8::Local<v8::String> name, v8::Loca
     return;
 }
 
+static v8::Handle<v8::Value> floatArrayAttrGetter(v8::Local<v8::String> name, const v8::AccessorInfo& info)
+{
+    INC_STATS("DOM.TestObj.floatArray._get");
+    TestObj* imp = V8TestObj::toNative(info.Holder());
+    return v8NumberArray(imp->floatArray());
+}
+
+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);
+    imp->setFloatArray(v);
+    return;
+}
+
+static v8::Handle<v8::Value> doubleArrayAttrGetter(v8::Local<v8::String> name, const v8::AccessorInfo& info)
+{
+    INC_STATS("DOM.TestObj.doubleArray._get");
+    TestObj* imp = V8TestObj::toNative(info.Holder());
+    return v8NumberArray(imp->doubleArray());
+}
+
+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);
+    imp->setDoubleArray(v);
+    return;
+}
+
 static v8::Handle<v8::Value> descriptionAttrGetter(v8::Local<v8::String> name, const v8::AccessorInfo& info)
 {
     INC_STATS("DOM.TestObj.description._get");
@@ -1375,6 +1408,10 @@ static const BatchedAttribute TestObjAttrs[] = {
     {"cachedAttribute1", TestObjInternal::cachedAttribute1AttrGetter, 0, 0 /* no data */, static_cast<v8::AccessControl>(v8::DEFAULT), static_cast<v8::PropertyAttribute>(v8::None), 0 /* on instance */},
     // Attribute 'cachedAttribute2' (Type: 'readonly attribute' ExtAttr: 'CachedAttribute')
     {"cachedAttribute2", TestObjInternal::cachedAttribute2AttrGetter, 0, 0 /* no data */, static_cast<v8::AccessControl>(v8::DEFAULT), static_cast<v8::PropertyAttribute>(v8::None), 0 /* on instance */},
+    // Attribute 'floatArray' (Type: 'attribute' ExtAttr: '')
+    {"floatArray", TestObjInternal::floatArrayAttrGetter, TestObjInternal::floatArrayAttrSetter, 0 /* no data */, static_cast<v8::AccessControl>(v8::DEFAULT), static_cast<v8::PropertyAttribute>(v8::None), 0 /* on instance */},
+    // Attribute 'doubleArray' (Type: 'attribute' ExtAttr: '')
+    {"doubleArray", TestObjInternal::doubleArrayAttrGetter, TestObjInternal::doubleArrayAttrSetter, 0 /* no data */, static_cast<v8::AccessControl>(v8::DEFAULT), static_cast<v8::PropertyAttribute>(v8::None), 0 /* on instance */},
     // Attribute 'description' (Type: 'readonly attribute' ExtAttr: '')
     {"description", TestObjInternal::descriptionAttrGetter, 0, 0 /* no data */, static_cast<v8::AccessControl>(v8::DEFAULT), static_cast<v8::PropertyAttribute>(v8::None), 0 /* on instance */},
     // Attribute 'id' (Type: 'attribute' ExtAttr: '')
index f72876d..6c1ac19 100644 (file)
@@ -349,6 +349,15 @@ namespace WebCore {
 
     v8::Handle<v8::Value> v8StringOrFalse(const String& str);
 
+    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;
+    }
+
     double toWebCoreDate(v8::Handle<v8::Value> object);
 
     v8::Handle<v8::Value> v8DateOrNull(double value);
@@ -379,6 +388,22 @@ 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(v8::Integer::New(i));
+            result.append(static_cast<T>(indexedValue->NumberValue()));
+        }
+        return result;
+    }
+
     PassRefPtr<DOMStringList> v8ValueToWebCoreDOMStringList(v8::Handle<v8::Value>);
 
     class V8ParameterBase {