lua和c++全部源码

LUA :

--[[
print("1111111111111111")
a = 10
dofile("111.lua")  --执行另外一个脚本
print(a)
]]
local html = [[
<html>
</html>
]]
local b,e = string.find(html,"html");
print("b = "..b .."e=" ..e);
----------lua 表的插入
local tab1 = {"001","002","003"}
for i,v in ipairs(tab1) do
print("value=" .. v);
end
print("=====insert ======")
table.insert(tab1,3,"002-2")
for i,v in ipairs(tab1) do
print("value = " ..v);
end
print("============insert back======")
table.insert(tab1,"004");
for i,v in ipairs(tab1) do
print("value=" ..v);
end
print("==========remove===========")
table.remove(tab1,3)  ---删除第三个
for  i,v in ipairs(tab1) do
print("value = "..v);
end

print("===========不加个数默认最后一个=====")
table.remove(tab1)
for i,v in ipairs(tab1) do
print("value = " .. v);
end
print("=============哈希表==================")
--[[
local tab2 = {id=123,age=20}
tab2["name"] = "xiaoming"  --直接加到后面
tab3["id"] = nil;  -- 删除id
for k,v in pairs(tab2) do
print(k..":" ..v)
end
]]
--遍历二维数组
--[[
local tab3 = {}
tab3[1] = {"namel","name2"};
tab3[2] = {"value1","value2"};
for k,v in pairs(tab3) do
for k2,v2 in pairs(v) do
print("k2 =" ..k2 .."v2="..v2)
end
end
]]
--lua函数
function test(p1,p2)
print(p1)
if(p1 == nil) then
p1="001"
end
if(p2 == nil) then
p2 = "002"
end
print("p1="..p1.."p2="..p2)
print("in test function")
end
--可变参函数
function test1(...)
local len = table.getn(arg);  --获取个数
print("int test1 function len = "..len);
for a=1,len do
print("arg"..a.."="..arg[a])   --取到所有参数
end
end
-----下面是函数调用
test(1,2)
test1(1,"name")
----------函数带参数返回
function test2(...)
print("this is test2");
return 1,"name";
----可以同时返回多个值
end
local re,n= test2(123,"name")
print("re = "..re..","..n);
--------函数调用
local fun1  = test2;
local re,n= fun1(123,"name")
print("re = "..re..","..n);
-------也可以在函数定义的时候直接赋给变量
local event = function (p1)
print("event = ".. p1);
end
event("key");
-----------函数名重复调用,则最后一个定义的会覆盖前面的
function test2(...)
print("new test2");
end
test2();
--------------Lua 调用c++函数无参数------------------------------
--在这里调用c++的函数
print("Call c + + functions without parameters");
Ctest();
--------------Lua 调用c++函数有参数------------------------------
Ctest1("lixiao",123,false);
Ctest1("lixiao",123,true);
-------------lua调用c++传递数组参数-----------------------
local arr = {"A001","A002","A003"};
local arr1 = {"B001","B002","B003","B004"};
CtestArr(arr,arr1);
-------------lua调用C++传递key-value表----------]
local tab = {name="xiaoming",age="18",id="123"};
CtestArr1(tab); 
-----------------c++查看表中一个值-----------------
CtestArr2(tab);
------------------lua调用c++参数类型检查-----------------
local size = 108;
CtestArr3(tab,size);
------------------lua 获取c++函数返回值-----------------
local re = CTestCToLuaReturn();
print("re = "..re);
------------------Lua调用c++返回表(示例返回对象)-----------------
local table = CTestReTable();
print("name = "..table["name"]);
------------------传递多个表值-----------------
local tab= CTestReTableN();
print("2name :"..tab["name"]);
print("age:"..tab["age"]);
----------------c++调用Lua的全局变量--------------------------------
width = 1920;
----------------luc调用c++全局变量--------------------------------
print("c++ test = "..testCToLua);
print(testCToLua);
print(testCToLua);
print(testCToLua);
print(testCToLua);
print(testCToLua);
print(testCToLua);
print(testCToLua);
------------------c++调用Lua给Lua传递表和访问Lua的表-----------------------
conf = {
titlename="first lua",
height = 1080
}
-------------------c++传递给Lua全局变量表--------------------------------------
print("test name c++ -> lua table="..testCToLuaTable["name"]);
print("test age c++ -> lua table="..testCToLuaTable.age);
--------------------c++调用lua函数------------------------------------------
function event111()
print("c++ call lua function 66666666666666");
end
--------------------c++调用lua函数错误处理-----------------------
function event222()
print("c++ callk lua function 777777777777777");
end
--------------------3.C++调用Lua函数,函数传参出错处理-----------------
function event333(e)    --e这个参数是c++传递过来的
print("c++ call lua function");
print(e);
end
----------------------4.c++调用lua函数,lua返回字符串---------------------
function event444(e)
print("c++ call lua function");
print(e);
return "lua event return"; ---返回值
end
------------------------5C++调用Lua函数出错是执行错误处理函数---------------
function ferror(e)
print("c++ call lua failed ferror = "..e);
return "lua change error";
end
function event555(e)
print("5c++ call lua function");
print(e);
return "lua event return"; ---返回值
end
------------------------6C++调用Lua函数出错是执行错误处理函数(改进)----------
function event666(e)
print("6c++ call lua function");
print(e);
return "lua event return"; ---返回值
end
------------------------7C++调用Lua函数获取返回表---------
function ferror777(e)
print("7c++ call lua failed ferror = "..e);
return "lua change error";
end
function event777(e,obj)
print("7c++ call lua function");
print(e);
print(obj.name);
local re = {id=123};
return re; ---返回值
end


// first.cpp : 定义控制台应用程序的入口点。

//

extern "C"
{
#include<lua.h>
#include<lauxlib.h>
#include<lualib.h>
}
#include<string.h>
现在c++定义一个函数,然后要把这个函数通知给Lua

int CTest(lua_State *L)
{
printf("int CTest");
return 0;
}
int CTest1(lua_State *L)
{
printf("int CTest1");
size_t len;
const char *name = lua_tolstring(L, 1, &len);//第一个参数
int age = lua_tonumber(L, 2); //第二个参数
bool is = lua_toboolean(L, 3);
printf("Lua name =%s age=%d is = %d\n", name, age,(int)is);//第三个参数
return 0;
}

//传递数组参数
int CTestArr(lua_State *L)
{
printf("In CTestArr\n");
int len = luaL_getn(L, 2);//获取L的第二个参数的大小
for (int i = 1; i <= len; i++)
{
lua_pushnumber(L, i);//往栈里面压一个数字i,表示我们要取表里面那个下标的值
lua_gettable(L, 2);//取出表,2代表表在栈中的位置,pop index push table[i]  1.先把上一条语句的i出栈2.再把tale[i]压入栈顶
size_t size;
printf("%s\n", lua_tolstring(L, -1, &size));
lua_pop(L, 1);//把上一个加的出栈
}
return 0;
}
//lua调用c++函数传递表key-value和参数类型,遍历
int CTestTable1(lua_State *L)
{
lua_pushnil(L);  //先在栈顶压入一个空的,因为lua_next会先弹出一个key
while (lua_next(L, 1) != 0)//表在栈底
{
//先push key,push value
printf("%s = ", lua_tostring(L, -2));
printf("%s\n", lua_tostring(L, -1));
lua_pop(L, 1);//因为上面Lua_next会把key出栈,不会把value出栈所以要把value出栈
}
return 0;
}

//只取表中一个value
int CTestTable2(lua_State *L)
{
lua_getfield(L, 1, "name");  //只要一个name
printf("name = %s\n", lua_tostring(L, -1));
return 0;
}

//类型检查
int CTestTable3(lua_State *L)
{
luaL_checktype(L, 1, LUA_TTABLE);
if (lua_type(L, 2) != LUA_TNUMBER)
{
printf("pata 2 is not number\n");
}
lua_getfield(L, 1, "name");
printf("name =%s\n", lua_tostring(L, -1));
return 0;
}

//lua 获取c++函数返回值
int CTestRe(lua_State *L)
{
lua_pushstring(L, "return value");
//在吧字符串压倒栈顶
return 1;  //作为返回值传递给Lua,返回1个
}
//Lua调用c++返回表(示例返回对象)
int CTestReTable(lua_State *L)
{
lua_newtable(L);
//插入key value
lua_pushstring(L, "name");
lua_pushstring(L, "xiaoming");
lua_settable(L, -3);//将值写入表中,注意这里讲值放入表中之后会将上面两个出栈
return 1;//注意这里要返回
}

//传递多个表值
int CTestReTableN(lua_State *L)
{
lua_newtable(L);
//插入key value
lua_pushstring(L, "name");
lua_pushstring(L, "xiaoming");
lua_settable(L, -3);//将值写入表中,注意这里讲值放入表中之后会将上面两个出栈
lua_pushstring(L,"age");
lua_pushstring(L,"21");
lua_settable(L, -3);//注意这里还是-3
return 1;
}
int main(int argc, char* argv[])
{
lua_State *L = lua_open();
//打开库
luaopen_base(L);   
luaopen_string(L);
luaopen_table(L);
//lua 有个库用来将c++的函数通知给Lua
//通过下面这个函数就可以在Lua脚本上通过调用第二个参数自定义的名字来调用c++中的函数了
lua_register(L, "Ctest", CTest);
lua_register(L, "Ctest1", CTest1);
lua_register(L, "CtestArr", CTestArr);
lua_register(L, "CtestArr1", CTestTable1);
lua_register(L, "CtestArr2", CTestTable2); 
lua_register(L, "CtestArr3", CTestTable3);
//lua 获取c++函数返回值
lua_register(L, "CTestCToLuaReturn", CTestRe);
//Lua调用c++返回表(示例返回对象)
lua_register(L, "CTestReTable", CTestReTable);
//传递多个表值
lua_register(L, "CTestReTableN", CTestReTableN);

//luc调用c++全局变量
lua_pushstring(L, "Hello");
lua_setglobal(L, "testCToLua");  //test = hell0

//c++给Lua传递表
lua_newtable(L);
lua_pushstring(L, "name");
lua_pushstring(L, "xiaoming");
lua_settable(L, -3);

lua_pushstring(L, "age");
lua_pushnumber(L, 20);
lua_settable(L, -3);
lua_setglobal(L, "testCToLuaTable"); //设为全局变量

if (luaL_loadfile(L, "main.lua"))
{
const char *error = lua_tostring(L, -1);   //lua的错误会放到栈顶,lua_tostring是取栈顶
printf("lua load error:%s", error);
return -1;
}
if (lua_pcall(L, 0, 0, 0))
{
const char *error = lua_tostring(L, -1);
printf("lua_pcall error:%s", error);
return -1;
}
//c++调用Lua的全局变量
lua_getglobal(L, "width");
int width = lua_tonumber(L, -1);
printf("width = %d\n", width);
lua_pop(L, 1);//恢复现场

//c++调用Lua给Lua传递表和访问Lua的表-----------------------
第一种
lua_getglobal(L,"conf");    //取表
lua_getfield(L,-1,"height");
printf("height= %d\n",(int)lua_tonumber(L,-1));

lua_getfield(L,-2,"titlename");  //取值
printf("title  = %s\n",lua_tostring(L,-1));
lua_pop(L,3);

//第二种,每取一次就把值出栈,第二次取得时候也就跟第一次一样了
lua_getglobal(L,"conf");    //取表
lua_getfield(L,-1,"height");
printf("height= %d\n",(int)lua_tonumber(L,-1));
lua_pop(L,1);

lua_getfield(L,-1,"titlename");  //取值
printf("title  = %s\n",lua_tostring(L,-1));
lua_pop(L,1);
lua_pop(L,1); //最后要表出栈

//1.c++调用Lua函数
lua_getglobal(L,"event111");
lua_pcall(L,0,0,0); //2.要调用函数的参数,3,函数处理返回值,4,错误输出的函数,0则说明不出,用pcall本身来处理,并将错误压入栈中
//2.c++调用Lua函数,出错处理
printf("2top is %d\n",lua_gettop(L));   //打印栈大小
lua_getglobal(L,"event222");
if(lua_pcall(L,0,0,0) != 0)
{
printf("call event failed %s\n",lua_tostring(L,-1)); //错误的时候会把错误信息压倒栈顶
lua_pop(L,1); //将错误信息出栈
} 

printf("2top is %d\n",lua_gettop(L));

//3.C++调用Lua函数,函数传参出错处理
printf("3top is %d\n",lua_gettop(L));   //打印栈大小
lua_getglobal(L,"event333");
lua_pushstring(L,"key");//参数
if(lua_pcall(L,1,0,0) != 0)   //2.代表有一个参数就是上面的key
{
printf("call event failed %s\n",lua_tostring(L,-1)); //错误的时候会把错误信息压倒栈顶
lua_pop(L,1); 
} 
printf("3top is %d\n",lua_gettop(L));

//4.c++调用lua函数,lua返回字符串
printf("\n4top is %d\n",lua_gettop(L));   //打印栈大小
lua_getglobal(L,"event444");
lua_pushstring(L,"key");
if(lua_pcall(L,1,1,0) != 0)   //2.代表有一个参数就是上面的key,3.代表返回值个数,写1
{
printf("call event failed %s\n",lua_tostring(L,-1)); //错误的时候会把错误信息压倒栈顶
lua_pop(L,1); //将错误信息出栈
} 
else{  //正确的时候才有返回值
printf("lua return : %s\n",lua_tostring(L,-1));
lua_pop(L,1);   //出栈,因为返回值被压到栈顶了
}
printf("4top is %d\n",lua_gettop(L));

//5.调用Lua函数出错是执行错误处理函数
printf("5top is %d\n",lua_gettop(L));   //打印栈大小
lua_getglobal(L,"ferror"); //错误处理函数
lua_getglobal(L,"event555");

lua_pushstring(L,"key");
if(lua_pcall(L,1,1,-3) != 0)   //2.代表有一个参数就是上面的key,3.代表返回值个数,写1 ,4.是出错处理函数的栈空间
{
printf("call event failed %s\n",lua_tostring(L,-1)); 
//现在这里就跟之前不太一样了,之前是pcall返回的错误被压入到栈中,现在是一出错就调用出错处理函数ferror,等ferror处理完,将//返回值压入到栈中,这时才到这里,所以这里的值是出错函数返回的值
lua_pop(L,1); //将错误信息出栈
} 
else{  //正确的时候才有返回值
printf("5lua return : %s\n",lua_tostring(L,-1));
lua_pop(L,1);   //出栈,因为返回值被压到栈顶了
}
lua_pop(L,1); //因为之前的出错处理函数也入栈了,在这里也要将他出栈
printf("5top is %d\n",lua_gettop(L));
//6.调用Lua函数出错是执行错误处理函数(改进)
printf("6top is %d\n",lua_gettop(L));   //打印栈大小
int errfun = lua_gettop(L);    //记录栈顶位置
lua_getglobal(L,"ferror"); //错误处理函数
errfun++; //每加一个函数,都+1,因为栈是从上往下加的,所以从下往上数是不变的,注意加函数只能在定义出错处理函数上面才正确
lua_getglobal(L,"event666");
lua_pushstring(L,"key");
if(lua_pcall(L,1,1,errfun) != 0)   //2.代表有一个参数就是上面的key,3.代表返回值个数,写1 ,4.是出错处理函数的栈空间
{
printf("call event failed %s\n",lua_tostring(L,-1)); //现在这里就跟之前不太一样了,之前是pcall返回的错误被压入到栈中,现在是一出错就调用出错处理函数ferror,等ferror处理完,将//返回值压入到栈中,这时才到这里,所以这里的值是出错函数返回的值
lua_pop(L,1);
} 
else{  //正确的时候才有返回值
printf("6lua return : %s\n",lua_tostring(L,-1));
lua_pop(L,1);   //出栈,因为返回值被压到栈顶了
}
lua_pop(L,1); //因为之前的出错处理函数也入栈了,在这里也要将他出栈
printf("6top is %d\n",lua_gettop(L));
//7.c++调用Lua函数获取返回表
printf("7top is %d\n",lua_gettop(L));   //打印栈大小
int errfun77 = lua_gettop(L);    //记录栈顶位置
lua_getglobal(L,"ferror777"); //错误处理函数
errfun77++; //每加一个函数,都+1,因为栈是从上往下加的,所以从下往上数是不变的,注意加函数只能在定义出错处理函数上面才正确
lua_getglobal(L,"event777");
lua_pushstring(L,"key7");
lua_newtable(L);//创建一个表
lua_pushstring(L,"name");
lua_pushstring(L,"xiaoming");
lua_settable(L,-3);
if(lua_pcall(L,2,1,errfun77) != 0)   //2.代表有一个参数就是上面的key,3.代表返回值个数,写1 ,4.是出错处理函数的栈空间
{
printf("7call event failed %s\n",lua_tostring(L,-1)); //现在这里就跟之前不太一样了,之前是pcall返回的错误被压入到栈中,现在是一出错就调用出错处理函数ferror,等ferror处理完,将//返回值压入到栈中,这时才到这里,所以这里的值是出错函数返回的值
lua_pop(L,1); //将错误信息出栈
} 
else{  //正确的时候才有返回值
lua_getfield(L,-1,"id");
printf("return table id is %d\n",(int)lua_tonumber(L,-1));
lua_pop(L,2);   //出栈,因为返回值被压到栈顶了
}
lua_pop(L,1); //因为之前的出错处理函数也入栈了,在这里也要将他出栈
printf("7top is %d\n",lua_gettop(L));
lua_close(L);

return 0;
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值