-
(This was originally posted to StackOverflow, but since this type of error seems to be specific to this library, I'm reposting it here to see if somebody can help) I am trying to make a class that implements BiConsumer and calls a function pointer upon being called. I have the following parts to this: Rust code that instantiates the class that implements BiConsumer: // 'mc is defined by the struct that holds this function
pub fn from_rust_fn(
env: &JNIEnv<'mc>,
f: &'static dyn Fn(jni::objects::JObject, jni::objects::JObject),
) -> Result<Self, Box<dyn Error>> {
// Create the closure.
let closure: &'static _ =
Box::leak(Box::new(move |a: *mut _jobject, b: *mut _jobject| unsafe {
f(
jni::objects::JObject::from_raw(a),
jni::objects::JObject::from_raw(b),
);
}));
let callback = Closure2::new(closure);
let &code = callback.code_ptr();
let ptr: unsafe extern "C" fn(*const jobject) = unsafe { std::mem::transmute(code) };
std::mem::forget(callback);
// Create the java object.
let obj = env.new_object(
"net/ioixd/blackbox/BiConsumerLink",
"(J)V",
&[(ptr as i64).into()],
);
let obj = env.translate_error_no_gen(obj)?;
Self::from_raw(env, obj)
} The Java code for the class that implements BiConsumer: import java.util.function.BiConsumer;
public class BiConsumerLink implements BiConsumer {
long address;
BiConsumerLink(long address) {
this.address = address;
}
public native void acceptNative(Object t, Object u);
public void accept(Object t, Object u) {
System.out.println((Object) t + "," + (Object) u);
acceptNative((Object) t, (Object) u);
}
} And the other piece of native Rust code which is linked to this class: type BiConsumer = unsafe extern "system" fn(t: JObject, u: JObject);
pub extern "system" fn Java_net_ioixd_blackbox_BiConsumerLink_acceptNative<'a>(
mut env: JNIEnv<'a>,
this: JObject,
obj1: JObject,
obj2: JObject,
) {
|| -> Result<(), Box<dyn Error>> {
let address = env.get_field(this, "address", "long")?.i()? as u64;
println!("{:#x}", address);
unsafe {
let func: BiConsumer = std::mem::transmute(address);
func(obj1, obj2);
}
Ok(())
}()
.unwrap();
} When the BiConsumer's "native" method is called from JNI, it throws an error that just contains the function signature, without attemping to execute the Rust code:
Usually when jni-rs throws this error it indicates that I've gotten the function signature wrong. But in this case, I've triple checked it, and this is what I should be getting. I'm left with the assumption that this is a result of me trying to do it from an Overriden method. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
I forgot |
Beta Was this translation helpful? Give feedback.
I forgot
[#no_mangle]