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