Subject:
Re: [ruby-ffi] void pointer to a ruby object in a callback function
From:
Matijs van Zuijlen
Date:
10/30/11 6:50 AM
To:
ruby-ffi@googlegroups.com

Hi Mark,

You can't pass the actual address of the ruby object in memory like you can with regular C extensions.

However, you can pass some other identifier, as long as it's numeric, such as the object_id.

You could map back from the object_id to objects using _id2ref. However, if you want things to work with JRuby, I would recommend against it, since you need to set a special flag to enable it. Instead, store the mapping from object_id to object in a hash somewhere.

For example:

  MAPPING = {}
  o = Object.new
  MAPPING[o.object_id] = o
  YourLib.function_with_callback(FFI::Pointer.new(o.object_id)) do |id|
    obj = MAPPING[id]
    # now do something with obj, which is the same as o.
  end

On 10/30/2011 09:23 AM, Mark wrote:
I am trying to use an api that has a callback function. The api takes
a void pointer that is passes through to the callback function. Is it
possible to get access to a ruby object via that void pointer? I.e.
get a pointer to a ruby object or something functionally equivalent?

Kind regards,
-- 
Matijs