proto-debuger/protoDebuger/lua_wraper.cpp

103 lines
2.1 KiB
C++

#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");
PrintError(mVM);
return -1;
}
}
return 0;
}
void LuaDelegate::PrintError(lua_State *L)
{
qDebug()<<"\nFATAL ERROR:%s\n\n"<< lua_tostring(L, -1);
}
void LuaDelegate::OnSerialData(QString data){
int i = lua_getglobal(mVM,"OnDataReady");
lua_pushstring(mVM,data.toStdString().data());
lua_call(mVM,1,0);
}
void LuaDelegate::OnNetworkData(char *addr, char *data, uint32_t port)
{
int ret = lua_getglobal(mVM,"OnNetworkData");
lua_pushstring(mVM,addr);
lua_pushstring(mVM,data);
lua_pushnumber(mVM,port);
lua_call(mVM,3,0);
}
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;
}
LuaDelegate::~LuaDelegate()
{
if(nullptr != mVM){
lua_close(mVM);
}
}
LuaDelegate::LuaDelegate():
mVM(nullptr)
{
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);
}
int LuaDelegate::DoFile(QString path)
{
if(mVM != nullptr){
int ret = luaL_dofile(mVM, path.toStdString().c_str());
if (ret > 0){
printf("lua error");
return -1;
}
}
return 0;
}