How do you fully Reinitialize an embedded ruby VM in a C (actually swift) application?

140 views Asked by At

I'm using the C Ruby Interface to embed a ruby interpreter in a swift application (though, that I'm using swift is irrelevant Imo)

I feel like Ruby must have some way of completely reinitializing itself.

I'm initializing the vm like so:

 var variable_in_this_stack_frame:UnsafeMutablePointer<VALUE>? = nil
 ruby_init_stack(variable_in_this_stack_frame)
 ruby_init()
 ruby_init_loadpath()
 rb_require("enc/encdb")
 rb_require("enc/trans/transdb")
 var node = ruby_options(Int32(options.count), &cargs)

evaluating some code:

var state: Int32 = 0;
if ruby_executable_node(node, &state) != 0 {
   state = ruby_exec_node(node)
}
if state != 0 {
   throw RubyError(err: RTypedValue(VALUE: rb_errinfo()))
}

and destroying the vm like so:

    var state: Int32 = 0
    ruby_cleanup(state)

however, second time around doing all this, I get EXC_BAD_ACCESS when running: ruby_init_loadpath()

how am I supposed to reinit?

1

There are 1 answers

0
ForeverZer0 On

It is not possible to reinitialize Ruby's VM after it has been closed, as ruby_cleanup and ruby_finalize do not return it to a pristine state that is reusable, and are only intended for shutting it down in an elegant manner.

The only real option is to spawn a new process, which may be less than ideal depending on your implementation. I am not 100% sure if attempting again on a different thread is a viable option, though I assume it is highly unlikely due to the the Ruby VM's single-threaded nature.