lua-users home
lua-l archive

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


On 19/10/2009, at 12:02 PM, Peter Cawley wrote:

On Sun, Oct 18, 2009 at 11:46 PM, Duncan Cross <duncan.cross@gmail.com> 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.

Thanks Duncan and Peter,

You both confirmed what I thought - there's no really clean way to do, so my mess is sufficient for my purposes (debugging). I control the objects, so I don't set __metatable. I could use the C API and lua_topointer, but what I've got is ok, and bytecode hacks just sounds scary!

Cheers,
Geoff