1
1
package com.reactnativefastopenpgp
2
2
3
+ import android.util.Log
3
4
import androidx.annotation.NonNull
4
5
import com.facebook.react.bridge.*
5
6
6
7
@ExperimentalUnsignedTypes
7
8
internal class FastOpenpgpModule (reactContext : ReactApplicationContext ) :
8
9
ReactContextBaseJavaModule (reactContext) {
9
10
11
+ val TAG = " [FastRsaModule]"
12
+
10
13
external fun initialize (jsiPtr : Long );
11
14
external fun destruct ();
12
15
external fun callJSI (jsiPtr : Long , name : String , payload : ByteArray ): ByteArray ;
@@ -18,56 +21,75 @@ internal class FastOpenpgpModule(reactContext: ReactApplicationContext) :
18
21
}
19
22
}
20
23
21
- @ReactMethod
22
- fun callJSI (name : String , payload : ReadableArray , promise : Promise ) {
23
- Thread {
24
- try {
25
- val bytes = ByteArray (payload.size()) { pos -> payload.getInt(pos).toByte() }
26
- var result =
27
- callJSI(this .reactApplicationContext.javaScriptContextHolder.get(), name, bytes)
28
- val resultList = Arguments .createArray()
29
- for (i in result.indices) {
30
- resultList.pushInt(result[i].toInt())
31
- }
32
- result = ByteArray (0 );
33
- promise.resolve(resultList)
34
- } catch (e: Exception ) {
35
- promise.reject(e)
36
- }
37
- }.start()
38
- }
39
-
40
- @ReactMethod
41
- fun call (name : String , payload : ReadableArray , promise : Promise ) {
42
- Thread {
43
- try {
44
- val bytes = ByteArray (payload.size()) { pos -> payload.getInt(pos).toByte() }
45
- var result = callNative(name, bytes)
46
- val resultList = Arguments .createArray()
47
- for (i in result.indices) {
48
- resultList.pushInt(result[i].toInt())
49
- }
50
- result = ByteArray (0 );
51
- promise.resolve(resultList)
52
- } catch (e: Exception ) {
53
- promise.reject(e)
54
- }
55
- }.start()
56
- }
24
+ @ReactMethod
25
+ fun callJSI (name : String , payload : ReadableArray , promise : Promise ) {
26
+ Thread {
27
+ reactApplicationContext.runOnJSQueueThread {
28
+ try {
29
+ val contextHolder = this .reactApplicationContext.javaScriptContextHolder.get()
30
+ if (contextHolder.toInt() == 0 ) {
31
+ call(name, payload, promise)
32
+ return @runOnJSQueueThread
33
+ }
34
+ val bytes = ByteArray (payload.size()) { pos -> payload.getInt(pos).toByte() }
35
+ val result = callJSI(contextHolder, name, bytes)
36
+ val resultList = Arguments .createArray()
37
+ for (i in result.indices) {
38
+ resultList.pushInt(result[i].toInt())
39
+ }
40
+ promise.resolve(resultList)
41
+ } catch (e: Exception ) {
42
+ promise.reject(e)
43
+ }
44
+ }
45
+ }.start()
46
+ }
57
47
58
- @NonNull
59
- override fun getName (): String {
60
- return " FastOpenPGP"
61
- }
48
+ @ReactMethod
49
+ fun call (name : String , payload : ReadableArray , promise : Promise ) {
50
+ Thread {
51
+ try {
52
+ val bytes = ByteArray (payload.size()) { pos -> payload.getInt(pos).toByte() }
53
+ val result = callNative(name, bytes)
54
+ val resultList = Arguments .createArray()
55
+ for (i in result.indices) {
56
+ resultList.pushInt(result[i].toInt())
57
+ }
58
+ promise.resolve(resultList)
59
+ } catch (e: Exception ) {
60
+ promise.reject(e)
61
+ }
62
+ }.start()
63
+ }
62
64
63
- override fun initialize () {
64
- super .initialize()
65
- reactApplicationContext.runOnJSQueueThread {
66
- initialize(this .reactApplicationContext.javaScriptContextHolder.get())
65
+ @ReactMethod()
66
+ fun install (promise : Promise ) {
67
+ Thread {
68
+ reactApplicationContext.runOnJSQueueThread {
69
+ Log .d(TAG , " installing" )
70
+ try {
71
+ val contextHolder = this .reactApplicationContext.javaScriptContextHolder.get()
72
+ if (contextHolder.toInt() == 0 ) {
73
+ promise.resolve(false )
74
+ return @runOnJSQueueThread
75
+ }
76
+ initialize(contextHolder)
77
+ Log .i(TAG , " successfully installed" )
78
+ promise.resolve(true )
79
+ } catch (exception: java.lang.Exception ) {
80
+ Log .e(TAG , " failed to install JSI" , exception)
81
+ promise.reject(exception)
67
82
}
68
- }
83
+ }
84
+ }.start()
85
+ }
69
86
70
- override fun onCatalystInstanceDestroy () {
71
- destruct();
72
- }
87
+ override fun getName (): String {
88
+ return " FastOpenPGP"
89
+ }
90
+
91
+ override fun onCatalystInstanceDestroy () {
92
+ destruct();
93
+ }
73
94
}
95
+
0 commit comments