lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

On Sun, Oct 18, 2009 at 11:46 PM, Duncan Cross <> wrote:
> Not if the metatable has __metatable set to protect it from being
> changed. In that case, you could use
> debug.getmetatable/debug.setmetatable instead.
> If you have access to the C API rather than looking for a pure-Lua
> solution, you could add a Lua function that wraps lua_topointer() to
> get the pointers for objects. I think this:

If you don't have access to the C API, or the debug table, then you
can (in theory) use bytecode hacks to get the address of any (garbage
collectable) object, provided that you are using the standard Lua 5.1
interpreter / library, and sizeof(lua_Number) >= sizeof(void*). In
short, OP_FORLOOP will interpret its arguments as lua_Numbers even
when they are not, so a void* is reinterpreted as a number, said
number is returned to Lua, and then we can perform some work to
reinterpret it back to a void*. This method is at least an order of
magnitude muckier than the method in the OP, but it could allow you to
get addresses in situations you otherwise wouldn't. If you're
interested, I can try and write some proof of concept code rather than
just theorising, but I'm guessing that nobody needs code to do this.