[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

How do I embed a Ruby interpreter in my C program?

Randy R

1/10/2007 7:35:00 PM

How do I embed a Ruby interpreter in my Win32 C/C++ program? The PicAxe
book is disturbingly laconic on the subject and leaves many seemingly
important details out. Is there a library I can link against that will
contain the interpreter? Do I have to compile the Ruby interpreter source
code with my project? How does any of this crazy stuff work?
I have been able to find very few resources on the internets regading
this so links to good sites on the matter will be greatly appreciated.
Thank you...


9 Answers

Bill Kelly

1/10/2007 7:47:00 PM

0


From: "Just Another Victim of the Ambient Morality" <ihatespam@hotmail.com>
>
> How do I embed a Ruby interpreter in my Win32 C/C++ program? The PicAxe
> book is disturbingly laconic on the subject and leaves many seemingly
> important details out. Is there a library I can link against that will
> contain the interpreter? Do I have to compile the Ruby interpreter source
> code with my project? How does any of this crazy stuff work?

Here's a simple example:

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-t...


If you installed using the one-click installer, look in your
ruby/lib directory... you should see:

msvcrt-ruby18-static.lib
msvcrt-ruby18.lib

...and in ruby/bin:

msvcrt-ruby18.dll

So you have your choice as to link statically or dynamically
with the ruby interpreter.


Hope this helps,

Bill




Ivan Gromov

5/1/2008 6:15:00 PM

0

Hello all.
Tell me please, what initialisation order and params are required for
ruby in multithreaded app,
if i do not want to receive this magnificent error:
=============Error=================
SystemStackError: Message = stack level too deep
Backtrace = from D:\_BTA\_dev\Data\Script/test.rb:7:in `print
=======================================
Script:
=============test.rb=================
def buildHotkeysList( file1, file2 )

file1_work= File.new( file1)
file2_work= File.new( file2 , "w+" )

file1_work.each_line {|line|
file2_work.print( line )
}
end
buildHotkeysList (file1.txt, file2.txt)
=======================================
In simple "for ruby" app everything is ok:
=============main.cpp=================
int main( int argc, char **argv )
{
NtInitialize(&argc, &argv);
ruby_init();
ruby_script("embed");

rb_load_protect( rb_str_new2( "Test.rb" ), 0, &status);

ruby_finalize();
ruby_cleanup(0);
}
=======================================

Platform: Windows
Compiler: Microsoft
Ruby : 1.86
Thread API: ACE thread.

Thank you.
--
Posted via http://www.ruby-....

Jason Roelofs

5/1/2008 6:40:00 PM

0

@The OP:

Embedding the Ruby interpreter isn't that difficult, but there are
tools to make it easier. For example, the Rice library
(http://rice.rub...) has the VM class that makes it trivially
easy to embed:

Rice::VM *vm = new Rice::VM(argc, argv);

now you're free to call whatever ruby calls you want, as well as Rice
objects and other wrapping you may need to do. Rice will also do the
necessary cleanup when the VM object is destroyed.

@Ivan:

You can only run the Ruby VM in a single thread.

Jason R.

Jason Roelofs

5/1/2008 6:44:00 PM

0

On Thu, May 1, 2008 at 2:40 PM, Jason Roelofs <jameskilton@gmail.com> wrote:
> @The OP:
>
> Embedding the Ruby interpreter isn't that difficult, but there are
> tools to make it easier. For example, the Rice library
> (http://rice.rub...) has the VM class that makes it trivially
> easy to embed:
>
> Rice::VM *vm = new Rice::VM(argc, argv);
>
> now you're free to call whatever ruby calls you want, as well as Rice
> objects and other wrapping you may need to do. Rice will also do the
> necessary cleanup when the VM object is destroyed.
>
> @Ivan:
>
> You can only run the Ruby VM in a single thread.
>
> Jason R.
>

Sorry, my Rice example isn't quite right. From the docs:

Rice::VM vm(argc, argv);
vm.run()

will get you up and running.

Jason R

Ivan Gromov

5/2/2008 9:34:00 AM

0

Yes 'Rice' is quite impressive.

But i just want to use 'ruby_init()', 'ruby_finalize()' at the main
thread.
And 'rb_load_protect()' at the other thread.
Is it possible ? And how ?
--
Posted via http://www.ruby-....

Jason Roelofs

5/2/2008 12:44:00 PM

0

No, you cannot reliably run Ruby across threads. It's just not
possible. Pick one thread to house Ruby and keep it there.

Jason

On Fri, May 2, 2008 at 5:34 AM, Ivan Gromov <kthecm@yahoo.com> wrote:
> Yes 'Rice' is quite impressive.
>
> But i just want to use 'ruby_init()', 'ruby_finalize()' at the main
> thread.
> And 'rb_load_protect()' at the other thread.
> Is it possible ? And how ?
>
>
> --
> Posted via http://www.ruby-....
>
>

Suraj Kurapati

5/2/2008 2:18:00 PM

0

Ivan Gromov wrote:
> i just want to use 'ruby_init()', 'ruby_finalize()' at the main
> thread. And 'rb_load_protect()' at the other thread.
> Is it possible ? And how ?

Yes this is possible. The trick is to embed the *execution* of Ruby
interpreter inside a POSIX thread[1], while doing the initialization
inside the main thread. See http://www.ruby-...to... for
examples and discussion.

In fact, I've used this exact technique for my Ruby-VPI project[1],
which embeds Ruby inside a Verilog simulator.

[1] http://ruby-vpi.rub...
--
Posted via http://www.ruby-....

Suraj Kurapati

5/2/2008 2:28:00 PM

0

Suraj Kurapati wrote:
> Ivan Gromov wrote:
>> i just want to use 'ruby_init()', 'ruby_finalize()' at the main
>> thread. And 'rb_load_protect()' at the other thread.
>> Is it possible ? And how ?
>
> Yes this is possible. The trick is to embed the *execution* of Ruby
> interpreter inside a POSIX thread, while doing the initialization
> inside the main thread. See http://www.ruby-...to... for
> examples and discussion.

Since your target platform is Win32, I'm not sure whether you have the
pthreads library available. So another approach would be to embed the
*execution* of your Ruby program inside a Ruby thread
(rb_thread_create()). You can synchronize between the thread and your
main C program through the use of Ruby's thread-safe Queue class.

See http://www.ruby-...to...#652515 for an example.
--
Posted via http://www.ruby-....

Ivan Gromov

5/2/2008 6:38:00 PM

0

Understood. Thank you.
--
Posted via http://www.ruby-....