Forum: Ruby-core [Feature #3731] Easier Embedding API for Ruby

C4e88907313843cf07f6d85ba8162120?d=identicon&s=25 Beoran Aegul (Guest)
on 2010-08-22 00:32
(Received via mailing list)
Feature #3731: Easier Embedding API for Ruby
http://redmine.ruby-lang.org/issues/show/3731

Author: Beoran Aegul
Status: Open, Priority: Normal

With Ruby 1.9, it has become more difficult to embed Ruby in a C
application correctly.
It would be nice if there was a clearly documented and simple C API to
embed ruby in C programs.
I know Ruby was not designed from the start to be embedded, but Ruby was
used before in several
products as an embedded scripting langauge. It should therefore be
possible to do so in a
more straightforward way.

Kind Regards,

B.
7df7b1298469ef2cc384febe8cde00f0?d=identicon&s=25 Asher Haig (asher)
on 2010-08-22 00:58
(Received via mailing list)
What is difficult about this?

Example:

#include <ruby.h>

int main( int argc __attribute__((unused)), char *argv[]
__attribute__((unused)) )  {

  ruby_init();

  //  Ruby Options are just like /usr/bin/ruby
  //  interpreter name, script name, argv ...
  char*  options[]  =  { "", "rpdom_test.rb",  };
  void*  node    =  ruby_options( 2, options );

  return ruby_run_node( node );
}

Struck me as potentially a documentation issue, but the actual
functionality I think is pretty straightforward?

Asher
C4e88907313843cf07f6d85ba8162120?d=identicon&s=25 Beoran Aegul (Guest)
on 2010-08-22 10:36
(Received via mailing list)
Issue #3731 has been updated by Beoran Aegul.


Dear Asher,

Well, I certainly agree documentation should be improved for this use
case! :)

However, the problem with what you suggest is that it doesn't work more
than once.

For example; I get a segmentation violation (crash) with a "You may
encounter a bug of Ruby interpreter."
on the second call to ruby_run_node();
if I try this on a ruby1.9.1 p0:


#include <ruby.h>

RUBY_GLOBAL_SETUP
int main( int argc __attribute__((unused)), char *argv[]
__attribute__((unused)) )  {
        RUBY_INIT_STACK
  ruby_init();
        {
  char*  options[]  =  { "", "-e", "puts 'hello'"  };
  void*  node    =  ruby_options( 2, options );
  char*  options2[]  =  { "", "-e", "puts 'world'"  };
  void*  node2    =  ruby_options( 2, options2 );
        ruby_run_node( node  );
        ruby_run_node( node2 );
        }
}

It may be that this bug is fixed in later versions, but I still have to
install them to try.

Another problem is that it's a rather unwieldy API. It's nicer if you
can run multiple scripts, or call rb_eval_string multiple times.
It's also nice if you can catch syntax errors in the ruby files loaded
and handle them on the C side.

Kind Regards,

B.

----------------------------------------
http://redmine.ruby-lang.org/issues/show/3731
This topic is locked and can not be replied to.