How do I access a ruby array from my c extension?

对着背影说爱祢 提交于 2019-12-21 05:55:13

问题


I'm getting this error

ev.c:11: error: subscripted value is neither array nor pointer

for this line

printf("%d\n", pairs[0][0]);

In this code

static VALUE EV;
static VALUE PairCounter;

static VALUE 
sort_pairs_2(VALUE self) {
    VALUE pairs;

    pairs = rb_ivar_get(self, rb_intern("pairs"));
    printf("%d\n", pairs[0][0]);
  return Qnil;
}

void Init_ev() {
    rb_eval_string("require './lib/ev/pair_counter'");
    VALUE PairCounter = rb_path2class("EV::PairCounter");
    rb_define_method(PairCounter, "sort_pairs_2", sort_pairs_2, 0);
}

Am I using self incorrectly, and rb_ivar_get is not actually pointing to the PairCounter class?


回答1:


I'm pretty sure you need to use the RARRAY_PTR macro on pairs to get at the underlying array; for example, the internal implementation of Array#push (for 1.9.2) looks like this:

static VALUE
rb_ary_push_1(VALUE ary, VALUE item)
{
    long idx = RARRAY_LEN(ary);

    if (idx >= ARY_CAPA(ary)) {
        ary_double_capa(ary, idx); 
    }
    RARRAY_PTR(ary)[idx] = item;
    ARY_SET_LEN(ary, idx + 1);   
    return ary;
}

The if just sorts out any necessary resizing, then there's RARRAY_PTR(ary)[idx] for accessing a single slot in the array.

I don't have any official references to back this up but hopefully this will be of some use.




回答2:


Ruby arrays are accessed with rb_ functions - not like normal C arrays.

Use rb_ary_entry

VALUE rb_ary_entry(VALUE self, long index")

Returns array self's element at index.

Reference:

http://ruby-doc.org/docs/ProgrammingRuby/html/ext_ruby.html

See a list of common Array functions under "Commonly Used Methods".



来源:https://stackoverflow.com/questions/6446148/how-do-i-access-a-ruby-array-from-my-c-extension

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!