Skip to content

Commit

Permalink
Fix clippy errors
Browse files Browse the repository at this point in the history
  • Loading branch information
rukai committed Feb 29, 2024
1 parent f64ed20 commit 95047f1
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 121 deletions.
2 changes: 1 addition & 1 deletion rust/src/api/invocation_arg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,7 @@ mod inv_arg_unit_tests {
.unwrap()
.into_primitive()
.is_ok());
assert!(InvocationArg::try_from(1_32)
assert!(InvocationArg::try_from(1_i32)
.unwrap()
.into_primitive()
.is_ok());
Expand Down
182 changes: 85 additions & 97 deletions rust/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ impl Jvm {
format!("Could not create the JVM: {}", error_message).to_string(),
))
} else {
let jvm = Self::try_from(jni_environment)?;
let jvm = unsafe { Self::try_from(jni_environment)? };
if let Some(libname) = lib_name_to_load {
// Pass to the Java world the name of the j4rs library.
debug(&format!(
Expand All @@ -213,105 +213,91 @@ impl Jvm {
}
}

pub fn try_from(jni_environment: *mut JNIEnv) -> errors::Result<Jvm> {
pub unsafe fn try_from(jni_environment: *mut JNIEnv) -> errors::Result<Jvm> {
if cache::get_thread_local_env_opt().is_none() {
// Create and set the environment in Thread Local
unsafe {
let _ = cache::get_jni_get_method_id()
.or_else(|| cache::set_jni_get_method_id((**jni_environment).GetMethodID));
let _ = cache::get_jni_get_static_method_id().or_else(|| {
cache::set_jni_get_static_method_id((**jni_environment).GetStaticMethodID)
});
let _ = cache::get_jni_new_object()
.or_else(|| cache::set_jni_new_object((**jni_environment).NewObject));
let _ = cache::get_jni_new_string_utf()
.or_else(|| cache::set_jni_new_string_utf((**jni_environment).NewStringUTF));
let _ = cache::get_jni_get_string_utf_chars().or_else(|| {
cache::set_jni_get_string_utf_chars((**jni_environment).GetStringUTFChars)
});
let _ = cache::get_jni_release_string_utf_chars().or_else(|| {
cache::set_jni_release_string_utf_chars(
(**jni_environment).ReleaseStringUTFChars,
)
});
let _ = cache::get_jni_call_object_method().or_else(|| {
cache::set_jni_call_object_method((**jni_environment).CallObjectMethod)
});
let _ = cache::get_jni_call_byte_method().or_else(|| {
cache::set_jni_call_byte_method((**jni_environment).CallByteMethod)
});
let _ = cache::get_jni_call_short_method().or_else(|| {
cache::set_jni_call_short_method((**jni_environment).CallShortMethod)
});
let _ = cache::get_jni_call_int_method()
.or_else(|| cache::set_jni_call_int_method((**jni_environment).CallIntMethod));
let _ = cache::get_jni_call_long_method().or_else(|| {
cache::set_jni_call_long_method((**jni_environment).CallLongMethod)
});
let _ = cache::get_jni_call_float_method().or_else(|| {
cache::set_jni_call_float_method((**jni_environment).CallFloatMethod)
});
let _ = cache::get_jni_call_double_method().or_else(|| {
cache::set_jni_call_double_method((**jni_environment).CallDoubleMethod)
});
let _ = cache::get_jni_call_void_method().or_else(|| {
cache::set_jni_call_void_method((**jni_environment).CallVoidMethod)
});
let _ = cache::get_jni_call_static_object_method().or_else(|| {
cache::set_jni_call_static_object_method(
(**jni_environment).CallStaticObjectMethod,
)
});
let _ = cache::get_jni_new_object_array().or_else(|| {
cache::set_jni_new_object_array((**jni_environment).NewObjectArray)
});
let _ = cache::get_jni_set_object_array_element().or_else(|| {
cache::set_jni_set_object_array_element(
(**jni_environment).SetObjectArrayElement,
)
});
let ec = cache::get_jni_exception_check()
.or_else(|| cache::set_jni_exception_check((**jni_environment).ExceptionCheck));
let ed = cache::get_jni_exception_describe().or_else(|| {
cache::set_jni_exception_describe((**jni_environment).ExceptionDescribe)
});
let exclear = cache::get_jni_exception_clear()
.or_else(|| cache::set_jni_exception_clear((**jni_environment).ExceptionClear));
let _ = cache::get_jni_delete_local_ref().or_else(|| {
cache::set_jni_delete_local_ref((**jni_environment).DeleteLocalRef)
});
let _ = cache::get_jni_delete_global_ref().or_else(|| {
cache::set_jni_delete_global_ref((**jni_environment).DeleteGlobalRef)
});
let _ = cache::get_jni_new_global_ref()
.or_else(|| cache::set_jni_new_global_ref((**jni_environment).NewGlobalRef));
let _ = cache::get_jni_throw_new()
.or_else(|| cache::set_jni_throw_new((**jni_environment).ThrowNew));
let _ = cache::get_is_same_object()
.or_else(|| cache::set_is_same_object((**jni_environment).IsSameObject));

match (ec, ed, exclear) {
(Some(ec), Some(ed), Some(exclear)) => {
if (ec)(jni_environment) == JNI_TRUE {
(ed)(jni_environment);
(exclear)(jni_environment);
Err(J4RsError::JavaError("The VM cannot be started... Please check the logs.".to_string()))
} else {
let jvm = Jvm {
jni_env: jni_environment,
detach_thread_on_drop: true,
};

cache::set_thread_local_env(Some(jni_environment));
cache::add_active_jvm();

Ok(jvm)
}
}
(_, _, _) => {
Err(J4RsError::JniError(format!("Could not initialize the JVM: Error while trying to retrieve JNI functions.")))
let _ = cache::get_jni_get_method_id()
.or_else(|| cache::set_jni_get_method_id((**jni_environment).GetMethodID));
let _ = cache::get_jni_get_static_method_id().or_else(|| {
cache::set_jni_get_static_method_id((**jni_environment).GetStaticMethodID)
});
let _ = cache::get_jni_new_object()
.or_else(|| cache::set_jni_new_object((**jni_environment).NewObject));
let _ = cache::get_jni_new_string_utf()
.or_else(|| cache::set_jni_new_string_utf((**jni_environment).NewStringUTF));
let _ = cache::get_jni_get_string_utf_chars().or_else(|| {
cache::set_jni_get_string_utf_chars((**jni_environment).GetStringUTFChars)
});
let _ = cache::get_jni_release_string_utf_chars().or_else(|| {
cache::set_jni_release_string_utf_chars((**jni_environment).ReleaseStringUTFChars)
});
let _ = cache::get_jni_call_object_method().or_else(|| {
cache::set_jni_call_object_method((**jni_environment).CallObjectMethod)
});
let _ = cache::get_jni_call_byte_method()
.or_else(|| cache::set_jni_call_byte_method((**jni_environment).CallByteMethod));
let _ = cache::get_jni_call_short_method()
.or_else(|| cache::set_jni_call_short_method((**jni_environment).CallShortMethod));
let _ = cache::get_jni_call_int_method()
.or_else(|| cache::set_jni_call_int_method((**jni_environment).CallIntMethod));
let _ = cache::get_jni_call_long_method()
.or_else(|| cache::set_jni_call_long_method((**jni_environment).CallLongMethod));
let _ = cache::get_jni_call_float_method()
.or_else(|| cache::set_jni_call_float_method((**jni_environment).CallFloatMethod));
let _ = cache::get_jni_call_double_method().or_else(|| {
cache::set_jni_call_double_method((**jni_environment).CallDoubleMethod)
});
let _ = cache::get_jni_call_void_method()
.or_else(|| cache::set_jni_call_void_method((**jni_environment).CallVoidMethod));
let _ = cache::get_jni_call_static_object_method().or_else(|| {
cache::set_jni_call_static_object_method((**jni_environment).CallStaticObjectMethod)
});
let _ = cache::get_jni_new_object_array()
.or_else(|| cache::set_jni_new_object_array((**jni_environment).NewObjectArray));
let _ = cache::get_jni_set_object_array_element().or_else(|| {
cache::set_jni_set_object_array_element((**jni_environment).SetObjectArrayElement)
});
let ec = cache::get_jni_exception_check()
.or_else(|| cache::set_jni_exception_check((**jni_environment).ExceptionCheck));
let ed = cache::get_jni_exception_describe().or_else(|| {
cache::set_jni_exception_describe((**jni_environment).ExceptionDescribe)
});
let exclear = cache::get_jni_exception_clear()
.or_else(|| cache::set_jni_exception_clear((**jni_environment).ExceptionClear));
let _ = cache::get_jni_delete_local_ref()
.or_else(|| cache::set_jni_delete_local_ref((**jni_environment).DeleteLocalRef));
let _ = cache::get_jni_delete_global_ref()
.or_else(|| cache::set_jni_delete_global_ref((**jni_environment).DeleteGlobalRef));
let _ = cache::get_jni_new_global_ref()
.or_else(|| cache::set_jni_new_global_ref((**jni_environment).NewGlobalRef));
let _ = cache::get_jni_throw_new()
.or_else(|| cache::set_jni_throw_new((**jni_environment).ThrowNew));
let _ = cache::get_is_same_object()
.or_else(|| cache::set_is_same_object((**jni_environment).IsSameObject));

match (ec, ed, exclear) {
(Some(ec), Some(ed), Some(exclear)) => {
if (ec)(jni_environment) == JNI_TRUE {
(ed)(jni_environment);
(exclear)(jni_environment);
Err(J4RsError::JavaError(
"The VM cannot be started... Please check the logs.".to_string(),
))
} else {
let jvm = Jvm {
jni_env: jni_environment,
detach_thread_on_drop: true,
};

cache::set_thread_local_env(Some(jni_environment));
cache::add_active_jvm();

Ok(jvm)
}
}
(_, _, _) => Err(J4RsError::JniError(format!(
"Could not initialize the JVM: Error while trying to retrieve JNI functions."
))),
}
} else {
// Use the environment from the Thread Local
Expand Down Expand Up @@ -1317,7 +1303,9 @@ impl Jvm {

/// Throws an exception in the Java World
pub fn throw_invocation_exception(&self, message: &str) -> errors::Result<()> {
let _ = jni_utils::throw_exception(message, self.jni_env)?;
unsafe {
let _ = jni_utils::throw_exception(message, self.jni_env)?;
}
Ok(())
}

Expand Down
38 changes: 17 additions & 21 deletions rust/src/jni_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,27 +486,23 @@ pub(crate) unsafe fn string_from_jobject(
}
}

pub fn jstring_to_rust_string(jvm: &Jvm, java_string: jstring) -> errors::Result<String> {
unsafe {
let s = (opt_to_res(cache::get_jni_get_string_utf_chars())?)(
jvm.jni_env,
java_string,
ptr::null_mut(),
) as *mut c_char;
let rust_string = utils::to_rust_string(s);
(opt_to_res(cache::get_jni_release_string_utf_chars())?)(jvm.jni_env, java_string, s);
Jvm::do_return(jvm.jni_env, rust_string)
}
pub unsafe fn jstring_to_rust_string(jvm: &Jvm, java_string: jstring) -> errors::Result<String> {
let s = (opt_to_res(cache::get_jni_get_string_utf_chars())?)(
jvm.jni_env,
java_string,
ptr::null_mut(),
) as *mut c_char;
let rust_string = utils::to_rust_string(s);
(opt_to_res(cache::get_jni_release_string_utf_chars())?)(jvm.jni_env, java_string, s);
Jvm::do_return(jvm.jni_env, rust_string)
}

pub(crate) fn throw_exception(message: &str, jni_env: *mut JNIEnv) -> errors::Result<i32> {
unsafe {
let message_jstring = utils::to_c_string_struct(message);
let i = (opt_to_res(cache::get_jni_throw_new())?)(
jni_env,
cache::get_invocation_exception_class()?,
message_jstring.as_ptr(),
);
Ok(i)
}
pub(crate) unsafe fn throw_exception(message: &str, jni_env: *mut JNIEnv) -> errors::Result<i32> {
let message_jstring = utils::to_c_string_struct(message);
let i = (opt_to_res(cache::get_jni_throw_new())?)(
jni_env,
cache::get_invocation_exception_class()?,
message_jstring.as_ptr(),
);
Ok(i)
}
2 changes: 1 addition & 1 deletion rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ pub extern "C" fn Java_org_astonbitecode_j4rs_api_invocation_NativeCallbackToRus
}

#[no_mangle]
pub extern "C" fn Java_org_astonbitecode_j4rs_api_invocation_NativeCallbackToRustFutureSupport_failcallbacktochannel(
pub unsafe extern "C" fn Java_org_astonbitecode_j4rs_api_invocation_NativeCallbackToRustFutureSupport_failcallbacktochannel(
_jni_env: *mut JNIEnv,
_class: *const c_void,
ptr_address: jlong,
Expand Down
2 changes: 1 addition & 1 deletion rust/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ mod utils_unit_tests {
assert!(
primitive_of(&InvocationArg::try_from(1_i16).unwrap()) == Some("short".to_string())
);
assert!(primitive_of(&InvocationArg::try_from(1_32).unwrap()) == Some("int".to_string()));
assert!(primitive_of(&InvocationArg::try_from(1_i32).unwrap()) == Some("int".to_string()));
assert!(primitive_of(&InvocationArg::try_from(1_i64).unwrap()) == Some("long".to_string()));
assert!(
primitive_of(&InvocationArg::try_from(0.1_f32).unwrap()) == Some("float".to_string())
Expand Down

0 comments on commit 95047f1

Please sign in to comment.