LUA 02

来源:互联网 发布:康熙王朝 知乎 编辑:程序博客网 时间:2024/06/05 08:23
#include <iostream>
#include <string>
#include <cstdio>
using namespace std;
extern "C" {
#include "lua.h" 
#include "lualib.h" 
#include "lauxlib.h"
#pragma comment(lib,"lua5.1.lib")
}
/* Lua解释器指针 */
lua_State* L;
int main ( int argc, char *argv[] )

/* 初始化Lua */
L = lua_open();
/* 载入Lua基本库 */ 
luaL_openlibs(L); 

/* 载入语句块 */
if(luaL_loadfile(L, "1.lua") || lua_pcall(L, 0, 0, 0))
{
printf("载入错误\n");
printf("%s\n",lua_tostring(L, -1));
goto EXIT;
}


lua_getglobal(L, "whatfuck");
lua_getglobal(L, "b");


/* lua_to* 不会POP数据 */
printf("%s\n",lua_tostring(L, -2));
printf("%s\n",lua_tostring(L, -1));
lua_pop(L, -1);
lua_pop(L, -1);
//lua_settop(L, 1);
//lua_pop(L, -1);


lua_getglobal(L, "whatfucktable");
lua_pushstring(L, "a");
/* 调用会POP掉KEY PUSH相应的值 */
/*
table.value:whatfucktable.a
table:whatfucktable
*/
lua_gettable(L, -2);
printf("%s\n",lua_tostring(L, -1));
//printf("%s\n",lua_istable(L, -2)?"true":"false");
//lua_pop(L, -2);


//lua_pushstring(L, "b");
//lua_gettable(L, -3);
printf("%s\n",lua_tostring(L, -1));
//printf("%d\n",lua_gettop(L));
///* 运行脚本 */ 
////luaL_dofile(L, "print.lua"); 
//string file;
//cin>>file;
//getchar();
//getchar();
//file.push_back('.');
//file.push_back('l');
//file.push_back('u');
//file.push_back('a');
//luaL_dofile(L, file.c_str()); 
EXIT:
/* 清除Lua */ 
lua_close(L); 
/* 暂停 */ 
printf( "Press enter to exit…" ); 
getchar(); 
return 0;
}
原创粉丝点击