[MacRuby-devel] Embedding MacRuby

Laurent Sansonetti lsansonetti at apple.com
Tue Aug 19 12:39:42 PDT 2008


Hi Rakoth,

On Aug 19, 2008, at 7:21 AM, Rakoth Lichlord wrote:

> While writing portions of mac programs in Ruby and mixing it with
> Objective-C is cool, but it's not all that MacRuby could do.
>
> I'm attempting to write an application which would contain scriptable
> parts, utilizing MacRuby (Lua has been used for this purpose for a
> long time, but MacRuby with access to ObjC classes is a much more
> powerful tool for the job!).

Thanks :)

> So far I've run into these issues, any help appreciated:
>
> - Need to execute MacRuby code from any source (parse a string)
> Solution 1: "rb_eval_string" - does not cut it because of a bug (#109
> in trac) - it seems MacRuby forgets variables created by previous
> calls to rb_eval_string, maybe there's more to it then that
> Solution 2: [RBKernel eval:@"..."] - This example has been mentioned
> in this mailist but I've found no references to RBKernel or anything
> similar in the MacRuby.framework. Guess it was more of a proposal.

I would like to introduce an Objective-C API in MacRuby core to permit  
this kind of things from a pure Objective-C environment. Basically  
initializing the VM, evaluating expressions, but also creating blocks,  
etc...

Right now, one thing you can do is to mimic what macruby_main() does,  
to initialize the VM.

Then, in theory, rb_eval_string() should just work.

You could also try to call Kernel#eval directly:

(gdb) po [rb_mKernel eval:@"1+1"]
2

Here rb_mKernel is a static variable that points to the Kernel module,  
but you can also retrieve it dynamically.

(gdb) po [objc_getClass("Kernel") eval:@"1+1"]
2

> - Need to be able to clean up (full reset) of the MacRuby VM - release
> all defined objects, undefine classes.
> Is this even possible? How costly would it be, time-wise?

I don't think this is possible, right now. But we can work on this.

At a glance, the only problematic thing would be to remove C  
extensions that were loaded.

Laurent


More information about the MacRuby-devel mailing list