From d109b6570bafb30740ce35449d8989f08ca79560 Mon Sep 17 00:00:00 2001 From: Alexander Gavrilov Date: Sun, 1 Apr 2012 19:38:42 +0400 Subject: [PATCH] Add dfhack.with_suspend(f[, args...]) that calls f with core suspended. The lock is properly removed in case of error, which is then propagated. Just for fun, it also can be yielded from within in a coroutine. --- depends/lua/CMakeLists.txt | 3 +-- library/LuaTools.cpp | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/depends/lua/CMakeLists.txt b/depends/lua/CMakeLists.txt index 2e74ab0c9..3a56aa639 100644 --- a/depends/lua/CMakeLists.txt +++ b/depends/lua/CMakeLists.txt @@ -1,8 +1,7 @@ PROJECT ( lua CXX ) CMAKE_MINIMUM_REQUIRED(VERSION 2.8) -# TODO: make this RelWithDebInfo only -ADD_DEFINITIONS(-DLUA_USE_APICHECK) +SET(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-DLUA_USE_APICHECK") IF(WIN32) ADD_DEFINITIONS(-D_CRT_SECURE_NO_DEPRECATE ) diff --git a/library/LuaTools.cpp b/library/LuaTools.cpp index 77eda125a..f81396f95 100644 --- a/library/LuaTools.cpp +++ b/library/LuaTools.cpp @@ -337,12 +337,44 @@ static int lua_dfhack_interpreter(lua_State *state) return 1; } +static int lua_dfhack_with_suspend(lua_State *L) +{ + int ctx; + int rv = lua_getctx(L, &ctx); + + // Non-resume entry point: + if (rv == LUA_OK) + { + int nargs = lua_gettop(L); + + luaL_checktype(L, 1, LUA_TFUNCTION); + + Core::getInstance().Suspend(); + + lua_pushcfunction(L, traceback); + lua_insert(L, 1); + + rv = lua_pcallk(L, nargs-1, LUA_MULTRET, 1, 0, lua_dfhack_with_suspend); + } + + // Return, resume, or error entry point: + lua_remove(L, 1); + + Core::getInstance().Resume(); + + if (rv != LUA_OK && rv != LUA_YIELD) + lua_error(L); + + return lua_gettop(L); +} + static const luaL_Reg dfhack_funcs[] = { { "print", lua_dfhack_print }, { "println", lua_dfhack_println }, { "printerr", lua_dfhack_printerr }, { "traceback", traceback }, { "interpreter", lua_dfhack_interpreter }, + { "with_suspend", lua_dfhack_with_suspend }, { NULL, NULL } };