Multiple environments in a single process

I’m using the Ruby C API to execute Ruby scripts, and I need each script
to be in a separate environment. Each environment would have its own
global variables and classes, and they wouldn’t be accessible from the
other environments. I imagine it being something like the Binding class
but with more protection.

I’ve read some things around here about multiple VMs in a single
process. This sounds perfect, but I haven’t seen anything concrete. I’d
rather not use multiple processes, as that might not even work for this
application. Have there been any new developments on this topic?

I believe this can be done. We can enclose all the VM state variables
into a structure, then pass that structure into the functions that need
it. Each function would then perform its task inside the specified VM.
Since we’re not working with multiple processes, each VM context can
work with variables from other VMs. It might look like this in C:

VM* vm_a = rb_vm_context_new();
VM* vm_b = rb_vm_context_new();

rb_define_global_const(vm_a, “hello”, rb_str_new2(“Hello!”));
rb_define_global_const(vm_b, “hi”, rb_str_new2(“Hi!”));

rb_eval_string(vm_a, “$hello”); // “Hello!”
rb_eval_string(vm_a, “$hi”); // Qnil
rb_eval_string(vm_b, “$hello”); // Qnil
rb_eval_string(vm_b, “$hi”); // “Hi!”

We can use it within Ruby somewhat like this:
https://gist.github.com/timahoney/5197905

Is it a possibility?

Tony A. wrote in post #1102324:

Might want to read this thread. It provides a bit of a post mortem on
MVM.
There were big plans to do this in like 2008 but they all fizzled out:

http://www.ruby-forum.com/topic/1450200

Thanks for the link. I see how the extensions would be a problem. How
difficult would it be to rewrite the core extensions to work with MVM?

It seems like finishing MVM would help us use native threads. Is that an
important problem to solve?

Might want to read this thread. It provides a bit of a post mortem on
MVM.
There were big plans to do this in like 2008 but they all fizzled out:

http://www.ruby-forum.com/topic/1450200