Skip to content

Commit

Permalink
Switch Rust module to use UniFFI (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
dguenther authored Mar 27, 2024
1 parent 62d96c4 commit ebe3562
Show file tree
Hide file tree
Showing 14 changed files with 659 additions and 126 deletions.
627 changes: 561 additions & 66 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ resolver = "2"

members = [
"packages/ironfish-native-module/rust_lib",
"packages/ironfish-native-module/uniffi-bindgen"
]
33 changes: 29 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/ironfish-native-module/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ android.iml
android/app/libs
android/keystores/debug.keystore
android/src/main/jniLibs
android/src/main/java/uniffi

# Cocoapods
#
Expand Down
1 change: 1 addition & 0 deletions packages/ironfish-native-module/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,5 @@ repositories {
dependencies {
implementation project(':expo-modules-core')
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:${getKotlinVersion()}"
implementation "net.java.dev.jna:jna:5.14.0@aar"
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,6 @@ import expo.modules.kotlin.modules.Module
import expo.modules.kotlin.modules.ModuleDefinition

class IronfishNativeModule : Module() {
companion object {
// Load the native library
init {
System.loadLibrary("rust_lib")
}
}

external fun rustAdd(a: Int, b: Int): Int

// Each module class must implement the definition function. The definition consists of components
// that describes the module's functionality and behavior.
// See https://docs.expo.dev/modules/module-api for more details about available components.
Expand All @@ -36,7 +27,7 @@ class IronfishNativeModule : Module() {
}

AsyncFunction("rustAdd") { a: Int, b: Int ->
rustAdd(a, b)
uniffi.rust_lib.rustAdd(a, b)
}

// Defines a JavaScript function that always returns a Promise and whose native code
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class IronfishNativeModule: Module {
}

AsyncFunction("rustAdd") { (a: Int32, b: Int32) -> Int32 in
return rust_add(a, b)
return rustAdd(left: a, right: b)
}

// Defines a JavaScript function that always returns a Promise and whose native code
Expand Down
2 changes: 1 addition & 1 deletion packages/ironfish-native-module/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"expo-module": "expo-module",
"cargo-ios": "tsx scripts/cargo-ios.ts",
"cargo-android": "tsx scripts/cargo-android.ts",
"build-rust": "npm run cargo-android && npm run cargo-ios -- --target=ios-sim && pod install --project-directory=ios"
"build-rust": "npm run cargo-android && npm run cargo-ios -- --target=ios-sim"
},
"keywords": [
"react-native",
Expand Down
2 changes: 1 addition & 1 deletion packages/ironfish-native-module/rust_lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
jni = "0.21.1"
uniffi = "0.26.1"

[lib]
crate-type = ["staticlib", "cdylib"]
25 changes: 4 additions & 21 deletions packages/ironfish-native-module/rust_lib/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,6 @@
#[no_mangle]
pub extern "C" fn rust_add(left: i32, right: i32) -> i32 {
left + right
}
uniffi::setup_scaffolding!();

/// cbindgen:ignore
#[cfg(target_os = "android")]
pub mod android {
use crate::rust_add;
use jni::JNIEnv;
use jni::objects::JClass;
use jni::sys::jint;

#[no_mangle]
pub unsafe extern "C" fn Java_expo_modules_ironfishnativemodule_IronfishNativeModule_rustAdd(
_env: JNIEnv,
_class: JClass,
a: jint,
b: jint
) -> jint {
rust_add(a, b)
}
#[uniffi::export]
fn rust_add(left: i32, right: i32) -> i32 {
left + right
}
28 changes: 28 additions & 0 deletions packages/ironfish-native-module/scripts/cargo-android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,34 @@ function main() {

fs.copyFileSync(sourcePath, path.join(architecturePath, androidLibName));
});

const bindingPath = path.join(
dirUtils.nativeModuleProjectDir,
"android",
"src",
"main",
"java",
);

console.log("Generating bindings for android");
spawnSync(
"cargo",
[
"run",
"-p",
"uniffi-bindgen",
"generate",
"--library",
sourcePath,
"--language",
"kotlin",
"--out-dir",
bindingPath,
],
{
stdio: "inherit",
},
);
}

main();
Loading

0 comments on commit ebe3562

Please sign in to comment.