8000 closure: accept symbol as type · ruby/fiddle@dc2da66 · GitHub
[go: up one dir, main page]

Skip to content

Commit dc2da66

Browse files
committed
closure: accept symbol as type
1 parent 831522e commit dc2da66

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

ext/fiddle/closure.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ initialize(int rbargc, VALUE argv[], VALUE self)
221221
{
222222
VALUE ret;
223223
VALUE args;
224+
VALUE normalized_args;
224225
VALUE abi;
225226
fiddle_closure * cl;
226227
ffi_cif * cif;
@@ -239,21 +240,26 @@ initialize(int rbargc, VALUE argv[], VALUE self)
239240

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

243+
normalized_args = rb_ary_new_capa(argc);
242244
for (i = 0; i < argc; i++) {
243-
int type = NUM2INT(RARRAY_AREF(args, i));
244-
cl->argv[i] = INT2FFI_TYPE(type);
245+
VALUE arg = rb_fiddle_type_ensure(RARRAY_AREF(args, i));
246+
rb_ary_push(normalized_args, arg);
247+
cl->argv[i] = rb_fiddle_int_to_ffi_type(NUM2INT(arg));
245248
}
246249
cl->argv[argc] = NULL;
247250

251+
ret = rb_fiddle_type_ensure(ret);
248252
rb_iv_set(self, "@ctype", ret);
249-
rb_iv_set(self, "@args", args);
253+
rb_iv_set(self, "@args", normalized_args);
250254

251255
cif = &cl->cif;
252256
pcl = cl->pcl;
253257

254-
result = ffi_prep_cif(cif, NUM2INT(abi), argc,
255-
INT2FFI_TYPE(NUM2INT(ret)),
256-
cl->argv);
258+
result = ffi_prep_cif(cif,
259+
NUM2INT(abi),
260+
argc,
261+
rb_fiddle_int_to_ffi_type(NUM2INT(ret)),
262+
cl->argv);
257263

258264
if (FFI_OK != result)
259265
rb_raise(rb_eRuntimeError, "error prepping CIF %d", result);

test/fiddle/test_closure.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,18 @@ def test_argument_errors
2020
end
2121
end
2222

23+
def test_type_symbol
24+
closure = Closure.new(:int, [:void])
25+
assert_equal([
26+
TYPE_INT,
27+
[TYPE_VOID],
28+
],
29+
[
30+
closure.instance_variable_get(:@ctype),
31+
closure.instance_variable_get(:@args),
32+
])
33+
end
34+
2335
def test_call
2436
closure = Class.new(Closure) {
2537
def call

0 commit comments

Comments
 (0)
0