From c63e91285b0775d1e3f35f0253b8ca1a35e932f5 Mon Sep 17 00:00:00 2001 From: maelstrom Date: Fri, 11 Jul 2025 09:46:50 +0200 Subject: [PATCH] refactor(lua): pre-parse pcall wrapper ahead of time --- core/src/datatypes/signal.cpp | 3 +-- core/src/objects/script.cpp | 5 +---- core/src/objects/service/script/scriptcontext.cpp | 6 ++++++ 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/core/src/datatypes/signal.cpp b/core/src/datatypes/signal.cpp index 1ec0986..e369ea3 100644 --- a/core/src/datatypes/signal.cpp +++ b/core/src/datatypes/signal.cpp @@ -7,7 +7,6 @@ #include #include -extern const char* WRAPPER_SRC; // TODO: Move this to a shared header int script_errhandler(lua_State*); // extern SignalSource::SignalSource() : std::shared_ptr(std::make_shared()) {} @@ -62,7 +61,7 @@ void LuaSignalConnection::Call(std::vector args) { lua_State* thread = lua_newthread(state); // Push wrapepr as thread function - luaL_loadbuffer(thread, WRAPPER_SRC, strlen(WRAPPER_SRC), "=PCALL_WRAPPER"); + lua_getfield(thread, LUA_REGISTRYINDEX, "LuaPCallWrapper"); // Push function as upvalue for wrapper lua_rawgeti(thread, LUA_REGISTRYINDEX, function); diff --git a/core/src/objects/script.cpp b/core/src/objects/script.cpp index 33a625f..1588c6f 100644 --- a/core/src/objects/script.cpp +++ b/core/src/objects/script.cpp @@ -16,9 +16,6 @@ int script_wait(lua_State*); int script_delay(lua_State*); int script_errhandler(lua_State*); -// TODO: Move this to a shared header -const char* WRAPPER_SRC = "local func, errhandler = ... return function(...) local args = {...} xpcall(function() func(unpack(args)) end, errhandler) end"; - Script::Script(): Instance(&TYPE) { source = "print(\"Hello, world!\")"; } @@ -59,7 +56,7 @@ void Script::Run() { lua_pop(Lt, 1); // _G // Push wrapper as thread function - luaL_loadbuffer(Lt, WRAPPER_SRC, strlen(WRAPPER_SRC), "=PCALL_WRAPPER"); + lua_getfield(Lt, LUA_REGISTRYINDEX, "LuaPCallWrapper"); // Load source code and push onto thread as upvalue for wrapper int status = luaL_loadbuffer(Lt, source.c_str(), source.size(), this->GetFullName().c_str()); diff --git a/core/src/objects/service/script/scriptcontext.cpp b/core/src/objects/service/script/scriptcontext.cpp index 6c3c0f4..b071350 100644 --- a/core/src/objects/service/script/scriptcontext.cpp +++ b/core/src/objects/service/script/scriptcontext.cpp @@ -8,6 +8,8 @@ #include #include "luaapis.h" // IWYU pragma: keep +const char* WRAPPER_SRC = "local func, errhandler = ... return function(...) local args = {...} xpcall(function() func(unpack(args)) end, errhandler) end"; + static int g_print(lua_State*); static int g_require(lua_State*); static const struct luaL_Reg luaglobals [] = { @@ -48,6 +50,10 @@ void ScriptContext::InitService() { Color3::PushLuaLibrary(state); Instance::PushLuaLibrary(state); + // Add wrapper function + luaL_loadbuffer(state, WRAPPER_SRC, strlen(WRAPPER_SRC), "=PCALL_WRAPPER"); + lua_setfield(state, LUA_REGISTRYINDEX, "LuaPCallWrapper"); + // TODO: custom os library // Override print