Skip to content

Commit

Permalink
closure: accept symbol as type
Browse files Browse the repository at this point in the history
  • Loading branch information
kou committed Dec 24, 2020
1 parent 831522e commit dc2da66
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
18 changes: 12 additions & 6 deletions ext/fiddle/closure.c
Expand Up @@ -221,6 +221,7 @@ initialize(int rbargc, VALUE argv[], VALUE self)
{
VALUE ret;
VALUE args;
VALUE normalized_args;
VALUE abi;
fiddle_closure * cl;
ffi_cif * cif;
Expand All @@ -239,21 +240,26 @@ initialize(int rbargc, VALUE argv[], VALUE self)

cl->argv = (ffi_type **)xcalloc(argc + 1, sizeof(ffi_type *));

normalized_args = rb_ary_new_capa(argc);
for (i = 0; i < argc; i++) {
int type = NUM2INT(RARRAY_AREF(args, i));
cl->argv[i] = INT2FFI_TYPE(type);
VALUE arg = rb_fiddle_type_ensure(RARRAY_AREF(args, i));
rb_ary_push(normalized_args, arg);
cl->argv[i] = rb_fiddle_int_to_ffi_type(NUM2INT(arg));
}
cl->argv[argc] = NULL;

ret = rb_fiddle_type_ensure(ret);
rb_iv_set(self, "@ctype", ret);
rb_iv_set(self, "@args", args);
rb_iv_set(self, "@args", normalized_args);

cif = &cl->cif;
pcl = cl->pcl;

result = ffi_prep_cif(cif, NUM2INT(abi), argc,
INT2FFI_TYPE(NUM2INT(ret)),
cl->argv);
result = ffi_prep_cif(cif,
NUM2INT(abi),
argc,
rb_fiddle_int_to_ffi_type(NUM2INT(ret)),
cl->argv);

if (FFI_OK != result)
rb_raise(rb_eRuntimeError, "error prepping CIF %d", result);
Expand Down
12 changes: 12 additions & 0 deletions test/fiddle/test_closure.rb
Expand Up @@ -20,6 +20,18 @@ def test_argument_errors
end
end

def test_type_symbol
closure = Closure.new(:int, [:void])
assert_equal([
TYPE_INT,
[TYPE_VOID],
],
[
closure.instance_variable_get(:@ctype),
closure.instance_variable_get(:@args),
])
end

def test_call
closure = Class.new(Closure) {
def call
Expand Down

0 comments on commit dc2da66

Please sign in to comment.