Open amirrajan opened 6 years ago
Another thing about Ruby's C api is that it had a way to alloc and free data structures via the
Data_Wrap_Structure
methods. I don't see this functionality ported to RM unless there is another mechanism RM uses. Here is an example of using it in mRuby:
#include <stdlib.h>
void foo_free(int* data)
{
free(data);
}
VALUE foo_alloc(VALUE self)
{
/* allocate */
int* data = malloc(sizeof(int));
/* wrap */
return Data_Wrap_Struct(self, NULL, foo_free, data);
}
VALUE foo_m_initialize(VALUE self, VALUE val)
{
int* data;
/* unwrap */
Data_Get_Struct(self, int, data);
*data = NUM2INT(val);
return self;
}
void some_func()
{
/* ... */
VALUE cFoo = rb_define_class("Foo", rb_cObject);
rb_define_alloc_func(cFoo, foo_alloc);
rb_define_method(cFoo, "initialize", foo_m_initialize, 1);
/* ... */
}
Cool, for reference, I haven't used any FFI stuff before, just direct native extensions and stuff. Also I haven't worked on the RM internals. Sounds like getting the headers together and finding the delta with the CRuby API and triaging/prioritizing is the way to go. Agree that memory allocation/releasing will probably be the hardest, and both CRuby and MRuby have some weird methods there, but at a minimum, just being able to attach a native memory reference to a Ruby object is key (and registering a "free" method when the Ruby objects gets GCed).
Get a good suite of header files that will support Yoga FFIs and Ruby2D work.