questions about embedding ruby 1.9

Discussion in 'Ruby' started by Rolando Abarca, Apr 21, 2009.

  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!
    --
    Rolando Abarca M.
    Rolando Abarca, Apr 21, 2009
    #1
    1. Advertising

  2. 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
    Nobuyoshi Nakada, Apr 21, 2009
    #2
    1. Advertising

  3. 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.
    Rolando Abarca, Apr 21, 2009
    #3
  4. Rolando Abarca

    Graham Agnew Guest

    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/.
    Graham Agnew, Apr 21, 2009
    #4
  5. 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 :p)

    > Cheers,
    > Gra.


    regards,
    --
    Rolando Abarca M.
    Rolando Abarca, Apr 21, 2009
    #5
  6. it's working now :)
    with ruby version from stable snapshot :-D
    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 :p)
    >
    >> Cheers,
    >> Gra.



    cheers,
    --
    Rolando Abarca M.
    Rolando Abarca, Apr 22, 2009
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jordi
    Replies:
    0
    Views:
    396
    jordi
    Jul 16, 2003
  2. Miki Tebeka

    Several questions about embedding Python

    Miki Tebeka, May 5, 2004, in forum: Python
    Replies:
    5
    Views:
    640
    Miki Tebeka
    May 9, 2004
  3. Basile STARYNKEVITCH

    embedding ruby - some questions

    Basile STARYNKEVITCH, Aug 31, 2003, in forum: Ruby
    Replies:
    0
    Views:
    117
    Basile STARYNKEVITCH
    Aug 31, 2003
  4. Basile STARYNKEVITCH

    questions about embedding ruby

    Basile STARYNKEVITCH, Sep 1, 2003, in forum: Ruby
    Replies:
    3
    Views:
    138
  5. Charles Comstock
    Replies:
    1
    Views:
    117
    Lyle Johnson
    Jun 25, 2004
Loading...

Share This Page