About native method

This is where you talk about the NXJ software itself, installation issues, and programming talk.

Moderators: 99jonathan, roger, imaqine

About native method

Postby ygxinqing » Tue Jul 17, 2012 3:04 am

I want to create a new native method, it returns String:
for example:
public static native String myTest();

I want to get a String, for example, "Hello,world!"
In the nativeemul.c, how write it?
I write "push_word("Hello,world!");", but get a error: Segmentation fault
if I want to get an integer, this code is right. But if I want to get a string, this code is wrong, who can help me? Thanks very much!
ygxinqing
New User
 
Posts: 5
Joined: Tue Jul 17, 2012 2:54 am

Re: About native method

Postby gloomyandy » Tue Jul 17, 2012 7:28 am

What is it you are trying to do? The code you have used will push the address of a C string on to the stack. This means nothing to Java, Java can not use addresses directly. Why do you need to create a native method, what will this method actually do?
User avatar
gloomyandy
leJOS Team Member
 
Posts: 4174
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: About native method

Postby ygxinqing » Tue Jul 17, 2012 2:46 pm

gloomyandy wrote:What is it you are trying to do? The code you have used will push the address of a C string on to the stack. This means nothing to Java, Java can not use addresses directly. Why do you need to create a native method, what will this method actually do?

Thanks for your reply.
I need to transplant the jvm to the platform of uclinux, so I must implement the file system. In reading text file, I want to get the string by line, so I write a new native method that return the string. Now, I think that it can return byte[], unfortunately, I don't konw how to write the code in nativeemlu.c. Can you help me? Thanks very much.
ygxinqing
New User
 
Posts: 5
Joined: Tue Jul 17, 2012 2:54 am

Re: About native method

Postby skoehler » Tue Jul 17, 2012 3:07 pm

ygxinqing wrote:
gloomyandy wrote:What is it you are trying to do? The code you have used will push the address of a C string on to the stack. This means nothing to Java, Java can not use addresses directly. Why do you need to create a native method, what will this method actually do?

Thanks for your reply.
I need to transplant the jvm to the platform of uclinux, so I must implement the file system. In reading text file, I want to get the string by line, so I write a new native method that return the string. Now, I think that it can return byte[], unfortunately, I don't konw how to write the code in nativeemlu.c. Can you help me? Thanks very much.

I'm sorry, but that doesn't sound like you have worked with Java before. To access Files, implement FileInputStream and FileOutputStream. Then, use InputStreamReader/Writer to read/write characters. Use BufferedReader to have a readLine() method. Also, in Java, Strings are UTF-16 and also contain a length while you attempted to return a zero-terminated C-string with 8-Bits per character. Also, the UTF-16 character array and length have to wrapped in an instance of java.lang.String before a Java program can actually work with the String.
skoehler
leJOS Team Member
 
Posts: 1448
Joined: Thu Oct 30, 2008 4:54 pm

Re: About native method

Postby ygxinqing » Wed Jul 18, 2012 2:19 am

skoehler wrote:
ygxinqing wrote:
gloomyandy wrote:What is it you are trying to do? The code you have used will push the address of a C string on to the stack. This means nothing to Java, Java can not use addresses directly. Why do you need to create a native method, what will this method actually do?

Thanks for your reply.
I need to transplant the jvm to the platform of uclinux, so I must implement the file system. In reading text file, I want to get the string by line, so I write a new native method that return the string. Now, I think that it can return byte[], unfortunately, I don't konw how to write the code in nativeemlu.c. Can you help me? Thanks very much.

I'm sorry, but that doesn't sound like you have worked with Java before. To access Files, implement FileInputStream and FileOutputStream. Then, use InputStreamReader/Writer to read/write characters. Use BufferedReader to have a readLine() method. Also, in Java, Strings are UTF-16 and also contain a length while you attempted to return a zero-terminated C-string with 8-Bits per character. Also, the UTF-16 character array and length have to wrapped in an instance of java.lang.String before a Java program can actually work with the String.

Thanks for your reply.
In uclinux platform, lejos doesn't implement the function of file reading and writing. So, I must write my own file system. In order to get the String of file, jvm must return string or byte[] in nativeemlu.c . I find the similar code: "push_word((STACKWORD) ptr2word(&display_array));", but the display_array isn't the normal array, it's a struct.
ygxinqing
New User
 
Posts: 5
Joined: Tue Jul 17, 2012 2:54 am

Re: About native method

Postby skoehler » Wed Jul 18, 2012 8:25 am

ygxinqing wrote:I find the similar code: "push_word((STACKWORD) ptr2word(&display_array));", but the display_array isn't the normal array, it's a struct.

Yes, of course it's a struct, since in Java an array also has a length attribute. But actually you don't want to write a function that returns an array.
You want to implement something similar to the various read/write methods that leJOS has, which have an array, an offset, and a length as an argument.
skoehler
leJOS Team Member
 
Posts: 1448
Joined: Thu Oct 30, 2008 4:54 pm

Re: About native method

Postby ygxinqing » Wed Jul 18, 2012 12:50 pm

skoehler wrote:
ygxinqing wrote:I find the similar code: "push_word((STACKWORD) ptr2word(&display_array));", but the display_array isn't the normal array, it's a struct.

Yes, of course it's a struct, since in Java an array also has a length attribute. But actually you don't want to write a function that returns an array.
You want to implement something similar to the various read/write methods that leJOS has, which have an array, an offset, and a length as an argument.


Thanks. I think I find the solution. :-)
ygxinqing
New User
 
Posts: 5
Joined: Tue Jul 17, 2012 2:54 am

Re: About native method

Postby gloomyandy » Wed Jul 18, 2012 1:59 pm

In general it is not a good idea to return new objects from native methods in leJOS, Doing this requires allocation of storage which in turns means you need to make the method restart-able so that it can handle allocation failure and a gc run. I would also suggest that you may need to spend some time understanding how the leJOS VM works and how the various Java data types map onto C versions.

As Sven has suggested you are probably better off allocating the space in Java and then using some sort of read method to obtain the data from the VM...
User avatar
gloomyandy
leJOS Team Member
 
Posts: 4174
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK


Return to NXJ Software

Who is online

Users browsing this forum: No registered users and 2 guests

more stuff