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

Fiddle::Function responds to to_proc

This lets us cast a Fiddle::Function to a block, allowing is to write
things like:

```ruby
f = Fiddle::Function.new(@libc['strcpy'], [TYPE_VOIDP, TYPE_VOIDP], TYPE_VOIDP)
define_method :strcpy, &f
```
This commit is contained in:
Aaron Patterson 2021-02-26 09:57:13 -08:00
parent cfc23903df
commit 0590e9b677
No known key found for this signature in database
GPG key ID: 953170BCB4FFAFC6
2 changed files with 18 additions and 0 deletions

View file

@ -19,5 +19,10 @@ module Fiddle
def to_i
ptr.to_i
end
# Turn this function in to a proc
def to_proc
lambda { |*args| self.call(*args) }
end
end
end

View file

@ -102,6 +102,19 @@ module Fiddle
assert_equal("123", str.to_s)
end
def call_proc(string_to_copy)
buff = +"000"
str = yield(buff, string_to_copy)
[buff, str]
end
def test_function_as_proc
f = Function.new(@libc['strcpy'], [TYPE_VOIDP, TYPE_VOIDP], TYPE_VOIDP)
buff, str = call_proc("123", &f)
assert_equal("123", buff)
assert_equal("123", str.to_s)
end
def test_nogvl_poll
# XXX hack to quiet down CI errors on EINTR from r64353
# [ruby-core:88360] [Misc #14937]