Questions about embedding ruby 1.9


#1

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!


#2

Hi,

At Tue, 21 Apr 2009 08:43:00 +0900,
Rolando A. wrote in [ruby-talk:334502]:

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


#3

On Apr 21, 2009, at 2:05 AM, Nobuyoshi N. wrote:

Hi,

Hi,

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


#4

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.


#5

On Apr 21, 2009, at 11:01 AM, Graham A. 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 :-P)

Cheers,
Gra.

regards,


#6

it’s working now :slight_smile:
with ruby version from stable snapshot :smiley:
apparently it was a bug in the 1.9.1-p0

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

actually, i’m setting the loadpath global myself:

rb_protect.

Cheers,
Gra.

cheers,