#include <stdio.h>
#include <string.h>
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
/** file description:
*
* 想要lua表或者函数等其他信息,必须把C中的这些信息先存入到lua中; lua跟C之间交互式通过一个栈来完成的;首先lua会把定义的函数
*或者其他定义等信息通过loadstring/dostring/dofile/loadfile函数加载到lua的状态机中。然后C中想要使用这些函数或者变量时,会用lua_getglobal
*来获取,lua干了什么呢?它会把用户需要的信息push到一个栈里面;如果是函数的话,用户需要把C中的参数按照顺序依次push到栈顶,然后执行
*lua_pcall(L,输入参数个数,输出参数个数,0)来调用,这时候lua又干了什么呢?lua会把原先压栈的函数和输入参数都pop出去,然后把函数结果push到
*栈顶;注意:当要调用其他函数时,记得要把栈顶的前一个返回值给lua_pop(L, 输出参数个数)弹出栈阿。
* 如果是全局的table呢?同样的,首先用lua_getglobal把表先压入到栈底,然后使用lua_getfiedl(L, -1, key)--注意这里的索引是-1,这是由于当前
*表是在栈底的。
*/
//这个东西定义成全局是方便之后其他函数不需要在传递,that's all
lua_State* L;
#define funcStr "function add(x, y) return x + y end \
function sub(x, y) return x - y end \
function initF() maintable={x=100, y=200} print( maintable.x .. \"|\" .. maintable.y); return \"initF done\";end \
function setValue(k, v) maintable[k]=v; for k,v in pairs(maintable) do print (\"key: \" .. k .. \" val: \" .. v) end; return 0 end \
function getValue(k) return maintable[k] end "
void showStack(lua_State *L, char *pNote)
{
int top = lua_gettop(L);
printf("------------------- %s [%d]-----------------\n", pNote, top);
for (int i = top; i >= 1; i--)
{
printf("[%d] -> ", i);
int t = lua_type(L,i);
switch(t)
{
case LUA_TSTRING:
printf("'%s' \n",lua_tostring(L,i));
break;
case LUA_TBOOLEAN:
printf(lua_toboolean(L,i) ? "true\n" : "false\n");
break;
case LUA_TNUMBER:
printf("%g\n",lua_tonumber(L,i));
break;
case LUA_TFUNCTION:
printf("function here\n");
break;
case LUA_TTABLE:
printf("table here\n");
break;
default:
printf("%s\n",lua_typename(L,t));
break;
}
}
}
int main()
{
int ret =0;
L = luaL_newstate();
L = lua_open();
luaL_openlibs(L);
/*dostring*/
ret = luaL_loadstring(L, funcStr) || lua_pcall(L, 0, 0, 0);
showStack(L, "doString");
/*add*/
lua_getglobal(L, "add");
lua_pushnumber(L, 1);
lua_pushnumber(L, 2);
showStack(L, "add");
lua_pcall(L, 2, 1, 0);
showStack(L, "pcall add");
lua_pop(L, 1);
/*sub*/
lua_getglobal(L, "sub");
lua_pushnumber(L, 20);
lua_pushnumber(L, 10);
showStack(L, "sub");
lua_pcall(L, 2, 1, 0);
ret = (int)lua_tonumber(L, -1);
showStack(L, "pcall sub");
lua_pop(L, 1);
/*init*/
lua_getglobal(L, "initF");
showStack(L, "initF");
ret = lua_pcall(L,0,1,0);
lua_pop(L, 1);
/*set value by func*/
lua_getglobal(L, "setValue");
lua_pushstring(L, "m");
lua_pushstring(L, "100n");
showStack(L, "setValue");
ret = lua_pcall(L, 2,1,0);
lua_pop(L, 1);
/*get value*/
lua_getglobal(L, "getValue");
lua_pushstring(L, "x");
lua_pcall(L, 1, 1, 0);
int a = (int)lua_tonumber(L, -1);
showStack(L, "getValue");
lua_pop(L, 1);
printf("a:[%d]\n", a);
/*get global table*/
lua_getglobal(L, "maintable");
showStack(L, "maintable");
/*get table filedd*/
lua_getfield(L, -1, "m");
showStack(L, "table field");
lua_pop(L,1);//对于table中的域必须先pop 出来
lua_getfield(L, -1, "x");
showStack(L, "table field");
lua_pop(L, 1);
/*set table value by oper table*/
/*
* 直接操作全局时,必须先把表放到栈底;然后把想要修改的值压入栈顶,然后调用lua_setfield(L, -2, "m")来修改(注意中间的idx是 -2)
*
*/
lua_pushnumber(L, 99999);
showStack(L, "set table");
lua_setfield(L, -2, "m");
showStack(L, "set table");
lua_getfield(L, -1, "m");
showStack(L, "set table");
lua_close(L);
return 0;
}
lua 在C文件中的调用方式
最新推荐文章于 2022-10-08 16:54:59 发布