diff options
author | Bent Bisballe Nyeng <deva@aasimon.org> | 2012-08-23 20:23:19 +0200 |
---|---|---|
committer | Bent Bisballe Nyeng <deva@aasimon.org> | 2012-08-23 20:23:19 +0200 |
commit | dcecacbf52121d6f8adf076d2cebaeec1e141339 (patch) | |
tree | 04f36b2a55f221985108c1a3d701b184ac5dc52d /src/luascript.cc | |
parent | 1fc6eb2c1a8500602f2c45662dabbf164210a5b3 (diff) |
Implement automatic reset/reload when script file changes on disc.
Diffstat (limited to 'src/luascript.cc')
-rw-r--r-- | src/luascript.cc | 154 |
1 files changed, 58 insertions, 96 deletions
diff --git a/src/luascript.cc b/src/luascript.cc index 1699cdf..fb6a7db 100644 --- a/src/luascript.cc +++ b/src/luascript.cc @@ -27,6 +27,8 @@ */ #include "luascript.h" +#include <QApplication> + // For atoi #include <stdlib.h> @@ -69,6 +71,12 @@ static int _forward(lua_State *L) return 1; } + if(lua->lua_stop) { + printf("stopping...\n"); + lua_pushstring(L, "stop"); + lua_error(L); + } + lua->out->forward(x * 5); return 0; @@ -93,7 +101,13 @@ static int _turn(lua_State *L) return 1; } - lua->out->turn(-x); + if(lua->lua_stop) { + printf("stopping...\n"); + lua_pushstring(L, "stop"); + lua_error(L); + } + + lua->out->turn(-x * 10); return 0; } @@ -106,7 +120,7 @@ static int _reset(lua_State *L) */ printf("reset\n"); - + lua_getglobal(L, GLOBAL_POINTER); LUAScript *lua = (LUAScript*)lua_touserdata(L, lua_gettop(L)); @@ -116,24 +130,31 @@ static int _reset(lua_State *L) return 1; } + if(lua->lua_stop) { + lua_pushstring(L, "stop"); + lua_error(L); + } + lua->out->reset(); return 0; } -LUAScript::LUAScript(OutputWindow *o) +LUAScript::LUAScript(OutputWindow *o, QString f) { DEBUG(luascript, "LUAScript()\n"); + file = f; L = NULL; out = o; + + // Don't call init from here, it need to be done in the thread. + + lua_stop = false; + lua_stopped = true; } LUAScript::~LUAScript() { - if(L) { - free(L); - L = NULL; - } DEBUG(luascript, "~LUAScript()\n"); } @@ -157,58 +178,45 @@ void LUAScript::init() lua_register(L, "fremad", _forward); lua_register(L, "drej", _turn); lua_register(L, "reset", _reset); - - connect(&watcher, SIGNAL(fileChanged(const QString &)), - this, SLOT(reload())); } -void LUAScript::addFile(std::string src) +void LUAScript::cleanup() { - file = src.c_str(); - FILE *fp = fopen(src.c_str(), "r"); - if(fp) { - char buf[64]; - size_t sz; - std::string inc; - while((sz = fread(buf, 1, sizeof(buf), fp)) != 0) { - inc.append(buf, sz); - } - fclose(fp); - addCode(inc, src); + if(L) { + lua_close(L); + L = NULL; } - - watcher.addPath(file); -} - -void LUAScript::reload() -{ - printf("Reload\n"); - // luaL_error(L, "Terminated due to reload."); - // runScript(); -} - -void LUAScript::addCode(std::string c, std::string name) -{ - scripts.push_back(std::make_pair(c, name)); } void LUAScript::run() { - /* - while(true) { - printf("."); fflush(stdout); - } - */ + lua_stopped = false; try { + init(); runScript(); } catch(Exception &e) { printf("LUA Error: %s\n", e.msg.c_str()); } + cleanup(); + lua_stopped = true; + printf("LUA Thread Stopped!\n"); +} + +void LUAScript::stopScript() +{ + lua_stop = true; + while(!lua_stopped) { + wait(25); + qApp->processEvents(); + printf("!\n"); + } } void LUAScript::runScript() throw(Exception) { + lua_stop = false; + try { init(); } catch(Exception &e) { @@ -222,62 +230,16 @@ void LUAScript::runScript() top = lua_gettop(L); - std::vector<std::pair<std::string, std::string> >::iterator i = - scripts.begin(); - while(i != scripts.end()) { - std::string program = i->first; - std::string codename = name(); - if(i->second != "") codename += ": " + i->second; - - DEBUG(luascript, "Running %s: %s\n", codename.c_str(), program.c_str()); + // DEBUG(luascript, "Running %s\n", file); - if(luaL_loadbuffer(L, program.c_str(), program.size(), codename.c_str())) { - ERR(luascript, "loadbuffer: %s\n", lua_tostring(L, lua_gettop(L))); - throw Exception(lua_tostring(L, lua_gettop(L))); - } - - // Run the loaded code - if(lua_pcall(L, 0, LUA_MULTRET, 0)) { - ERR(luascript, "pcall: %s\n" , lua_tostring(L, lua_gettop(L))); - throw Exception(lua_tostring(L, lua_gettop(L))); - } - - i++; + if(luaL_loadfile(L, file.toStdString().c_str())) { + ERR(luascript, "loadbuffer: %s\n", lua_tostring(L, lua_gettop(L))); + throw Exception(lua_tostring(L, lua_gettop(L))); } -} - -std::string LUAScript::resultString() throw(Exception) -{ - if(top != lua_gettop(L) - 1) { - ERR(luascript, "Program did not return a single value.\n"); - throw Exception("Program did not return a single value."); - } - - if(lua_isstring(L, lua_gettop(L)) == false) { - ERR(luascript, "Program did not return a string value.\n"); - throw Exception("Program did not return a string value."); + + // Run the loaded code + if(lua_pcall(L, 0, LUA_MULTRET, 0)) { + ERR(luascript, "pcall: %s\n" , lua_tostring(L, lua_gettop(L))); + throw Exception(lua_tostring(L, lua_gettop(L))); } - - std::string res = lua_tostring(L, lua_gettop(L)); - lua_pop(L, 1); - - return res; } - -#ifdef TEST_LUASCRIPT -//Additional dependency files -//deps: -//Required cflags (autoconf vars may be used) -//cflags: -//Required link options (autoconf vars may be used) -//libs: -#include "test.h" - -TEST_BEGIN; - -// TODO: Put some testcode here (see test.h for usable macros). -TEST_TRUE(false, "No tests yet!"); - -TEST_END; - -#endif/*TEST_LUASCRIPT*/ |