mirror of
https://gitlab.winehq.org/wine/wine-gecko.git
synced 2024-09-13 09:24:08 -07:00
Bug 1200343 - Add JNI wrapper for object arrays; r=snorp
This patch adds a specialization for jni::Ref<jni::ObjectArray>, which includes members for getting the length of the array and accessing array elements.
This commit is contained in:
parent
838356f205
commit
626c1f5c1a
@ -8,6 +8,7 @@
|
|||||||
|
|
||||||
#include "nsError.h" // for nsresult
|
#include "nsError.h" // for nsresult
|
||||||
#include "nsString.h"
|
#include "nsString.h"
|
||||||
|
#include "nsTArray.h"
|
||||||
|
|
||||||
namespace mozilla {
|
namespace mozilla {
|
||||||
namespace jni {
|
namespace jni {
|
||||||
@ -541,6 +542,76 @@ public:
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
// Ref specialization for jobjectArray.
|
||||||
|
template<>
|
||||||
|
class Ref<ObjectArray> : public RefBase<ObjectArray, jobjectArray>
|
||||||
|
{
|
||||||
|
friend class RefBase<ObjectArray, jobjectArray>;
|
||||||
|
friend class detail::TypeAdapter<Ref<ObjectArray>>;
|
||||||
|
|
||||||
|
typedef RefBase<ObjectArray, jobjectArray> Base;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
Ref(jobject instance) : Base(instance) {}
|
||||||
|
|
||||||
|
Ref(const Ref& ref) : Base(ref.mInstance) {}
|
||||||
|
|
||||||
|
public:
|
||||||
|
MOZ_IMPLICIT Ref(decltype(nullptr)) : Base(nullptr) {}
|
||||||
|
|
||||||
|
// Get the length of the object array.
|
||||||
|
size_t Length() const
|
||||||
|
{
|
||||||
|
MOZ_ASSERT(ObjectArray::mInstance);
|
||||||
|
JNIEnv* const env = GetEnvForThread();
|
||||||
|
const size_t ret = env->GetArrayLength(jarray(ObjectArray::mInstance));
|
||||||
|
HandleUncaughtException(env);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
Object::LocalRef GetElement(size_t index) const
|
||||||
|
{
|
||||||
|
MOZ_ASSERT(ObjectArray::mInstance);
|
||||||
|
JNIEnv* const env = GetEnvForThread();
|
||||||
|
auto ret = Object::LocalRef::Adopt(env, env->GetObjectArrayElement(
|
||||||
|
jobjectArray(ObjectArray::mInstance), jsize(index)));
|
||||||
|
HandleUncaughtException(env);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
nsTArray<Object::LocalRef> GetElements() const
|
||||||
|
{
|
||||||
|
MOZ_ASSERT(ObjectArray::mInstance);
|
||||||
|
JNIEnv* const env = GetEnvForThread();
|
||||||
|
const jsize len = size_t(env->GetArrayLength(
|
||||||
|
jarray(ObjectArray::mInstance)));
|
||||||
|
|
||||||
|
nsTArray<Object::LocalRef> array((size_t(len)));
|
||||||
|
for (jsize i = 0; i < len; i++) {
|
||||||
|
array.AppendElement(Object::LocalRef::Adopt(
|
||||||
|
env, env->GetObjectArrayElement(
|
||||||
|
jobjectArray(ObjectArray::mInstance), i)));
|
||||||
|
HandleUncaughtException(env);
|
||||||
|
}
|
||||||
|
return array;
|
||||||
|
}
|
||||||
|
|
||||||
|
operator nsTArray<Object::LocalRef>() const
|
||||||
|
{
|
||||||
|
return GetElements();
|
||||||
|
}
|
||||||
|
|
||||||
|
void SetElement(size_t index, Object::Param element) const
|
||||||
|
{
|
||||||
|
MOZ_ASSERT(ObjectArray::mInstance);
|
||||||
|
JNIEnv* const env = GetEnvForThread();
|
||||||
|
env->SetObjectArrayElement(jobjectArray(ObjectArray::mInstance),
|
||||||
|
jsize(index), element.Get());
|
||||||
|
HandleUncaughtException(env);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
// Ref specialization for jstring.
|
// Ref specialization for jstring.
|
||||||
template<>
|
template<>
|
||||||
class Ref<String> : public RefBase<String, jstring>
|
class Ref<String> : public RefBase<String, jstring>
|
||||||
@ -548,7 +619,7 @@ class Ref<String> : public RefBase<String, jstring>
|
|||||||
friend class RefBase<String, jstring>;
|
friend class RefBase<String, jstring>;
|
||||||
friend struct detail::TypeAdapter<Ref<String>>;
|
friend struct detail::TypeAdapter<Ref<String>>;
|
||||||
|
|
||||||
typedef RefBase<TypedObject<jstring>, jstring> Base;
|
typedef RefBase<String, jstring> Base;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
Ref(jobject instance) : Base(instance) {}
|
Ref(jobject instance) : Base(instance) {}
|
||||||
|
Loading…
Reference in New Issue
Block a user