lua 模板封装

This commit is contained in:
zcy 2022-01-29 10:37:21 +08:00
parent 9e0316c15a
commit ad524aab97
3 changed files with 45 additions and 28 deletions

View File

@ -27,70 +27,87 @@ public:
int UpdateScript(std::string); int UpdateScript(std::string);
void pushstack(lua_Number arg) { int pushstack(lua_Number arg) {
lua_pushnumber(mVM, arg); lua_pushnumber(mVM, arg);
return 1;
} }
void pushstack(lua_Integer arg) { int pushstack(lua_Integer arg) {
lua_pushinteger(mVM, arg); lua_pushinteger(mVM, arg);
return 1;
} }
void pushstack(void* arg) { int pushstack(void* arg) {
lua_pushlightuserdata(mVM, arg); lua_pushlightuserdata(mVM, arg);
return 1;
} }
void pushstack(const char* arg) { int pushstack(const char* arg) {
lua_pushstring(mVM,arg); lua_pushstring(mVM,arg);
return 1;
} }
void pushstack(bool arg) { int pushstack(bool arg) {
lua_pushboolean (mVM, arg); lua_pushboolean (mVM, arg);
return 1;
} }
void pushstack(std::string arg) { int pushstack(std::string arg) {
lua_pushstring(mVM, arg.c_str()); lua_pushstring(mVM, arg.c_str());
return 1;
} }
void pushstack(lua_CFunction fn, int n) { int pushstack(lua_CFunction fn, int n) {
lua_pushcclosure(mVM, fn, n); lua_pushcclosure(mVM, fn, n);
return 1;
} }
template <typename ... Types> template <typename ... Types>
void pushstack(lua_Number arg1, Types... rest) { int pushstack(lua_Number arg1, Types... rest) {
lua_pushnumber(mVM, arg1); lua_pushnumber(mVM, arg1);
pushstack(rest...); int ret = pushstack(rest...) + 1;
return ret;
} }
template <typename ... Types> template <typename ... Types>
void pushstack(lua_Integer arg1, Types... rest) { int pushstack(lua_Integer arg1, Types... rest) {
lua_pushinteger (mVM, arg1); lua_pushinteger (mVM, arg1);
pushstack(rest...); int ret = pushstack(rest...) + 1;
return ret;
} }
template <typename ... Types> template <typename ... Types>
void pushstack(bool arg1, Types... rest) { int pushstack(bool arg1, Types... rest) {
lua_pushboolean(mVM, arg1); lua_pushboolean(mVM, arg1);
pushstack(rest...); int ret = pushstack(rest...) + 1;
return ret;
} }
template <typename ... Types> template <typename ... Types>
void pushstack(std::string arg1, Types... rest) { int pushstack(std::string arg1, Types... rest) {
lua_pushstring(mVM, arg1.c_str()); lua_pushstring(mVM, arg1.c_str());
pushstack(rest...); int ret = pushstack(rest...) + 1;
return ret;
} }
template <typename ... Types> template <typename ... Types>
void pushstack(void* arg1, Types... rest) { int pushstack(void* arg1, Types... rest) {
lua_pushlightuserdata(mVM, arg1); lua_pushlightuserdata(mVM, arg1);
pushstack(rest...); int ret = pushstack(rest...) + 1;
return ret;
} }
template <typename ... Types> template <typename ... Types>
void pushstack(lua_CFunction fn, int n, Types... rest) { int pushstack(lua_CFunction fn, int n, Types... rest) {
lua_pushcclosure(mVM, fn,n); lua_pushcclosure(mVM, fn,n);
pushstack(rest...); int ret = pushstack(rest...) + 1;
return ret;
} }
template <typename ... Types> template <typename ... Types>
void pushstack(const char* arg1, Types... rest) { int pushstack(const char* arg1, Types... rest) {
lua_pushstring(mVM, arg1); lua_pushstring(mVM, arg1);
pushstack(rest...); int ret = pushstack(rest...) + 1;
return ret;
} }
template<typename... T> template<typename... T>
void CallFuntion(std::string name,T... para){ int CallFuntion(std::string name,T... para){
int i = lua_getglobal(mVM,name.c_str()); int i = lua_getglobal(mVM,name.c_str());
if(i < 0){ if(LUA_TNIL == i){
return ; return -1;
} }
pushstack(para...); int ret = pushstack(para...);
std::cout << "parameter count is " << ret;
lua_call(mVM, ret, 0);
return 0;
} }
lua_State* VM(); lua_State* VM();
void PrintError(lua_State *L); void PrintError(lua_State *L);

View File

@ -57,8 +57,8 @@ int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
LuaDelegate lua; LuaDelegate lua;
std::cout << std::endl; std::cout << std::endl;
lua.CallFuntion< lua_Number>(std::string("sds"), // lua.CallFuntion< lua_Number>(std::string("sds"),
123.0); // 123.0);
UNREFERENCED_PARAMETER(hPrevInstance); UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine); UNREFERENCED_PARAMETER(lpCmdLine);

View File

@ -48,7 +48,7 @@ UartForm::UartForm(ui::Window* hwnd,std::wstring name,
if (0 < SerialPort::ReadPort(PortNum, recv, 1024)) { if (0 < SerialPort::ReadPort(PortNum, recv, 1024)) {
printf("recv data: %s", recv); printf("recv data: %s", recv);
this->m_show_recv += string2wstring(recv); this->m_show_recv += string2wstring(recv);
this->mLua->OnSerialData(recv); this->mLua->CallFuntion<std::string>("OnUartData", std::string(recv));
if (this->mEditRecv != nullptr) { if (this->mEditRecv != nullptr) {
this->mEditRecv->AppendText(string2wstring(recv), false); this->mEditRecv->AppendText(string2wstring(recv), false);
::PostMessage(this->GetWindow()->GetHWND(), WM_ADD_UART_RECVDATA,0, 0); ::PostMessage(this->GetWindow()->GetHWND(), WM_ADD_UART_RECVDATA,0, 0);