2021-04-14 16:33:28 +00:00
|
|
|
#include "lua_wraper.h"
|
|
|
|
|
|
|
|
void LuaDelegate::Stop()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
int LuaDelegate::DoString(QString scr)
|
|
|
|
{
|
|
|
|
if (mVM != nullptr){
|
|
|
|
int ret = luaL_dostring(mVM,scr.toStdString().c_str());
|
|
|
|
if (ret > 0){
|
|
|
|
printf("lua error");
|
2021-04-24 17:21:31 +00:00
|
|
|
PrintError(mVM);
|
2021-04-14 16:33:28 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-09-01 16:16:47 +00:00
|
|
|
int LuaDelegate::UpdateScript(QString scr)
|
|
|
|
{
|
|
|
|
if(nullptr != mVM){
|
|
|
|
lua_close(mVM);
|
|
|
|
}
|
|
|
|
free(mVM);
|
|
|
|
mVM = luaL_newstate(); //打开lua
|
|
|
|
if(nullptr != mVM){
|
|
|
|
printf("shit is nullptr");
|
|
|
|
}
|
|
|
|
luaL_openlibs(mVM); //打开标准库
|
|
|
|
lua_register(mVM, "showbuffer", LuaShowData);
|
|
|
|
lua_register(mVM, "serial_send", LuaWriteUart);
|
|
|
|
|
|
|
|
if (mVM != nullptr){
|
|
|
|
int ret = luaL_dostring(mVM,scr.toStdString().c_str());
|
|
|
|
if (ret > 0){
|
|
|
|
printf("lua error");
|
|
|
|
PrintError(mVM);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-04-20 15:35:18 +00:00
|
|
|
void LuaDelegate::PrintError(lua_State *L)
|
|
|
|
{
|
2021-04-24 17:21:31 +00:00
|
|
|
qDebug()<<"\nFATAL ERROR:%s\n\n"<< lua_tostring(L, -1);
|
2021-04-20 15:35:18 +00:00
|
|
|
}
|
|
|
|
|
2021-05-03 08:49:33 +00:00
|
|
|
void LuaDelegate::OnSerialData(QString data){
|
2021-04-20 15:35:18 +00:00
|
|
|
int i = lua_getglobal(mVM,"OnDataReady");
|
2021-09-01 16:16:47 +00:00
|
|
|
if(i == 0)
|
|
|
|
return;
|
2021-04-17 17:57:39 +00:00
|
|
|
lua_pushstring(mVM,data.toStdString().data());
|
2021-04-20 15:35:18 +00:00
|
|
|
lua_call(mVM,1,0);
|
2021-04-14 16:52:05 +00:00
|
|
|
}
|
|
|
|
|
2021-05-03 08:49:33 +00:00
|
|
|
void LuaDelegate::OnNetworkData(char *addr, char *data, uint32_t port)
|
|
|
|
{
|
2021-08-31 14:23:45 +00:00
|
|
|
qDebug()<<"call lua network callback";
|
2021-08-29 08:13:50 +00:00
|
|
|
int ret = lua_getglobal(mVM,"OnNetworkData");
|
2021-09-01 16:16:47 +00:00
|
|
|
if(ret == 0)
|
|
|
|
return;
|
|
|
|
qDebug()<<ret;
|
2021-05-03 08:49:33 +00:00
|
|
|
lua_pushstring(mVM,addr);
|
|
|
|
lua_pushstring(mVM,data);
|
|
|
|
lua_pushnumber(mVM,port);
|
|
|
|
lua_call(mVM,3,0);
|
|
|
|
}
|
|
|
|
|
2021-09-09 16:22:33 +00:00
|
|
|
void LuaDelegate::OnNewTcpClient(QString ip, uint32_t port, uint32_t sockptr)
|
|
|
|
{
|
|
|
|
qDebug()<<"call lua network new client callback";
|
|
|
|
int ret = lua_getglobal(mVM,"OnNewClient");
|
|
|
|
if(ret == 0)
|
|
|
|
return;
|
|
|
|
qDebug()<<ret;
|
|
|
|
lua_pushstring(mVM,ip.toStdString().c_str());
|
|
|
|
lua_pushnumber(mVM,port);
|
|
|
|
lua_pushnumber(mVM,sockptr);
|
|
|
|
lua_call(mVM,3,0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LuaDelegate::OnClientLeave(QString ip, uint32_t port, uint32_t sockptr)
|
|
|
|
{
|
|
|
|
qDebug()<<"call lua network leave callback";
|
|
|
|
int ret = lua_getglobal(mVM,"OnClientLeave");
|
|
|
|
if(ret == 0)
|
|
|
|
return;
|
|
|
|
qDebug()<<ret;
|
|
|
|
lua_pushstring(mVM,ip.toStdString().c_str());
|
|
|
|
lua_pushnumber(mVM,port);
|
|
|
|
lua_pushnumber(mVM,sockptr);
|
|
|
|
lua_call(mVM,3,0);
|
|
|
|
}
|
|
|
|
|
2021-09-11 17:28:36 +00:00
|
|
|
|
2021-04-18 15:16:03 +00:00
|
|
|
void LuaDelegate::DumpStack()
|
|
|
|
{
|
|
|
|
static int count = 0;
|
|
|
|
printf("begin dump lua stack:%d\n", count);
|
|
|
|
int top = lua_gettop(mVM);
|
|
|
|
for (int i = top; i > 0; --i)
|
|
|
|
{
|
|
|
|
int t = lua_type(mVM, i);
|
|
|
|
switch (t)
|
|
|
|
{
|
|
|
|
case LUA_TSTRING:
|
|
|
|
qDebug()<<("%s\n", lua_tostring(mVM, i));
|
|
|
|
break;
|
|
|
|
case LUA_TBOOLEAN:
|
|
|
|
qDebug()<<(lua_toboolean(mVM, i) ? "true\n" : "false\n");
|
|
|
|
break;
|
|
|
|
case LUA_TNUMBER:
|
|
|
|
qDebug()<<("%g\n", lua_tonumber(mVM, i));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
qDebug()<<("%s\n", lua_typename(mVM, t));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
++count;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-04-14 16:33:28 +00:00
|
|
|
LuaDelegate::~LuaDelegate()
|
|
|
|
{
|
|
|
|
if(nullptr != mVM){
|
|
|
|
lua_close(mVM);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LuaDelegate::LuaDelegate():
|
|
|
|
mVM(nullptr)
|
|
|
|
{
|
|
|
|
mVM = luaL_newstate(); //打开lua
|
|
|
|
if(nullptr != mVM){
|
|
|
|
printf("shit is nullptr");
|
|
|
|
}
|
2021-04-17 17:57:39 +00:00
|
|
|
luaL_openlibs(mVM); //打开标准库
|
|
|
|
lua_register(mVM, "showbuffer", LuaShowData);
|
2021-05-03 15:07:13 +00:00
|
|
|
lua_register(mVM, "serial_send", LuaWriteUart);
|
2021-04-18 15:16:03 +00:00
|
|
|
|
2021-04-14 16:33:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int LuaDelegate::DoFile(QString path)
|
|
|
|
{
|
|
|
|
if(mVM != nullptr){
|
2021-04-15 17:13:04 +00:00
|
|
|
|
2021-04-14 16:52:05 +00:00
|
|
|
int ret = luaL_dofile(mVM, path.toStdString().c_str());
|
2021-04-14 16:33:28 +00:00
|
|
|
if (ret > 0){
|
|
|
|
printf("lua error");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|