1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| #include <iostream> #include <string.h- #include <cassert> #include <lua.hpp>
using namespace std;
int main() { lua_State* L = luaL_newstate(); if (L == NULL) { cout << "state is null\n"; return 0; } int bRet = luaL_loadfile(L, "test.lua"); if (bRet) { cout << "load file error" << endl; return 0; } bRet = lua_pcall(L, 0, 0, 0); if (bRet) { cout << "pcall error" << endl; return 0; } lua_getglobal(L, "str"); string str = lua_tostring(L, -1); cout << "str = " << str.c_str() << endl; lua_getglobal(L, "tbl"); lua_getfield(L, -1, "name"); str = lua_tostring(L, -1); cout << "tbl:name = " << str.c_str() << endl; lua_getglobal(L, "add"); lua_pushnumber(L, 10); lua_pushnumber(L, 20); int iRet = lua_pcall(L, 2, 1, 0); if (iRet) { const char* pErrorMsg = lua_tostring(L, -1); cout << pErrorMsg << endl; lua_close(L); return 0; } if (lua_isnumber(L, -1)) { double fValue = lua_tonumber(L, -1); cout << "Result is " << fValue << endl; } lua_close(L); return 0; }
|