Skip to content

Accessing C from Java (WIP)

Calum Freeman edited this page Sep 12, 2018 · 1 revision

This is just barely sketched out and not really documentation. I'm also not 100% sure of it all.

use long peerHandle as a replacement for self? basically pass a long which is the pointer to the PyObject which represents self. So first arg to native function is long peerHandle. Last arg to native function is long threadstate. An example of this is PyCPeer. Threadstate is obtained from JyTState method?

After adding the method signature public native <returnType> methodName(long self, <other args>, long threadState); to java(may need to be JyNI.java) and running make, you need to get headers which are autogenerated withjavah (make suer Jython.jar is on the classpath). Something like: javah -cp /Path/To/Jython.jar JyNI.JyNI ran from Path/To/JyNI/JyNI-Java/bin. This will change in future versions of java.

The header file generated should be moved to the include folder, override the one that is there?

Then open the header file ad find the signature and javadoc for your method, copy them. go to JyNI-Loader/JyNILoader.c and bottom post your signature to the list of signatures (adding variable names, so ...methodName(PyObject *, ... becomes ...methodName(PyObject *self, ...). There is also a seperate JyNILoader.c for windows in a PC folder, you should also bottom post your method signature there. There may also be somewhere else in each of the two JyNILoader.c that it needs to go.

You must put the method signature in JyNI.h. You can implement the function anywhere you like, you may need to forward it from JyNI.c. ie: JyNI.c can just contain a function that forwards the call static int function(PyObject *args){ return (*actualFunction)(args); } then the actual function is implemented elsewhere. I'm not sure about the details on this.

In C code you'll need to use RE_ENTER_JyNI and RE_LEAVE_JyNI, this deals with the GIL and uses threadstate. Note ENTER_JyNI and LEAVE_JyNI are deprecated.

Clone this wiki locally