123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320 |
- // LuaTest.cpp : 定义控制台应用程序的入口点。
- //
- #include "stdafx.h"
- #include "LuaTest.h"
- extern "C" {
- #include "lua.h"
- #include "lualib.h"
- #include "lauxlib.h"
- }
- #include "Lua51.h"
- #include "Lua53.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #endif
- // 唯一的应用程序对象
- CWinApp theApp;
- using namespace std;
- //////////////////////////////////////////////////////////////////////////
- int l_TestFunction(lua_State *luaVM);
- int RunLua(const char* pScript, const int nlen);
- int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
- {
- int nRetCode = 0;
- // 初始化 MFC 并在失败时显示错误
- if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
- {
- // TODO: 更改错误代码以符合您的需要
- _tprintf(_T("错误: MFC 初始化失败\n"));
- nRetCode = 1;
- }
- else
- {
- Lua51 lua51;
- lua_State *pVM = lua51.Open("lua5.1.lua");
- lua51.CallLuaTableVariable(pVM, "tb_User");
- lua_close(pVM);
- // TODO: 在此处为应用程序的行为编写代码。
- #if 1 // 基本操作;
- // 创建lua对象;
- lua_State *luaVM = luaL_newstate();
- // 加载所有lua支持的lib;
- luaL_openlibs(luaVM);
- // 加载脚本文件;
- luaL_dofile(luaVM, "main.lua");
- // 获取全局变量,压栈索引从-1开始;
- lua_getglobal(luaVM, "nWidth");
- lua_getglobal(luaVM, "nHeight");
- // 注意,number在lua中表示的是浮点数;
- // 只有在更高版本的lua中, 才有integer;
- if (lua_isnumber(luaVM, -1))
- {
- printf("nWidth=%f\n", lua_tonumber(luaVM, -1));
- }
- if (lua_isnumber(luaVM, -2)) // lua_isinteger 在 lua5.3中才有;
- {
- printf("nHeight=%f\n", lua_tonumber(luaVM, -2));
- }
- // 恢复lua的栈;
- lua_pop(luaVM, 1);
- // 加载字符串脚本;
- luaL_dostring(luaVM, "print(100);");
- // 关闭lua;
- lua_close(luaVM);
- #endif
- }
- #if 0 // lua调用vc里的函数;
- const char* pScript = "TestFunction(true, 1,\"运行lua,这是lua中的函数, 调用程序中的函数来处理\");";
- RunLua(pScript, strlen(pScript));
- #endif
- #if 0 // vc调用lua里的函数;
- // 创建lua对象;
- lua_State *luaVM = luaL_newstate();
- // 加载所有lua支持的lib;
- luaL_openlibs(luaVM);
- // 加载脚本文件;
- luaL_dofile(luaVM, "m2.lua");
- // 取到一个全局标号add,取的同时会把add函数压栈;
- lua_getglobal(luaVM, "add");
- // 把两个函数所需要的参数压入栈里;
- lua_pushnumber(luaVM, 100);
- lua_pushnumber(luaVM, 200);
- // 执行lua中的脚本;
- if (lua_pcall(luaVM, 2/*参数个数*/, 1/*返回值个数*/, 0) != 0)
- {
- printf("error run lua %s\n", lua_tostring(luaVM, -1));
- // 将错误消息占用内存从栈中弹出;
- lua_pop(luaVM, 1);/* pop error message from the stack */
- }
- // 函数执行完了,执行结果被压栈,所以取得最顶端的一个数就是结果值,-1就是指取栈顶的值;
- // 检查返回值类型;
- if (lua_isnumber(luaVM, -1))
- {
- int nRet = (int)lua_tonumber(luaVM, -1);
- printf("vc调用lua中的函数, 返回值: %d\n", nRet);
- }
- // 将lua函数返回值出栈;
- lua_pop(luaVM, 1);
- // 关闭lua;
- lua_close(luaVM);
- #endif
- #if 0 // lua调用vc里的函数的另一种实现方式;
- // 创建lua对象;
- lua_State *luaVM = luaL_newstate();
- // 加载所有lua支持的lib;
- luaL_openlibs(luaVM);
- // 将vc函数指针压入栈;<不仅只有纯C函数才可以, VC函数也可以>
- // 首先,将函数名入栈;
- lua_pushstring(luaVM, "TestFunction");
- // 其次,将函数指针入栈;
- lua_pushcfunction(luaVM, l_TestFunction);
- // 最后,调用lua_settable注册函数;
- // lua中定义的变量和函数存放在一个全局table中,索引值为LUA_GLOBALSINDEX,table相关操作接口:
- lua_settable(luaVM, LUA_GLOBALSINDEX);
- // 加载脚本文本;
- luaL_dostring(luaVM, "TestFunction(true, 1,\"运行lua,这是lua中的函数, 调用程序中的函数来处理\");");
- // 关闭lua环境;
- lua_close(luaVM);
- #endif
- // 等待退出;
- system("pause");
- return nRetCode;
- }
- /************************************************************************/
- /* 函数:[1/15/2018 Jeff];
- /* 描述:;
- /* 参数:;
- /* [IN] :;
- /* [OUT] :;
- /* [IN/OUT] :;
- /* 返回:void;
- /* 注意:;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- int l_TestFunction(lua_State *luaVM)
- {
- // 判断参数有效性;
- if (!lua_isboolean(luaVM, 1) || !lua_isnumber(luaVM, 2) || !lua_isstring(luaVM, 3))
- {
- return -1;
- }
- // 第一个参数;
- BOOL bVisible = lua_toboolean(luaVM, 1);
- // 获取第二个参数;
- int nIndex = (int)lua_tonumber(luaVM, 2);
- // 测试第三个参数是否为字串形式,并取得这个字串;//lua_tostring//luaL_checkstring
- // const char* pText = luaL_checkstring(luaVM, 3);
- const char* pText = lua_tostring(luaVM, 3);
- // 用户自定义处理;
- printf("lua函数返回: %s, %d, %s \n", bVisible ? "true" : "false", nIndex, pText);
- return 0;
- }
- /************************************************************************/
- /* 函数:[1/15/2018 Jeff];
- /* 描述:;
- /* 参数:;
- /* [IN] pScript:lua脚本;
- /* [IN] nlen:脚本长度;
- /* [IN/OUT] :;
- /* 返回:void;
- /* 注意:;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- int RunLua(const char* pScript, const int nlen)
- {
- // 参数校验;
- if (pScript == NULL || nlen == 0)
- return -1;
- // 打开lua,创建lua虚拟环境;
- #ifndef LUA53
- lua_State *luaVM = lua_open();
- #else
- lua_State *luaVM = luaL_newstate();
- #endif
- if (luaVM == NULL)
- return -1;
- // 为lua环境加载全部lib库;
- luaL_openlibs(luaVM);
- /* 也可以单独加载需要的库,而不用全部加载;
- luaopen_base(luaVM ); // opens the basic library.
- luaopen_table(luaVM ); // opens the table library.
- luaopen_io(luaVM ); // opens the I/O library.
- luaopen_string(luaVM ); // opens the string lib.
- luaopen_math(luaVM ); // opens the math lib.
- luaopen_debug (lua);
- */
- // 注册所有函数;
- lua_register(luaVM, "TestFunction", l_TestFunction);
- // luaL_loadbuffer 将脚本加载进lua环境中;
- // lua_pcall执行lua环境中的脚本;
- int nErr = luaL_loadbuffer(luaVM, pScript, nlen, "main") || lua_pcall(luaVM, 0, 0, 0);
- if (nErr)
- {
- printf("error run lua %s\n", lua_tostring(luaVM, -1));
- // 将错误消息占用内存从栈中弹出;
- lua_pop(luaVM, 1);/* pop error message from the stack */
- }
- // 关闭lua环境;
- lua_close(luaVM);
- return 0;
- }
- /************************************************************************/
- /* 函数:[1/15/2018 Jeff];
- /* 描述:;
- /* 参数:;
- /* [IN] :;
- /* [OUT] :;
- /* [IN/OUT] :;
- /* 返回:void;
- /* 注意:;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- int StackDump(lua_State* luaVM)
- {
- // 得到栈的元素个数,栈顶的位置;
- int nTop = lua_gettop(luaVM);
- // 输出栈顶位置;
- printf("The Length of stack is %d/n", nTop);
- for (int i = 1; i <= nTop; ++i)
- {
- int t = lua_type(luaVM, i);
- // 这里的typename是把类型的枚举变成字符串,是类型名,不是栈中的位置;
- printf("%s:", lua_typename(luaVM, t));
- switch (t)
- {
- case LUA_TNUMBER:
- printf("%f", lua_tonumber(luaVM, i));
- break;
- case LUA_TSTRING:
- printf("%s", lua_tostring(luaVM, i));
- break;
- case LUA_TTABLE:
- //printf("%s/n", lua_tostring(luaVM,i));
- break;
- case LUA_TFUNCTION:
- //printf("%s/n", lua_tostring(luaVM,i));
- break;
- case LUA_TNIL:
- printf("Is NULL");
- break;
- case LUA_TBOOLEAN:
- printf("%s", lua_toboolean(luaVM, i) ? "true" : "false");
- break;
- default:
- break;
- }
- printf("/n");
- }
- return 0;
- }
|