lua-users home
lua-l archive

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

Hello Thomas,

thanks for your mail. Just to make sure ..

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

In this case I don't overwrite the old function but only make sure, that whenever the new function is called, the old is available vie the upvalue, isn't it? But I would need it the other way: When the old function is called, my function should also be called...

I need this actually to get notified of a certain event and put something into my own mainloop due to this. I strictly try to avoid to kludge the library code (with a hook) because of incompabilities to future library versions...


Hope this help you


On Wed, Sep 05, 2007 at 12:11:11PM +0200, Eva Schmidt wrote:

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!



  \ /           Sol-3 GmbH & Co. KG             Eva Schmidt
 --o-- Sol-3    Eltviller Straße 2              Phone: ++49 (0) 6123 7029 23
  / \           D-65399 Kiedrich                Fax:   ++49 (0) 6123 7029 29
                USt-ID:   DE 204978307          eMail:
                Register: WI HRA 6607
                Komplementär:    Sol-3 Verwaltungs GmbH
                Register:        WI HRB 117786
                Geschäftsführer: Norbert Geus, Dirk Zoller