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-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-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)
|
|
|
|
{
|
|
|
|
int i = lua_getglobal(mVM,"OnNetworkData");
|
|
|
|
lua_pushstring(mVM,addr);
|
|
|
|
lua_pushstring(mVM,data);
|
|
|
|
lua_pushnumber(mVM,port);
|
|
|
|
|
|
|
|
lua_call(mVM,3,0);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|