Lua newbie question

Started by
1 comment, last by mozi 17 years, 5 months ago
Hi there, I am working on a project which is written using c++. I am asked by my boss to test each components of the whole project using kind of scripting language such as Lua. As I know, Lua is a kind of embeded language, so is it possible to build a stand-alone Lua programme (filename.lua, for instance) inside which we can call a c/c++ function? Any ideas to use Lua to test the project? I am totally new to Lua, any suggestions will be highly appreciated! Thanks in advance mozi
Advertisement
Indeed you can call c(++) from stand-alone Lua; especially with the "module" system introduced in Lua 5.1. However you can not simply call _any_ c(++) code, for each function you want to call from Lua you need a wrapper function, which is actually exported.
The purpose of these functions is to push/pop ~values~ on/off the Lua stack, the only way
data can be exchanged between the languages.

I found the online version of 'Programming in Lua' http://www.lua.org/pil/ very helpful;
also note: http://lua-users.org/wiki/

There is software that will generate those wrappers for you (given some input-definition);
LuaBind is 'the big one' (which goes beyond my own limited understanding of Lua) doing it all and probably more.
ToLua++ can also be a good choice; in fact there are many options, I even generated some of my binding code with m4.

To actually (try to) answer your question: if the goal is to make tests of otherwise Lua-less c++ code, imo you might be better off by writing them in c++ as well.
Unless you can find a quick way to generate the exported code for all of your code I would think it too time-consuming just for writing tests in Lua.
Quote:Original post by Anonymous Poster
Indeed you can call c(++) from stand-alone Lua; especially with the "module" system introduced in Lua 5.1. However you can not simply call _any_ c(++) code, for each function you want to call from Lua you need a wrapper function, which is actually exported.
The purpose of these functions is to push/pop ~values~ on/off the Lua stack, the only way
data can be exchanged between the languages.

I found the online version of 'Programming in Lua' http://www.lua.org/pil/ very helpful;
also note: http://lua-users.org/wiki/

There is software that will generate those wrappers for you (given some input-definition);
LuaBind is 'the big one' (which goes beyond my own limited understanding of Lua) doing it all and probably more.
ToLua++ can also be a good choice; in fact there are many options, I even generated some of my binding code with m4.

To actually (try to) answer your question: if the goal is to make tests of otherwise Lua-less c++ code, imo you might be better off by writing them in c++ as well.
Unless you can find a quick way to generate the exported code for all of your code I would think it too time-consuming just for writing tests in Lua.



Many thanks indeed. they are really invaluable information to me. I will have a look at LuaBind and ToLua++. I totoally agree with you --- writing a c++ code for the test will be more easier than Lua, don't know what my stupid boss thinks in his mind!

This topic is closed to new replies.

Advertisement