lua脚本文件
--cCallLuaTest.lua
--read variable
var_int = 5
var_str = "hello,lua!!!"
var_bool = true
var_table_color = {r=12,g=34,b=56}
--call function
var_fun1 = function(a,b)
print("hello,world!!!")
return a+b
end
var_fun2 = function(a,b)
return a+b,a*a+b*b
end
c++文件
/*C/C++调用Lua
*/
#include<iostream>
using namespace std;
#include<lua.hpp>
void read_lua_variable(lua_State *L)
{
//void lua_getglobal (lua_State *L, const char *name);把全局变量 name 里的值压入堆栈。--是name变量值,不是name
lua_getglobal(L,"var_int");
lua_getglobal(L,"var_str");
lua_getglobal(L,"var_bool");
if (!lua_isnumber(L,-3))
{
cout<<"is not number"<<endl;
return;
}
if (!lua_isstring(L,-2))
{
cout<<"is not string"<<endl;
return;
}
if (!lua_isboolean(L,-1))
{
cout<<"is not boolean"<<endl;
return;
}
cout<<"var_int="<<lua_tointeger(L,-3)<<"\nvar_str="<<lua_tostring(L,-2)<<"\nvar_bool="<<lua_toboolean(L,-1)<<endl;
}
void read_lua_table(lua_State *L)
{
lua_getglobal(L,"var_table_color");
if(!lua_istable(L,-1))
{
cout<<"is not table"<<endl;
return;
}
cout<<"var_table_color:"<<lua_typename(L,lua_type(L,-1))<<" ,len="<<lua_gettop(L)<<endl;
//void lua_getfield(lua_State *L, int idx, const char *k); 第二个参数是table变量在栈中的索引值,最后一个参数是table的键值,该函数执行成功后会将字段值table[k]压入栈中。
lua_getfield(L,-1,"r");
lua_getfield(L,-2,"g");
lua_getfield(L,-3,"b");
cout<<"r="<<lua_tointeger(L,-3)<<",g="<<lua_tointeger(L,-2)<<",b="<<lua_tointeger(L,-1)<<endl;
lua_pop(L,1);
lua_pop(L,1);
lua_pop(L,1);
cout<<"var_table_color,end:"<<lua_typename(L,lua_type(L,-1))<<" ,len="<<lua_gettop(L)<<endl;
}
/*调用lua函数,使用API调用函数的方法是很简单的
1 , 将被调用的函数入栈;
2,依次将所有参数入栈;
3,使用lua_pcall调用函数;
4,从栈中获取函数执行返回的结果。
*/
void call_lua_funciton(lua_State*L)
{
lua_getglobal(L,"var_fun1");
if (!lua_isfunction(L,-1))
{
cout<<"is not funciton"<<endl;
return;
}
cout<<"var_fun1:"<<lua_typename(L,lua_type(L,-1))<<" ,len="<<lua_gettop(L)<<endl;
lua_pushnumber(L,3);
lua_pushnumber(L,4);
//下面的第二个参数表示带调用的lua函数存在两个参数。
//第三个参数表示即使带调用的函数存在多个返回值,那么也只有一个在执行后会被压入栈中。
//lua_pcall调用后,虚拟栈中的函数参数和函数名均被弹出。
if(lua_pcall(L,2,1,0) !=0 )
{
cout<<"error running function is:"<<lua_tostring(L,-1)<<endl;
return;
}
cout<<"var_fun1 res:"<<lua_typename(L,lua_type(L,-1))<<",res="<<lua_tonumber(L,-1)<<" ,len="<<lua_gettop(L)<<endl;
lua_pop(L,1);
lua_getglobal(L,"var_fun2");
if (!lua_isfunction(L,-1))
{
cout<<"is not funciton"<<endl;
return;
}
cout<<"var_fun2:"<<lua_typename(L,lua_type(L,-1))<<" ,len="<<lua_gettop(L)<<endl;
lua_pushnumber(L,3);
lua_pushnumber(L,4);
lua_pcall(L,2,2,0);
cout<<"var_fun2 res:"<<lua_typename(L,lua_type(L,-1))<<",res1="<<lua_tonumber(L,-1)<<",res2="<<lua_tonumber(L,-2)<<" ,len="<<lua_gettop(L)<<endl;
lua_pop(L,1);
lua_pop(L,1);
}
int main()
{
lua_State *L = luaL_newstate();
luaL_openlibs(L);
luaL_dofile(L,"cCallLuaTest.lua");
cout<<"---------------------------read_lua_variable"<<endl;
read_lua_variable(L);
cout<<"---------------------------read_lua_table"<<endl;
read_lua_table(L);
cout<<"---------------------------call_lua_funciton"<<endl;
call_lua_funciton(L);
cout<<"---------------------------call_lua_funciton"<<endl;
lua_close(L);
system("pause");
return 0;
}
结果