// lua.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <stdio h=""> #include <stdlib h=""> #include <string h=""> #include <errno h=""> #include <assert h=""> //lua头文件 extern "C" //在c++中调用C库 { #include <lua h=""> #include <lualib h=""> #include <lauxlib h=""> }; #define TOSTRING(P) #P //转成字符串 #define err_exit(num,fmt,args) / do{printf("[%s:%d]"fmt"/n",__FILE__,__LINE__,##args);exit(num);} while(0) #define err_return(num,fmt,args) / do{printf("[%s:%d]"fmt"/n",__FILE__,__LINE__,##args);return(num);} while(0) //#define exampleNum(n) num##n //int num9=9; //int num=exampleNum(9); //将会扩展成 int num=num9; //lua中调用的c函数定义,实现加法 int csum(lua_State* L) { int a = lua_tointeger(L,1); int b = lua_tointeger(L,2); lua_pushinteger(L,a+b); return 1 ; } int _tmain(int argc, _TCHAR* argv[]) { lua_State * L = luaL_newstate() ; //创建lua运行环境 if (L == NULL) { printf("创建lua运行环境出错!!!/n"); } luaL_openlibs(L); //加载所有lua库,很重要,没这个语句不能用table.getn等 int ret = 0 ; ret = luaL_loadfile(L,"func.lua") ; //加载lua脚本文件 if ( ret != 0 ) { printf("加载lua脚本文件出错!!!/n"); } ret = lua_pcall(L,0,0,0) ; //这句的作用应该是初始化lua栈,和重新加载函数的作用建议luaL_loadfile后都调用一次 if ( ret != 0 ) err_return(-1,"lua_pcall failed:%s",lua_tostring(L,-1)) ; //lua_pushcfunction(L,csum) ; //注册在lua中使用的c函数 //lua_setglobal(L,"csum") ; //绑定到lua中的名字csum //相当于给全局变量赋值 //lua_register(L,TOSTRING(csum),csum); //和上面两句作用相同 //可以注册成库的形式,比上面单个注册来的方便 luaL_Reg Reg[] = { {TOSTRING(csum),csum}, {NULL,NULL}, //这里一定要的不然会有问题,因为它不知道什么时候结束 }; luaL_register(L,"mylib",Reg); lua_getglobal(L,"width"); //获取lua中定义的变量 lua_getglobal(L,"height"); printf("height:%ld width:%ld/n",lua_tointeger(L,-1),lua_tointeger(L,-2)) ; lua_pop(L,2) ; //恢复lua的栈 //小心bug lua_getglobal会入栈 lua_pushinteger(L,15); //push操作都会入栈 lua_setglobal(L,"width"); //此操作会平衡栈,出栈 lua_pushinteger(L,16); lua_setglobal(L,"height"); //设置lua中定义的变量 lua_getglobal(L,"width"); //获取lua中定义的变量 lua_getglobal(L,"height"); printf("height:%ld width:%ld/n",lua_tointeger(L,-1),lua_tointeger(L,-2)) ; lua_pop(L,2) ; //恢复lua的栈 //小心bug lua_getglobal会入栈 int a = 11 ; int b = 12 ; lua_getglobal(L,"sum"); //调用lua中的函数sum lua_pushinteger(L,a) ; lua_pushinteger(L,b) ; ret = lua_pcall(L,2,1,0) ; if ( ret != 0 ) err_return(-1,"lua_pcall failed:%s",lua_tostring(L,-1)) ; printf("sum:%d + %d = %ld/n",a,b,lua_tointeger(L,-1)) ; lua_pop(L,1) ; const char str1[] = "hello" ; const char str2[] = "world" ; lua_getglobal(L,"mystrcat"); //调用lua中的函数mystrcat lua_pushstring(L,str1) ; lua_pushstring(L,str2) ; ret = lua_pcall(L,2,1,0) ; if ( ret != 0 ) err_return(-1,"lua_pcall failed:%s",lua_tostring(L,-1)) ; printf("mystrcat:%s%s = %s/n",str1,str2,lua_tostring(L,-1)) ; lua_pop(L,1) ; lua_getglobal(L,"mysum"); //调用lua中的mysum函数,该函数调用本程序中定义的csum函数实现加法 lua_pushinteger(L,a) ; lua_pushinteger(L,b) ; ret = lua_pcall(L,2,1,0) ; if ( ret != 0 ) err_return(-1,"lua_pcall failed:%s",lua_tostring(L,-1)) ; printf("mysum:%d + %d = %ld/n",a,b,lua_tointeger(L,-1)) ; lua_pop(L,1) ; /下面是table的用法/ lua_getglobal(L,"retargnum"); //调用lua中的函数retargnum lua_newtable(L); lua_pushnumber(L,8); //压入key lua_pushstring(L,"w"); //压入Value lua_settable(L,-3); //只能一次一次的压入 lua_pushnumber(L,7); //压入key lua_pushstring(L,"w2"); //压入Value lua_settable(L,-3); ret = lua_pcall(L,1,1,0) ; if ( ret != 0 ) err_return(-1,"lua_pcall failed:%s",lua_tostring(L,-1)) ; printf("retargnum[1] ret = %s/n",lua_tostring(L,-1)) ; lua_pop(L,1); // ret = luaL_loadfile(L,"func2.lua") ; //加载lua脚本文件 if ( ret != 0 ) { printf("加载lua脚本文件出错!!!/n"); } ret = lua_pcall(L,0,0,0) ; //如果没有这句的话retargnum将会调用以前lua的同名函数 if ( ret != 0 ) err_return(-1,"lua_pcall failed:%s",lua_tostring(L,-1)) ; //覆盖了func.lua里面的retargnum lua_getglobal(L,"retargnum"); //调用lua中的函数retargnum lua_newtable(L); //以数字为下标的数组 lua_pushinteger(L,8); //push value lua_rawseti(L,-2,1); //下标从1开始 lua_pushinteger(L,7); //push value lua_rawseti(L,-2,2); //下标从1开始 ret = lua_pcall(L,1,1,0) ; if ( ret != 0 ) err_return(-1,"lua_pcall failed:%s",lua_tostring(L,-1)) ; printf("retargnum[1] ret = %d/n",lua_tointeger(L,-1)) ; lua_pop(L,1); lua_getglobal(L,"mysum"); //调用lua中的mysum函数,该函数调用本程序中定义的csum函数实现加法 lua_pushinteger(L,a) ; lua_pushinteger(L,b) ; ret = lua_pcall(L,2,1,0) ; if ( ret != 0 ) err_return(-1,"lua_pcall failed:%s",lua_tostring(L,-1)) ; printf("mysum:%d + %d = %ld/n",a,b,lua_tointeger(L,-1)) ; lua_pop(L,1) ; lua_getglobal(L,"retglobalwidth"); // ret = lua_pcall(L,0,1,0) ; if ( ret != 0 ) err_return(-1,"lua_pcall failed:%s",lua_tostring(L,-1)) ; printf("retglobalwidth = %d/n",lua_tointeger(L,-1)) ; lua_pop(L,1) ; lua_close(L) ; //释放lua运行环境 getchar(); return 0 ; }