ruby/fiddle

Closures cannot accept arguments of TYPE_CONST_STRING

codykrieger opened this issue · 4 comments

When attempting to invoke a closure with an argument of Fiddle::TYPE_CONST_STRING, I get this error:

test.rb:43:in `call': closure args: 10 (RuntimeError)

That's coming from here:

rb_raise(rb_eRuntimeError, "closure args: %d", type);

It appears that the switch in with_gvl_callback() doesn't handle TYPE_CONST_STRING.

kou commented

Could you show a Ruby script that reproduces this case?

Sure.

test.rb:

require 'fiddle'
require 'fiddle/import'

module MyModule
  extend Fiddle::Importer

  MyType = struct [
    %q{void (*foo)(const char *value)}
  ]
end

closure = Class.new(Fiddle::Closure) {
  def call(value)
    puts "value! #{value}"
  end
}.new(Fiddle::TYPE_VOID, [Fiddle::TYPE_CONST_STRING])

my_type = MyModule::MyType.malloc
my_type.foo = closure

# ...

fn = Fiddle::Function.new my_type.foo, [Fiddle::TYPE_CONST_STRING], Fiddle::TYPE_VOID
fn.call "bar"

Then:

% ruby test.rb
Traceback (most recent call last):
        1: from test.rb:24:in `<main>'
test.rb:24:in `call': closure args: 10 (RuntimeError)

It seems like the behavior is specific to when closures are wrapped in a Function.

kou commented

Thanks.
I've implemented.

Thanks @kou!