#include #include #include #include using namespace std; extern "C" { #include #include }; int main(int argc, char **argv) { lua_State *state = luaL_newstate(); if (state == NULL) return EXIT_FAILURE; // init stack lua_pushstring(state, "I am so cool~"); lua_pushnumber(state, 3); if (lua_isstring(state, 1)) printf("%s\n", lua_tostring(state, 1)); if (lua_isnumber(state, 2)) printf("%.0f\n", lua_tonumber(state, 2)); // push table lua_createtable(state, 0, 0); printf("stack top index is %d.\n", lua_gettop(state)); // set table.age lua_pushnumber(state, 9); printf("stack top index is %d.\n", lua_gettop(state)); lua_setfield(state, -2, "age"); printf("stack top index is %d.\n\n", lua_gettop(state)); // copy table ptr, and change value lua_pushvalue(state, -1); lua_pushnumber(state, 6); lua_setfield(state, -3, "age"); // view value lua_getfield(state, 3, "age"); printf("%.0f\n", lua_tonumber(state, -1)); lua_getfield(state, 4, "age"); printf("%.0f\n", lua_tonumber(state, -1)); printf("stack top index is %d.\n\n", lua_gettop(state)); // insert element in stack lua_insert(state, 4); printf("is table %d.\n", lua_istable(state, 5)); printf("is number %d.\n", lua_isnumber(state, 6)); printf("stack top index is %d.\n\n", lua_gettop(state)); // replace lua_replace(state, 5); printf("is number %d.\n", lua_isnumber(state, 5)); printf("stack top index is %d.\n\n", lua_gettop(state)); // remove stack element lua_remove(state, -1); printf("stack top index is %d.\n", lua_gettop(state)); out: lua_close(state); #ifdef _MSC_VER getchar(); #endif return EXIT_SUCCESS; }