From 39ce7ff9c963dc7f10b10b67168547b88e85a248 Mon Sep 17 00:00:00 2001 From: gilzoide Date: Sat, 23 Aug 2025 18:37:38 -0300 Subject: [PATCH 1/9] Change GODOT_CLASSES and GODOT_SINGLETONS to eagerly add objects to _G This makes creating sandboxed environments easier --- CHANGELOG.md | 2 + doc_classes/LuaState.xml | 4 +- src/LuaState.cpp | 2 - src/luaopen/classes.cpp | 8 ++-- src/luaopen/godot.cpp | 2 +- src/luaopen/singleton_access.cpp | 11 ++++-- src/utils/_G_metatable.cpp | 67 -------------------------------- src/utils/_G_metatable.hpp | 33 ---------------- 8 files changed, 18 insertions(+), 111 deletions(-) delete mode 100644 src/utils/_G_metatable.cpp delete mode 100644 src/utils/_G_metatable.hpp diff --git a/CHANGELOG.md b/CHANGELOG.md index 8f65731a..e509bd9f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,6 +27,8 @@ + In "Always Evaluate" behavior, Lua code will always be evaluated + In "Don't Load" behavior, Lua code will not be loaded nor evaluated at all + Note that only evaluated scripts can be attached to Godot Objects. +- Opening `GODOT_CLASSES` now registers all classes at once instead of setting up a lazy getter in `_G`'s metatable +- Opening `GODOT_SINGLETONS` now registers all singletons at once instead of setting up a lazy getter in `_G`'s metatable ### Fixed - Fixed cyclic references from `LuaScriptInstance` <-> `LuaState`, avoiding leaks of `LuaScript`s diff --git a/doc_classes/LuaState.xml b/doc_classes/LuaState.xml index bd8e1b69..506e1528 100644 --- a/doc_classes/LuaState.xml +++ b/doc_classes/LuaState.xml @@ -285,7 +285,7 @@ [b]Note: This may override some Lua functions like [code]print[/code].[/b] - Godot singletons library. + Godot singletons library. Adds global singletons like [Engine] and [ResourceLoader] to _G. Godot classes library. Adds Godot classes like [Node] to _G. @@ -293,7 +293,7 @@ [gdscript] var lua_state = LuaState.new() lua_state.open_libraries(LuaState.Library.LUA_BASE | LuaState.Library.GODOT_CLASSES) - var vec3 = lua_state.do_string(""" + var node = lua_state.do_string(""" -- Create a Node in Lua local node = Node:new() return node diff --git a/src/LuaState.cpp b/src/LuaState.cpp index 1295fb71..94cb418d 100644 --- a/src/LuaState.cpp +++ b/src/LuaState.cpp @@ -25,7 +25,6 @@ #include "LuaTable.hpp" #include "LuaThread.hpp" #include "luaopen/godot.hpp" -#include "utils/_G_metatable.hpp" #include "utils/convert_godot_lua.hpp" #include "utils/module_names.hpp" @@ -75,7 +74,6 @@ LuaState::LuaState() : lua_state(lua_panic_handler, lua_alloc) #endif { - setup_G_metatable(lua_state); #ifdef HAVE_LUA_WARN lua_setwarnf(lua_state, lua_warn_handler, this); #endif diff --git a/src/luaopen/classes.cpp b/src/luaopen/classes.cpp index 2512d742..5bc3c433 100644 --- a/src/luaopen/classes.cpp +++ b/src/luaopen/classes.cpp @@ -19,10 +19,9 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ - #include "../utils/Class.hpp" -#include "../utils/module_names.hpp" +#include #include using namespace luagdextension; @@ -30,8 +29,11 @@ using namespace luagdextension; extern "C" int luaopen_godot_classes(lua_State *L) { sol::state_view state = L; - state.registry()[module_names::classes] = true; Class::register_usertype(state); + ClassDBSingleton *classdb = ClassDBSingleton::get_singleton(); + for (auto&& class_name : classdb->get_class_list()) { + state.set(class_name.ascii().get_data(), Class(class_name)); + } return 0; } diff --git a/src/luaopen/godot.cpp b/src/luaopen/godot.cpp index 47a78cd8..6cca070a 100644 --- a/src/luaopen/godot.cpp +++ b/src/luaopen/godot.cpp @@ -33,8 +33,8 @@ extern "C" int luaopen_godot(lua_State *L) { state.require(module_names::variant, &luaopen_godot_variant, false); state.require(module_names::utility_functions, &luaopen_godot_utility_functions, false); - state.require(module_names::singleton_access, &luaopen_godot_singleton_access, false); state.require(module_names::classes, &luaopen_godot_classes, false); + state.require(module_names::singleton_access, &luaopen_godot_singleton_access, false); state.require(module_names::enums, &luaopen_godot_enums, false); state.require(module_names::local_paths, &luaopen_godot_local_paths, false); diff --git a/src/luaopen/singleton_access.cpp b/src/luaopen/singleton_access.cpp index 41963c2a..d4a7cdc4 100644 --- a/src/luaopen/singleton_access.cpp +++ b/src/luaopen/singleton_access.cpp @@ -19,17 +19,22 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ +#include "../utils/convert_godot_lua.hpp" -#include "../utils/module_names.hpp" - +#include #include +using namespace godot; using namespace luagdextension; extern "C" int luaopen_godot_singleton_access(lua_State *L) { sol::state_view state = L; - state.registry()[module_names::singleton_access] = true; + Engine *engine = Engine::get_singleton(); + for (auto&& singleton_name : engine->get_singleton_list()) { + lua_push(L, engine->get_singleton(singleton_name)); + lua_setglobal(L, singleton_name.ascii().get_data()); + } return 0; } diff --git a/src/utils/_G_metatable.cpp b/src/utils/_G_metatable.cpp deleted file mode 100644 index 6b7eae85..00000000 --- a/src/utils/_G_metatable.cpp +++ /dev/null @@ -1,67 +0,0 @@ -/** - * Copyright (C) 2025 Gil Barbosa Reis. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy of - * this software and associated documentation files (the “Software”), to deal in - * the Software without restriction, including without limitation the rights to - * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies - * of the Software, and to permit persons to whom the Software is furnished to do - * so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -#include "_G_metatable.hpp" - -#include "Class.hpp" -#include "convert_godot_lua.hpp" -#include "module_names.hpp" - -#include -#include - -using namespace godot; - -namespace luagdextension { - -sol::object __index(sol::this_state state, sol::global_table _G, sol::stack_object key) { - static Engine *engine = Engine::get_singleton(); - - if (key.get_type() != sol::type::string) { - return sol::nil; - } - - auto registry = sol::state_view(state).registry(); - if (registry.get_or(module_names::singleton_access, false)) { - auto class_name = key.as(); - if (engine->has_singleton(class_name)) { - Variant singleton = engine->get_singleton(class_name); - return _G[key] = to_lua(state, singleton); - } - } - if (registry.get_or(module_names::classes, false)) { - StringName class_name = key.as(); - if (ClassDB::class_exists(class_name)) { - Class cls(class_name); - return _G[key] = sol::make_object(state, cls); - } - } - return sol::nil; -} - -void setup_G_metatable(sol::state_view& state) { - state.globals()[sol::metatable_key] = state.create_table_with( - sol::meta_function::index, &__index - ); -} - -} - diff --git a/src/utils/_G_metatable.hpp b/src/utils/_G_metatable.hpp deleted file mode 100644 index 64659bd2..00000000 --- a/src/utils/_G_metatable.hpp +++ /dev/null @@ -1,33 +0,0 @@ -/** - * Copyright (C) 2025 Gil Barbosa Reis. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy of - * this software and associated documentation files (the “Software”), to deal in - * the Software without restriction, including without limitation the rights to - * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies - * of the Software, and to permit persons to whom the Software is furnished to do - * so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -#ifndef __UTILS_G_METATABLE_HPP__ -#define __UTILS_G_METATABLE_HPP__ - -#include - -namespace luagdextension { - -void setup_G_metatable(sol::state_view& state); - -} - -#endif // __UTILS_G_METATABLE_HPP__ From 791bf1d442d012977c9464e2f9fab97868c584ab Mon Sep 17 00:00:00 2001 From: gilzoide Date: Sat, 23 Aug 2025 18:43:06 -0300 Subject: [PATCH 2/9] Fix import order of GODOT_SINGLETONS and GODOT_CLASSES --- src/LuaState.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/LuaState.cpp b/src/LuaState.cpp index 94cb418d..a3072174 100644 --- a/src/LuaState.cpp +++ b/src/LuaState.cpp @@ -151,12 +151,12 @@ void LuaState::open_libraries(BitField libraries) { if (libraries.has_flag(GODOT_UTILITY_FUNCTIONS)) { lua_state.require(module_names::utility_functions, &luaopen_godot_utility_functions, false); } - if (libraries.has_flag(GODOT_SINGLETONS)) { - lua_state.require(module_names::singleton_access, &luaopen_godot_singleton_access, false); - } if (libraries.has_flag(GODOT_CLASSES)) { lua_state.require(module_names::classes, &luaopen_godot_classes, false); } + if (libraries.has_flag(GODOT_SINGLETONS)) { + lua_state.require(module_names::singleton_access, &luaopen_godot_singleton_access, false); + } if (libraries.has_flag(GODOT_ENUMS)) { lua_state.require(module_names::enums, &luaopen_godot_enums, false); } From 63e0987163d2d2d8f84ee5e05cb74be84a34e730 Mon Sep 17 00:00:00 2001 From: gilzoide Date: Sat, 23 Aug 2025 19:01:06 -0300 Subject: [PATCH 3/9] Change test_entrypoint to print error count --- src/luaopen/singleton_access.cpp | 2 -- test/test_entrypoint.gd | 9 +++++---- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/luaopen/singleton_access.cpp b/src/luaopen/singleton_access.cpp index d4a7cdc4..4d5d5cb4 100644 --- a/src/luaopen/singleton_access.cpp +++ b/src/luaopen/singleton_access.cpp @@ -28,8 +28,6 @@ using namespace godot; using namespace luagdextension; extern "C" int luaopen_godot_singleton_access(lua_State *L) { - sol::state_view state = L; - Engine *engine = Engine::get_singleton(); for (auto&& singleton_name : engine->get_singleton_list()) { lua_push(L, engine->get_singleton(singleton_name)); diff --git a/test/test_entrypoint.gd b/test/test_entrypoint.gd index 2588e088..d5db46f5 100644 --- a/test/test_entrypoint.gd +++ b/test/test_entrypoint.gd @@ -4,7 +4,7 @@ const LUA_TEST_DIR = "res://lua_tests" const GDSCRIPT_TEST_DIR = "res://gdscript_tests" func _initialize(): - var all_success = true + var error_count = 0 print("Starting Lua GDExtension tests (runtime: ", LuaState.get_lua_runtime(), ")") for lua_script in DirAccess.get_files_at(LUA_TEST_DIR): @@ -16,7 +16,7 @@ func _initialize(): var file_name = str(LUA_TEST_DIR, "/", lua_script) var result = lua_state.do_file(file_name) if result is LuaError: - all_success = false + error_count += 1 print("! ", lua_script) push_error(result.message) else: @@ -38,11 +38,12 @@ func _initialize(): obj._setup() # actual test if not obj.call(method_name): - all_success = false + error_count += 1 printerr(" ! ", method_name) else: print(" ✓ ", method_name) if obj is Node: obj.queue_free() - quit(0 if all_success else -1) + print("\nFailed tests: ", error_count) + quit(0 if error_count == 0 else -1) From d237882d317382f53dccba717922cfbc0c0228d6 Mon Sep 17 00:00:00 2001 From: gilzoide Date: Sun, 24 Aug 2025 08:39:38 -0300 Subject: [PATCH 4/9] Update Godot to 4.4.1 in automated tests --- .github/workflows/build.yml | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 85d758c5..e0129eb6 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -191,23 +191,23 @@ jobs: include: - name: Linux runner-os: ubuntu-latest - godot-release: 4.4-stable/Godot_v4.4-stable_linux.x86_64.zip - godot-bin: ./Godot_v4.4-stable_linux.x86_64 + godot-release: 4.4.1-stable/Godot_v4.4.1-stable_linux.x86_64.zip + godot-bin: ./Godot_v4.4.1-stable_linux.x86_64 - name: Linux (arm64) runner-os: ubuntu-24.04-arm - godot-release: 4.4-stable/Godot_v4.4-stable_linux.arm64.zip - godot-bin: ./Godot_v4.4-stable_linux.arm64 + godot-release: 4.4.1-stable/Godot_v4.4.1-stable_linux.arm64.zip + godot-bin: ./Godot_v4.4.1-stable_linux.arm64 - name: Windows runner-os: windows-latest - godot-release: 4.4-stable/Godot_v4.4-stable_win64.exe.zip - godot-bin: ./Godot_v4.4-stable_win64.exe + godot-release: 4.4.1-stable/Godot_v4.4.1-stable_win64.exe.zip + godot-bin: ./Godot_v4.4.1-stable_win64.exe - name: Windows (arm64) runner-os: windows-11-arm - godot-release: 4.4-stable/Godot_v4.4-stable_windows_arm64.exe.zip - godot-bin: ./Godot_v4.4-stable_windows_arm64.exe + godot-release: 4.4.1-stable/Godot_v4.4.1-stable_windows_arm64.exe.zip + godot-bin: ./Godot_v4.4.1-stable_windows_arm64.exe - name: macOS runner-os: macos-latest - godot-release: 4.4-stable/Godot_v4.4-stable_macos.universal.zip + godot-release: 4.4.1-stable/Godot_v4.4.1-stable_macos.universal.zip godot-bin: ./Godot.app/Contents/MacOS/Godot env: GODOT_BIN: ${{ matrix.godot-bin }} From 14f9ff93bcbee9c59f06a5e4ffa90abd1a6cf742 Mon Sep 17 00:00:00 2001 From: gilzoide Date: Sun, 24 Aug 2025 09:48:18 -0300 Subject: [PATCH 5/9] Remove unnecessary include --- src/luaopen/classes.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/luaopen/classes.cpp b/src/luaopen/classes.cpp index 5bc3c433..0f93bacc 100644 --- a/src/luaopen/classes.cpp +++ b/src/luaopen/classes.cpp @@ -22,7 +22,6 @@ #include "../utils/Class.hpp" #include -#include using namespace luagdextension; From b2580628ee141e6bf9dc912adfb594f894a9c3b0 Mon Sep 17 00:00:00 2001 From: gilzoide Date: Sat, 1 Nov 2025 12:05:59 -0300 Subject: [PATCH 6/9] Set singletons using sol --- src/luaopen/singleton_access.cpp | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/luaopen/singleton_access.cpp b/src/luaopen/singleton_access.cpp index 4d5d5cb4..9da4d2d3 100644 --- a/src/luaopen/singleton_access.cpp +++ b/src/luaopen/singleton_access.cpp @@ -19,20 +19,17 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -#include "../utils/convert_godot_lua.hpp" +#include "../utils/custom_sol.hpp" #include -#include using namespace godot; -using namespace luagdextension; extern "C" int luaopen_godot_singleton_access(lua_State *L) { + sol::state_view state(L); Engine *engine = Engine::get_singleton(); for (auto&& singleton_name : engine->get_singleton_list()) { - lua_push(L, engine->get_singleton(singleton_name)); - lua_setglobal(L, singleton_name.ascii().get_data()); + state.set(singleton_name.ascii().get_data(), engine->get_singleton(singleton_name)); } - return 0; } From 11944a68cd0fbc50f2121136ef3c07b44056ebda Mon Sep 17 00:00:00 2001 From: gilzoide Date: Tue, 30 Dec 2025 15:05:24 -0300 Subject: [PATCH 7/9] Fix registration of global classes and autoloaded nodes --- src/luaopen/classes.cpp | 11 ++------- src/luaopen/singleton_access.cpp | 6 ++++- src/script-language/LuaScriptLanguage.cpp | 30 +++++++++++++++++++++++ src/script-language/LuaScriptLanguage.hpp | 4 +++ 4 files changed, 41 insertions(+), 10 deletions(-) diff --git a/src/luaopen/classes.cpp b/src/luaopen/classes.cpp index 29050da8..7b79202d 100644 --- a/src/luaopen/classes.cpp +++ b/src/luaopen/classes.cpp @@ -19,30 +19,23 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ +#include "../script-language/LuaScriptLanguage.hpp" #include "../utils/Class.hpp" -#include "../utils/convert_godot_std.hpp" #include -#include -#include using namespace luagdextension; extern "C" int luaopen_godot_classes(lua_State *L) { sol::state_view state = L; - sol::table global_class_paths = state.registry().create_named("_GDEXTENSION_GLOBAL_CLASS_PATHS"); Class::register_usertype(state); ClassDBSingleton *classdb = ClassDBSingleton::get_singleton(); for (auto&& class_name : classdb->get_class_list()) { state.set(class_name.ascii().get_data(), Class(class_name)); } - TypedArray global_class_list = ProjectSettings::get_singleton()->get_global_class_list(); - for (int64_t i = 0; i < global_class_list.size(); ++i) { - Dictionary type_info = global_class_list[i]; - global_class_paths[to_std_string(type_info["class"])] = to_std_string(type_info["path"]); - } + LuaScriptLanguage::get_singleton()->register_global_classes(L); return 0; } diff --git a/src/luaopen/singleton_access.cpp b/src/luaopen/singleton_access.cpp index 9da4d2d3..9b9be994 100644 --- a/src/luaopen/singleton_access.cpp +++ b/src/luaopen/singleton_access.cpp @@ -19,11 +19,12 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -#include "../utils/custom_sol.hpp" +#include "../script-language/LuaScriptLanguage.hpp" #include using namespace godot; +using namespace luagdextension; extern "C" int luaopen_godot_singleton_access(lua_State *L) { sol::state_view state(L); @@ -31,5 +32,8 @@ extern "C" int luaopen_godot_singleton_access(lua_State *L) { for (auto&& singleton_name : engine->get_singleton_list()) { state.set(singleton_name.ascii().get_data(), engine->get_singleton(singleton_name)); } + + LuaScriptLanguage::get_singleton()->register_named_globals(L); + return 0; } diff --git a/src/script-language/LuaScriptLanguage.cpp b/src/script-language/LuaScriptLanguage.cpp index 2dd6c652..47ac2c16 100644 --- a/src/script-language/LuaScriptLanguage.cpp +++ b/src/script-language/LuaScriptLanguage.cpp @@ -31,6 +31,7 @@ #include "../LuaState.hpp" #include "../generated/lua_script_globals.h" #include "../utils/project_settings.hpp" +#include "../utils/convert_godot_lua.hpp" #include #include @@ -66,6 +67,10 @@ void LuaScriptLanguage::_init() { // Additional globals defined in Lua code lua_state->do_string(lua_script_globals); + + // Now, after everything is setup, we can load global classes (which might be LuaScripts) + global_class_list = project_settings->get_global_class_list(); + register_global_classes(lua_state->get_lua_state()); } String LuaScriptLanguage::_get_type() const { @@ -424,6 +429,31 @@ const Dictionary& LuaScriptLanguage::get_named_globals() const { return named_globals; } +void LuaScriptLanguage::register_named_globals(lua_State *L) const { + lua_pushglobaltable(L); + Array keys = named_globals.keys(); + for (int64_t i = 0, count = keys.size(); i < count; ++i) { + Variant key = keys[i]; + lua_push(L, key); + lua_push(L, named_globals[key]); + lua_rawset(L, -3); + } + lua_pop(L, 1); +} + +void LuaScriptLanguage::register_global_classes(lua_State *L) const { + ResourceLoader *resource_loader = ResourceLoader::get_singleton(); + lua_pushglobaltable(L); + for (int64_t i = 0, count = global_class_list.size(); i < count; ++i) { + Dictionary type_info = global_class_list[i]; + Ref script = resource_loader->load(type_info["path"]); + lua_push(L, type_info["class"]); + lua_push(L, script); + lua_rawset(L, -3); + } + lua_pop(L, 1); +} + LuaState *LuaScriptLanguage::get_lua_state() { return lua_state.ptr(); } diff --git a/src/script-language/LuaScriptLanguage.hpp b/src/script-language/LuaScriptLanguage.hpp index 169756c2..e4c7c7fc 100644 --- a/src/script-language/LuaScriptLanguage.hpp +++ b/src/script-language/LuaScriptLanguage.hpp @@ -100,6 +100,9 @@ class LuaScriptLanguage : public ScriptLanguageExtension { PackedStringArray get_lua_member_keywords() const; const Dictionary& get_named_globals() const; + void register_named_globals(lua_State *L) const; + void register_global_classes(lua_State *L) const; + LuaState *get_lua_state(); LuaParser *get_lua_parser() const; @@ -113,6 +116,7 @@ class LuaScriptLanguage : public ScriptLanguageExtension { Ref lua_state; Ref lua_parser; Dictionary named_globals; + TypedArray global_class_list; private: static LuaScriptLanguage *instance; From f549a6c577f4a396f17163b5d34e3ec083901638 Mon Sep 17 00:00:00 2001 From: gilzoide Date: Tue, 30 Dec 2025 15:18:40 -0300 Subject: [PATCH 8/9] Remove LuaState reference from LuaScriptMethod --- src/script-language/LuaScriptMethod.cpp | 15 ++++++++------- src/script-language/LuaScriptMethod.hpp | 4 +--- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/src/script-language/LuaScriptMethod.cpp b/src/script-language/LuaScriptMethod.cpp index 756810d2..cde7e7a9 100644 --- a/src/script-language/LuaScriptMethod.cpp +++ b/src/script-language/LuaScriptMethod.cpp @@ -22,23 +22,24 @@ #include "LuaScriptMethod.hpp" #include "../LuaDebug.hpp" +#include "../LuaFunction.hpp" #include "../utils/stack_top_checker.hpp" namespace luagdextension { LuaScriptMethod::LuaScriptMethod(const StringName& name, sol::protected_function method) : name(name) - , method(LuaObject::wrap_object(method)) + , method(method) { } bool LuaScriptMethod::is_valid() const { - return method.is_valid(); + return method.valid(); } int LuaScriptMethod::get_line_defined() const { #ifdef DEBUG_ENABLED - return method->get_debug_info()->get_line_defined(); + return LuaObject::wrap_object(method)->get_debug_info()->get_line_defined(); #else return -1; #endif @@ -46,7 +47,7 @@ int LuaScriptMethod::get_line_defined() const { Variant LuaScriptMethod::get_argument_count() const { #if defined(DEBUG_ENABLED) && LUA_VERSION_NUM >= 502 - return method->get_debug_info()->get_nparams(); + return LuaObject::wrap_object(method)->get_debug_info()->get_nparams(); #else return {}; #endif @@ -57,11 +58,11 @@ MethodInfo LuaScriptMethod::to_method_info() const { mi.name = name; #if defined(DEBUG_ENABLED) && LUA_VERSION_NUM >= 502 - sol::state_view state = method->get_function().lua_state(); + sol::state_view state = LuaObject::wrap_object(method)->get_function().lua_state(); StackTopChecker topcheck(state); - auto debug_info = method->get_debug_info(); - auto methodpop = sol::stack::push_pop(state, method->get_function()); + auto debug_info = LuaObject::wrap_object(method)->get_debug_info(); + auto methodpop = sol::stack::push_pop(state, method); for (int i = 0; i < debug_info->get_nparams(); i++) { String arg_name = lua_getlocal(state, nullptr, i + 1); if (i == 0 && arg_name == "self") { diff --git a/src/script-language/LuaScriptMethod.hpp b/src/script-language/LuaScriptMethod.hpp index fb6f33e0..64d1aa92 100644 --- a/src/script-language/LuaScriptMethod.hpp +++ b/src/script-language/LuaScriptMethod.hpp @@ -25,8 +25,6 @@ #include #include -#include "../LuaFunction.hpp" - typedef struct lua_State lua_State; using namespace godot; @@ -35,7 +33,7 @@ namespace luagdextension { struct LuaScriptMethod { StringName name; - Ref method; + sol::protected_function method; LuaScriptMethod() = default; LuaScriptMethod(const StringName& name, sol::protected_function method); From 818075115e3d46cce4354caa9b307c8b99eaf392 Mon Sep 17 00:00:00 2001 From: gilzoide Date: Sat, 17 Jan 2026 16:13:35 -0300 Subject: [PATCH 9/9] Remove LuaScriptLanguage::get_named_globals, use StackTopResetter --- src/script-language/LuaScriptLanguage.cpp | 11 ++++------- src/script-language/LuaScriptLanguage.hpp | 1 - 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/src/script-language/LuaScriptLanguage.cpp b/src/script-language/LuaScriptLanguage.cpp index 9d7762c8..fa433e89 100644 --- a/src/script-language/LuaScriptLanguage.cpp +++ b/src/script-language/LuaScriptLanguage.cpp @@ -30,8 +30,9 @@ #include "../LuaTable.hpp" #include "../LuaState.hpp" #include "../generated/lua_script_globals.h" -#include "../utils/project_settings.hpp" #include "../utils/convert_godot_lua.hpp" +#include "../utils/project_settings.hpp" +#include "../utils/stack_top_resetter.hpp" #include #include @@ -426,11 +427,8 @@ PackedStringArray LuaScriptLanguage::get_lua_member_keywords() const { ); } -const Dictionary& LuaScriptLanguage::get_named_globals() const { - return named_globals; -} - void LuaScriptLanguage::register_named_globals(lua_State *L) const { + StackTopResetter resettop(L); lua_pushglobaltable(L); Array keys = named_globals.keys(); for (int64_t i = 0, count = keys.size(); i < count; ++i) { @@ -439,10 +437,10 @@ void LuaScriptLanguage::register_named_globals(lua_State *L) const { lua_push(L, named_globals[key]); lua_rawset(L, -3); } - lua_pop(L, 1); } void LuaScriptLanguage::register_global_classes(lua_State *L) const { + StackTopResetter resettop(L); ResourceLoader *resource_loader = ResourceLoader::get_singleton(); lua_pushglobaltable(L); for (int64_t i = 0, count = global_class_list.size(); i < count; ++i) { @@ -452,7 +450,6 @@ void LuaScriptLanguage::register_global_classes(lua_State *L) const { lua_push(L, script); lua_rawset(L, -3); } - lua_pop(L, 1); } LuaState *LuaScriptLanguage::get_lua_state() { diff --git a/src/script-language/LuaScriptLanguage.hpp b/src/script-language/LuaScriptLanguage.hpp index 2eb791b0..03cebfe7 100644 --- a/src/script-language/LuaScriptLanguage.hpp +++ b/src/script-language/LuaScriptLanguage.hpp @@ -98,7 +98,6 @@ class LuaScriptLanguage : public ScriptLanguageExtension { PackedStringArray get_lua_keywords() const; PackedStringArray get_lua_member_keywords() const; - const Dictionary& get_named_globals() const; void register_named_globals(lua_State *L) const; void register_global_classes(lua_State *L) const;