fix(java): errors in send buffer glue, safer error handling

This commit is contained in:
zaaarf 2024-09-16 02:59:43 +02:00
parent 795423de2a
commit 5cf6c4d4eb
No known key found for this signature in database
GPG key ID: C91CFF9E2262BBA1
3 changed files with 21 additions and 9 deletions

View file

@ -147,8 +147,8 @@ pub extern "system" fn Java_mp_code_BufferController_send<'local>(
self_ptr: jlong,
input: JObject<'local>,
) {
let Ok(start) = env.get_field(&input, "start", "I")
.and_then(|sr| sr.i())
let Ok(start) = env.get_field(&input, "start", "J")
.and_then(|sr| sr.j())
.jexcept(&mut env)
.try_into()
else {
@ -157,8 +157,8 @@ pub extern "system" fn Java_mp_code_BufferController_send<'local>(
return;
};
let Ok(end) = env.get_field(&input, "end", "I")
.and_then(|er| er.i())
let Ok(end) = env.get_field(&input, "end", "J")
.and_then(|er| er.j())
.jexcept(&mut env)
.try_into()
else {
@ -175,7 +175,7 @@ pub extern "system" fn Java_mp_code_BufferController_send<'local>(
.map(|b| b.into())
.jexcept(&mut env);
let hash = env.get_field(&input, "hash", "Ljava/util/OptionalLong")
let hash = env.get_field(&input, "hash", "Ljava/util/OptionalLong;")
.and_then(|hash| hash.l())
.and_then(|hash| {
if env.call_method(&hash, "isPresent", "()Z", &[]).and_then(|r| r.z()).jexcept(&mut env) {

View file

@ -110,8 +110,12 @@ impl<T> JExceptable<T> for Result<T, jni::errors::Error> where T: Default {
fn jexcept(self, env: &mut jni::JNIEnv) -> T {
if let Err(err) = &self {
let msg = format!("{err}");
env.throw_new("mp/code/exceptions/JNIException", msg)
.expect("A severe error occurred: we were unable to create a JNIException. This is an unrecoverable state.");
if let Err(err) = env.throw_new("mp/code/exceptions/JNIException", msg) {
if let Err(err) = env.exception_describe() {
tracing::error!("An exception occurred and we failed to even describe it: {err:#?}.");
}
panic!("A severe error occurred: we were unable to create a JNIException from {err:#?}. This is an unrecoverable state.");
}
}
self.unwrap_or_default()
}
@ -121,8 +125,12 @@ impl<T> JExceptable<T> for Result<T, uuid::Error> where T: Default {
fn jexcept(self, env: &mut jni::JNIEnv) -> T {
if let Err(err) = &self {
let msg = format!("{err}");
env.throw_new("java/lang/IllegalArgumentException", msg)
.expect("A severe error occurred: we were unable to create a JNIException. This is an unrecoverable state.");
if let Err(err) = env.throw_new("java/lang/IllegalArgumentException", msg) {
if let Err(err) = env.exception_describe() {
tracing::error!("An exception occurred and we failed to even describe it: {err:#?}.");
}
panic!("A severe error occurred: we were unable to create a JNIException from {err:#?}. This is an unrecoverable state.");
}
}
self.unwrap_or_default()
}

View file

@ -33,6 +33,10 @@ pub extern "system" fn Java_mp_code_Workspace_get_1buffer<'local>(
self_ptr: jlong,
input: JString<'local>
) -> jobject {
if input.is_null() {
return std::ptr::null_mut();
}
let workspace = unsafe { Box::leak(Box::from_raw(self_ptr as *mut Workspace)) };
let path = unsafe { env.get_string_unchecked(&input) }
.map(|path| path.to_string_lossy().to_string())