Skip to content

Commit 09db629

Browse files
committed
initial commit with base structure
1 parent 2d90e2f commit 09db629

39 files changed

+10009
-189
lines changed

Makefile

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
default: test
2+
3+
test:
4+
yarn test
5+
6+
upgrade: upgrade-libs upgrade-flatbuffers
7+
8+
upgrade-libs:
9+
./scripts/upgrade_bridge_libs.sh
10+
11+
upgrade-flatbuffers:
12+
./scripts/upgrade_bridge_flatbuffers.sh

android/CMakeLists.txt

+31-10
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,36 @@
1-
cmake_minimum_required(VERSION 3.4.1)
1+
cmake_minimum_required(VERSION 3.10.0)
22

3-
set (CMAKE_VERBOSE_MAKEFILE ON)
4-
set (CMAKE_CXX_STANDARD 11)
3+
include_directories(
4+
../cpp
5+
../node_modules/react-native/ReactCommon/jsi
6+
)
57

6-
add_library(cpp
7-
SHARED
8-
../cpp/example.cpp
9-
cpp-adapter.cpp
8+
add_definitions(
9+
-DFOLLY_USE_LIBCPP=1
10+
-DFOLLY_NO_CONFIG=1
11+
-DFOLLY_HAVE_MEMRCHR=1
1012
)
1113

12-
# Specifies a path to native header files.
13-
include_directories(
14-
../cpp
14+
add_library(openpgp_bridge SHARED IMPORTED )
15+
16+
set_target_properties(openpgp_bridge
17+
PROPERTIES
18+
IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libopenpgp_bridge.so
19+
)
20+
21+
add_library(fast-openpgp
22+
SHARED
23+
../node_modules/react-native/ReactCommon/jsi/jsi/jsi.cpp
24+
../cpp/react-native-fast-openpgp.cpp
25+
../cpp/react-native-fast-openpgp.h
26+
fast-openpgp-adapter.cpp
1527
)
28+
29+
find_library(
30+
log-lib
31+
log )
32+
33+
target_link_libraries(fast-openpgp
34+
openpgp_bridge
35+
android
36+
${log-lib})

android/build.gradle

+4-3
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,16 @@ android {
3636

3737
externalNativeBuild {
3838
cmake {
39-
cppFlags "-O2 -frtti -fexceptions -Wall -fstack-protector-all"
39+
cppFlags "-O2 -frtti -fexceptions -Wall -Wno-unused-variable -fstack-protector-all"
4040
abiFilters 'x86', 'x86_64', 'armeabi-v7a', 'arm64-v8a'
4141
}
4242
}
43-
43+
4444
}
45-
45+
4646
externalNativeBuild {
4747
cmake {
48+
version '3.10.0+'
4849
path "CMakeLists.txt"
4950
}
5051
}

android/cpp-adapter.cpp

-8
This file was deleted.

android/fast-openpgp-adapter.cpp

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#include <jni.h>
2+
#include "react-native-fast-openpgp.h"
3+
#include <android/log.h>
4+
#include <libopenpgp_bridge.h>
5+
6+
extern "C"
7+
JNIEXPORT void JNICALL
8+
Java_com_reactnativefastopenpgp_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_reactnativefastopenpgp_FastOpenpgpModule_destruct(JNIEnv *env, jobject thiz) {
18+
fastOpenPGP::cleanup();
19+
}
20+
extern "C"
21+
JNIEXPORT jbyteArray JNICALL
22+
Java_com_reactnativefastopenpgp_FastOpenpgpModule_call(JNIEnv *env, jobject thiz,
23+
jstring name, jbyteArray payload) {
24+
25+
jboolean iscopy;
26+
auto nameConstChar = env->GetStringUTFChars(name, &iscopy);
27+
auto payloadBytes = env->GetByteArrayElements(payload, &iscopy);
28+
auto size = env->GetArrayLength(payload);
29+
30+
auto nameChar = const_cast<char *>(nameConstChar);
31+
auto response = OpenPGPBridgeCall(nameChar, payloadBytes, size);
32+
//env->ReleaseStringUTFChars(name, nameConstChar);
33+
34+
if (response->error != nullptr) {
35+
auto error = response->error;
36+
// free(response);
37+
jclass Exception = env->FindClass("java/lang/Exception");
38+
env->ThrowNew(Exception, error);
39+
return nullptr;
40+
}
41+
42+
auto result = env->NewByteArray(response->size);
43+
memcpy(result, response->message, response->size);
44+
// free(response);
45+
return result;
46+
}
47+
48+
49+
extern "C"
50+
JNIEXPORT jbyteArray JNICALL
51+
Java_com_reactnativefastopenpgp_FastOpenpgpModule_callJSI(JNIEnv *env, jobject thiz, jlong jsi_ptr,
52+
jstring name, jbyteArray payload) {
53+
auto runtime = reinterpret_cast<jsi::Runtime *>(jsi_ptr);
54+
jboolean iscopy;
55+
auto nameConstChar = env->GetStringUTFChars(name, &iscopy);
56+
auto nameValue = jsi::String::createFromAscii(*runtime, nameConstChar);
57+
env->ReleaseStringUTFChars(name, nameConstChar);
58+
59+
auto payloadArray = env->GetByteArrayElements(payload, &iscopy);
60+
int size = env->GetArrayLength(payload);
61+
62+
auto arrayBuffer = runtime->global().getPropertyAsFunction(*runtime, "ArrayBuffer");
63+
jsi::Object o = arrayBuffer.callAsConstructor(*runtime, size).getObject(*runtime);
64+
jsi::ArrayBuffer payloadValue = o.getArrayBuffer(*runtime);
65+
memcpy(payloadValue.data(*runtime), payloadArray, size);
66+
67+
auto response = fastOpenPGP::call(*runtime, nameValue, payloadValue);
68+
69+
if (response.isString()) {
70+
auto error = response.asString(*runtime);
71+
jclass Exception = env->FindClass("java/lang/Exception");
72+
env->ThrowNew(Exception, error.utf8(*runtime).c_str());
73+
return nullptr;
74+
75+
}
76+
auto byteResult = response.asObject(*runtime).getArrayBuffer(*runtime);
77+
auto sizeResult = byteResult.size(*runtime);
78+
auto result = env->NewByteArray(sizeResult);
79+
memcpy(result, byteResult.data(*runtime), sizeResult);
80+
return result;
81+
}
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,68 @@
11
package com.reactnativefastopenpgp
22

3-
import com.facebook.react.bridge.ReactApplicationContext
4-
import com.facebook.react.bridge.ReactContextBaseJavaModule
5-
import com.facebook.react.bridge.ReactMethod
6-
import com.facebook.react.bridge.Promise
3+
import androidx.annotation.NonNull
4+
import com.facebook.react.bridge.*
75

8-
class FastOpenpgpModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext) {
96

10-
override fun getName(): String {
11-
return "FastOpenpgp"
7+
internal class FastOpenpgpModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext) {
8+
9+
external fun initialize(jsiPtr: Long);
10+
external fun destruct();
11+
external fun callJSI(jsiPtr: Long, name: String, payload: ByteArray): ByteArray;
12+
external fun call(name: String, payload: ByteArray): ByteArray;
13+
14+
companion object {
15+
init {
16+
System.loadLibrary("fast-openpgp")
17+
}
18+
}
19+
20+
@ReactMethod
21+
fun callJSI(name: String, payload: ReadableArray, promise: Promise) {
22+
Thread {
23+
try {
24+
val bytes = ByteArray(payload.size()) { pos -> payload.getInt(pos).toByte() }
25+
val result = callJSI(this.reactApplicationContext.javaScriptContextHolder.get(), name, bytes)
26+
val resultList = Arguments.createArray()
27+
for (i in result.indices) {
28+
resultList.pushInt(result.get(i).toInt())
29+
}
30+
promise.resolve(resultList)
31+
} catch (e: Exception) {
32+
promise.reject(e)
33+
}
34+
}.start()
1235
}
1336

14-
// Example method
15-
// See https://reactnative.dev/docs/native-modules-android
1637
@ReactMethod
17-
fun multiply(a: Int, b: Int, promise: Promise) {
18-
19-
promise.resolve(nativeMultiply(a, b));
20-
38+
fun call(name: String, payload: ReadableArray, promise: Promise) {
39+
Thread {
40+
try {
41+
val bytes = ByteArray(payload.size()) { pos -> payload.getInt(pos).toByte() }
42+
val result = call(name, bytes)
43+
44+
val resultList = Arguments.createArray()
45+
for (i in result.indices) {
46+
resultList.pushInt(result.get(i).toInt())
47+
}
48+
promise.resolve(resultList)
49+
} catch (e: Exception) {
50+
promise.reject(e)
51+
}
52+
}.start()
2153
}
2254

23-
24-
external fun nativeMultiply(a: Int, b: Int): Int;
55+
@NonNull
56+
override fun getName(): String {
57+
return "FastOpenPGP"
58+
}
2559

26-
companion object
27-
{
60+
override fun initialize() {
61+
super.initialize()
62+
initialize(this.reactApplicationContext.javaScriptContextHolder.get())
63+
}
2864

29-
// Used to load the 'native-lib' library on application startup.
30-
init
31-
{
32-
System.loadLibrary("cpp")
33-
}
65+
override fun onCatalystInstanceDestroy() {
66+
destruct();
3467
}
35-
3668
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/* Code generated by cmd/cgo; DO NOT EDIT. */
2+
3+
/* package command-line-arguments */
4+
5+
6+
#line 1 "cgo-builtin-export-prolog"
7+
8+
#include <stddef.h> /* for ptrdiff_t below */
9+
10+
#ifndef GO_CGO_EXPORT_PROLOGUE_H
11+
#define GO_CGO_EXPORT_PROLOGUE_H
12+
13+
#ifndef GO_CGO_GOSTRING_TYPEDEF
14+
typedef struct { const char *p; ptrdiff_t n; } _GoString_;
15+
#endif
16+
17+
#endif
18+
19+
/* Start of preamble from import "C" comments. */
20+
21+
22+
#line 3 "main.go"
23+
#include <stdint.h>
24+
#include <stdlib.h>
25+
typedef struct { void* message; int size; char* error; } BytesReturn;
26+
27+
#line 1 "cgo-generated-wrapper"
28+
29+
30+
/* End of preamble from import "C" comments. */
31+
32+
33+
/* Start of boilerplate cgo prologue. */
34+
#line 1 "cgo-gcc-export-header-prolog"
35+
36+
#ifndef GO_CGO_PROLOGUE_H
37+
#define GO_CGO_PROLOGUE_H
38+
39+
typedef signed char GoInt8;
40+
typedef unsigned char GoUint8;
41+
typedef short GoInt16;
42+
typedef unsigned short GoUint16;
43+
typedef int GoInt32;
44+
typedef unsigned int GoUint32;
45+
typedef long long GoInt64;
46+
typedef unsigned long long GoUint64;
47+
typedef GoInt64 GoInt;
48+
typedef GoUint64 GoUint;
49+
typedef __SIZE_TYPE__ GoUintptr;
50+
typedef float GoFloat32;
51+
typedef double GoFloat64;
52+
typedef float _Complex GoComplex64;
53+
typedef double _Complex GoComplex128;
54+
55+
/*
56+
static assertion to make sure the file is being used on architecture
57+
at least with matching size of GoInt.
58+
*/
59+
typedef char _check_for_64_bit_pointer_matching_GoInt[sizeof(void*)==64/8 ? 1:-1];
60+
61+
#ifndef GO_CGO_GOSTRING_TYPEDEF
62+
typedef _GoString_ GoString;
63+
#endif
64+
typedef void *GoMap;
65+
typedef void *GoChan;
66+
typedef struct { void *t; void *v; } GoInterface;
67+
typedef struct { void *data; GoInt len; GoInt cap; } GoSlice;
68+
69+
#endif
70+
71+
/* End of boilerplate cgo prologue. */
72+
73+
#ifdef __cplusplus
74+
extern "C" {
75+
#endif
76+
77+
extern BytesReturn* OpenPGPBridgeCall(char* name, void* payload, int payloadSize);
78+
79+
#ifdef __cplusplus
80+
}
81+
#endif
Binary file not shown.

0 commit comments

Comments
 (0)