Re: A way to associate a Lua value with a FFI pointer?

  • From: Richard Hundt <richardhundt@xxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Wed, 09 Apr 2014 11:02:02 +0200

On 4/9/14 10:54 AM, Daniel Kolesa wrote:
> Hello,
> 
> I would like to know if there is any way to associate a generic Lua
> value with a FFI pointer.

I'm not sure if I have the full context, but I do this sort of thing:

```
local reg = { }
function setptrvalue(ptr, val)
   local key = tonumber(ffi.cast('intptr_t', ptr))
   reg[key] = val
end
function getptrvalue(ptr)
   local key = tonumber(ffi.cast('intptr_t', ptr))
   return reg[key]
end
```

You can always get your pointer back from `intptr_t` with a cast.

Other related posts: