Passing blocks around in a C extension

Hi

I’m trying to wrap up a library, and one of its functions accepts a
callback function as an argument. What I want to do is to make the
function exported to ruby accept a block, and yield this block on the
callback function. So, to do that, I guess what I need is some way to do
the equivalent of

def foo(&block)

end

in C, ie, store the code block in a variable so I can pass it around.

I could make my code work using a Proc object explicitely as one of the
C function’s arguments, as in

static VALUE
my_func(…, VALUE block)
{

func_with_callback(…, my_callback, (void *)block);

}

static void
my_callback(void *arg)
{
rb_funcall((VALUE)arg, rb_intern(“call”), 0);
}

And from ruby I use it as

obj.my_func(proc { puts ‘blah’ })

How could I be able to call it as

obj.my_func { puts ‘blah’ }

and still be able to pass the code block to the callback function?

Thanks in advance,
Andre

“A” == Andre N. [email protected] writes:

A> def foo(&block)

In ruby, this can be seen like this

 def foo
    block = Proc.new if block_given?

A> …
A> end

A> in C, ie, store the code block in a variable so I can pass it around.

Proc.new is rb_block_proc()
block_given? is rb_block_given_p()

Guy Decoux

On Mon, 2006-08-28 at 02:06 +0900, ts wrote:

Proc.new is rb_block_proc()
block_given? is rb_block_given_p()

Thanks Guy!

Now, it must be something stupid, but this works:

func_with_callback(…, my_callback, (void *)rb_block_proc());

and this segfaults:

VALUE block = rb_block_proc();
func_with_callback(…, my_callback, (void *)block);

Any idea why? I don’t really need the second form, but it kinda puzzled
me.

Thanks again,
Andre

“A” == Andre N. [email protected] writes:

A> Any idea why?

Well, I must see the source but if the proc is called after the end of
the
C function it must be protected against the GC.

Guy Decoux