Skip to content

Commit

Permalink
feat: rewrite to C++ with JSI
Browse files Browse the repository at this point in the history
  • Loading branch information
dcangulo committed Aug 7, 2022
1 parent 0a48c10 commit db9f4bb
Show file tree
Hide file tree
Showing 16 changed files with 500 additions and 62 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ build/
.gradle
local.properties
*.iml
.cxx

# BUCK
buck-out/
Expand Down
29 changes: 29 additions & 0 deletions android/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
cmake_minimum_required(VERSION 3.9.0)

add_library(
reactnativegetrandomvalues
SHARED
../../react-native/ReactCommon/jsi/jsi/jsi.cpp
../cpp/react-native-get-random-values.cpp
../cpp/base64.cpp
./cpp-adapter.cpp
)

include_directories(
../../react-native/React
../../react-native/React/Base
../../react-native/ReactCommon/jsi
../cpp
)

set_target_properties(
reactnativegetrandomvalues PROPERTIES
CXX_STANDARD 17
CXX_EXTENSIONS OFF
POSITION_INDEPENDENT_CODE ON
)

target_link_libraries(
reactnativegetrandomvalues
android
)
29 changes: 29 additions & 0 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ buildscript {
repositories {
mavenCentral()
google()
jcenter()
}
def buildGradleVersion = ext.has('buildGradlePluginVersion') ? ext.get('buildGradlePluginVersion') : '4.2.2'

Expand All @@ -30,19 +31,47 @@ android {
targetSdkVersion safeExtGet('targetSdkVersion', 30)
versionCode 1
versionName "1.0"

externalNativeBuild {
cmake {
cppFlags "-O2 -frtti -fexceptions -Wall -fstack-protector-all"
abiFilters 'x86', 'x86_64', 'armeabi-v7a', 'arm64-v8a'
}
}
}

externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}

buildTypes {
release {
minifyEnabled false
}
}

lintOptions {
abortOnError false
disable 'GradleCompatible'
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}

repositories {
mavenLocal()
mavenCentral()
google()
maven {
// All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
url "$rootDir/../node_modules/react-native/android"
}
jcenter()
}

dependencies {
Expand Down
13 changes: 13 additions & 0 deletions android/cpp-adapter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <jni.h>
#include "react-native-get-random-values.h"

extern "C" JNIEXPORT void JNICALL
Java_org_linusu_RNGetRandomValuesModule_nativeInstall(JNIEnv *env, jobject thiz, jlong jsi)
{
auto runtime = reinterpret_cast<facebook::jsi::Runtime *>(jsi);

if (runtime)
{
reactnativegetrandomvalues::install(*runtime);
}
}
30 changes: 14 additions & 16 deletions android/src/main/java/org/linusu/RNGetRandomValuesModule.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
package org.linusu;

import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

import android.util.Base64;

import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.Callback;

public class RNGetRandomValuesModule extends ReactContextBaseJavaModule {

private final ReactApplicationContext reactContext;
private native void nativeInstall(long jsiPtr, String docDir);

public RNGetRandomValuesModule(ReactApplicationContext reactContext) {
super(reactContext);
this.reactContext = reactContext;
}

@Override
Expand All @@ -25,12 +17,18 @@ public String getName() {
}

@ReactMethod(isBlockingSynchronousMethod = true)
public String getRandomBase64(int byteLength) throws NoSuchAlgorithmException {
byte[] data = new byte[byteLength];
SecureRandom random = new SecureRandom();

random.nextBytes(data);

return Base64.encodeToString(data, Base64.NO_WRAP);
public boolean install() {
try {
System.loadLibrary("reactnativegetrandomvalues");

ReactApplicationContext context = getReactApplicationContext();
nativeInstall(
context.getJavaScriptContextHolder().get(),
context.getFilesDir().getAbsolutePath()
);
return true;
} catch (Exception exception) {
return false;
}
}
}
Loading

0 comments on commit db9f4bb

Please sign in to comment.