amirrajan / rubymotion-applied

RubyMotion documentation provided by the community. Submit a pull request to the docs for a free one year indie subscription.
Apache License 2.0
49 stars 10 forks source link

Rubymotion header files export #15

Open amirrajan opened 6 years ago

amirrajan commented 6 years ago

Get a good suite of header files that will support Yoga FFIs and Ruby2D work.

amirrajan commented 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);

    /* ... */
}
amirrajan commented 6 years ago

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).