Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > questions about embedding ruby 1.9

Reply
Thread Tools

questions about embedding ruby 1.9

 
 
Rolando Abarca
Guest
Posts: n/a
 
      04-20-2009
I'm facing some problems about exception handling using the ruby 1.9.1-
p0 interpreter, this is how I'm initializing the interpreter:

RUBY_INIT_STACK;
ruby_init();

and then, I load the main ruby script:

int state;
rb_protect((VALUE (*)())_ShinyCocosRequire,
rb_str_new2(script_location), &state);

_ShinyCocosRequire is defined like this:

VALUE _ShinyCocosRequire(VALUE path) {
//rb_require_safe(path, rb_safe_level());
rb_require(STR2CSTR(path));
return Qnil;
}

Everything works just fine if there's no exception raised in the code.
If an exception is raised, I get a BAD_ACCESS error, on vm.c, line 743:

ary = vm_backtrace_each(th, RUBY_VM_NEXT_CONTROL_FRAME(cfp),
top_of_cfp, RSTRING_PTR(th->vm->progname), 0, ary);

The backtrace (in the test case) is because of a method missing not
being handled... What's the way to go with ruby1.9 and exception
handling when embedding the interpreter?
I've searched the net, but so far I've only found articles about
embedding ruby 1.8 and not 1.9...

thanks for any tip!
--
Rolando Abarca M.





 
Reply With Quote
 
 
 
 
Nobuyoshi Nakada
Guest
Posts: n/a
 
      04-21-2009
Hi,

At Tue, 21 Apr 2009 08:43:00 +0900,
Rolando Abarca wrote in [ruby-talk:334502]:
> Everything works just fine if there's no exception raised in the code.
> If an exception is raised, I get a BAD_ACCESS error, on vm.c, line 743:
>
> ary = vm_backtrace_each(th, RUBY_VM_NEXT_CONTROL_FRAME(cfp),
> top_of_cfp, RSTRING_PTR(th->vm->progname), 0, ary);
>
> The backtrace (in the test case) is because of a method missing not
> being handled... What's the way to go with ruby1.9 and exception
> handling when embedding the interpreter?
> I've searched the net, but so far I've only found articles about
> embedding ruby 1.8 and not 1.9...


I can't see any problem with following code.

#include "ruby.h"

int
main(int argc, char **argv)
{
int state;
ruby_sysinit(&argc, &argv);
{
RUBY_INIT_STACK;
ruby_init();
rb_protect(RUBY_METHOD_FUNC(rb_require), (VALUE)"./main.rb", &state);
}
return ruby_cleanup(state);
}

--
Nobu Nakada

 
Reply With Quote
 
 
 
 
Rolando Abarca
Guest
Posts: n/a
 
      04-21-2009
On Apr 21, 2009, at 2:05 AM, Nobuyoshi Nakada wrote:

> Hi,


Hi,

> I can't see any problem with following code.
>
> #include "ruby.h"
>
> int
> main(int argc, char **argv)
> {
> int state;
> ruby_sysinit(&argc, &argv);
> {
> RUBY_INIT_STACK;
> ruby_init();
> rb_protect(RUBY_METHOD_FUNC(rb_require), (VALUE)"./main.rb", &state);
> }
> return ruby_cleanup(state);
> }


totally right... you don't need to create a new to just require a
file. Is the ruby_sysinit call required? Even if I add it, I'm still
getting a bad access error in vm.c:743.
For testing purpose, this is my ruby code:

# main.rb
raise "test exception"

And i'm using the exact same code to run the ruby script as the one
you proposed. In the line where I get the bad access (vm.c:743), I
noticed that th->vm->progname is NULL... I'm guessing this might be
the error?

> --
> Nobu Nakada



thanks!,
--
Rolando Abarca M.





 
Reply With Quote
 
Graham Agnew
Guest
Posts: n/a
 
      04-21-2009
Do you nee to add rb_init_loadpath()?

ruby_sysinit(&argc, &argv);
{
RUBY_INIT_STACK;
ruby_init();
ruby_init_loadpath();


My own embedded ruby interpreter require's a file, and then calls
functions within it. The rb_require and rb_funcall are wrapped in
rb_protect.

The protected rb_require allows me to log any syntax errors in the
required code. The protected rb_funcall allows me to catch any
exceptions.

Ruby's own main.c uses something different to load the script:

ruby_sysinit(&argc, &argv);
{
RUBY_INIT_STACK;
ruby_init();
return ruby_run_node(ruby_options(argc, argv));
}

Cheers,
Gra.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Rolando Abarca
Guest
Posts: n/a
 
      04-21-2009
On Apr 21, 2009, at 11:01 AM, Graham Agnew wrote:

> Do you nee to add rb_init_loadpath()?
>
> ruby_sysinit(&argc, &argv);
> {
> RUBY_INIT_STACK;
> ruby_init();
> ruby_init_loadpath();


actually, i'm setting the loadpath global myself:

VALUE load_path = rb_gv_get(":");
rb_funcall(load_path, rb_intern("push"), 1,
rb_str_new2(my_resource_path));

not sure if it's the right way though... But I'm running rb_require
and it finds the script just fine.

> My own embedded ruby interpreter require's a file, and then calls
> functions within it. The rb_require and rb_funcall are wrapped in
> rb_protect.


same as I'm doing it right now. The problem I'm having is with
exception handling. Do I need to call ruby_sysinit()?

> The protected rb_require allows me to log any syntax errors in the
> required code. The protected rb_funcall allows me to catch any
> exceptions.


mmm... I'm sorry, I lost you here. What function are you calling with
rb_funcall?

> Ruby's own main.c uses something different to load the script:
>
> ruby_sysinit(&argc, &argv);
> {
> RUBY_INIT_STACK;
> ruby_init();
> return ruby_run_node(ruby_options(argc, argv));
> }


yes, I saw that (I always turn to the source when lost )

> Cheers,
> Gra.


regards,
--
Rolando Abarca M.





 
Reply With Quote
 
Rolando Abarca
Guest
Posts: n/a
 
      04-21-2009
it's working now
with ruby version from stable snapshot
apparently it was a bug in the 1.9.1-p0

On Apr 21, 2009, at 6:42 PM, Rolando Abarca wrote:

> On Apr 21, 2009, at 11:01 AM, Graham Agnew wrote:
>
>> Do you nee to add rb_init_loadpath()?
>>
>> ruby_sysinit(&argc, &argv);
>> {
>> RUBY_INIT_STACK;
>> ruby_init();
>> ruby_init_loadpath();

>
> actually, i'm setting the loadpath global myself:
>
> VALUE load_path = rb_gv_get(":");
> rb_funcall(load_path, rb_intern("push"), 1,
> rb_str_new2(my_resource_path));
>
> not sure if it's the right way though... But I'm running rb_require
> and it finds the script just fine.
>
>> My own embedded ruby interpreter require's a file, and then calls
>> functions within it. The rb_require and rb_funcall are wrapped in
>> rb_protect.

>
> same as I'm doing it right now. The problem I'm having is with
> exception handling. Do I need to call ruby_sysinit()?
>
>> The protected rb_require allows me to log any syntax errors in the
>> required code. The protected rb_funcall allows me to catch any
>> exceptions.

>
> mmm... I'm sorry, I lost you here. What function are you calling
> with rb_funcall?
>
>> Ruby's own main.c uses something different to load the script:
>>
>> ruby_sysinit(&argc, &argv);
>> {
>> RUBY_INIT_STACK;
>> ruby_init();
>> return ruby_run_node(ruby_options(argc, argv));
>> }

>
> yes, I saw that (I always turn to the source when lost )
>
>> Cheers,
>> Gra.



cheers,
--
Rolando Abarca M.





 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Questions about SWIG, Ruby/DL and embedding calls to ruby withinC++ Code Charles Comstock Ruby 1 06-25-2004 09:49 PM
Several questions about embedding Python Miki Tebeka Python 5 05-09-2004 11:01 AM
questions about embedding ruby Basile STARYNKEVITCH Ruby 3 09-02-2003 10:57 AM
embedding ruby - some questions Basile STARYNKEVITCH Ruby 0 08-31-2003 10:16 PM
Some questions embedding Pythong in C (callback for variable assignment in the dictionary?) jordi Python 0 07-16-2003 02:31 PM



Advertisments