How to return an array from a native call?

26 views
Skip to first unread message

Thomas

unread,
Sep 11, 2019, 8:01:35 PM9/11/19
to CodenameOne Discussions
I need to return an array of primitives ( a double[] ) as the result of a native call in a NativeInterface. However, even if I guess it is possible, I could not find any example of this in the documentation or the various cn1libs I already looked at. And I don't know what would be the correct mapping for this in the diiferent native languages (the mapping table given into https://www.codenameone.com/manual/advanced-topics.html#_native_interfaces is a complete mess with column headers that do not match the types in the rows (since when do Java have a NSData* type or Android a BOOL one for example?) and a "byte" column that isn't a language and do not make any sense (at least without any additional explanation, it is completely incomprehensible). You really shoud fix this part of the documentation!)

So, if, in my NativeInterface I define a function like this:

  public double[] myfunction();

what woud be its mapping in the various languages (c# for ioS, javascript...)?

for example for iOS would it be 

(double[]) myfunction {}

or

(NSData*myfunction {}

or something else...?


same question if I would like to return a byte[] array instead of a double[] one

Steve Hannah

unread,
Sep 11, 2019, 8:23:27 PM9/11/19
to codenameone...@googlegroups.com
The easiest way to see is just to create a native interface and then generate the native implementations using the IDEs “generate native interfaces” option.

--
You received this message because you are subscribed to the Google Groups "CodenameOne Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to codenameone-discu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/codenameone-discussions/dfa74779-54c4-46ff-b55a-a923111225b6%40googlegroups.com.
--
Steve Hannah
Software Developer
Codename One

Thomas

unread,
Sep 12, 2019, 2:52:38 PM9/12/19
to CodenameOne Discussions
well it is not really convenient when you are modifying an already existing cn1 lib and can't override the native files you already wrote.
Also, the generated native interface do not give you any hint on how to convert you native object to the expected interface binding object when these differ.
For example in objective C, if I have an  NSArray or an NSMutableArray holding my primitives how must I convert it to NSData* before returning it (there is different methods using NSKeyedArchiver or NSPropertyListSerialization and I don't which one to use since I have no information on the deserialisation method CN1 is using to bind it back to java arrays of primitives..). 
In any case you should fix the documentation on native interfaces types mapping and rules


On Thursday, September 12, 2019 at 2:23:27 AM UTC+2, Steve Hannah wrote:
The easiest way to see is just to create a native interface and then generate the native implementations using the IDEs “generate native interfaces” option.
On Wed, Sep 11, 2019 at 5:01 PM Thomas <tomber...@gmail.com> wrote:
I need to return an array of primitives ( a double[] ) as the result of a native call in a NativeInterface. However, even if I guess it is possible, I could not find any example of this in the documentation or the various cn1libs I already looked at. And I don't know what would be the correct mapping for this in the diiferent native languages (the mapping table given into https://www.codenameone.com/manual/advanced-topics.html#_native_interfaces is a complete mess with column headers that do not match the types in the rows (since when do Java have a NSData* type or Android a BOOL one for example?) and a "byte" column that isn't a language and do not make any sense (at least without any additional explanation, it is completely incomprehensible). You really shoud fix this part of the documentation!)

So, if, in my NativeInterface I define a function like this:

  public double[] myfunction();

what woud be its mapping in the various languages (c# for ioS, javascript...)?

for example for iOS would it be 

(double[]) myfunction {}

or

(NSData*myfunction {}

or something else...?


same question if I would like to return a byte[] array instead of a double[] one

--
You received this message because you are subscribed to the Google Groups "CodenameOne Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to codenameone-discussions+unsub...@googlegroups.com.

Steve Hannah

unread,
Sep 13, 2019, 3:30:03 PM9/13/19
to codenameone...@googlegroups.com
Good point.  Upon looking into it, it doesn't appear anyone has *ever* used this functionality.  In my cn1libs I had generally passed in JSON data when I needed to pass complex data.  So I decided to put together a test case, to use as a demonstration, and I found a few bugs in the current implementation.  I have fixed these bugs, but they won't be available until the next server update next friday.  

I have posted a small unit test for this which demonstrates how it works.

The native interface:

The iOS implementation:

The unit test.


Steve


To unsubscribe from this group and stop receiving emails from it, send an email to codenameone-discu...@googlegroups.com.
--
Steve Hannah
Software Developer
Codename One

--
You received this message because you are subscribed to the Google Groups "CodenameOne Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to codenameone-discu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/codenameone-discussions/918c7fb9-2325-4b8b-9601-8f281f547244%40googlegroups.com.

Dave Dyer

unread,
Sep 13, 2019, 4:44:16 PM9/13/19
to CodenameOne Discussions
I've passed byte[] as parameters, but not as return values.

Steve Hannah

unread,
Sep 13, 2019, 5:06:52 PM9/13/19
to codenameone...@googlegroups.com
byte[] was the only one that worked out of the primitive arrays.

They’ll all work in the next server update though.

On Fri, Sep 13, 2019 at 1:44 PM Dave Dyer <ddyer-...@real-me.net> wrote:
I've passed byte[] as parameters, but not as return values.

--
You received this message because you are subscribed to the Google Groups "CodenameOne Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to codenameone-discu...@googlegroups.com.

Thomas

unread,
Sep 13, 2019, 5:16:06 PM9/13/19
to CodenameOne Discussions
TY Steve.


On Friday, September 13, 2019 at 11:06:52 PM UTC+2, Steve Hannah wrote:
byte[] was the only one that worked out of the primitive arrays.

They’ll all work in the next server update though.
On Fri, Sep 13, 2019 at 1:44 PM Dave Dyer <ddyer-...@real-me.net> wrote:
I've passed byte[] as parameters, but not as return values.

--
You received this message because you are subscribed to the Google Groups "CodenameOne Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to codenameone-discussions+unsub...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages