Ruby to C to another language (perhaps Java (I Don't Need JRuby))

Z

Zach Dennis

This posting is more for a learning thing then anything else at this
point. I've been playing with Java to C via JNI lately doing some pretty
basic tests; calling methods, getting and settings fields, passing
primitive types around, etc...

A while back (a few months ago) I did some Ruby to C tests of pretty
much the same caliber.

I would like to combine the two approaches and share some objects
between Ruby to C to Java and vise versa. I am not looking for the JVM
to be able to process ruby code. (no JRuby)

Has anyone had a similar approach (even if it wasn't Java) where they
were able to wrap a few Objects and use C as the middle man to communicate?

If so, did you have to embed the ruby compiler inside the *.so or *.dll
file when running the interpretor of the other language (i presume yes?)
and what was the level of difficulty (one word answers are fine. I can
live with easy, hard, difficult, if you want to share a whole paragraph
then please do)?

Is it difficult to share data structures between three different
languages. Again Ruby/C/"3rd Language"?

What I am trying to get out of this is in the long run is to be able to
have a few data structures that I can share between Ruby and Java, via
C. I currently have java application and I feel I could code quicker
certain aspects alot quicker in Ruby if only I could share a data
structure/object. Ideally I would like to do something like:

---In My Ruby Code---
class StringProcessor
def process_str( str )
#process
return process_str
end
end


---In my Java Code----
class StringProcessor{
public native String _processString( String str );
public void processString( String str ){
this._processString( str );
}
public static void main( String[] args ){
new StringProcessor().processString( aBigString );
}
}


-----In my C Code (pseudo)-----
Java_StringProcessor__processString jstring ( jstring str ){
char c = jStringToCharArray( str )
c = Ruby_StringProcessor_processString( c )
return charToJString( c )
}

jStringToCharArray char[] ( jstring str ){
char[] c
//convert
return c
}

charToJString jstring( char[] c )
jstring j
//convert
return j
}

Ruby_StringProcessor_processString char[] ( rstring str ){
char[] c;
//Process
return c;
}



This is not a well thought out example. It's just off the top of my head.

So does anyone know if this is possible?

Thanks,

Zach
 
K

Kaspar Schiess

Has anyone had a similar approach (even if it wasn't Java) where they
were able to wrap a few Objects and use C as the middle man to
communicate?

Hello Denis,

I know of rjava (using a tcp connection) and rjb (using a jni native
interface) as far as ruby to java interfacing goes. I have tested rjb and
found it to work (with some tweaking) as I wanted it to. I don't know about
the difficulty of these efforts, but now that you have the source, I guess
duplicating them is a breeze, depending on how much cheating you allow
yourself to do.

best regards, kaspar

hand manufactured code - www.tua.ch/ruby
 
L

Lyndon Samson

Sounds like you are better off skipping the C/C++ layer and using
something like shared memory/memory mapped files to communicate
between ruby and java.

You could use real threads, or some sort of co-routine.

Simple solution would be to use XML, otherwise you'll need to write
something that unpacks complex structures from byte arrays.


BTW, your subject line is a little confusing, I thought the post would
be about a Ruby to C translator. :)


This posting is more for a learning thing then anything else at this
point. I've been playing with Java to C via JNI lately doing some pretty
basic tests; calling methods, getting and settings fields, passing
primitive types around, etc...

A while back (a few months ago) I did some Ruby to C tests of pretty
much the same caliber.

I would like to combine the two approaches and share some objects
between Ruby to C to Java and vise versa. I am not looking for the JVM
to be able to process ruby code. (no JRuby)

Has anyone had a similar approach (even if it wasn't Java) where they
were able to wrap a few Objects and use C as the middle man to communicate?

If so, did you have to embed the ruby compiler inside the *.so or *.dll
file when running the interpretor of the other language (i presume yes?)
and what was the level of difficulty (one word answers are fine. I can
live with easy, hard, difficult, if you want to share a whole paragraph
then please do)?

Is it difficult to share data structures between three different
languages. Again Ruby/C/"3rd Language"?

What I am trying to get out of this is in the long run is to be able to
have a few data structures that I can share between Ruby and Java, via
C. I currently have java application and I feel I could code quicker
certain aspects alot quicker in Ruby if only I could share a data
structure/object. Ideally I would like to do something like:

---In My Ruby Code---
class StringProcessor
def process_str( str )
#process
return process_str
end
end

---In my Java Code----
class StringProcessor{
public native String _processString( String str );
public void processString( String str ){
this._processString( str );
}
public static void main( String[] args ){
new StringProcessor().processString( aBigString );
}
}

-----In my C Code (pseudo)-----
Java_StringProcessor__processString jstring ( jstring str ){
char c = jStringToCharArray( str )
c = Ruby_StringProcessor_processString( c )
return charToJString( c )
}

jStringToCharArray char[] ( jstring str ){
char[] c
//convert
return c
}

charToJString jstring( char[] c )
jstring j
//convert
return j
}

Ruby_StringProcessor_processString char[] ( rstring str ){
char[] c;
//Process
return c;
}

This is not a well thought out example. It's just off the top of my head.

So does anyone know if this is possible?

Thanks,

Zach
 
M

Mauricio Fernández

This posting is more for a learning thing then anything else at this
point. I've been playing with Java to C via JNI lately doing some pretty
basic tests; calling methods, getting and settings fields, passing
primitive types around, etc...

A while back (a few months ago) I did some Ruby to C tests of pretty
much the same caliber.

I would like to combine the two approaches and share some objects
between Ruby to C to Java and vise versa. I am not looking for the JVM
to be able to process ruby code. (no JRuby)

Has anyone had a similar approach (even if it wasn't Java) where they
were able to wrap a few Objects and use C as the middle man to communicate?

Take a look at the third issue of rubima (Rubyist Magazine); the Ruby
Library Report introduces rjb and rjni:

http://jp.rubyist.net/magazine/?0003-RLR

They both use JNI. rjb is implemented as a C extension, and rjni as a
simple wrapping of JNI (extension) + Ruby code for the higher layers.

You might need http://excite.co.jp/world/url
 
F

Florian Gross

Zach said:
This posting is more for a learning thing then anything else at this
point. I've been playing with Java to C via JNI lately doing some pretty
basic tests; calling methods, getting and settings fields, passing
primitive types around, etc...

A while back (a few months ago) I did some Ruby to C tests of pretty
much the same caliber.

I would like to combine the two approaches and share some objects
between Ruby to C to Java and vise versa. I am not looking for the JVM
to be able to process ruby code. (no JRuby)

There's work being done on the Ruby -> C part. See
http://zenspider.com/~ryand/Ruby2C.pdf

That aside maybe SWIG can be useful for you?
 
Z

Zach Dennis

Kaspar said:
Hello Denis,

I know of rjava (using a tcp connection) and rjb (using a jni native
interface) as far as ruby to java interfacing goes. I have tested rjb and
found it to work (with some tweaking) as I wanted it to. I don't know about
the difficulty of these efforts, but now that you have the source, I guess
duplicating them is a breeze, depending on how much cheating you allow
yourself to do.

Thanks Kaspar, I will definitely look into rjava and rjb.

Zach
 
Z

Zach Dennis

Lyndon said:
Sounds like you are better off skipping the C/C++ layer and using
something like shared memory/memory mapped files to communicate
between ruby and java.

You could use real threads, or some sort of co-routine.

Simple solution would be to use XML, otherwise you'll need to write
something that unpacks complex structures from byte arrays.

I will have to do some tests and see speed comparisons. Especially if I
can get Java to generate some YAML files, and have my ruby program read
it in.
BTW, your subject line is a little confusing, I thought the post would
be about a Ruby to C translator. :)

Sorry about that. I see what you mean.

Thanks for replying,

Zach
 
D

Dido Sevilla

Has anyone had a similar approach (even if it wasn't Java) where they
were able to wrap a few Objects and use C as the middle man to communicate?

Um, why not just use the networking layer of your system and use some
kind of remote procedure call to make your Java and Ruby programs
communicate? That strikes me as far simpler than the scheme that you
seem to be proposing. I don't know if there's a Java package that does
DRb or a Ruby package able to RMI/IIOP, but such a thing might work.
At last resort you may be able to use XML-RPC or even SOAP to get your
programs to do this, but IMHO such contrivances are an abuse of what
XML was designed to do.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top