lua-users home
lua-l archive

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


Thanks for your attempt to help. I am afraid, however, that you opened more questions than provided answers to my problem. Reading the Lua manual can be very frustrating. There are no code snippets given except pushing some number values around. But how to use the push_cclosure functions, lua_getmetatable, etc. in general usecases completely stays in the dark (except one tries to understands a l l of Lua in the complete picture)

Your new metamethods are closures, and as such they have an
environment, and upvalues, even if implemented in C. Therefore you can
save a reference to the previous metamethod in one of these places.

The thing is that I do not have any idea how to get the function pointer to the old __index function from Lua. The metatable I am referring to has been created using some cryptic wrapper code. The object is some userdata. In order to get the __index method handle I would need to create the userdata, push it to the stack, then get its metatable and push it to the stack. Then I would need to get the field __index of this metatable and somehow obtain the function pointer. But since I have no idea how to create the userdata (without digging into the wrapper code) I can only get it by calling a Lua script. However, I could pass Lua code that creates local variables. But afterwards examining the variables is not possible since they are supposed to be garbage collected.

You can use a Lua script such as the one you propose, but instead of
making the new functions accessible in a global variables, you can
pass them as parameters to the script. To pass them from C, push the
functions on the stack before calling the script, and pass appropriate
arguments to lua_call. From within the script, to access the
parameters passed when the script was called you have to use the three
dots "...". You would simply prefix the script with a line or two like
:

local myown_index_method,myown_newindex_method = ...

I wanted to try this workflow by simply pushing a number from C to a Lua script that calls the print function. That means, I should pass "print(...)" to luaL_loadstring and before I have to push the number. Well, it failed naturally. Do you have some code snippets from which I learn how to use the interface?