2024-09-21 17:55:13 +02:00
|
|
|
# jni-toolbox
|
2024-09-22 02:12:04 +02:00
|
|
|
[![Actions Status](https://github.com/hexedtech/jni-toolbox/actions/workflows/test.yml/badge.svg)](https://github.com/hexedtech/jni-toolbox/actions)
|
|
|
|
[![Crates.io Version](https://img.shields.io/crates/v/jni-toolbox)](https://crates.io/crates/jni-toolbox)
|
|
|
|
[![docs.rs](https://img.shields.io/docsrs/jni-toolbox)](https://docs.rs/jni-toolbox)
|
|
|
|
|
2024-09-23 00:50:52 +02:00
|
|
|
This is a simple crate built around [jni-rs](https://github.com/jni-rs/jni-rs) to automatically generate JNI-compatible extern functions.
|
2024-09-22 02:12:04 +02:00
|
|
|
|
2024-09-23 00:50:52 +02:00
|
|
|
It also wraps functions returning `Result<>`, making short-circuiting easy.
|
2024-09-21 17:09:06 +02:00
|
|
|
|
2024-09-23 00:50:52 +02:00
|
|
|
## Usage
|
|
|
|
Just specify package and class on your function, and done!
|
2024-09-21 17:09:06 +02:00
|
|
|
|
|
|
|
```rust
|
2024-09-21 17:55:13 +02:00
|
|
|
#[jni_toolbox::jni(package = "your.package.path", class = "ContainerClass")]
|
2024-09-22 01:54:26 +02:00
|
|
|
fn your_function_name(arg: String) -> Result<Vec<String>, String> {
|
2024-09-22 01:57:39 +02:00
|
|
|
Ok(arg.split('/').map(|x| x.to_string()).collect())
|
2024-09-21 17:09:06 +02:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2024-09-23 00:50:52 +02:00
|
|
|
### Conversions
|
|
|
|
Every type that is meant to be sent to Java must implement `IntoJavaObject` (or, unlikely, `IntoJavaPrimitive`); every type that is meant to be
|
|
|
|
received from Java must implement `FromJava`. Most primitives and a few common types should already be implemented.
|
2024-09-22 01:54:26 +02:00
|
|
|
|
2024-09-22 02:03:21 +02:00
|
|
|
```rust
|
2024-09-23 00:50:52 +02:00
|
|
|
impl<'j> IntoJavaObject for MyClass {
|
|
|
|
type T = jni::objects::JObject<'j>
|
2024-09-22 02:03:21 +02:00
|
|
|
fn into_java(self, env: &mut jni::JNIEnv<'j>) -> Result<Self::T, jni::errors::Error> {
|
|
|
|
let hello = env.new_string("world")?;
|
|
|
|
// TODO!!
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
2024-09-22 01:54:26 +02:00
|
|
|
|
2024-09-23 00:50:52 +02:00
|
|
|
### Pointers
|
|
|
|
Note that, while it is possible to pass raw pointers to the JVM, it is not safe by default and must be done with extreme care.
|
2024-09-22 02:03:21 +02:00
|
|
|
|
2024-09-23 00:50:52 +02:00
|
|
|
### Exceptions
|
2024-09-22 01:54:26 +02:00
|
|
|
Errors are thrown automatically when a `Result` is an error. For your errors to work, you must implement the `JniToolboxError` trait for your errors,
|
|
|
|
(which just returns the path to your Java error class) and then make a Java error wrapper which can be constructed with a single string argument.
|
2024-09-23 00:50:52 +02:00
|
|
|
|
|
|
|
Functions returning `Result`s will automatically have their return value unwrapped and, if is an err, throw an exception and return early.
|
2024-09-22 01:54:26 +02:00
|
|
|
|
2024-09-22 02:03:21 +02:00
|
|
|
```rust
|
|
|
|
impl JniToolboxError for MyError {
|
|
|
|
fn jclass(&self) -> String {
|
|
|
|
"my/package/some/MyError".to_string()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
```java
|
|
|
|
package my.package.some;
|
2024-09-23 00:50:52 +02:00
|
|
|
public class MyError extends Throwable {
|
2024-09-22 02:03:21 +02:00
|
|
|
public MyError(String x) {
|
|
|
|
// TODO
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2024-09-23 00:50:52 +02:00
|
|
|
To throw simple exceptions, it's possible to use the `exception` attribute. Pass the exception's fully qualified name (must have a constructor
|
|
|
|
that takes in a single `String` argument).
|
2024-09-21 17:09:06 +02:00
|
|
|
|
2024-09-23 00:50:52 +02:00
|
|
|
### Examples
|
|
|
|
The following function:
|
2024-09-21 17:09:06 +02:00
|
|
|
```rust
|
2024-09-22 01:54:26 +02:00
|
|
|
#[jni(package = "mp.code", class = "Client", ptr)]
|
|
|
|
fn connect(config: Config) -> Result<Client, ConnectionError> {
|
2024-09-23 18:02:27 +02:00
|
|
|
super::tokio().block_on(Client::connect(config))
|
2024-09-21 17:09:06 +02:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2024-09-23 00:50:52 +02:00
|
|
|
generates a matching expanded function invoking it:
|
2024-09-22 01:54:26 +02:00
|
|
|
|
2024-09-21 17:09:06 +02:00
|
|
|
```rust
|
2024-09-23 18:02:27 +02:00
|
|
|
fn connect(config: Config) -> Result<Client, ConnectionError> {
|
|
|
|
super::tokio().block_on(Client::connect(config))
|
|
|
|
}
|
2024-09-24 02:51:59 +02:00
|
|
|
|
2024-09-21 17:09:06 +02:00
|
|
|
#[no_mangle]
|
2024-09-23 18:02:27 +02:00
|
|
|
#[allow(unused_unit)]
|
2024-09-22 01:54:26 +02:00
|
|
|
pub extern "system" fn Java_mp_code_Client_connect<'local>(
|
2024-09-22 01:57:39 +02:00
|
|
|
mut env: jni::JNIEnv<'local>,
|
|
|
|
_class: jni::objects::JClass<'local>,
|
2024-09-23 18:02:27 +02:00
|
|
|
config: <Config as jni_toolbox::FromJava<'local>>::From,
|
|
|
|
) -> <Client as jni_toolbox::IntoJava<'local>>::Ret {
|
2024-09-22 01:57:39 +02:00
|
|
|
use jni_toolbox::{FromJava, IntoJava, JniToolboxError};
|
|
|
|
let config_new = match jni_toolbox::from_java_static::<Config>(&mut env, config) {
|
|
|
|
Ok(x) => x,
|
|
|
|
Err(e) => {
|
2024-09-24 05:03:42 +02:00
|
|
|
let _ = env.throw_new(e.jclass(), format!("{e:?}"));
|
2024-09-22 01:57:39 +02:00
|
|
|
return std::ptr::null_mut();
|
|
|
|
}
|
|
|
|
};
|
2024-09-23 18:02:27 +02:00
|
|
|
let result = connect(config_new);
|
|
|
|
let ret = match result {
|
|
|
|
Ok(x) => x,
|
2024-09-24 04:44:09 +02:00
|
|
|
Err(e) => match env.find_class(e.jclass()) {
|
2024-09-24 02:51:59 +02:00
|
|
|
Err(e) => panic!("error throwing Java exception -- failed resolving error class: {e}"),
|
2024-09-24 04:44:09 +02:00
|
|
|
Ok(class) => match env.new_string(format!("{e:?}")) {
|
2024-09-24 02:51:59 +02:00
|
|
|
Err(e) => panic!("error throwing Java exception -- failed creating error string: {e}"),
|
2024-09-24 04:44:09 +02:00
|
|
|
Ok(msg) => match env.new_object(class, "(Ljava/lang/String;)V", &[jni::objects::JValueGen::Object(&msg)]) {
|
2024-09-24 02:51:59 +02:00
|
|
|
Err(e) => panic!("error throwing Java exception -- failed creating object: {e}"));
|
2024-09-24 04:44:09 +02:00
|
|
|
Ok(obj) => match env.throw(jni::objects::JThrowable::from(obj)) {
|
2024-09-24 02:51:59 +02:00
|
|
|
Err(e) => panic!("error throwing Java exception -- failed throwing: {e}"),
|
2024-09-22 01:57:39 +02:00
|
|
|
Ok(_) => return std::ptr::null_mut(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2024-09-23 18:02:27 +02:00
|
|
|
};
|
2024-09-24 04:44:09 +02:00
|
|
|
match ret.into_java(&mut env) {
|
2024-09-23 18:02:27 +02:00
|
|
|
Ok(fin) => fin,
|
|
|
|
Err(e) => {
|
2024-09-24 05:03:42 +02:00
|
|
|
let _ = env.throw_new(e.jclass(), format!("{e:?}"));
|
2024-09-23 18:02:27 +02:00
|
|
|
std::ptr::null_mut()
|
|
|
|
}
|
2024-09-22 01:57:39 +02:00
|
|
|
}
|
2024-09-21 17:09:06 +02:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2024-09-23 00:50:52 +02:00
|
|
|
## Status
|
|
|
|
This crate is early and intended mostly to maintain [`codemp`](https://github.com/hexedtech/codemp)'s Java bindings, so things not used
|
|
|
|
there may be missing or slightly broken. However, the crate is also quite small and only runs at compile time, so trying it out in your
|
|
|
|
own project should not be a problem.
|