Skip to content

ffi_backend: convert numeric function args to pointers - #162

Merged
tenderlove merged 11 commits into
ruby:masterfrom
danini-the-panini:ds-ffi-fixes
Dec 11, 2024
Merged

ffi_backend: convert numeric function args to pointers#162
tenderlove merged 11 commits into
ruby:masterfrom
danini-the-panini:ds-ffi-fixes

Conversation

@danini-the-panini

Copy link
Copy Markdown
Contributor

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 jruby/jruby#8423)

@koukou left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a test for this case?

Comment threadlib/fiddle/ffi_backend.rb Outdated
@headius

Copy link
Copy Markdown

Passing integers as pointers might be a good feature to add, but patching it into JRuby led to failures in FFI specs that confirm integers are prevented from being passed as pointers. I've asked @danini-the-panini to propose it to ffi/ffi and we can discuss from there.

The workaround provided here will be needed until that discussion and feature can happen.

@danini-the-paninidanini-the-panini changed the title ffi_backend: convert int function args to pointersffi_backend: convert numeric function args to pointersDec 5, 2024
@danini-the-panini

Copy link
Copy Markdown
ContributorAuthor

I've also allowed any integer coercible to passed in as a pointer address, to align with native Fiddle

Comment threadlib/fiddle/ffi_backend.rb Outdated
Co-authored-by: Benoit Daloze <eregontp@gmail.com>
@larskanis

Copy link
Copy Markdown
Contributor

This looks good to me now.

@larskanis

Copy link
Copy Markdown
Contributor

The idea of accepting integers where a pointer is expected in a function call is more or less rejected in ffi/ffi#1130 . So this PR seems to be the way to proceed.

@headiusheadius left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Comment threadlib/fiddle/ffi_backend.rb Outdated
Comment threadlib/fiddle/ffi_backend.rb Outdated
Comment threadtest/fiddle/test_function.rb Outdated
Comment threadtest/fiddle/test_pointer.rb Outdated
assert_equal ptr, Pointer[0]
end

def test_to_ptr_with_num

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
deftest_to_ptr_with_num
deftest_to_ptr_with_float

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this isn't really testing float specific (see my comment below) but rather testing integer coercion. Perhaps this is a more apt name?

Suggested change
deftest_to_ptr_with_num
deftest_to_ptr_with_int_coercion

Comment threadtest/fiddle/test_pointer.rb Outdated

def test_to_ptr_with_num
ptr = Pointer.new 0
assert_equal ptr, Pointer[0.0]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, it may be better that we reject Float for address. Because Float address is invalid.

@tenderlove What do you think about this?

@danini-the-paninidanini-the-paniniDec 11, 2024

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only reason I added this test is to make sure the functionality of the FFI backend matches the C implementation. I just picked an arbitrary builtin that can be coerced into an integer 🤷🏻‍♀️ There might be other types that make more sense, or I could just create a dummy int-wrapper and use that instead, e.g.

IntWrapper=Struct.new(:value)dodefto_intvalueendend

and then

Suggested change
assert_equalptr,Pointer[0.0]
assert_equalptr,Pointer[IntWrapper.new(0)]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, this is just a test for coercion, I guess. @danini-the-panini maybe adding the wrapper will show the intention of the test better? I don't really have a strong opinion about float vs wrapper object as long as the intention of the test is clear. Either a comment or the wrapper class seems sufficient to me.

I assume this is calling to_int on things because it wants to accept other pointer instances as a parameter.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's calling rb_Integer in C, and Kernel#Integer in the FFI backend

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kou@tenderlove I've updated it to use a wrapper and renamed the test method

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's calling rb_Integer in C, and Kernel#Integer in the FFI backend

ah, gotcha.

@kou@tenderlove I've updated it to use a wrapper and renamed the test method

Thank you!

@tenderlove
tenderlove merged commit e2f0952 into ruby:masterDec 11, 2024
matzbot pushed a commit to ruby/ruby that referenced this pull request Dec 16, 2024
(ruby/fiddle#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
jruby/jruby#8423)
---------
ruby/fiddle@e2f0952e9b
Co-authored-by: Benoit Daloze <eregontp@gmail.com>
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants

@danini-the-panini@headius@larskanis@tenderlove@kou@eregon