diff options
| author | Dani Smith <code@danini.dev> | 2024-12-12 01:43:51 +0200 |
|---|---|---|
| committer | Hiroshi SHIBATA <hsbt@ruby-lang.org> | 2024-12-16 10:22:30 +0900 |
| commit | 7c260bd424c25755554885fb1bc25492e5df598f (patch) | |
| tree | 6be4705fe9d99ca9aa63ce423b418cca55369467 /test | |
| parent | cce7cffbd36d6d6c1d4b0dc2b5b08c6ce3dbb7c3 (diff) | |
[ruby/fiddle] ffi_backend: convert numeric function args to pointers
(https://github.com/ruby/fiddle/pull/162)
This allows for passing integers as pointer arguments to functions when
using the FFI backend. This is a workaround until we can get JRuby's FFI
implementation to allow for it directly (see also
https://github.com/jruby/jruby/pull/8423)
---------
https://github.com/ruby/fiddle/commit/e2f0952e9b
Co-authored-by: Benoit Daloze <eregontp@gmail.com>
Diffstat (limited to 'test')
| -rw-r--r-- | test/fiddle/test_function.rb | 9 | ||||
| -rw-r--r-- | test/fiddle/test_pointer.rb | 8 |
2 files changed, 16 insertions, 1 deletions
diff --git a/test/fiddle/test_function.rb b/test/fiddle/test_function.rb index 146dcc7205..b408a14ccd 100644 --- a/test/fiddle/test_function.rb +++ b/test/fiddle/test_function.rb @@ -98,6 +98,15 @@ module Fiddle assert_in_delta 1.0, func.call(90 * Math::PI / 180), 0.0001 end + def test_integer_pointer_conversion + func = Function.new(@libc['memcpy'], [TYPE_VOIDP, TYPE_VOIDP, TYPE_SIZE_T], TYPE_VOIDP) + str = 'hello' + Pointer.malloc(str.bytesize, Fiddle::RUBY_FREE) do |dst| + func.call(dst.to_i, str, dst.size) + assert_equal(str, dst.to_str) + end + end + def test_argument_count closure_class = Class.new(Closure) do def call one diff --git a/test/fiddle/test_pointer.rb b/test/fiddle/test_pointer.rb index 673e7ca445..9d490f9f26 100644 --- a/test/fiddle/test_pointer.rb +++ b/test/fiddle/test_pointer.rb @@ -157,11 +157,17 @@ module Fiddle end end - def test_to_ptr_with_num + def test_to_ptr_with_int ptr = Pointer.new 0 assert_equal ptr, Pointer[0] end + MimicInteger = Struct.new(:to_int) + def test_to_ptr_with_to_int + ptr = Pointer.new 0 + assert_equal ptr, Pointer[MimicInteger.new(0)] + end + def test_equals ptr = Pointer.new 0 ptr2 = Pointer.new 0 |
