Unable to understand Java FFI example

Moderators: LCMark, LCfraser

Post Reply
Newbie4
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 255
Joined: Sun Apr 15, 2012 1:17 am
Location: USA
Contact:

Unable to understand Java FFI example

Post by Newbie4 » Tue Apr 11, 2017 8:58 pm

I am trying to understand how to use the Java FF interface in LiveCode. The example of the battery percentage is confusing to me probably because I do not understand some of the steps and functions interfacing with the battery. Is there a simpler example that I can follow?

I know Java well but am not sure how to interface it with LiveCode.

Suppose I have a simple Java class (program) that takes 2 integers as parameters and returns their sum. How would I go about making use of this class in LiveCode? Can I call it from LiveCode, pass it the 2 numbers and get the sum? I know this is a very simple case but is it possible to do? Or is the interface not at a point where it can easily be used Like this?

I assume that I go into liveCode Builder and generate a LCB wrapper. Is there a list of steps to perform? Or any other documentation ( or examples to follow?)

Thanks

livecodeali
Livecode Staff Member
Livecode Staff Member
Posts: 128
Joined: Thu Apr 18, 2013 2:48 pm

Re: Unable to understand Java FFI example

Post by livecodeali » Tue Apr 11, 2017 9:37 pm

It is not currently possible to compile your own class and call into it from LiveCode Builder - at the moment you can only use the java standard libraries.

The battery percentage example is complicated by the hoops it has to jump through to get the Intent - probably better to look at the BigInteger example for an idea of how to access things in the Java standard library.

Once we've made it possible to use your own compiled class, in theory it will be as simple as the following:

Suppose your class is

Code: Select all

public class MyClass {
   public static int addIntegers(int left, int right) {
      return Math.addExact(left, right);
   }
}
Then you would create a foreign binding to the handler in LCB using a foreign handler declaration

Code: Select all

foreign handler MyClassAddIntegers(in pLeft as CInt, in pRight as CInt) returns CInt binds to "java:path.to.MyClass>addIntegers(II)I!static"
CInt being an LCB type that bridges to the java int primitive type, (II)I being the type signature of the int addIntegers(int, int) method. (http://journals.ecs.soton.ac.uk/java/tu ... ethod.html)

You can't use the Java FFI directly from LiveCode, you need to have a LCB wrapper library with public handlers which call the foreign handlers that are bound to java calls, load the library in LiveCode and then use it like that. Are you familiar with creating and loading LiveCode Builder libraries already? If not that's probably the place to start. Check out the Extending LiveCode guide https://github.com/livecode/livecode-id ... iveCode.md (or in the guides tab in the dictionary) for a step by step guide to creating an extension.

In terms of docs for the java ffi itself, the best place to look is probably the LCB language reference https://github.com/livecode/livecode/bl ... n-handlers, but I think the blog you've already read is the most straightforward reference.

If there's any specific part you do not understand, feel free to ask about it here too - and hopefully it will help us improve our documentation.

Post Reply

Return to “LiveCode Builder”