A userdata object does not have fields like a table and Lua has no knowledge whatsoever about internals of the underlying C object. In order to achieve what you want, you'd have to handle the __index
and __newindex
metamethods.
So, for example, when doing the assignment like userdataObject.tableAttached = self
, the __newindex
metamethod is triggered. Inside it, you could just store the value in the metatable itself (subject to a possible name collision) or in another table, which itself is stored in the metatable.
To retrieve the data back, you'd have to handle the __index
metamethod. It can get a bit tricky with userdata, so let me know, if you run into problems.