1 #pragma once 2 3 #include "TypedArray.h" 4 #include "JavaScriptObject.h" 5 #include "WeakRuntimeHolder.h" 6 7 #include <fbjni/fbjni.h> 8 #include <fbjni/ByteBuffer.h> 9 #include <jsi/jsi.h> 10 11 #include <memory> 12 13 namespace expo { 14 15 class JavaScriptRuntime; 16 17 namespace jni = facebook::jni; 18 namespace jsi = facebook::jsi; 19 20 class JavaScriptTypedArray : public jni::HybridClass<JavaScriptTypedArray, JavaScriptObject> { 21 public: 22 static auto constexpr 23 kJavaDescriptor = "Lexpo/modules/kotlin/jni/JavaScriptTypedArray;"; 24 static auto constexpr TAG = "JavaScriptTypedArray"; 25 26 static void registerNatives(); 27 28 static jni::local_ref<JavaScriptTypedArray::javaobject> newInstance( 29 JSIInteropModuleRegistry *jsiInteropModuleRegistry, 30 std::weak_ptr<JavaScriptRuntime> runtime, 31 std::shared_ptr<jsi::Object> jsObject 32 ); 33 34 JavaScriptTypedArray( 35 std::weak_ptr<JavaScriptRuntime> runtime, 36 std::shared_ptr<jsi::Object> jsObject 37 ); 38 39 JavaScriptTypedArray( 40 WeakRuntimeHolder runtime, 41 std::shared_ptr<jsi::Object> jsObject 42 ); 43 44 /** 45 * Gets a raw kind of the underlying typed array. 46 */ 47 int getRawKind(); 48 49 /** 50 * Converts typed array into a direct byte buffer. 51 */ 52 jni::local_ref<jni::JByteBuffer> toDirectBuffer(); 53 54 private: 55 std::shared_ptr<expo::TypedArray> typedArrayWrapper; 56 57 /** 58 * Cached pointer to the beginning of the byte buffer. 59 */ 60 char *rawPointer; 61 62 void readBuffer(jni::alias_ref<jni::JArrayByte> buffer, int position, int size); 63 64 void writeBuffer(jni::alias_ref<jni::JArrayByte> buffer, int position, int size); 65 66 template<class T> read(int position)67 T read(int position) { 68 return *reinterpret_cast<T *>(rawPointer + position); 69 } 70 71 template<class T> write(int position,T value)72 void write(int position, T value) { 73 *reinterpret_cast<T *>(rawPointer + position) = value; 74 } 75 }; 76 } // namespace expo 77