-
Notifications
You must be signed in to change notification settings - Fork 40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ffi_backend: convert numeric function args to pointers #162
Conversation
There was a problem hiding this comment.
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?
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. |
7881b8e
to
b3a6c29
Compare
60ffd67
to
1e99098
Compare
I've also allowed any integer coercible to passed in as a pointer address, to align with native Fiddle |
1e99098
to
e4f1307
Compare
Co-authored-by: Benoit Daloze <eregontp@gmail.com>
This looks good to me now. |
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
test/fiddle/test_pointer.rb
Outdated
ptr = Pointer.new 0 | ||
assert_equal ptr, Pointer[0] | ||
end | ||
|
||
def test_to_ptr_with_num |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
def test_to_ptr_with_num | |
def test_to_ptr_with_float |
There was a problem hiding this comment.
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?
def test_to_ptr_with_num | |
def test_to_ptr_with_int_coercion |
test/fiddle/test_pointer.rb
Outdated
ptr = Pointer.new 0 | ||
assert_equal ptr, Pointer[0] | ||
end | ||
|
||
def test_to_ptr_with_num | ||
ptr = Pointer.new 0 | ||
assert_equal ptr, Pointer[0.0] |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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) do
def to_int
value
end
end
and then
assert_equal ptr, Pointer[0.0] | |
assert_equal ptr, Pointer[IntWrapper.new(0)] |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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, andKernel#Integer
in the FFI backend
ah, gotcha.
@kou @tenderlove I've updated it to use a wrapper and renamed the test method
Thank you!
1588897
to
091454a
Compare
(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>
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)