1.什么是Lua
Lua [1] 是一个小巧的脚本语言。是巴西里约热内卢天主教大学(Pontifical Catholic University of Rio de Janeiro)里的一个研究小组,由Roberto Ierusalimschy、Waldemar Celes 和 Luiz Henrique de Figueiredo所组成并于1993年开发。 其设计目的是为了嵌入应用程序中,从而为应用程序提供灵活的扩展和定制功能。Lua由标准C编写而成,几乎在所有操作系统和平台上都可以编译,运行。Lua并没有提供强大的库,这是由它的定位决定的。所以Lua不适合作为开发独立应用程序的语言。Lua 有一个同时进行的JIT项目,提供在特定平台上的即时编译功能。
Lua脚本可以很容易的被C/C++ 代码调用,也可以反过来调用C/C++的函数,这使得Lua在应用程序中可以被广泛应用。不仅仅作为扩展脚本,也可以作为普通的配置文件,代替XML,ini等文件格式,并且更容易理解和维护。 [2] Lua由标准C编写而成,代码简洁优美,几乎在所有操作系统和平台上都可以编译,运行。 [2] 一个完整的Lua解释器不过200k,在目前所有脚本引擎中,Lua的速度是最快的。这一切都决定了Lua是作为嵌入式脚本的最佳选择。
2.在C/C++中如何与Lua交互(使用Visual Studio开发环境)
先下载lua的开源代码,并将其添加到项目中。
之后编译项目,如果编译不通过,需要移除lua.c和luac.c两个文件(这两个是用来编译lua代码的,与C/C++交互时不需要。)
如何在C/C++中执行lua脚本, 在Lua脚本中调用C函数:
lua_State* g_lua_State = NULL;
static void
reg_func2lua(const char* name, int(*CFunction)(lua_State* L)) {
lua_pushcfunction(g_lua_State, CFunction);
lua_setglobal(g_lua_State, name);
}
static void
CFunc(lua_State* L) {
const char* msg = lua_checkstring(g_lua_State, -1);
if (msg) {
printf("%s\n", msg);
}
return 0;
}
// init lua_state
static void
init_luaState() {
g_lua_State = luaL_newstate();
luaL_openlibs(g_lua_State);
// join this func in your code to push a CFunc into lua-stack,
// on this step you can use "lua_func_name"
// in your lua files to execute a C funtion
reg_func2lua("lua_func_name", CFunc);
}
// execute lua script
static bool
entry_lua_file(const char* file_name) {
// 如果luaL_dofile() 返回值不为0, 则有错误
if (luaL_dofile(g_lua_State, file_name)) {
return false;
}
return true;
}
// exit lua_state
static void
exit_lua_state() {
if (g_lua_State) {
lua_close(g_lua_State);
g_lua_State = NULL;
}
}
打印Lua日志(打印错误信息或执行的C函数行数)
static void
debug_log(const char* msg) {
lua_Debug ar;
int depth;
while (lua_getstack(g_lua_State, depth, &ar)) {
lua_getinfo(g_lua_State, "Snl", &ar);
if (ar.source[0] == '@') {
printf("%s %d %s\n", ar.source[1], ar.currentline, msg);
return;
}
++depth;
}
if (depth == 0) {
printf("thrunk %d %s\n", 0, msg);
}
}
有时不需要在lua脚本出错的时候清除掉解释器,我们需要自己写一个lua_panic函数
在init_luaState()初始化解释器的时候使用lua_atpanic()来覆盖掉默认的panic函数修改init的代码即可
static int
lua_panic(lua_State *L) {
const char* msg = luaL_checkstring(g_lua_State, -1);
if (msg) {
debug_log(msg);
}
return 0;
}
static void
init_luaState() {
g_lua_State = luaL_newstate();
lua_atpanic(g_lua_State, lua_panic);
luaL_openlibs(g_lua_State);
reg_func2lua("lua_file_name", CFunc);
}