|
1 |
| -#include <jni.h> |
2 |
| -#include "react-native-fast-openpgp.h" |
3 | 1 | #include <android/log.h>
|
| 2 | +#include <jni.h> |
4 | 3 | #include <libopenpgp_bridge.h>
|
5 | 4 |
|
6 |
| -extern "C" |
7 |
| -JNIEXPORT void JNICALL |
8 |
| -Java_com_fastopenpgp_FastOpenpgpModule_initialize(JNIEnv *env, jobject thiz, |
9 |
| - jlong jsi_ptr) { |
10 |
| - __android_log_print(ANDROID_LOG_VERBOSE, "react-native-fast-openpgp", |
11 |
| - "Initializing"); |
12 |
| - fastOpenPGP::install(*reinterpret_cast<facebook::jsi::Runtime *>(jsi_ptr)); |
13 |
| -} |
14 |
| - |
15 |
| -extern "C" |
16 |
| -JNIEXPORT void JNICALL |
17 |
| -Java_com_fastopenpgp_FastOpenpgpModule_destruct(JNIEnv *env, jobject thiz) { |
18 |
| - fastOpenPGP::cleanup(); |
19 |
| -} |
20 |
| -extern "C" |
21 |
| -JNIEXPORT jbyteArray JNICALL |
22 |
| -Java_com_fastopenpgp_FastOpenpgpModule_callNative(JNIEnv *env, jobject thiz, |
23 |
| - jstring name, jbyteArray payload) { |
| 5 | +#include "react-native-fast-openpgp.h" |
24 | 6 |
|
25 |
| - auto nameConstChar = env->GetStringUTFChars(name, nullptr); |
26 |
| - auto payloadBytes = env->GetByteArrayElements(payload, nullptr); |
27 |
| - auto size = env->GetArrayLength(payload); |
| 7 | +extern "C" JNIEXPORT void JNICALL |
| 8 | +Java_com_fastopenpgp_FastOpenpgpModule_initialize(JNIEnv* env, |
| 9 | + jobject /* thiz */, |
| 10 | + jlong jsContext) { |
| 11 | + if (jsContext == 0) { |
| 12 | + __android_log_print(ANDROID_LOG_ERROR, "react-native-fast-openpgp", "Failed to initialize: jsContext is null"); |
| 13 | + jclass Exception = env->FindClass("java/lang/IllegalArgumentException"); |
| 14 | + env->ThrowNew(Exception, "JSI context is null"); |
| 15 | + return; |
| 16 | + } |
28 | 17 |
|
29 |
| - auto nameChar = const_cast<char *>(nameConstChar); |
30 |
| - auto response = OpenPGPBridgeCall(nameChar, payloadBytes, size); |
| 18 | + __android_log_print(ANDROID_LOG_VERBOSE, "react-native-fast-openpgp", "Initializing JSI bindings"); |
31 | 19 |
|
32 |
| - env->ReleaseStringUTFChars(name, nameConstChar); |
33 |
| - env->ReleaseByteArrayElements(payload, payloadBytes, 0); |
| 20 | + try { |
| 21 | + auto* runtime = reinterpret_cast<facebook::jsi::Runtime*>(jsContext); |
34 | 22 |
|
35 |
| - if (response->error != nullptr) { |
36 |
| - auto error = response->error; |
37 |
| - free(response); |
38 |
| - jclass Exception = env->FindClass("java/lang/Exception"); |
39 |
| - env->ThrowNew(Exception, error); |
40 |
| - return nullptr; |
41 |
| - } |
| 23 | + fastOpenPGP::install(*runtime); |
42 | 24 |
|
43 |
| - auto result = env->NewByteArray(response->size); |
44 |
| - env->SetByteArrayRegion(result, 0, response->size, (jbyte*) response->message); |
45 |
| - free(response); |
46 |
| - return result; |
| 25 | + __android_log_print(ANDROID_LOG_INFO, "react-native-fast-openpgp", "JSI bindings successfully installed"); |
| 26 | + } catch (const std::exception& e) { |
| 27 | + __android_log_print(ANDROID_LOG_ERROR, "react-native-fast-openpgp", "Exception during initialization: %s", e.what()); |
| 28 | + jclass Exception = env->FindClass("java/lang/RuntimeException"); |
| 29 | + env->ThrowNew(Exception, e.what()); |
| 30 | + } catch (...) { |
| 31 | + __android_log_print(ANDROID_LOG_ERROR, "react-native-fast-openpgp", "Unknown error during initialization"); |
| 32 | + jclass Exception = env->FindClass("java/lang/RuntimeException"); |
| 33 | + env->ThrowNew(Exception, "Unknown error occurred during JSI initialization"); |
| 34 | + } |
47 | 35 | }
|
| 36 | +extern "C" JNIEXPORT void JNICALL |
| 37 | +Java_com_fastopenpgp_FastOpenpgpModule_destruct(JNIEnv* env, jobject thiz) { |
| 38 | + fastOpenPGP::cleanup(); |
| 39 | +} |
| 40 | +extern "C" JNIEXPORT jbyteArray JNICALL |
| 41 | +Java_com_fastopenpgp_FastOpenpgpModule_callNative(JNIEnv* env, |
| 42 | + jobject thiz, |
| 43 | + jstring name, |
| 44 | + jbyteArray payload) { |
| 45 | + if (name == nullptr || payload == nullptr) { |
| 46 | + jclass Exception = env->FindClass("java/lang/NullPointerException"); |
| 47 | + env->ThrowNew(Exception, "Input parameters 'name' or 'payload' cannot be null"); |
| 48 | + return nullptr; |
| 49 | + } |
48 | 50 |
|
| 51 | + const char* nameConstChar = env->GetStringUTFChars(name, nullptr); |
| 52 | + if (nameConstChar == nullptr) { |
| 53 | + jclass Exception = env->FindClass("java/lang/OutOfMemoryError"); |
| 54 | + env->ThrowNew(Exception, "Failed to allocate memory for 'name'"); |
| 55 | + return nullptr; |
| 56 | + } |
49 | 57 |
|
50 |
| -extern "C" |
51 |
| -JNIEXPORT jbyteArray JNICALL |
52 |
| -Java_com_fastopenpgp_FastOpenpgpModule_callJSI(JNIEnv *env, jobject thiz, jlong jsi_ptr, |
53 |
| - jstring name, jbyteArray payload) { |
54 |
| - auto &runtime = *reinterpret_cast<jsi::Runtime *>(jsi_ptr); |
55 |
| - auto nameConstChar = env->GetStringUTFChars(name, nullptr); |
56 |
| - auto payloadBytes = env->GetByteArrayElements(payload, nullptr); |
57 |
| - auto size = env->GetArrayLength(payload); |
58 |
| - |
59 |
| - auto nameValue = jsi::String::createFromAscii(runtime, nameConstChar); |
| 58 | + jbyte* payloadBytes = env->GetByteArrayElements(payload, nullptr); |
| 59 | + if (payloadBytes == nullptr) { |
60 | 60 | env->ReleaseStringUTFChars(name, nameConstChar);
|
| 61 | + jclass Exception = env->FindClass("java/lang/OutOfMemoryError"); |
| 62 | + env->ThrowNew(Exception, "Failed to allocate memory for 'payload'"); |
| 63 | + return nullptr; |
| 64 | + } |
61 | 65 |
|
| 66 | + jsize size = env->GetArrayLength(payload); |
| 67 | + auto response = |
| 68 | + OpenPGPBridgeCall(const_cast<char*>(nameConstChar), payloadBytes, size); |
62 | 69 |
|
63 |
| - auto arrayBuffer = runtime.global().getPropertyAsFunction(runtime, "ArrayBuffer"); |
64 |
| - jsi::Object o = arrayBuffer.callAsConstructor(runtime, size).getObject(runtime); |
65 |
| - jsi::ArrayBuffer payloadValue = o.getArrayBuffer(runtime); |
66 |
| - memcpy(payloadValue.data(runtime), payloadBytes, size); |
67 |
| - env->ReleaseByteArrayElements(payload, payloadBytes, 0); |
| 70 | + // Release resources |
| 71 | + env->ReleaseStringUTFChars(name, nameConstChar); |
| 72 | + env->ReleaseByteArrayElements(payload, payloadBytes, JNI_ABORT); |
68 | 73 |
|
69 |
| - auto response = fastOpenPGP::call(runtime, nameValue, payloadValue); |
| 74 | + if (response->error != nullptr) { |
| 75 | + const char* error = response->error; |
| 76 | + free(response); |
| 77 | + jclass Exception = env->FindClass("java/lang/Exception"); |
| 78 | + env->ThrowNew(Exception, error); |
| 79 | + return nullptr; |
| 80 | + } |
| 81 | + |
| 82 | + jbyteArray result = env->NewByteArray(response->size); |
| 83 | + if (result == nullptr) { |
| 84 | + free(response); |
| 85 | + jclass Exception = env->FindClass("java/lang/OutOfMemoryError"); |
| 86 | + env->ThrowNew(Exception, "Failed to allocate memory for result"); |
| 87 | + return nullptr; |
| 88 | + } |
70 | 89 |
|
71 |
| - if (response.isString()) { |
72 |
| - auto error = response.asString(runtime); |
73 |
| - jclass Exception = env->FindClass("java/lang/Exception"); |
74 |
| - env->ThrowNew(Exception, error.utf8(runtime).c_str()); |
75 |
| - return nullptr; |
| 90 | + env->SetByteArrayRegion(result, 0, response->size, reinterpret_cast<jbyte*>(response->message)); |
| 91 | + free(response); |
76 | 92 |
|
77 |
| - } |
78 |
| - auto byteResult = response.asObject(runtime).getArrayBuffer(runtime); |
79 |
| - auto sizeResult = byteResult.size(runtime); |
80 |
| - auto result = env->NewByteArray(sizeResult); |
81 |
| - env->SetByteArrayRegion(result, 0, sizeResult, (jbyte*) byteResult.data(runtime)); |
82 |
| - return result; |
| 93 | + return result; |
83 | 94 | }
|
0 commit comments