Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apparent error in string conversion #10

Open
Jazzpirate opened this issue Jan 15, 2022 · 10 comments
Open

Apparent error in string conversion #10

Jazzpirate opened this issue Jan 15, 2022 · 10 comments
Labels
bug Something isn't working

Comments

@Jazzpirate
Copy link

I have a struct with

impl<'env,'borrow> MyStruct<'env,'borrow> {
  pub extern "java" fn message(&self,_env:&'borrow JNIEnv<'env>,_s:String) -> JniResult<bool> {}
}

...but when calling this function, I get an Error:

Error(JavaException, State { next:error:None, bracktrace: InternalBacktrace { backtrace: Some(
  0: error_chain::backtrace::imp::InternalBacktrace::new
  1: <error_chain::State as core::default::Default>::default
  2: <T as core::convert::Into<U>>::into
  3: jni::wrapper::jnienv::JNIEnv::new_string
  4: <alloc::string::String as robusta_jni::convert::safe::TryIntoJavaValue>::try_into
  5: ...::message
  ...
}}

...which looks to me like a bug in the string conversion to java. It's difficult to find the precise cause, since I can't set break points in rust when running the program from java (or, actually, Scala, but that shouldn't make a difference).

Any help would be very much appreciated :)

@Jazzpirate
Copy link
Author

(also: I noticed that java booleans seem to be the opposite from the ones in rust: Passing the java value true to a rust function prints false and vice versa)

@Jazzpirate
Copy link
Author

Jazzpirate commented Jan 15, 2022

Also, I'm getting a similar error when calling a java function that takes an Integer array as input. When converting the corresponding Vec<i32>, it throws:
panicked at 'called Result::unwrap() on an Err value: "unexpected parseInput: java/lang/Integer"' in <i32 as robusta_jni::convert::JavaValue>::autobox...

@giovanniberti
Copy link
Owner

Unfortunately I can't check the code right now, I'll get back to you later this week. From the stack trace it seems that it's the string conversion that fails from Rust as you suggested.

(also: I noticed that java booleans seem to be the opposite from the ones in rust: Passing the java value true to a rust function prints false and vice versa)

🤦 Right here. To get to 1.0 I'm going to need an extensive test suite, that's for sure.

Could you provide a minimal example? Or try switching to a different call_type?

@Jazzpirate
Copy link
Author

Hi, I'm really sorry I can't be more informative. The string bug I tried to narrow down to something simpler, but in all my toy examples it worked - only in the actual setting where I needed it to work did it not, and I have no idea what was different (the strings were definitely equal on an as_bytes()-level). So all I can say is that it's not a specific string that caused the problem, and in the setting where it happened, it seemed to happen with every string.
safe/unsafe call types I also tried, didn't make a difference.
I switched to the vanilla jni implementation for now, because the parts where I need jni are rather small and self contained anyway,

@giovanniberti giovanniberti added the bug Something isn't working label Feb 14, 2022
@uvlad7
Copy link
Contributor

uvlad7 commented Jan 16, 2024

When converting the corresponding Vec, it throws

This is definitely related to signature parsing in jni-rs

@uvlad7
Copy link
Contributor

uvlad7 commented Jan 16, 2024

@Jazzpirate I know it's been a while since you posted this, but can you provide a full traceback of the failed string conversion? Or a link to the whole project, if it's possible.

@uvlad7
Copy link
Contributor

uvlad7 commented Jan 16, 2024

BTW, can't a pending exception be the case?
At the point the issue was created, robusta was v0.2, and it used jni-rs v0.17.0.

@Jazzpirate
Copy link
Author

Hi, I appreciate you looking into this, but I unfortunately can't be of much help (anymore). I never pushed the code anywhere since I couldn't get it to work, and I switched to jni-rs directly soon after...

@uvlad7
Copy link
Contributor

uvlad7 commented Jan 16, 2024

But may be you can remember if it's possible that you had a java exception thrown before you call MyStruct::message

@Jazzpirate
Copy link
Author

Hmm, I'm fairly certain I didn't - the idea is that I wrote a TeX-to-HTML converter in rust, which I want to use from java and use functions such as the one sketched above to pass output messages back. So all the java code did was basically something like compile("foo.tex",mystruct), the rust converter would get going, and occasionally call mystruct.message("(/.../article.sty") or whatever. So there was little to throw errors on the java side in the first place, and little on the rust side to even get to the point where message would be called if anything failed before, if that makes sense...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants