From 629f672d38fff9ca8b730fb7d24863dffb847836 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Mon, 27 Apr 2026 00:24:25 +0300 Subject: [PATCH] cleanup internal functions --- res/scripts/stdlib.lua | 24 ++++++++++++++++-------- res/scripts/stdmin.lua | 18 ++++++++++++------ src/logic/scripting/lua/lua_engine.cpp | 2 ++ src/logic/scripting/lua/lua_util.cpp | 6 +++--- src/logic/scripting/lua/lua_util.hpp | 3 ++- src/logic/scripting/scripting.cpp | 8 ++------ 6 files changed, 37 insertions(+), 24 deletions(-) diff --git a/res/scripts/stdlib.lua b/res/scripts/stdlib.lua index 3a7ecf02d..0e72ad86d 100644 --- a/res/scripts/stdlib.lua +++ b/res/scripts/stdlib.lua @@ -540,20 +540,27 @@ function __vc_on_world_save() file.write(RULES_FILE, toml.tostring(rule_values)) end +local __close_all_descriptors = file.__close_all_descriptors +local __gui_util_reset_local = gui_util.__reset_local +local __stdcomp_reset = stdcomp.__reset +file.__close_all_descriptors = nil +gui_util.__reset_local = nil +stdcomp.reset = nil + function __vc_on_world_quit() _rules.clear() - gui_util:__reset_local() - stdcomp.__reset() - file.__close_all_descriptors() + __gui_util_reset_local() + __stdcomp_reset() + __close_all_descriptors() end -local __post_runnables = {} - local fn_audio_reset_fetch_buffer = audio.__reset_fetch_buffer audio.__reset_fetch_buffer = nil core.get_core_token = audio.input.__get_core_token -local function __vc__process_post_runnables() +local __post_runnables = {} + +local function __process_post_runnables() if #__post_runnables > 0 then for _, func in ipairs(__post_runnables) do local status, result = xpcall(func, __vc__error) @@ -587,9 +594,9 @@ local function __vc__process_post_runnables() end end -function __process_post_runnables() +function __vc__process_post_runnables() __vc__is_post_runnable = true - local success, err = pcall(__vc__process_post_runnables) + local success, err = pcall(__process_post_runnables) if not success then debug.error("an error ocurred while processing post-runnables: ".. err) end @@ -624,3 +631,4 @@ require "core:internal/deprecated" ffi = nil __vc_app = nil __vc_lock_internal_modules() +__vc_lock_internal_modules = nil diff --git a/res/scripts/stdmin.lua b/res/scripts/stdmin.lua index fa1ec0422..70e27f561 100644 --- a/res/scripts/stdmin.lua +++ b/res/scripts/stdmin.lua @@ -192,7 +192,7 @@ function reload_module(name) end end -local internal_locked = false +local __internal_locked = false -- Load script with caching -- @@ -202,9 +202,12 @@ local internal_locked = false -- nocache - ignore cached script, load anyway function __load_script(path, nocache, env) local packname, filename = parse_path(path) + local is_internal = (packname == "res" or packname == "core") + and filename:find("modules/internal") == 1 - if internal_locked and (packname == "res" or packname == "core") - and filename:starts_with("modules/internal") then + nocache = nocache or is_internal + + if is_internal and __internal_locked then error("access to core:internal modules outside of [core]") end @@ -232,16 +235,19 @@ function __load_script(path, nocache, env) end function __vc_lock_internal_modules() - internal_locked = true + __internal_locked = true end +local __pack_envs = __vc__pack_envs +__vc__pack_envs = nil + function require(path) if not string.find(path, ':') then local prefix, _ = parse_path(_debug_getinfo(2).source) return require(prefix .. ':' .. path) end local prefix, file = parse_path(path) - local env = __vc__pack_envs[prefix] + local env = __pack_envs[prefix] return __load_script(prefix .. ":modules/" .. file .. ".lua", nil, env) end @@ -262,7 +268,7 @@ function __scripts_cleanup(non_reset_packs) __cached_scripts[k] = nil package.loaded[k] = nil end - __vc__pack_envs[packname] = nil + __pack_envs[packname] = nil ::continue:: end end diff --git a/src/logic/scripting/lua/lua_engine.cpp b/src/logic/scripting/lua/lua_engine.cpp index 5dfc8ebd1..1536cb41f 100644 --- a/src/logic/scripting/lua/lua_engine.cpp +++ b/src/logic/scripting/lua/lua_engine.cpp @@ -113,7 +113,9 @@ void lua::init_state(State* L, StateType stateType) { setglobal(L, "io"); createtable(L, 0, 0); + pushvalue(L, -1); setglobal(L, "__vc__pack_envs"); + setregistry(L, lua::PACK_ENVS_TABLE); const char* removed_os[] { "execute", "exit", "remove", "rename", "setlocale", "tmpname", nullptr}; diff --git a/src/logic/scripting/lua/lua_util.cpp b/src/logic/scripting/lua/lua_util.cpp index a563239a7..51bfb4d65 100644 --- a/src/logic/scripting/lua/lua_util.cpp +++ b/src/logic/scripting/lua/lua_util.cpp @@ -220,7 +220,7 @@ void lua::dump_stack(State* L) { static std::shared_ptr create_lambda_handler(State* L) { auto ptr = reinterpret_cast(topointer(L, -1)); auto name = util::mangleid(ptr); - reguireregistry(L, LAMBDAS_TABLE); + requireregistry(L, LAMBDAS_TABLE); pushvalue(L, -2); setfield(L, name); pop(L, 2); @@ -229,7 +229,7 @@ static std::shared_ptr create_lambda_handler(State* L) { new std::string(name), [=](std::string* name) { auto L = lua::get_main_state(); - reguireregistry(L, LAMBDAS_TABLE); + requireregistry(L, LAMBDAS_TABLE); pushnil(L); setfield(L, *name); pop(L); @@ -341,7 +341,7 @@ int lua::create_environment(State* L, int parent) { } int lua::restore_pack_environment(lua::State* L, const std::string& packid) { - if(!lua::getglobal(L, "__vc__pack_envs")) { + if(!lua::requireregistry(L, lua::PACK_ENVS_TABLE)) { return -1; } int id = nextEnvironment++; diff --git a/src/logic/scripting/lua/lua_util.hpp b/src/logic/scripting/lua/lua_util.hpp index c7fa60cca..d6a45fecd 100644 --- a/src/logic/scripting/lua/lua_util.hpp +++ b/src/logic/scripting/lua/lua_util.hpp @@ -14,6 +14,7 @@ namespace lua { inline std::string LAMBDAS_TABLE = "$L"; // lambdas storage inline std::string CHUNKS_TABLE = "$C"; // precompiled lua chunks + inline std::string PACK_ENVS_TABLE = "$P"; extern std::unordered_map usertypeNames; int userdata_destructor(lua::State* L); @@ -49,7 +50,7 @@ namespace lua { } } - inline int reguireregistry(lua::State* L, const std::string& name) { + inline int requireregistry(lua::State* L, const std::string& name) { if (getregistry(L, name)) { return 1; } else { diff --git a/src/logic/scripting/scripting.cpp b/src/logic/scripting/scripting.cpp index bf9227390..806f92c24 100644 --- a/src/logic/scripting/scripting.cpp +++ b/src/logic/scripting/scripting.cpp @@ -188,11 +188,7 @@ std::unique_ptr scripting::start_app_script(const io::path& script) { lua::pushstring(L, pack.id); lua::setfield(L, "PACK_ID"); - if(!lua::getglobal(L, "__vc__pack_envs")) { - lua::createtable(L, 0, 0); - lua::setglobal(L, "__vc__pack_envs"); - lua::pushvalue(L, -1); - } + lua::requireregistry(L, lua::PACK_ENVS_TABLE); lua::pushenv(L, id); lua::setfield(L, pack.id); lua::pop(L); @@ -248,7 +244,7 @@ std::unique_ptr scripting::start_app_script(const io::path& script) { void scripting::process_post_runnables() { auto L = lua::get_main_state(); - if (lua::getglobal(L, "__process_post_runnables")) { + if (lua::getglobal(L, "__vc__process_post_runnables")) { lua::call_nothrow(L, 0, 0); } }