aboutsummaryrefslogtreecommitdiff
path: root/src/script/lua_api/l_util.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/script/lua_api/l_util.h')
0 files changed, 0 insertions, 0 deletions
bal(L, "core"); int top = lua_gettop(L); // Push builtin initialization type lua_pushstring(L, "async"); lua_setglobal(L, "INIT"); jobDispatcher->prepareEnvironment(L, top); } /******************************************************************************/ AsyncWorkerThread::~AsyncWorkerThread() { sanity_check(!isRunning()); } /******************************************************************************/ void* AsyncWorkerThread::run() { lua_State *L = getStack(); try { loadMod(getServer()->getBuiltinLuaPath() + DIR_DELIM + "init.lua", BUILTIN_MOD_NAME); } catch (const ModError &e) { errorstream << "Execution of async base environment failed: " << e.what() << std::endl; FATAL_ERROR("Execution of async base environment failed"); } int error_handler = PUSH_ERROR_HANDLER(L); lua_getglobal(L, "core"); if (lua_isnil(L, -1)) { FATAL_ERROR("Unable to find core within async environment!"); } // Main loop LuaJobInfo j; while (!stopRequested()) { // Wait for job if (!jobDispatcher->getJob(&j) || stopRequested()) continue; lua_getfield(L, -1, "job_processor"); if (lua_isnil(L, -1)) FATAL_ERROR("Unable to get async job processor!"); luaL_checktype(L, -1, LUA_TFUNCTION); if (luaL_loadbuffer(L, j.function.data(), j.function.size(), "=(async)")) { errorstream << "ASYNC WORKER: Unable to deserialize function" << std::endl; lua_pushnil(L); } lua_pushlstring(L, j.params.data(), j.params.size()); // Call it setOriginDirect(j.mod_origin.empty() ? nullptr : j.mod_origin.c_str()); int result = lua_pcall(L, 2, 1, error_handler); if (result) { try { scriptError(result, ""); } catch (const ModError &e) { errorstream << e.what() << std::endl; } } else { // Fetch result size_t length; const char *retval = lua_tolstring(L, -1, &length); j.result.assign(retval, length); } lua_pop(L, 1); // Pop retval // Put job result if (!j.result.empty()) jobDispatcher->putJobResult(std::move(j)); } lua_pop(L, 2); // Pop core and error handler return 0; }