Running OK with luaconsole.cpp

This commit is contained in:
2021-01-23 16:31:16 -05:00
parent b897c10506
commit 37a05fe1ac
4 changed files with 30 additions and 37 deletions

View File

@@ -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);
}

View File

@@ -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) {

View File

@@ -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());
}

View File

@@ -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() {