lua-users home
lua-l archive

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


2009/5/14 Lloyd <lloyd@cdactvm.in>:
>  My application needs a continuously changing policy support. This policy
> will be implemented as Lua script. The policy will be changed only by the "C
> functions" which are called from the Lua script.As an example my initial
> policy may look like this:-
>
> if my_c_function1()==10 then
>  print("10 from my_c_function1")
> else
>  print("not 10 from my_c_function1")
> end
>
>
> Later I may need to add support for another function "my_c_function2()"
> "without recompiling my original application". So my new policy script would
> look like this:-
>
> if my_c_function1()==10 then
>  print("10")
> else
>  print("not 10")
> end
>
> if my_c_function2()==10 then
>  print("10 from my_c_function2")
> else
>  print("not 10 from my_c_function2")
> end
>
> Can I add this kind of functionality using Lua? I read the "C Libraries
> (http://www.lua.org/pil/26.2.html)" section in Lua book. Still I am not
> clear about extending without recompiling the main application code. Some
> hints would be very helpful.

If your functions are written in C, and you don't want to rebuild your
main program, you have to link these functions independently, in a
shared library. You can put them in a Lua C module, and load it at
runtime.

For the Lua script, you can replace it at runtime with another one. A
compiled script (with loadstring or loadfile) result in a Lua
function, that you then call. You can simply replace the function
(with the result of a new loadstring/loadfile) and call the new one.