# Questions about embedding ruby 1.9

**URL:** <https://www.ruby-forum.com/t/questions-about-embedding-ruby-1-9/166015>\
**Category:** Ruby\
**Created:** [April 21, 2009, 1:43am UTC](https://www.ruby-forum.com/t/questions-about-embedding-ruby-1-9/166015 "2009-04-21T01:43:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rolando\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/d9b06d/32.png) [@Rolando\_A](https://www.ruby-forum.com/u/Rolando_A)\
**Post date:** [April 21, 2009, 1:43am UTC](https://www.ruby-forum.com/t/questions-about-embedding-ruby-1-9/166015/1 "2009-04-21T01:43:41Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Rolando\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/d9b06d/32.png) [@Rolando\_A](https://www.ruby-forum.com/u/Rolando_A)\
**Post date:** [April 21, 2009, 8:06am UTC](https://www.ruby-forum.com/t/questions-about-embedding-ruby-1-9/166015/2 "2009-04-21T08:06:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Rolando\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/d9b06d/32.png) [@Rolando\_A](https://www.ruby-forum.com/u/Rolando_A)\
**Post date:** [April 21, 2009, 2:32pm UTC](https://www.ruby-forum.com/t/questions-about-embedding-ruby-1-9/166015/3 "2009-04-21T14:32:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Rolando\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/d9b06d/32.png) [@Rolando\_A](https://www.ruby-forum.com/u/Rolando_A)\
**Post date:** [April 21, 2009, 5:01pm UTC](https://www.ruby-forum.com/t/questions-about-embedding-ruby-1-9/166015/4 "2009-04-21T17:01:44Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Rolando\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/d9b06d/32.png) [@Rolando\_A](https://www.ruby-forum.com/u/Rolando_A)\
**Post date:** [April 22, 2009, 12:42am UTC](https://www.ruby-forum.com/t/questions-about-embedding-ruby-1-9/166015/5 "2009-04-22T00:42:58Z")

</div>

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,

---

<div class="post-metadata">

**Author:** ![Rolando\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/d9b06d/32.png) [@Rolando\_A](https://www.ruby-forum.com/u/Rolando_A)\
**Post date:** [April 22, 2009, 1:46am UTC](https://www.ruby-forum.com/t/questions-about-embedding-ruby-1-9/166015/6 "2009-04-22T01:46:46Z")

</div>

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 A. wrote:

> actually, i’m setting the loadpath global myself:
> 
> > rb\_protect.
> 
> > Cheers,  
> > Gra.

cheers,
