转载自:http://www.cppblog.com/lxyfirst/archive/2008/10/29/65447.html
lua作为小巧精悍的脚本语言,易于嵌入c/c++中 , 广泛应用于游戏AI ,实际上在任何经常变化的逻辑上都可以使用lua实现,配合c/c++实现的底层接口服务,能够大大降低系统的维护成本。下面对lua和c/c++的交互调用做一个实例分析:
lua提供了API用于在c/c++中构造lua的运行环境,相关接口如下:
//创建lua运行上下文
lua_State luaL_newstate(void) ;
//加载lua脚本文件
int luaL_loadfile(lua_State L, const char filename);
lua 和c/c++的数据交互通过"栈"进行 ,操作数据时,首先将数据拷贝到"栈"上,然后获取数据,栈中的每个数据通过索引值进行定位,索引值为正时表示相对于栈底的偏移索引,索引值为负时表示相 对于栈顶的偏移索引,索引值以1或-1为起始值,因此栈顶索引值永远为-1 ,栈底索引值永远为1 。 "栈"相当于数据在lua和c/c++之间的中转地。每种数据都有相应的存取接口 。
数据入"栈"接口:
void (lua_pushnil) (lua_State L);
void (lua_pushnumber) (lua_State L, lua_Number n);
void (lua_pushinteger) (lua_State L, lua_Integer n);
void (lua_pushlstring) (lua_State L, const char s, size_t l);
void (lua_pushstring) (lua_State L, const char s);
void (lua_pushboolean) (lua_State L, int b);
void (lua_pushcclosure) (lua_State L, lua_CFunction fn, int n);
数据获取接口:
lua_Number (lua_tonumber) (lua_State L, int idx);
lua_Integer (lua_tointeger) (lua_State L, int idx);
int (lua_toboolean) (lua_State L, int idx);
const char (lua_tolstring) (lua_State L, int idx, size_t len);
lua_CFunction (lua_tocfunction) (lua_State L, int idx);
"栈"操作接口:
int (lua_gettop) (lua_State L);
void (lua_settop) (lua_State L, int idx);
void (lua_pushvalue) (lua_State L, int idx);
void (lua_remove) (lua_State L, int idx);
void (lua_insert) (lua_State L, int idx);
void (lua_replace) (lua_State L, int idx);
int (lua_checkstack) (lua_State L, int sz);
lua中定义的变量和函数存放在一个全局table中,索引值为LUA_GLOBALSINDEX ,table相关操作接口:
void (lua_gettable) (lua_State L, int idx);
void (lua_getfield) (lua_State L, int idx, const char k);
void (lua_settable) (lua_State L, int idx);
void (lua_setfield) (lua_State L, int idx, const char k);
当"栈"中包含执行脚本需要的所有要素(函数名和参数)后,调用lua_pcall执行脚本:
int (lua_pcall) (lua_State *L, int nargs, int nresults, int errfunc);
下面进行实例说明:
func.lua
1 --变量定义
2 width=1 ;
3 height=2 ;
4 --lua函数定义,实现加法
5 function sum(a,b)
6 return a+b ;
7 end
8 --lua函数定义,实现字符串相加
9 function mystrcat(a,b)
10 return a..b ;
11 end
12 --lua函数定义,通过调用c代码中的csum函数实现加法
13 function mysum(a,b)
14 return csum(a,b) ;
15 end
test_lua.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <errno.h>
5 //lua头文件
6 #include <lua.h>
7 #include <lualib.h>
8 #include <lauxlib.h>
9
10
11 #define err_exit(num,fmt,args) \
12 do{printf("[%s:%d]"fmt"\n",__FILE__,__LINE__,##args);exit(num);} while(0)
13 #define err_return(num,fmt,args) \
14 do{printf("[%s:%d]"fmt"\n",__FILE__,__LINE__,##args);return(num);} while(0)
15
16 //lua中调用的c函数定义,实现加法
17 int csum(lua_State* l)
18 {
19 int a = lua_tointeger(l,1) ;
20 int b = lua_tointeger(l,2) ;
21 lua_pushinteger(l,a+b) ;
22 return 1 ;
23 }
24
25 int main(int argc,char** argv)
26 {
27 lua_State * l = luaL_newstate() ; //创建lua运行环境
28 if ( l == NULL ) err_return(-1,"luaL_newstat() failed");
29 int ret = 0 ;
30 ret = luaL_loadfile(l,"func.lua") ; //加载lua脚本文件
31 if ( ret != 0 ) err_return(-1,"luaL_loadfile failed") ;
32 ret = lua_pcall(l,0,0,0) ;
33 if ( ret != 0 ) err_return(-1,"lua_pcall failed:%s",lua_tostring(l,-1)) ;
34
35 lua_getglobal(l,"width"); //获取lua中定义的变量
36 lua_getglobal(l,"height");
37 printf("height:%ld width:%ld\n",lua_tointeger(l,-1),lua_tointeger(l,-2)) ;
38 lua_pop(l,1) ; //恢复lua的栈
39
40 int a = 11 ;
41 int b = 12 ;
42 lua_getglobal(l,"sum"); //调用lua中的函数sum
43 lua_pushinteger(l,a) ;
44 lua_pushinteger(l,b) ;
45 ret = lua_pcall(l,2,1,0) ;
46 if ( ret != 0 ) err_return(-1,"lua_pcall failed:%s",lua_tostring(l,-1)) ;
47 printf("sum:%d + %d = %ld\n",a,b,lua_tointeger(l,-1)) ;
48 lua_pop(l,1) ;
49
50 const char str1[] = "hello" ;
51 const char str2[] = "world" ;
52 lua_getglobal(l,"mystrcat"); //调用lua中的函数mystrcat
53 lua_pushstring(l,str1) ;
54 lua_pushstring(l,str2) ;
55 ret = lua_pcall(l,2,1,0) ;
56 if ( ret != 0 ) err_return(-1,"lua_pcall failed:%s",lua_tostring(l,-1)) ;
57 printf("mystrcat:%s%s = %s\n",str1,str2,lua_tostring(l,-1)) ;
58 lua_pop(l,1) ;
59
60 lua_pushcfunction(l,csum) ; //注册在lua中使用的c函数
61 lua_setglobal(l,"csum") ; //绑定到lua中的名字csum
62
63 lua_getglobal(l,"mysum"); //调用lua中的mysum函数,该函数调用本程序中定义的csum函数实现加法
64 lua_pushinteger(l,a) ;
65 lua_pushinteger(l,b) ;
66 ret = lua_pcall(l,2,1,0) ;
67 if ( ret != 0 ) err_return(-1,"lua_pcall failed:%s",lua_tostring(l,-1)) ;
68 printf("mysum:%d + %d = %ld\n",a,b,lua_tointeger(l,-1)) ;
69 lua_pop(l,1) ;
70
71 lua_close(l) ; //释放lua运行环境
72 return 0 ;
73 }
原文链接: https://www.cnblogs.com/zhangzhang/archive/2013/01/10/2854886.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/75099
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!