lua-users home
lua-l archive

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


Try something like this :

lua_getglobal(L, LUA_LIBNAME); -- get the table of the library
lua_getfield(L, -1, "functionname"); -- get the old function
lua_pushcclosure(L, function, 1); -- push the overloaded function
                                  -- with upvalue to the old
lua_setfield(L, -2, "functionname); set it to the library table

So when your new function is called, you have the in an upvalue in the
closure so you can retrieve it at index lua_upvalueindex(1) in the
stack.

Hope this help you

Tom

On Wed, Sep 05, 2007 at 12:11:11PM +0200, Eva Schmidt wrote:
>
> Hello,
>
> I want to overload or extend an existing function (stored in the __index 
> table) from a library. I want to do this from my C-program, NOT from Lua.
>
> Maybe I don't see the woods for the trees, but how can I access this 
> metatable and manipulate a function in it?
> I want to extend a certain function at the time of initializing (after 
> opening the libraries) to connect this function call in Lua with a call of 
> one of my own functions...
>
> I think it must be something like that:
>
>
> 	lua_getglobal (L, LUA_LIBNAME); -- get the table of the library
> 	lua_pushstring (L, "functionname") -- push the name
> 	lua_pushcfunction (L, function) -- push the overloaded function
> 	lua_settable (L, -3) -- set it to the library table
>
> ...
>
> I think this should overwrite the whole function with my own code. But I 
> really want to extend the old function. Probably I need to call the old 
> function from my new?!
>
> Maybe somebody has already done something like that and has some hints or 
> ideas for me?
>
> Thanks a lot!
> Bye
> Eva
>

-- 
Thomas Lavergne                       "Le vrai rêveur est celui qui rêve
                                       de l'impossible."  (Elsa Triolet)
thomas.lavergne@reveurs.org                           http://reveurs.org