diff --git a/luprex/cpp/luaconsole.cpp b/luprex/cpp/luaconsole.cpp index 60518cff..f48655e9 100644 --- a/luprex/cpp/luaconsole.cpp +++ b/luprex/cpp/luaconsole.cpp @@ -60,8 +60,8 @@ void LuaConsole::add(std::string line) { state_ = STATE_SYNTAX; } } else { - state_ = STATE_COMPLETE; syntax_ = ""; + state_ = STATE_COMPLETE; } lua_settop(lua_state_, top); } diff --git a/luprex/cpp/luastack.cpp b/luprex/cpp/luastack.cpp index 1e35fd3b..4d1bb38f 100644 --- a/luprex/cpp/luastack.cpp +++ b/luprex/cpp/luastack.cpp @@ -48,6 +48,7 @@ void LuaStack::register_all_userdata(lua_State *L) { LS.rawset(LuaRegistry, lud, tab); } } + LS.result(); } LuaFunctionReg::LuaFunctionReg(const char *m, const char *n, lua_CFunction f) { diff --git a/luprex/cpp/textgame.cpp b/luprex/cpp/textgame.cpp index d6550f7a..d0f82cda 100644 --- a/luprex/cpp/textgame.cpp +++ b/luprex/cpp/textgame.cpp @@ -55,51 +55,45 @@ static int report(lua_State *L, int status) return status; } -static int docall(lua_State *L, int narg, int nret) +static void doexp(lua_State *L, const std::string &exp) { + int status = luaL_loadbuffer(L, exp.c_str(), exp.size(), "=stdin"); + assert(status == LUA_OK); + globalL = L; signal(SIGINT, laction); - int status = traceback_pcall(L, narg, nret); + status = traceback_pcall(L, 0, LUA_MULTRET); signal(SIGINT, SIG_DFL); - /* force a complete garbage collection in case of errors */ - if (status != LUA_OK) + if (status != LUA_OK) { + report(L, status); lua_gc(L, LUA_GCCOLLECT, 0); - return status; + } + if (status == LUA_OK && lua_gettop(L) > 0) { + lua_getglobal(L, "pprint"); + if (lua_isnil(L, -1)) { + lua_pop(L, 1); + lua_getglobal(L, "print"); + } + lua_insert(L, 1); + if (lua_pcall(L, lua_gettop(L) - 1, 0, 0) != 0) { + l_message( + lua_pushfstring(L, "error calling 'print' (%s)", + lua_tostring(L, -1))); + } + } } -static void dotty(lua_State *L) +void TextGame::run() { LuaConsole console; - int status; - while (true) { + console.add_stdin(); switch (console.state()) { case LuaConsole::STATE_INCOMPLETE: - console.add_stdin(); break; - case LuaConsole::STATE_COMPLETE: { - const std::string &exp = console.expression(); - status = luaL_loadbuffer(L, exp.c_str(), exp.size(), "=stdin"); - assert(status == LUA_OK); - status = docall(L, 0, LUA_MULTRET); - report(L, status); - if (status == LUA_OK && lua_gettop(L) > 0) { - // any result to print? - lua_getglobal(L, "pprint"); - if (lua_isnil(L, -1)) { - lua_pop(L, 1); - lua_getglobal(L, "print"); - } - lua_insert(L, 1); - if (lua_pcall(L, lua_gettop(L) - 1, 0, 0) != 0) - { - l_message( - lua_pushfstring(L, "error calling 'print' (%s)", - lua_tostring(L, -1))); - } - } + case LuaConsole::STATE_COMPLETE: + doexp(viewer_.get_lua_state(), console.expression()); console.clear(); break; - } case LuaConsole::STATE_SLASHCOMMAND: std::cerr << "Slash commands not supported yet." << std::endl; console.clear(); @@ -112,8 +106,3 @@ static void dotty(lua_State *L) } } -void TextGame::run() -{ - dotty(viewer_.get_lua_state()); -} - diff --git a/luprex/cpp/world.cpp b/luprex/cpp/world.cpp index e0f04d03..ac060655 100644 --- a/luprex/cpp/world.cpp +++ b/luprex/cpp/world.cpp @@ -40,6 +40,9 @@ World::World() { // Initialize the SourceDB source_db_.initialize(lua_state_); source_db_.rebuild(); + + LS.result(); + assert (lua_gettop(lua_state_) == 0); } void Tangible::be_a_player() {