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

Problem: JNI binding generator can't handle function pointers as method parameters #776

Open
bluca opened this issue Jan 2, 2017 · 12 comments

Comments

@bluca
Copy link
Member

bluca commented Jan 2, 2017

https://github.com/zeromq/zproject/blob/master/zproject_java_lib.gsl#L156

        if resolve_container (argument) = 1
            my.method.okay = 0
            #echo "Skipping $(class.name).$(my.method.name) - can't deal with argument type $(argument.type)"

So the class is skipped given the constructor can't be generated.

This is a problem in CZMQ since it means that zactor bindings can't be generated. We'd need some help here from JNI experts! How can we fix this?

zeromq/czmq#1443

@jimrthy
Copy link

jimrthy commented May 21, 2017

It looks to me as though this is also a problem with ZLoop.

@jimrthy
Copy link

jimrthy commented Jun 4, 2017

I'm not an expert in any of the tech involved here.

I think one valid approach would be:

  • the caller needs to supply an instance that fulfills some sort of ICallback interface (much like you hand anonymous event handler instances to Swing)
  • we set up a C-level callback wrapper to hand to the 0mq API
  • that callback's "args" parameter is a struct that contains
    • JNI environment pointer
    • method ID
    • "real" arguments
  • that callback wrapper function does the "real" callback

I took a first pass at this approach (and started pointing out the places that look obviously broken) here:
jimrthy#1

I probably won't get another chance to look at this until next weekend, at the earliest.

Maybe someone who's more familiar with GSL can help push it further along?

@sappo
Copy link
Member

sappo commented Jun 4, 2017

Thanks for investigating I'll give it at try next week

@jimrthy
Copy link

jimrthy commented Jun 4, 2017

Thank you!

@sappo
Copy link
Member

sappo commented Jun 23, 2017

@jimrthy I havn't forgotten this issue but I'm a bit short on time for time being.

@jimrthy
Copy link

jimrthy commented Jul 8, 2017

I haven't forgotten either. Just also distracted by other things.

@diorcety
Copy link

diorcety commented Feb 12, 2018

Could you take a look on https://github.com/diorcety/zproject/tree/x_java2 ?

@diorcety
Copy link

python use ffi in order to handle the callbacks. I used jffi which is an Java implementation which allow to use java callback quickly. This is almost the same thing that python stuff

@sappo
Copy link
Member

sappo commented Feb 12, 2018 via email

@hgourvest
Copy link
Contributor

I managed to write a pure java class for zactor, and doing this I also discovered zsys::create_pipe was not working for the same reason, it pass a pointer to a handle.

@stephan57160
Copy link
Contributor

Found this link, but unable to see if really useful or not:
https://stackoverflow.com/questions/819536/how-to-call-java-functions-from-c

(my 2 cents).

@benjdero
Copy link
Member

This problem really questions the usage of JNI for this project. Is it only here for historical reasons? Can more modern solutions allow us to build sturdier bindings for Java?

As of today, there are 4 ways to call C code from Java code:

  • JNI: the first and oldest, very basic but still widely used
  • JNA: built on top of JNI, easy support of func ptrs parameters, even more widely used
  • JNR: not sure how many people use it, it seems supersed by Project Panama
  • Project Panama: seems like the modern way to call C code from Java, the embedded jextract tool generate bindings automagically. When I tried it like 2 years ago it was a major pain to use but I should give it another try.

More info: https://developer.okta.com/blog/2022/04/08/state-of-ffi-java#java-native-interface-jni

For now, I'm toying around with JNA. Function pointers as parameters are easy to use. What else should I try? What functions of zmq have limited usage in Java atm?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants