| 1372 | } |
| 1373 | |
| 1374 | static int dfhack_call_with_finalizer (lua_State *L) |
| 1375 | { |
| 1376 | int nargs = luaL_checkint(L, 1); |
| 1377 | if (nargs < 0) |
| 1378 | luaL_argerror(L, 1, "invalid cleanup argument count"); |
| 1379 | luaL_checktype(L, 3, LUA_TFUNCTION); |
| 1380 | |
| 1381 | // Inject errorfun |
| 1382 | lua_pushcfunction(L, dfhack_onerror); |
| 1383 | lua_insert(L, 3); |
| 1384 | |
| 1385 | int rvbase = 4+nargs; // rvbase+1 points to the function argument |
| 1386 | |
| 1387 | if (lua_gettop(L) < rvbase) |
| 1388 | luaL_error(L, "not enough arguments even to invoke cleanup"); |
| 1389 | |
| 1390 | // stack: [nargs] [always] [errorfun] [cleanup fun] [cleanup args...] |rvbase+1:| [fun] [args...] |
| 1391 | |
| 1392 | // Not enough stack to call and post-cleanup, or nothing to call? |
| 1393 | bool no_args = lua_gettop(L) == rvbase; |
| 1394 | |
| 1395 | if (!lua_checkstack(L, nargs+2) || no_args) |
| 1396 | { |
| 1397 | push_simple_error(L, no_args ? "fn argument expected" : "stack overflow"); |
| 1398 | lua_insert(L, 4); |
| 1399 | |
| 1400 | // stack: ... [errorfun] [error] [cleanup fun] [cleanup args...] |
| 1401 | do_invoke_cleanup(L, nargs, 3, false); |
| 1402 | lua_error(L); |
| 1403 | } |
| 1404 | |
| 1405 | // Actually invoke |
| 1406 | |
| 1407 | // stack: [nargs] [always] [errorfun] [cleanup fun] [cleanup args...] |rvbase+1:| [fun] [args...] |
| 1408 | return Lua::TailPCallK<dfhack_cleanup_cont>(L, lua_gettop(L)-rvbase-1, LUA_MULTRET, 3, 0); |
| 1409 | } |
| 1410 | |
| 1411 | static int lua_dfhack_with_suspend(lua_State *L) |
| 1412 | { |
nothing calls this directly
no test coverage detected