Bug 1335895 - part 23: Added ArrayRefBase::CopyTo() r=jchen draft
authorRandall Barker <rbarker@mozilla.com>
Sat, 01 Apr 2017 15:48:39 -0700
changeset 561589 5cf261897ecac149484c9971381df9b37d22bde1
parent 561588 c97e39c26169a1fe82e09dfad30b6a3bbf4473a1
child 561590 588d04a80b4e3b6bbe924610b37bc66f69dd2b14
push id53787
push userbmo:rbarker@mozilla.com
push dateWed, 12 Apr 2017 22:56:58 +0000
reviewersjchen
bugs1335895
milestone55.0a1
Bug 1335895 - part 23: Added ArrayRefBase::CopyTo() r=jchen
widget/android/jni/Refs.h
--- a/widget/android/jni/Refs.h
+++ b/widget/android/jni/Refs.h
@@ -848,24 +848,37 @@ public:
     }
 
     nsTArray<ElementType> GetElements() const
     {
         using JNIElemType = typename detail::TypeAdapter<ElementType>::JNIType;
         static_assert(sizeof(ElementType) == sizeof(JNIElemType),
                       "Size of native type must match size of JNI type");
 
-        const jsize len = size_t(Base::Env()->GetArrayLength(Base::Instance()));
+        const size_t len = size_t(Base::Env()->GetArrayLength(Base::Instance()));
+
+        nsTArray<ElementType> array(len);
+        array.SetLength(len);
+        CopyTo(array.Elements(), len);
+        return array;
+    }
 
-        nsTArray<ElementType> array((size_t(len)));
-        array.SetLength(size_t(len));
+    // returns number of elements copied
+    size_t CopyTo(ElementType* buffer, size_t size) const
+    {
+        using JNIElemType = typename detail::TypeAdapter<ElementType>::JNIType;
+        static_assert(sizeof(ElementType) == sizeof(JNIElemType),
+                      "Size of native type must match size of JNI type");
+
+        const size_t len = size_t(Base::Env()->GetArrayLength(Base::Instance()));
+        const size_t amountToCopy = (len > size ? size : len);
         (Base::Env()->*detail::TypeAdapter<ElementType>::GetArray)(
-                Base::Instance(), 0, len,
-                reinterpret_cast<JNIElemType*>(array.Elements()));
-        return array;
+                Base::Instance(), 0, jsize(amountToCopy),
+                reinterpret_cast<JNIElemType*>(buffer));
+        return amountToCopy;
     }
 
     ElementType operator[](size_t index) const
     {
         return GetElement(index);
     }
 
     operator nsTArray<ElementType>() const