I'm trying to understand how Ruby's Fiddle library works. I understand mostly how it interacts with libffi, but there's just one thing that's still baffling me: what on earth do Fiddle.dlwrap and Fiddle.dlunwrap do exactly?
The documentation just says
dlunwrap(addr)
Returns the hexadecimal representation of a memory pointer address
addr
dlwrap(val)
Returns a memory pointer of a function’s hexadecimal address location
val
(from ruby-doc.org)
I've tried experimenting with them passing in various different types of objects and strings. The methods always return values no matter what you pass in (not just hexadecimal strings of pointers to C functions). It seems that dlwrap is merely returning the memory address of the passed-in object, but two things don't make too much sense in this case:
- If I pass in a short string, and create a pointer using the result as the address, the memory it points to is not the string.
- If I pass in a number, it just returns the object ID of the number.
If anyone has some secret knowledge on the inner workings on Fiddle, and is willing to share, please help :)