lua-users home
lua-l archive

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

The approach I took to extending the functionality of userdata with lua
code is quite simple, but not totally transparent. I just aggregate the
userdata into a table. This is similar to the approach you mention,
except I didn't try to make it work like inheritance via metatables
since I cant see how it can work. Consider the following fictional code:

-- create a userdata object
local ud = system.createUD()

-- create the enclosing table
local wrapper = { userdata = ud }
setmetatable(wrapper, getmetatable(ud))

-- Now, if ud has a method "methodOne", the following call succeeds
since methodOne is found in the metatable of wrapper

but the first parameter to the C function that implements methodOne is
not the userdata, but the table. So the function fails.
It would work for userdata functions that don't expect the userdata as
the first parameter, but that is not the usual situation. The only way I
can get the above code to work is with:


Which defeats the entire purpose of setting the userdata metatable into
the table in the first place.

I am still a Lua newb though, so there may well be tried and tested ways
of extending userdata with additional lua functions. If there are, then
I am really interested in using them.

- DC