10000 closure: add support for const char * · ruby/fiddle@284b820 · GitHub
[go: up one dir, main page]

Skip to content

Commit 284b820

Browse files
committed
closure: add support for const char *
GitHub: fix GH-62 Reported by Cody Krieger. Thanks!!!
1 parent dc2da66 commit 284b820

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

ext/fiddle/closure.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,10 @@ with_gvl_callback(void *ptr)
130130
rb_ary_push(params, ULL2NUM(*(unsigned LONG_LONG *)x->args[i]));
131131
break;
132132
#endif
133+
case TYPE_CONST_STRING:
134+
rb_ary_push(params,
135+
rb_str_new_cstr(*((const char **)(x->args[i]))));
136+
break;
133137
default:
134138
rb_raise(rb_eRuntimeError, "closure args: %d", type);
135139
}
@@ -175,6 +179,10 @@ with_gvl_callback(void *ptr)
175179
*(unsigned LONG_LONG *)x->resp = NUM2ULL(ret);
176180
break;
177181
#endif
182+
case TYPE_CONST_STRING:
183+
/* Dangerous. Callback must keep reference of the String. */
184+
*((const char **)(x->resp)) = StringValueCStr(ret);
185+
break;
178186
default:
179187
rb_raise(rb_eRuntimeError, "closure retval: %d", type);
180188
}

test/fiddle/test_closure.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,19 @@ def call thing
5454
assert_equal 10, func.call(10)
5555
end
5656

57+
def test_const_string
58+
closure_class = Class.new(Closure) do
59+
def call(string)
60+
@return_string = "Hello! #{string}"
61+
@return_string
62+
end
63+
end
64+
closure = closure_class.new(:const_string, [:const_string])
65+
66+
func = Function.new(closure, [:const_string], :const_string)
67+
assert_equal("Hello! World!", func.call("World!"))
68+
end
69+
5770
def test_block_caller
5871
cb = Closure::BlockCaller.new(TYPE_INT, [TYPE_INT]) do |one|
5972
one

0 commit comments

Comments
 (0)
0