diff options
| author | Alexander Gavrilov | 2012-04-01 19:38:42 +0400 |
|---|---|---|
| committer | Alexander Gavrilov | 2012-04-01 19:38:42 +0400 |
| commit | d109b6570bafb30740ce35449d8989f08ca79560 (patch) | |
| tree | 00f72f8dab42ee393bf41d18e6b09381706d6b24 /library/LuaTools.cpp | |
| parent | e3d50b9b042165b352ffd041395844fd57d36db8 (diff) | |
| download | dfhack-d109b6570bafb30740ce35449d8989f08ca79560.tar.gz dfhack-d109b6570bafb30740ce35449d8989f08ca79560.tar.bz2 dfhack-d109b6570bafb30740ce35449d8989f08ca79560.tar.xz | |
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.
Diffstat (limited to 'library/LuaTools.cpp')
| -rw-r--r-- | library/LuaTools.cpp | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/library/LuaTools.cpp b/library/LuaTools.cpp index 77eda125..f81396f9 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 } }; |
