Unable to understand Java FFI example
-
- VIP Livecode Opensource Backer
- Posts: 327
- Joined: Sun Apr 15, 2012 1:17 am
- Location: USA
- Contact:
Unable to understand Java FFI example
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
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
Cyril Pruszko
https://sites.google.com/a/pgcps.org/livecode/
https://sites.google.com/a/setonhs.org/app-and-game-workshop/home
https://learntolivecode.com/
https://sites.google.com/a/pgcps.org/livecode/
https://sites.google.com/a/setonhs.org/app-and-game-workshop/home
https://learntolivecode.com/
-
- Livecode Staff Member
- Posts: 192
- Joined: Thu Apr 18, 2013 2:48 pm
Re: Unable to understand Java FFI example
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
Then you would create a foreign binding to the handler in LCB using a foreign handler declaration
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.
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);
}
}
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"
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.
-
- VIP Livecode Opensource Backer
- Posts: 3581
- Joined: Mon Jan 22, 2007 7:36 am
- Location: Berkeley, CA, US
- Contact:
Re: Unable to understand Java FFI example
The documentation at https://github.com/livecode/livecode/bl ... n-handlers states
Is it possible to have an aggregate that contains an aggregate or is it necessary to enumerate each element of the included aggregates? I fear I'm about to embark on a lot of needless work.
Is it possible to specify a 1024 character buffer in an aggregate declaration without typing 1024 'b' chars in the declaration string?
Can you give an example of importing and exporting an aggregate to and from a List? It's not clear to me how to access the elements of a C struct this way other than through a numerical offset.
and then statesNote: In a subsequent update you will be able to define record types (named collections of values - like structs in C) and handler types (allowing dynamic handler calls through a variable - like function pointers in C).
Assuming that the first statement is just outdated:C-style aggregates (e.g. structs) can now be accessed from LCB via the new aggregate parameterized type.
Is it possible to have an aggregate that contains an aggregate or is it necessary to enumerate each element of the included aggregates? I fear I'm about to embark on a lot of needless work.
Is it possible to specify a 1024 character buffer in an aggregate declaration without typing 1024 'b' chars in the declaration string?
Can you give an example of importing and exporting an aggregate to and from a List? It's not clear to me how to access the elements of a C struct this way other than through a numerical offset.
PowerDebug http://powerdebug.ahsoftware.net
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
Re: Unable to understand Java FFI example
Check out the libsodium thread on the list. Not sure if Monte’s answers are helpful for your situation. I was able to create a 32 byte buffer to retrieve info from the library. I’ll have to respond with more info from a computer later.
Brian Milby
Script Tracker https://github.com/bwmilby/scriptTracker
Script Tracker https://github.com/bwmilby/scriptTracker
-
- VIP Livecode Opensource Backer
- Posts: 3581
- Joined: Mon Jan 22, 2007 7:36 am
- Location: Berkeley, CA, US
- Contact:
Re: Unable to understand Java FFI example
Brian-
Yeah, I've seen the libsodium thread there, and I'm glad you're making progress with it.
Looks like you had a similar problem with the 'const unsigned char seed[32]' issue. Did you figure out way not to have to use 32 "c" characters? I'm stuck with needing to access elements of a struct with a 1024-byte buffer (not a pointer to same) as an element.
Yeah, I've seen the libsodium thread there, and I'm glad you're making progress with it.
Looks like you had a similar problem with the 'const unsigned char seed[32]' issue. Did you figure out way not to have to use 32 "c" characters? I'm stuck with needing to access elements of a struct with a 1024-byte buffer (not a pointer to same) as an element.
PowerDebug http://powerdebug.ahsoftware.net
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
Re: Unable to understand Java FFI example
Yes, I've been able to use a 32 byte array to retrieve data. But, it really is just a pointer going into the library. Hard to explain, but I've attached the code that I have so far. I have not done anything really useful yet (I can generate a KX pair, but have not tried doing anything with actual data encryption).
It also looks like the Data type may be usable to send a binary blob to a library, but have not tried anything with that yet (will probably end up doing so when I start working with actual data to encrypt).
Now that I know how to map a few of the variable types, I'm going to start adding more of the calls from the library. I'll probably copy from one of the other mappings to cut the 600+ down to something much smaller.
It also looks like the Data type may be usable to send a binary blob to a library, but have not tried anything with that yet (will probably end up doing so when I start working with actual data to encrypt).
Now that I know how to map a few of the variable types, I'm going to start adding more of the calls from the library. I'll probably copy from one of the other mappings to cut the 600+ down to something much smaller.
- Attachments
-
- sodium.zip
- lcSodium start
- (1.15 KiB) Downloaded 251 times
Brian Milby
Script Tracker https://github.com/bwmilby/scriptTracker
Script Tracker https://github.com/bwmilby/scriptTracker
-
- VIP Livecode Opensource Backer
- Posts: 3581
- Joined: Mon Jan 22, 2007 7:36 am
- Location: Berkeley, CA, US
- Contact:
Re: Unable to understand Java FFI example
Thanks for the code. Yeah, if I just had pointers to data to deal with I'd be home free.
Unfortunately, I have to deal with aggregate types as an analog for structs, not just pointers to structs.
And then I have to work with individual elements of a struct within a struct, and one of those included elements is a 1024-byte buffer (again, not a pointer to a buffer).
If I have to type 1024 "b" characters I'm just gonna take the easy way out and do this in C.
Unfortunately, I have to deal with aggregate types as an analog for structs, not just pointers to structs.
And then I have to work with individual elements of a struct within a struct, and one of those included elements is a 1024-byte buffer (again, not a pointer to a buffer).
If I have to type 1024 "b" characters I'm just gonna take the easy way out and do this in C.
PowerDebug http://powerdebug.ahsoftware.net
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
Re: Unable to understand Java FFI example
Yeah, that is tough. If you knew the memory layout then you could allocate the memory and pass the pointer, but then you would be implementing a struct in a string of binary bytes by hand.
Brian Milby
Script Tracker https://github.com/bwmilby/scriptTracker
Script Tracker https://github.com/bwmilby/scriptTracker