Bug 1696145 - Add Array::From(nsTArray) r=aklotz

Differential Revision: https://phabricator.services.mozilla.com/D111923
This commit is contained in:
Agi Sferro 2021-04-20 18:35:36 +00:00
Родитель 1bce404eaf
Коммит a5972b2933
6 изменённых файлов: 124 добавлений и 66 удалений

Просмотреть файл

@ -707,20 +707,11 @@ static java::sdk::CryptoInfo::LocalRef GetCryptoInfoFromSample(
tempIV.AppendElement(0);
}
auto numBytesOfPlainData = mozilla::jni::IntArray::New(
reinterpret_cast<const int32_t*>(&plainSizes[0]), plainSizes.Length());
auto numBytesOfEncryptedData = mozilla::jni::IntArray::New(
reinterpret_cast<const int32_t*>(&cryptoObj.mEncryptedSizes[0]),
cryptoObj.mEncryptedSizes.Length());
auto iv = mozilla::jni::ByteArray::New(reinterpret_cast<int8_t*>(&tempIV[0]),
tempIV.Length());
auto keyId = mozilla::jni::ByteArray::New(
reinterpret_cast<const int8_t*>(&cryptoObj.mKeyId[0]),
cryptoObj.mKeyId.Length());
cryptoInfo->Set(numSubSamples, numBytesOfPlainData, numBytesOfEncryptedData,
keyId, iv, java::sdk::MediaCodec::CRYPTO_MODE_AES_CTR);
cryptoInfo->Set(numSubSamples, mozilla::jni::IntArray::From(plainSizes),
mozilla::jni::IntArray::From(cryptoObj.mEncryptedSizes),
mozilla::jni::ByteArray::From(cryptoObj.mKeyId),
mozilla::jni::ByteArray::From(tempIV),
java::sdk::MediaCodec::CRYPTO_MODE_AES_CTR);
return cryptoInfo;
}

Просмотреть файл

@ -49,10 +49,9 @@ class GeckoTelemetryDelegate final
samples.AppendElement(static_cast<int64_t>(aSamples[i]));
}
// LongArray::New *copies* the elements
mProxy->DispatchHistogram(
aIsCategorical, aName,
mozilla::jni::LongArray::New(samples.Elements(), samples.Length()));
// LongArray::From *copies* the elements
mProxy->DispatchHistogram(aIsCategorical, aName,
mozilla::jni::LongArray::From(samples));
}
// Implement StreamingTelemetryDelegate.

Просмотреть файл

@ -13,6 +13,7 @@
#include "mozilla/fallible.h"
#include "mozilla/jni/Utils.h"
#include "mozilla/jni/TypeAdapter.h"
#include "nsError.h" // for nsresult
#include "nsString.h"
#include "nsTArray.h"
@ -804,6 +805,7 @@ struct TypeAdapter;
// Ref specialization for arrays.
template <typename JNIType, class ElementType>
class ArrayRefBase : public ObjectBase<TypedObject<JNIType>, JNIType> {
protected:
using Base = ObjectBase<TypedObject<JNIType>, JNIType>;
public:
@ -874,24 +876,55 @@ class ArrayRefBase : public ObjectBase<TypedObject<JNIType>, JNIType> {
operator nsTArray<ElementType>() const { return GetElements(); }
};
#define DEFINE_PRIMITIVE_ARRAY_REF(JNIType, ElementType) \
template <> \
class TypedObject<JNIType> : public ArrayRefBase<JNIType, ElementType> { \
public: \
explicit TypedObject(const Context& ctx) \
: ArrayRefBase<JNIType, ElementType>(ctx) {} \
#define DEFINE_PRIMITIVE_ARRAY_REF_HEADER(JNIType, ElementType) \
template <> \
class TypedObject<JNIType> : public ArrayRefBase<JNIType, ElementType> { \
public: \
explicit TypedObject(const Context& ctx) \
: ArrayRefBase<JNIType, ElementType>(ctx) {} \
static typename Base::LocalRef From(const nsTArray<ElementType>& aArray) { \
return New(aArray.Elements(), aArray.Length()); \
}
#define DEFINE_PRIMITIVE_ARRAY_REF_FOOTER }
#define DEFINE_PRIMITIVE_ARRAY_REF_FROM_IMPLICIT_CONVERSION(ElementType, \
ConvertFromType) \
static typename Base::LocalRef From( \
const nsTArray<ConvertFromType>& aArray) { \
return New(reinterpret_cast<const ElementType*>(aArray.Elements()), \
aArray.Length()); \
}
#define DEFINE_PRIMITIVE_ARRAY_REF(JNIType, ElementType) \
DEFINE_PRIMITIVE_ARRAY_REF_HEADER(JNIType, ElementType) \
DEFINE_PRIMITIVE_ARRAY_REF_FOOTER
#define DEFINE_PRIMITIVE_ARRAY_REF_WITH_IMPLICIT_CONVERSION( \
JNIType, ElementType, ConvertFromType) \
DEFINE_PRIMITIVE_ARRAY_REF_HEADER(JNIType, ElementType) \
DEFINE_PRIMITIVE_ARRAY_REF_FROM_IMPLICIT_CONVERSION(ElementType, \
ConvertFromType) \
DEFINE_PRIMITIVE_ARRAY_REF_FOOTER
DEFINE_PRIMITIVE_ARRAY_REF(jbooleanArray, bool);
DEFINE_PRIMITIVE_ARRAY_REF(jbyteArray, int8_t);
DEFINE_PRIMITIVE_ARRAY_REF_WITH_IMPLICIT_CONVERSION(jbyteArray, int8_t,
uint8_t);
DEFINE_PRIMITIVE_ARRAY_REF(jcharArray, char16_t);
DEFINE_PRIMITIVE_ARRAY_REF(jshortArray, int16_t);
DEFINE_PRIMITIVE_ARRAY_REF(jintArray, int32_t);
DEFINE_PRIMITIVE_ARRAY_REF(jlongArray, int64_t);
DEFINE_PRIMITIVE_ARRAY_REF_WITH_IMPLICIT_CONVERSION(jshortArray, int16_t,
uint16_t);
DEFINE_PRIMITIVE_ARRAY_REF_WITH_IMPLICIT_CONVERSION(jintArray, int32_t,
uint32_t);
DEFINE_PRIMITIVE_ARRAY_REF(jfloatArray, float);
DEFINE_PRIMITIVE_ARRAY_REF_WITH_IMPLICIT_CONVERSION(jlongArray, int64_t,
uint64_t);
DEFINE_PRIMITIVE_ARRAY_REF(jdoubleArray, double);
#undef DEFINE_PRIMITIVE_ARRAY_REF
#undef DEFINE_PRIMITIVE_ARRAY_REF_WITH_IMPLICIT_CONVERSION
#undef DEFINE_PRIMITIVE_ARRAY_HEADER
#undef DEFINE_PRIMITIVE_ARRAY_FROM_IMPLICIT_CONVERSION
#undef DEFINE_PRIMITIVE_ARRAY_FOOTER
class ByteBuffer : public ObjectBase<ByteBuffer, jobject> {
public:

Просмотреть файл

@ -0,0 +1,71 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef mozilla_jni_TypeAdapter_h__
#define mozilla_jni_TypeAdapter_h__
#include <jni.h>
#include "mozilla/jni/Refs.h"
namespace mozilla {
namespace jni {
namespace detail {
// TypeAdapter specializations are the interfaces between native/C++ types such
// as int32_t and JNI types such as jint. The template parameter T is the native
// type, and each TypeAdapter specialization can have the following members:
//
// * Call: JNIEnv member pointer for making a method call that returns T.
// * StaticCall: JNIEnv member pointer for making a static call that returns T.
// * Get: JNIEnv member pointer for getting a field of type T.
// * StaticGet: JNIEnv member pointer for getting a static field of type T.
// * Set: JNIEnv member pointer for setting a field of type T.
// * StaticGet: JNIEnv member pointer for setting a static field of type T.
// * ToNative: static function that converts the JNI type to the native type.
// * FromNative: static function that converts the native type to the JNI type.
template <typename T>
struct TypeAdapter;
#define DEFINE_PRIMITIVE_TYPE_ADAPTER(NativeType, JNIType, JNIName) \
\
template <> \
struct TypeAdapter<NativeType> { \
using JNI##Type = JNIType; \
\
static constexpr auto Call = &JNIEnv::Call##JNIName##MethodA; \
static constexpr auto StaticCall = &JNIEnv::CallStatic##JNIName##MethodA; \
static constexpr auto Get = &JNIEnv::Get##JNIName##Field; \
static constexpr auto StaticGet = &JNIEnv::GetStatic##JNIName##Field; \
static constexpr auto Set = &JNIEnv::Set##JNIName##Field; \
static constexpr auto StaticSet = &JNIEnv::SetStatic##JNIName##Field; \
static constexpr auto GetArray = &JNIEnv::Get##JNIName##ArrayRegion; \
static constexpr auto SetArray = &JNIEnv::Set##JNIName##ArrayRegion; \
static constexpr auto NewArray = &JNIEnv::New##JNIName##Array; \
\
static JNIType FromNative(JNIEnv*, NativeType val) { \
return static_cast<JNIType>(val); \
} \
static NativeType ToNative(JNIEnv*, JNIType val) { \
return static_cast<NativeType>(val); \
} \
}
DEFINE_PRIMITIVE_TYPE_ADAPTER(bool, jboolean, Boolean);
DEFINE_PRIMITIVE_TYPE_ADAPTER(int8_t, jbyte, Byte);
DEFINE_PRIMITIVE_TYPE_ADAPTER(char16_t, jchar, Char);
DEFINE_PRIMITIVE_TYPE_ADAPTER(int16_t, jshort, Short);
DEFINE_PRIMITIVE_TYPE_ADAPTER(int32_t, jint, Int);
DEFINE_PRIMITIVE_TYPE_ADAPTER(int64_t, jlong, Long);
DEFINE_PRIMITIVE_TYPE_ADAPTER(float, jfloat, Float);
DEFINE_PRIMITIVE_TYPE_ADAPTER(double, jdouble, Double);
#undef DEFINE_PRIMITIVE_TYPE_ADAPTER
} // namespace detail
} // namespace jni
} // namespace mozilla
#endif // mozilla_jni_Types_h__

Просмотреть файл

@ -10,6 +10,7 @@
#include <jni.h>
#include "mozilla/jni/Refs.h"
#include "mozilla/jni/TypeAdapter.h"
namespace mozilla {
namespace jni {
@ -28,9 +29,6 @@ namespace detail {
// * ToNative: static function that converts the JNI type to the native type.
// * FromNative: static function that converts the native type to the JNI type.
template <typename T>
struct TypeAdapter;
// TypeAdapter<LocalRef<Cls>> applies when jobject is a return value.
template <class Cls>
struct TypeAdapter<LocalRef<Cls>> {
@ -115,41 +113,6 @@ struct TypeAdapter<StringParam> : public TypeAdapter<String::Ref> {};
template <class Cls>
struct TypeAdapter<const Cls&> : public TypeAdapter<Cls> {};
#define DEFINE_PRIMITIVE_TYPE_ADAPTER(NativeType, JNIType, JNIName) \
\
template <> \
struct TypeAdapter<NativeType> { \
using JNI##Type = JNIType; \
\
static constexpr auto Call = &JNIEnv::Call##JNIName##MethodA; \
static constexpr auto StaticCall = &JNIEnv::CallStatic##JNIName##MethodA; \
static constexpr auto Get = &JNIEnv::Get##JNIName##Field; \
static constexpr auto StaticGet = &JNIEnv::GetStatic##JNIName##Field; \
static constexpr auto Set = &JNIEnv::Set##JNIName##Field; \
static constexpr auto StaticSet = &JNIEnv::SetStatic##JNIName##Field; \
static constexpr auto GetArray = &JNIEnv::Get##JNIName##ArrayRegion; \
static constexpr auto SetArray = &JNIEnv::Set##JNIName##ArrayRegion; \
static constexpr auto NewArray = &JNIEnv::New##JNIName##Array; \
\
static JNIType FromNative(JNIEnv*, NativeType val) { \
return static_cast<JNIType>(val); \
} \
static NativeType ToNative(JNIEnv*, JNIType val) { \
return static_cast<NativeType>(val); \
} \
}
DEFINE_PRIMITIVE_TYPE_ADAPTER(bool, jboolean, Boolean);
DEFINE_PRIMITIVE_TYPE_ADAPTER(int8_t, jbyte, Byte);
DEFINE_PRIMITIVE_TYPE_ADAPTER(char16_t, jchar, Char);
DEFINE_PRIMITIVE_TYPE_ADAPTER(int16_t, jshort, Short);
DEFINE_PRIMITIVE_TYPE_ADAPTER(int32_t, jint, Int);
DEFINE_PRIMITIVE_TYPE_ADAPTER(int64_t, jlong, Long);
DEFINE_PRIMITIVE_TYPE_ADAPTER(float, jfloat, Float);
DEFINE_PRIMITIVE_TYPE_ADAPTER(double, jdouble, Double);
#undef DEFINE_PRIMITIVE_TYPE_ADAPTER
} // namespace detail
using namespace detail;

Просмотреть файл

@ -14,6 +14,7 @@ EXPORTS.mozilla.jni += [
"GeckoResultUtils.h",
"Natives.h",
"Refs.h",
"TypeAdapter.h",
"Types.h",
"Utils.h",
]