Passing blocks around in a C extension

A

Andre Nathan

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
 
T

ts

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
 
A

Andre Nathan

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
 
T

ts

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top