A few assorted cleanups.

This commit is contained in:
2021-10-13 19:41:59 -04:00
parent bf3dd49cc8
commit 5b3f3b7ff0
7 changed files with 35 additions and 9 deletions

View File

@@ -391,6 +391,8 @@ public:
void makeclass(LuaSlot tab, const char *name) const; void makeclass(LuaSlot tab, const char *name) const;
void makeclass(LuaSlot tab, const std::string &name) const; void makeclass(LuaSlot tab, const std::string &name) const;
std::string classname(LuaSlot tab); std::string classname(LuaSlot tab);
// There's no 'isclass' operator, but 'classname' will return empty
// string if tab is not a valid class.
void movesortablekey(LuaSlot val, LuaStack &other, LuaSlot otherslot); void movesortablekey(LuaSlot val, LuaStack &other, LuaSlot otherslot);

View File

@@ -126,7 +126,6 @@ public:
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
//TextGame tg; TextGame tg;
TNTest4 tg;
driver_drive(&tg); driver_drive(&tg);
} }

View File

@@ -46,7 +46,9 @@ static void l_message(const char *msg)
} }
void TextGame::do_lua(const std::string &exp) { void TextGame::do_lua(const std::string &exp) {
assert(world_->stack_is_clear());
lua_State *L = world_->state(); lua_State *L = world_->state();
// push the compiled function.
int status = luaL_loadbuffer(L, exp.c_str(), exp.size(), "=stdin"); int status = luaL_loadbuffer(L, exp.c_str(), exp.size(), "=stdin");
assert(status == LUA_OK); assert(status == LUA_OK);
globalL = L; globalL = L;
@@ -67,6 +69,7 @@ void TextGame::do_lua(const std::string &exp) {
lua_pop(L, 1); lua_pop(L, 1);
lua_gc(L, LUA_GCCOLLECT, 0); lua_gc(L, LUA_GCCOLLECT, 0);
} }
assert(world_->stack_is_clear());
} }
void TextGame::do_view_command(const StringVec &cmd) { void TextGame::do_view_command(const StringVec &cmd) {
@@ -138,6 +141,17 @@ void TextGame::do_rollback_command(const StringVec &cmd) {
world_->rollback(); world_->rollback();
} }
void TextGame::do_tick_command(const StringVec &cmd) {
int64_t clock;
if (cmd.size() == 2) {
clock = util::strtoint(cmd[1], -1);
} else {
std::cerr << "t command (tick) expects a time value" << std::endl;
return;
}
world_->run_scheduled_threads(clock);
}
void TextGame::do_quit_command(const StringVec &cmd) { void TextGame::do_quit_command(const StringVec &cmd) {
if (cmd.size() != 1) { if (cmd.size() != 1) {
std::cerr << "q command (quit) takes no arguments" << std::endl; std::cerr << "q command (quit) takes no arguments" << std::endl;
@@ -153,6 +167,7 @@ void TextGame::do_command(const StringVec &words) {
else if (words[0] == "q") do_quit_command(words); else if (words[0] == "q") do_quit_command(words);
else if (words[0] == "s") do_snapshot_command(words); else if (words[0] == "s") do_snapshot_command(words);
else if (words[0] == "r") do_rollback_command(words); else if (words[0] == "r") do_rollback_command(words);
else if (words[0] == "t") do_tick_command(words);
else if (util::validinteger(words[0])) do_choose_command(words); else if (util::validinteger(words[0])) do_choose_command(words);
else { else {
std::cerr << "Unknown command: " << words[0] << std::endl; std::cerr << "Unknown command: " << words[0] << std::endl;

View File

@@ -22,6 +22,7 @@ private:
void do_quit_command(const StringVec &cmd); void do_quit_command(const StringVec &cmd);
void do_snapshot_command(const StringVec &cmd); void do_snapshot_command(const StringVec &cmd);
void do_rollback_command(const StringVec &cmd); void do_rollback_command(const StringVec &cmd);
void do_tick_command(const StringVec &cmd);
void do_lua(const std::string &exp); void do_lua(const std::string &exp);
void do_command(const StringVec &exp); void do_command(const StringVec &exp);

View File

@@ -40,7 +40,11 @@ LuaDefine(tangible_setclass, "c") {
LuaStack LS(L, tanobj, classname, classtab, mt); LuaStack LS(L, tanobj, classname, classtab, mt);
World *w = World::fetch_global_pointer(L); World *w = World::fetch_global_pointer(L);
w->tangible_get(LS, tanobj); w->tangible_get(LS, tanobj);
LS.getclass(classtab, classname); if (LS.classname(classname) != "") {
LS.set(classtab, classname);
} else {
LS.getclass(classtab, classname);
}
LS.getmetatable(mt, tanobj); LS.getmetatable(mt, tanobj);
LS.rawset(mt, "__index", classtab); LS.rawset(mt, "__index", classtab);
return LS.result(); return LS.result();

View File

@@ -403,10 +403,14 @@ void World::invoke_plan(int64_t actor_id, int64_t place_id, const std::string &a
// Create a new thread, set up function and parameters. // Create a new thread, set up function and parameters.
lua_State *CO = LS.newthread(thread); lua_State *CO = LS.newthread(thread);
// Push the function to be invoked and its arguments.
lua_pushvalue(L, func.index()); lua_pushvalue(L, func.index());
lua_pushvalue(L, actor.index()); lua_pushvalue(L, actor.index());
lua_pushvalue(L, place.index()); lua_pushvalue(L, place.index());
lua_pushvalue(L, invdata.index()); lua_pushvalue(L, invdata.index());
// Move actor, place, function and args to new thread.
lua_xmove(L, CO, 4); lua_xmove(L, CO, 4);
// Store the thread into place's thread table. // Store the thread into place's thread table.
@@ -454,7 +458,7 @@ void World::run_scheduled_threads(int64_t clk) {
// Resume the coroutine. // Resume the coroutine.
lua_State *CO = LS.ckthread(thread); lua_State *CO = LS.ckthread(thread);
int top = lua_gettop(CO); int top = lua_gettop(CO);
int status = lua_resume(CO, nullptr, (top > 0) ? (top - 1) : 0); int status = lua_resume(CO, nullptr, (top >= 4) ? 3 : 0);
// Three possible outcomes: finished, yielded, or errored. // Three possible outcomes: finished, yielded, or errored.
if (status == LUA_YIELD) { if (status == LUA_YIELD) {

View File

@@ -199,19 +199,20 @@ public:
void snapshot(); void snapshot();
void rollback(); void rollback();
private:
// Run any threads which according to the scheduler queue are ready. // Run any threads which according to the scheduler queue are ready.
// //
void run_scheduled_threads(int64_t clk); void run_scheduled_threads(int64_t clk);
// Store a pointer to a world model into a lua registry.
//
static void store_global_pointer(lua_State *L, World *w);
// Check that the main thread has nothing on the stack // Check that the main thread has nothing on the stack
// //
bool stack_is_clear() const { return lua_gettop(state()) == 0; } bool stack_is_clear() const { return lua_gettop(state()) == 0; }
private:
// Store a pointer to a world model into a lua registry.
//
static void store_global_pointer(lua_State *L, World *w);
// Invoke a plan. // Invoke a plan.
// //
void invoke_plan(int64_t actor_id, int64_t place_id, const std::string &action, const InvocationData &idata); void invoke_plan(int64_t actor_id, int64_t place_id, const std::string &action, const InvocationData &idata);