Uh oh!
There was an error while loading. Please reload this page.
ffi_backend: convert numeric function args to pointers - #162
Conversation
kou
left a comment
There was a problem hiding this comment.
Could you add a test for this case?
Uh oh!
There was an error while loading. Please reload this page.
headius
commented
Dec 4, 2024
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
b3a6c29Compare60ffd67 to
1e99098Comparedanini-the-panini
commented
Dec 5, 2024
I've also allowed any integer coercible to passed in as a pointer address, to align with native Fiddle |
1e99098 to
e4f1307CompareUh oh!
There was an error while loading. Please reload this page.
Co-authored-by: Benoit Daloze <eregontp@gmail.com>
larskanis
commented
Dec 10, 2024
This looks good to me now. |
larskanis
commented
Dec 10, 2024
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. |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
| assert_equal ptr, Pointer[0] | ||
| end | ||
| def test_to_ptr_with_num |
There was a problem hiding this comment.
| deftest_to_ptr_with_num | |
| deftest_to_ptr_with_float |
There was a problem hiding this comment.
this isn't really testing float specific (see my comment below) but rather testing integer coercion. Perhaps this is a more apt name?
| deftest_to_ptr_with_num | |
| deftest_to_ptr_with_int_coercion |
| def test_to_ptr_with_num | ||
| ptr = Pointer.new 0 | ||
| assert_equal ptr, Pointer[0.0] |
There was a problem hiding this comment.
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.
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_intvalueendendand then
| assert_equalptr,Pointer[0.0] | |
| assert_equalptr,Pointer[IntWrapper.new(0)] |
There was a problem hiding this comment.
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.
It's calling rb_Integer in C, and Kernel#Integer in the FFI backend
There was a problem hiding this comment.
@kou@tenderlove I've updated it to use a wrapper and renamed the test method
There was a problem hiding this comment.
It's calling
rb_Integerin C, andKernel#Integerin the FFI backend
ah, gotcha.
@kou@tenderlove I've updated it to use a wrapper and renamed the test method
Thank you!
1588897 to
091454aCompare(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)