Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embedding a Ruby interpreter in a C++ app

I'm hoping to use Ruby as a scripting language for my game engine. I've found the usual articles describing how to call Ruby classes from C++ code and vice versa (e.g. here) but I can't quite see how to do what I want with that way of working...

My engine currently uses a little language I wrote myself with Flex and Bison, and a little stack based virtual machine. Scripts don't always run right through from start to finish, for instance they sometimes includes commands like "sleep for 2 seconds" or "wait until character has finished walking", so the scheduler keeps tabs on the status of each script and an instruction pointer, and knows when to resume them, and so on.

So it seems that I really need some kind of embedded Ruby interpreter that I can exercise a certain degree of control over, rather than simply calling Ruby methods. Or am I just being obtuse and missing something?

I'm working in Microsoft Visual C++, so ideally any solution would compile nice and easily in that.

like image 989
andygeers Avatar asked Mar 09 '09 13:03

andygeers


People also ask

What is embedded Ruby interpreter?

eRuby stands for embedded Ruby. It's a tool that embeds fragments of Ruby code in other files such as HTML files similar to ASP, JSP and PHP. eRuby allows Ruby code to be embedded within (delimited by) a pair of <% and %> delimiters.

Can Ruby be embedded into HTML?

So far we've looked at using Ruby to create HTML output, but we can turn the problem inside out; we can actually embed Ruby in an HTML document. There are a number of packages that allow you to embed Ruby statements in some other sort of a document, especially in an HTML page.

What is Ruby interpreter written in?

The original Ruby interpreter is often referred to as Matz's Ruby Interpreter or MRI. This implementation is written in C and uses its own Ruby-specific virtual machine. The standardized and retired Ruby 1.8 implementation was written in C, as a single-pass interpreted language.

How does Ruby interpreter work?

During the parsing stage, Ruby transforms the text into something called an abstract syntax tree, or AST. The abstract syntax tree is a representation of your program in memory. You might say that programming languages in general are just more user-friendly ways of describing abstract syntax trees.


2 Answers

Here's an example including error handling.

#include <iostream>
#include <ruby.h>

using namespace std;

int main(void)
{
  ruby_init();
  ruby_init_loadpath();
  int status;
  rb_load_protect(rb_str_new2("./test.rb"), 0, &status);
  if (status) {
    VALUE rbError = rb_funcall(rb_gv_get("$!"), rb_intern("message"), 0);
    cerr << StringValuePtr(rbError) << endl;
  };
  ruby_finalize();
  return status;
}
like image 165
wedesoft Avatar answered Oct 14 '22 11:10

wedesoft


You're on the right track. The key is to do something similar to the section on Embedding Concepts in the link you posted. In simple terms it's little more than:

ruby_init();
ruby_script("some_script");

You may need to copy over all the #ifdef stuff from main.c to get everything working. From then it's a matter of building an API to your C++ functions you want to expose, and depending on your design, multi-threading the thing.

like image 30
Pesto Avatar answered Oct 14 '22 13:10

Pesto