From a23c6db5c52dccd670a530f08662adf485ab5151 Mon Sep 17 00:00:00 2001 From: Sutou Kouhei Date: Sat, 9 Nov 2024 15:33:24 +0900 Subject: [PATCH] [ruby/fiddle] Accept Symbol as Function name again GitHub: fix https://github.com/ruby/fiddle/pull/159 It's used by FFI test. So Symbol may be used by other use cases. https://github.com/ruby/fiddle/pull/139 introduced the "Function name is String" limitation. This commit removed the limitation. Reported by Mamoru TASAKA. Thanks!!! https://github.com/ruby/fiddle/commit/cea30fe5f9 --- ext/fiddle/function.c | 4 +++- test/fiddle/test_function.rb | 25 ++++++++++++++++++++++++- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/ext/fiddle/function.c b/ext/fiddle/function.c index 21a7ad6d23..3f574034f4 100644 --- a/ext/fiddle/function.c +++ b/ext/fiddle/function.c @@ -154,7 +154,9 @@ initialize(int argc, VALUE argv[], VALUE self) if (args[kw_name] != Qundef) { name = args[kw_name]; #ifdef HAVE_RB_STR_TO_INTERNED_STR - name = rb_str_to_interned_str(name); + if (RB_TYPE_P(name, RUBY_T_STRING)) { + name = rb_str_to_interned_str(name); + } #endif } if (args[kw_need_gvl] != Qundef) { diff --git a/test/fiddle/test_function.rb b/test/fiddle/test_function.rb index 6ec40bee06..146dcc7205 100644 --- a/test/fiddle/test_function.rb +++ b/test/fiddle/test_function.rb @@ -37,6 +37,11 @@ module Fiddle assert_equal 'sin', func.name end + def test_name_symbol + func = Function.new(@libm['sin'], [TYPE_DOUBLE], TYPE_DOUBLE, name: :sin) + assert_equal :sin, func.name + end + def test_need_gvl? if RUBY_ENGINE == "jruby" omit("rb_str_dup() doesn't exist in JRuby") @@ -261,7 +266,25 @@ module Fiddle def test_ractor_shareable omit("Need Ractor") unless defined?(Ractor) - assert_ractor_shareable(Function.new(@libm['sin'], [TYPE_DOUBLE], TYPE_DOUBLE)) + assert_ractor_shareable(Function.new(@libm["sin"], + [TYPE_DOUBLE], + TYPE_DOUBLE)) + end + + def test_ractor_shareable_name + omit("Need Ractor") unless defined?(Ractor) + assert_ractor_shareable(Function.new(@libm["sin"], + [TYPE_DOUBLE], + TYPE_DOUBLE, + name: "sin")) + end + + def test_ractor_shareable_name_symbol + omit("Need Ractor") unless defined?(Ractor) + assert_ractor_shareable(Function.new(@libm["sin"], + [TYPE_DOUBLE], + TYPE_DOUBLE, + name: :sin)) end private