1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

[ruby/fiddle] closure: add support for const char *

GitHub: fix GH-62

Reported by Cody Krieger. Thanks!!!

https://github.com/ruby/fiddle/commit/284b820f2d
This commit is contained in:
Sutou Kouhei 2020-12-25 06:02:19 +09:00 committed by Hiroshi SHIBATA
parent b2de5999d8
commit 881b2dc898
Notes: git 2021-05-18 12:49:06 +09:00
2 changed files with 21 additions and 0 deletions

View file

@ -130,6 +130,10 @@ with_gvl_callback(void *ptr)
rb_ary_push(params, ULL2NUM(*(unsigned LONG_LONG *)x->args[i]));
break;
#endif
case TYPE_CONST_STRING:
rb_ary_push(params,
rb_str_new_cstr(*((const char **)(x->args[i]))));
break;
default:
rb_raise(rb_eRuntimeError, "closure args: %d", type);
}
@ -175,6 +179,10 @@ with_gvl_callback(void *ptr)
*(unsigned LONG_LONG *)x->resp = NUM2ULL(ret);
break;
#endif
case TYPE_CONST_STRING:
/* Dangerous. Callback must keep reference of the String. */
*((const char **)(x->resp)) = StringValueCStr(ret);
break;
default:
rb_raise(rb_eRuntimeError, "closure retval: %d", type);
}

View file

@ -54,6 +54,19 @@ module Fiddle
assert_equal 10, func.call(10)
end
def test_const_string
closure_class = Class.new(Closure) do
def call(string)
@return_string = "Hello! #{string}"
@return_string
end
end
closure = closure_class.new(:const_string, [:const_string])
func = Function.new(closure, [:const_string], :const_string)
assert_equal("Hello! World!", func.call("World!"))
end
def test_block_caller
cb = Closure::BlockCaller.new(TYPE_INT, [TYPE_INT]) do |one|
one