ruby-changes:66247
From: Sutou <ko1@a...>
Date: Tue, 18 May 2021 12:48:57 +0900 (JST)
Subject: [ruby-changes:66247] b2de5999d8 (master): [ruby/fiddle] closuRe: accept symbol as type
https://git.ruby-lang.org/ruby.git/commit/?id=b2de5999d8 From b2de5999d88d81310b3c9f0c1f14451d7eca8e6e Mon Sep 17 00:00:00 2001 From: Sutou Kouhei <kou@c...> Date: Fri, 25 Dec 2020 06:01:12 +0900 Subject: [ruby/fiddle] closure: accept symbol as type https://github.com/ruby/fiddle/commit/dc2da6633e --- ext/fiddle/closure.c | 18 ++++++++++++------ test/fiddle/test_closure.rb | 12 ++++++++++++ 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/ext/fiddle/closure.c b/ext/fiddle/closure.c index 40cee55..fc29655 100644 --- a/ext/fiddle/closure.c +++ b/ext/fiddle/closure.c @@ -221,6 +221,7 @@ initialize(int rbargc, VALUE argv[], VALUE self) https://github.com/ruby/ruby/blob/trunk/ext/fiddle/closure.c#L221 { VALUE ret; VALUE args; + VALUE normalized_args; VALUE abi; fiddle_closure * cl; ffi_cif * cif; @@ -239,21 +240,26 @@ initialize(int rbargc, VALUE argv[], VALUE self) https://github.com/ruby/ruby/blob/trunk/ext/fiddle/closure.c#L240 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); diff --git a/test/fiddle/test_closure.rb b/test/fiddle/test_closure.rb index 2de0660..6ccd385 100644 --- a/test/fiddle/test_closure.rb +++ b/test/fiddle/test_closure.rb @@ -20,6 +20,18 @@ module Fiddle https://github.com/ruby/ruby/blob/trunk/test/fiddle/test_closure.rb#L20 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 -- cgit v1.1 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/