feat: Add SharedArray.getSize() (#2406)

* feat: Add `SharedArray.getSize()`

* Rename `count` to `size` on iOS

* `->` instead of `.`
This commit is contained in:
Marc Rousavy 2024-01-17 18:30:26 +01:00 committed by GitHub
parent 03ee0a2099
commit 2f21609e39
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 20 additions and 6 deletions

View File

@ -28,6 +28,7 @@ jni::global_ref<jni::JByteBuffer> JSharedArray::wrapInByteBuffer(jsi::Runtime& r
JSharedArray::JSharedArray(jsi::Runtime& runtime, std::shared_ptr<TypedArrayBase> array) { JSharedArray::JSharedArray(jsi::Runtime& runtime, std::shared_ptr<TypedArrayBase> array) {
_array = array; _array = array;
_byteBuffer = wrapInByteBuffer(runtime, _array); _byteBuffer = wrapInByteBuffer(runtime, _array);
_size = _array->size(runtime);
} }
JSharedArray::JSharedArray(const jni::alias_ref<JSharedArray::jhybridobject>& javaThis, JSharedArray::JSharedArray(const jni::alias_ref<JSharedArray::jhybridobject>& javaThis,
@ -43,12 +44,14 @@ JSharedArray::JSharedArray(const jni::alias_ref<JSharedArray::jhybridobject>& ja
__android_log_print(ANDROID_LOG_INFO, TAG, "Allocating ArrayBuffer with size %i and type %i...", size, dataType); __android_log_print(ANDROID_LOG_INFO, TAG, "Allocating ArrayBuffer with size %i and type %i...", size, dataType);
_array = std::make_shared<TypedArrayBase>(runtime, size, kind); _array = std::make_shared<TypedArrayBase>(runtime, size, kind);
_byteBuffer = wrapInByteBuffer(runtime, _array); _byteBuffer = wrapInByteBuffer(runtime, _array);
_size = size;
} }
void JSharedArray::registerNatives() { void JSharedArray::registerNatives() {
registerHybrid({ registerHybrid({
makeNativeMethod("initHybrid", JSharedArray::initHybrid), makeNativeMethod("initHybrid", JSharedArray::initHybrid),
makeNativeMethod("getByteBuffer", JSharedArray::getByteBuffer), makeNativeMethod("getByteBuffer", JSharedArray::getByteBuffer),
makeNativeMethod("getSize", JSharedArray::getSize),
}); });
} }
@ -56,6 +59,10 @@ jni::local_ref<jni::JByteBuffer> JSharedArray::getByteBuffer() {
return jni::make_local(_byteBuffer); return jni::make_local(_byteBuffer);
} }
jint JSharedArray::getSize() {
return _size;
}
std::shared_ptr<TypedArrayBase> JSharedArray::getTypedArray() { std::shared_ptr<TypedArrayBase> JSharedArray::getTypedArray() {
return _array; return _array;
} }

View File

@ -23,6 +23,7 @@ public:
static jni::local_ref<JSharedArray::javaobject> create(jsi::Runtime& runtime, TypedArrayBase array); static jni::local_ref<JSharedArray::javaobject> create(jsi::Runtime& runtime, TypedArrayBase array);
public: public:
jint getSize();
jni::local_ref<jni::JByteBuffer> getByteBuffer(); jni::local_ref<jni::JByteBuffer> getByteBuffer();
std::shared_ptr<TypedArrayBase> getTypedArray(); std::shared_ptr<TypedArrayBase> getTypedArray();
@ -35,6 +36,7 @@ private:
jni::global_ref<javaobject> _javaPart; jni::global_ref<javaobject> _javaPart;
jni::global_ref<jni::JByteBuffer> _byteBuffer; jni::global_ref<jni::JByteBuffer> _byteBuffer;
std::shared_ptr<TypedArrayBase> _array; std::shared_ptr<TypedArrayBase> _array;
int _size;
private: private:
explicit JSharedArray(jsi::Runtime& runtime, std::shared_ptr<TypedArrayBase> array); explicit JSharedArray(jsi::Runtime& runtime, std::shared_ptr<TypedArrayBase> array);

View File

@ -39,6 +39,11 @@ public final class SharedArray {
*/ */
public native ByteBuffer getByteBuffer(); public native ByteBuffer getByteBuffer();
/**
* Gets the size of the ByteBuffer.
*/
public native int getSize();
private native HybridData initHybrid(VisionCameraProxy proxy, int dataType, int size); private native HybridData initHybrid(VisionCameraProxy proxy, int dataType, int size);
/** /**

View File

@ -48,7 +48,7 @@ NS_ASSUME_NONNULL_BEGIN
#endif #endif
@property(nonatomic, readonly, nonnull) uint8_t* data; @property(nonatomic, readonly, nonnull) uint8_t* data;
@property(nonatomic, readonly) NSInteger count; @property(nonatomic, readonly) NSInteger size;
@end @end

View File

@ -15,7 +15,7 @@ using namespace facebook;
@implementation SharedArray { @implementation SharedArray {
uint8_t* _data; uint8_t* _data;
NSInteger _count; NSInteger _size;
std::shared_ptr<vision::TypedArrayBase> _array; std::shared_ptr<vision::TypedArrayBase> _array;
} }
@ -29,7 +29,7 @@ vision::TypedArrayKind getTypedArrayKind(int unsafeEnumValue) {
vision::TypedArrayKind kind = getTypedArrayKind((int)type); vision::TypedArrayKind kind = getTypedArrayKind((int)type);
_array = std::make_shared<vision::TypedArrayBase>(vision::TypedArrayBase(runtime, size, kind)); _array = std::make_shared<vision::TypedArrayBase>(vision::TypedArrayBase(runtime, size, kind));
_data = _array->getBuffer(runtime).data(runtime); _data = _array->getBuffer(runtime).data(runtime);
_count = size; _size = size;
} }
return self; return self;
} }
@ -38,7 +38,7 @@ vision::TypedArrayKind getTypedArrayKind(int unsafeEnumValue) {
if (self = [super init]) { if (self = [super init]) {
_array = typedArray; _array = typedArray;
_data = _array->getBuffer(runtime).data(runtime); _data = _array->getBuffer(runtime).data(runtime);
_count = _array->getBuffer(runtime).size(runtime); _size = _array->getBuffer(runtime).size(runtime);
} }
return self; return self;
} }
@ -51,8 +51,8 @@ vision::TypedArrayKind getTypedArrayKind(int unsafeEnumValue) {
return _data; return _data;
} }
- (NSInteger)count { - (NSInteger)size {
return _count; return _size;
} }
@end @end