lua-users home
lua-l archive

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


Hi,

I'd like to ask if there is an (easy) way to intercept invocation of a normal Lua function?

As far as I understand, it is easy to intercept calls done on Lua tables by means of a defining a __call method in the metatable.
But if you just want to intercept a simple function call, the only way I've found in the mailing lists is to use the debug APIs and do some tricks.
Is it a correct understanding? If so, I'd like to understand better the following:
- Are there any other ways to achieve it in the meantime?
- What is the reason that it is so easy to intercept a call on a Lua table, but so difficult to intercept a simple function call? Is it due to performance reasons, because such a global "call" hook would slow down all function calls? Or may be there are some other reasons?

The reason why I'm asking is to see how something like AOP (Aspect Oriented Programming) approach could be applied to Lua. For example, I'd like to be able to execute a custom code before or after a call, manipulate call arguments and results, etc. And I'd like to be able to specify what I want to intercept (e.g. using patterns like this: "all calls of functions that look like set* or libname.*") outside of the code, where interception is supposed to be applied. That is the source code should not be aware of interception if possible. Normally it is achieved by means of interception or instrumentation in most languages. I'm wondering how and if this can be done in Lua. 

May be someone has played with such ideas applied to Lua already? It would be interesting to hear about experiences with it.

Thanks,
  Leo