[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Newby question: How to change __index userdata method in C?
- From: Sebastian Wolff <sw@...>
- Date: Thu, 26 Nov 2009 12:27:00 +0100
Hi list,
I am pretty new to Lua and do have only experience wrapping C libraries
using SWIG and luabind. Both are great tools. Now I have a problem
where I have to write custom binding code and where I do not have any
idea how to solve it:
I want to write __index and __newindex methods which accept multiple
data types as a key, eg. I like to use
object[0] = number; object[{1,2}] = number; etc.
The thing is: the bindings of "object" have been created using one of
the mentioned wrappers which do not support such an extended way of
keys. What I want to do is to change the __index and __newindex methods
after the actual registration of my class. I already found out how this
could be done in Lua:
function myown_index_method(table,key)
if (type(key) == "number") then
-- do something elseif (... other types, eg. tables ...)
...
else
return __oldindex(table,key)
end
end
local metaarray = getmetatable(MyClass())
__oldindex = metaarray.__index
metaarray.__index = myown_index_method(table,key)
(analogously for __newindex)
The problem is: I would like to implement this on C level for
performance. Now I know how to write the new __index and __newindex
methods in C.
But what I do not know is:
(1) How to call the original __index/__newindex methods from their new
C replacements? Particularly I do not want to create global Lua
variables such as '__oldindex' in the example. Maybe a C function
pointer?
(2) How to set the __index/__newindex to the new methods? I think I
could register them manually in their own scope and then set them using
a Lua script such as the above. But then, again, I would waste the
namespace.
Does anyone have a simple idea how to solve this?
Thanks !