questions about embedding ruby 1.9

R

Rolando Abarca

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!
 
N

Nobuyoshi Nakada

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);
}
 
R

Rolando Abarca

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?


thanks!,
 
G

Graham Agnew

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.
 
R

Rolando Abarca

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 :p)
Cheers,
Gra.

regards,
 
R

Rolando Abarca

it's working now :)
with ruby version from stable snapshot :-D
apparently it was a bug in the 1.9.1-p0

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.


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


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


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


cheers,
 

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

Forum statistics

Threads
473,967
Messages
2,570,148
Members
46,694
Latest member
LetaCadwal

Latest Threads

Top