nim_duilib/examples/proto_debuger/lua_wraper.cpp

151 lines
3.0 KiB
C++
Raw Normal View History

2021-11-19 00:37:35 +08:00
#include "lua_wraper.h"
void LuaDelegate::Stop()
{
}
2021-11-27 17:42:34 +08:00
int LuaDelegate::DoString(std::string scr) {
2021-11-19 00:37:35 +08:00
if (mVM != nullptr){
int ret = luaL_dostring(mVM,scr.c_str());
if (ret > 0){
printf("lua error");
PrintError(mVM);
return -1;
}
}
this->mScript = scr;
2021-11-19 00:37:35 +08:00
return 0;
}
2022-01-23 23:47:00 +08:00
/*
return:
-1: error
0: success
*/
2021-11-27 17:42:34 +08:00
int LuaDelegate::UpdateScript(std::string scr) {
2022-01-23 23:47:00 +08:00
auto tmp = luaL_newstate(); //打开lua
if (nullptr == tmp) {
return -1;
}
luaL_openlibs(tmp); //打开标准库
int ret = luaL_dostring(tmp, scr.c_str());
if (ret > 0) {
printf("UpdateScript error\r\n");
PrintError(tmp);
return -1;
}
2021-11-19 00:37:35 +08:00
if(nullptr != mVM){
lua_close(mVM);
}
2022-01-23 23:47:00 +08:00
mVM = tmp;
2021-11-19 00:37:35 +08:00
for (auto x : mFunc) {
lua_register(mVM, x.first.c_str(), x.second);
}
2022-01-23 23:47:00 +08:00
mScript = scr;
/*
mScript = scr;
2022-01-23 23:47:00 +08:00
ret = luaL_dostring(mVM, scr.c_str());
if (ret > 0) {
PrintError(mVM);
return -1;
}
*/
2021-11-19 00:37:35 +08:00
return 0;
}
2021-11-27 17:42:34 +08:00
void LuaDelegate::PrintError(lua_State *L) {
2022-01-23 23:47:00 +08:00
auto err = std::string("\nFATAL ERROR:%s\n\n") + lua_tostring(L, -1);
std::cout << err;
2021-11-19 00:37:35 +08:00
}
int LuaDelegate::BindFunction(std::string name, lua_CFunction function)
{
if ((nullptr == function) || (name == ""))
return -1;
this->mFunc[name] = function;
lua_register(mVM, "serial_send", function);
return 0;
}
2022-01-23 23:47:00 +08:00
void LuaDelegate::OnSerialData(std::string data) {
int i = lua_getglobal(mVM, "OnUartData");
if (i == 0) {
std::cout << "OnUartData not found\r\n";
return;
2022-01-23 23:47:00 +08:00
}
lua_pushstring(mVM, data.data());
lua_call(mVM, 1, 0);
2021-11-19 00:37:35 +08:00
}
2021-12-29 00:17:57 +08:00
void LuaDelegate::DumpStack() {
2021-11-19 00:37:35 +08:00
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:
std::cout<<("%s\n", lua_tostring(mVM, i));
break;
case LUA_TBOOLEAN:
std::cout<<(lua_toboolean(mVM, i) ? "true\n" : "false\n");
break;
case LUA_TNUMBER:
std::cout<<("%g\n", lua_tonumber(mVM, i));
break;
default:
std::cout<<("%s\n", lua_typename(mVM, t));
break;
}
}
++count;
}
2021-12-29 00:17:57 +08:00
LuaDelegate::~LuaDelegate() {
2021-11-19 00:37:35 +08:00
if(nullptr != mVM){
lua_close(mVM);
}
}
LuaDelegate::LuaDelegate():
2021-12-29 00:17:57 +08:00
mVM(nullptr) {
2021-11-19 00:37:35 +08:00
mVM = luaL_newstate(); //打开lua
if(nullptr != mVM){
printf("shit is nullptr");
}
luaL_openlibs(mVM); //打开标准库
}
LuaDelegate::LuaDelegate(std::map<std::string, lua_CFunction> maps) :
2021-12-29 00:17:57 +08:00
mVM(nullptr) {
2021-11-19 00:37:35 +08:00
mVM = luaL_newstate(); //打开lua
if (nullptr != mVM) {
printf("shit is nullptr");
}
luaL_openlibs(mVM); //打开标准库
for (auto x : maps) {
lua_register(mVM, x.first.c_str(), x.second);
}
mFunc = maps;
}
2021-12-29 00:17:57 +08:00
int LuaDelegate::DoFile(std::string path) {
2021-11-19 00:37:35 +08:00
if(mVM != nullptr){
int ret = luaL_dofile(mVM, path.c_str());
if (ret > 0){
printf("lua error");
return -1;
}
}
return 0;
}