lua-users home
lua-l archive

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

On Wed, Sep 05, 2007 at 04:44:21PM +0200, Eva Schmidt wrote:
> 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
>> stack.
> 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...

with this method, when the function is called, _only_ your new function
is called. *But* your new function can call the old function, so you can
make your new function something this :

int myfunction(lua_State *L) {
	printf("your are in my function");
	lua_pushvalue(L, lua_upvalueindex(1));
	lua_call(L, lua_gettop(L) -1, 3);
	return 3;

You must adjust the number 3 to the number of return value of the
orginal function or use the lua multi return value.

The important thing is that the function called is your function, and
you have the arguments on the stack and the old function as an upvalue,
so you can call the old function with it's argument when you want.
And so you can do what you want with :
	- do nothing....
	- print a message like the example
	- forget the old function and do something different
but also :
	- modify the arguments
	- modify the return values of the old function

Lua give you a lot of power at this point. Think of it like overloading
a function in an OO language. When you overload a function in most OO
language you always have access of the old function with something like:
or something similar and you also ave access to the parameters.
But in lua you can also modify the type and number of parameters but
also of return values.

Reading your response I think you just want notification that the old
function is called. The example show you one way to do it. And do be
affraid, it's efficient, the function myfunction doesn't introduce a big
overhead to the call. But there is also other to do it by using
metatable for example. If you say us what exactly you want to do,
perhaps we can show you a better way to do it.


Thomas Lavergne                       "Le vrai rêveur est celui qui rêve
                                       de l'impossible."  (Elsa Triolet)