From 0febf6ea9cbd1a6e04e41339fc46d2e6b07da5e7 Mon Sep 17 00:00:00 2001 From: deva Date: Tue, 3 Jun 2008 14:45:48 +0000 Subject: LUA rocks --- client/lua.cc | 128 +++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 99 insertions(+), 29 deletions(-) (limited to 'client/lua.cc') diff --git a/client/lua.cc b/client/lua.cc index c803e23..2299acc 100644 --- a/client/lua.cc +++ b/client/lua.cc @@ -26,40 +26,91 @@ */ #include "lua.h" -LUA::LUA(Variables &variables) +#include "macrowindow.h" + +#define GLOBAL_POINTER "_pracroGlobalLUAObjectPointer" + +LUA *glua; + +static int _getValue(lua_State *L) { - L = luaL_newstate(); - if(L == NULL) { - // throw LUADataParserException("Could not create LUA state."); + int n = lua_gettop(L); // number of arguments + if(n != 1) { + char errstr[512]; + sprintf(errstr, "Number of args expected 0, got %d", n); + fprintf(stderr, errstr); + lua_pushstring(L, errstr); + lua_error(L); + return 1; } - luaL_openlibs(L); + QString name = lua_tostring(L, lua_gettop(L)); - std::string preload; - Variables::iterator var = variables.begin(); - while(var != variables.end()) { - preload += (*var).first + " = \"" + (*var).second + "\"\n"; - var++; + lua_getglobal(L, GLOBAL_POINTER); + LUA *lua = glua;//(LUA*)lua_touserdata(L, 1); + + if(!lua) { + printf("No LUA pointer!\n"); + lua_pushstring(L, "No LUA pointer!"); + lua_error(L); + return 1; } - - // printf("preload: [%s]\n", preload.c_str()); - int s = luaL_loadbuffer(L, preload.c_str(), preload.size(), "preload"); - switch(s) { - case 0: //no errors; - break; - case LUA_ERRSYNTAX: //syntax error during pre-compilation; - case LUA_ERRMEM: //memory allocation error. - case LUA_ERRFILE: //cannot open/read the file. - //throw LUADataParserException(lua_tostring(L, lua_gettop(L))); - break; - default: - //throw LUADataParserException("Unknown return value of luaL_loadfile."); - break; + QString value = lua->getValue(name); + lua_pushstring(L, value.toStdString().c_str()); + + return 1; +} + +static int _setValue(lua_State *L) +{ + int n = lua_gettop(L); // number of arguments + if(n != 2) { + char errstr[512]; + sprintf(errstr, "Number of args expected 0, got %d", n); + fprintf(stderr, errstr); + lua_pushstring(L, errstr); + lua_error(L); + return 0; } - // Run program (init) - lua_pcall(L, 0, LUA_MULTRET, 0); + QString value = lua_tostring(L, lua_gettop(L)); + lua_pop(L, 1); + QString name = lua_tostring(L, lua_gettop(L)); + lua_pop(L, 1); + + lua_getglobal(L, GLOBAL_POINTER); + LUA *lua = glua;//(LUA*)lua_touserdata(L, 1); + + if(!lua) { + printf("No LUA pointer!\n"); + lua_pushstring(L, "No LUA pointer!"); + lua_error(L); + return 1; + } + + lua->setValue(name, value); + + return 0; +} + +LUA::LUA(MacroWindow *macrowindow) +{ + glua = this; + this->macrowindow = macrowindow; + + L = luaL_newstate(); + if(L == NULL) { + // throw LUADataParserException("Could not create LUA state."); + } + + luaL_openlibs(L); + + lua_pushlightuserdata(L, this); // Push the pointer to 'this' instance + lua_setglobal(L, GLOBAL_POINTER); // Assign it to a global lua var. + + lua_register(L, "getValue", _getValue); + lua_register(L, "setValue", _setValue); } LUA::~LUA() @@ -67,9 +118,28 @@ LUA::~LUA() lua_close(L); } -std::string LUA::run(std::string &program) +QString LUA::getValue(QString name) +{ + return macrowindow->getValue(name); +} + +void LUA::setValue(QString name, QString value) +{ + macrowindow->setValue(name, value); +} + +bool LUA::run(QString program, QString value) { - int s = luaL_loadbuffer(L, program.c_str(), program.size(), "program"); + if(macrowindow->luaprograms.contains(program) == false) return false; + + QString luacode = "value = " + value + "\n"; + QString luaprogram = macrowindow->luaprograms.value(program); + luacode += luaprogram; + + int s = luaL_loadbuffer(L, + luacode.toStdString().c_str(), + luacode.size(), + program.toStdString().c_str()); switch(s) { case 0: //no errors; break; @@ -86,7 +156,7 @@ std::string LUA::run(std::string &program) // Run the loaded code lua_pcall(L, 0, LUA_MULTRET, 0); - std::string res = lua_tostring(L, lua_gettop(L)); + bool res = lua_toboolean(L, lua_gettop(L)); lua_pop(L, 1); return res; -- cgit v1.2.3