Question about AOP-like function calls interception

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Question about AOP-like function calls interception

Leo Romanoff
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 

Reply | Threaded
Open this post in threaded view
|

Re: Question about AOP-like function calls interception

Petite Abeille

On Jul 25, 2013, at 11:10 PM, Leo Romanoff <[hidden email]> wrote:

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

local _print = print

print = function( … )
  -- before...
  _print( … )
  -- after...
end

?!?

Out of curiosity… are you recent questions theoretical? or practical?


Reply | Threaded
Open this post in threaded view
|

Re: Question about AOP-like function calls interception

Coda Highland
In reply to this post by Leo Romanoff
On Thu, Jul 25, 2013 at 2:10 PM, Leo Romanoff <[hidden email]> wrote:

> 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
>

In Lua you can just monkey-patch functions. This won't help the case
where someone has cached a copy of the original function in a local
(which is good, because you'd need to do that yourself in order to
implement a transparent monkey-patch) but for exported functions you
can almost always get away with a monkey-patching.

If you want something more powerful, like the wildcard-based ones you
were citing, you'd have to patch the Lua core.

/s/ Adam

Reply | Threaded
Open this post in threaded view
|

Re: Question about AOP-like function calls interception

Luiz Henrique de Figueiredo
In reply to this post by Leo Romanoff
> I'd like to ask if there is an (easy) way to intercept invocation of a normal Lua function?

If it's a single or a few functions, just redefined them as posted earlier.

If the functions are part of a table, then you can try defining an index
metamethod for it.