Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for lua 5.4 to-be-closed variables #2046

Merged
merged 1 commit into from
Mar 30, 2024

Conversation

bartbes
Copy link
Member

@bartbes bartbes commented Mar 30, 2024

Closes #1946

Since love already handles double free-like cases for release using Proxy, this was as simple as calling release. Or technically, registering __close as an alias for release.

To test this I added some extra patches:

diff --git a/CMakeLists.txt b/CMakeLists.txt
index ce94ff61..fac9d552 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -223,7 +223,7 @@ Please see https://github.com/love2d/megasource
                set(LOVE_LUA_LIBRARY ${LUAJIT_LIBRARY})
                set(LOVE_LUA_INCLUDE_DIR ${LUAJIT_INCLUDE_DIR})
        else()
-               find_package(Lua51 REQUIRED)
+               find_package(Lua REQUIRED)
                set(LOVE_LUA_LIBRARY ${LUA_LIBRARY})
                set(LOVE_LUA_INCLUDE_DIR ${LUA_INCLUDE_DIR})
        endif()
diff --git a/src/common/runtime.h b/src/common/runtime.h
index 747c38e8..820bd4b9 100644
--- a/src/common/runtime.h
+++ b/src/common/runtime.h
@@ -36,6 +36,11 @@ extern "C" {
        #include <lauxlib.h>
 }
 
+#if LUA_VERSION_NUM >= 504
+#define luaL_checkint luaL_checkinteger
+#define luaL_optint luaL_optinteger
+#endif
+
 // C++
 #include <exception>
 #include <algorithm>

And in my case a few extra prints for the demo. The output I got running this directly from lua 5.4 was:

Lua 5.4.6  Copyright (C) 1994-2023 Lua.org, PUC-Rio
> package.preload.love = package.loadlib("./build/Debug/liblove-12.0.so", "luaopen_love")
> love = require "love"
> require "love.math"
table: 0x5584d2f30b50	:preload:
> do local rng <close> = love.math.newRandomGenerator(); print(rng:random()) end
0.68980386685727
Calling __close
Destroying RandomGenerator
> do local rng <close> = love.math.newRandomGenerator(); print(rng:random()); rng:release() end
0.68980386685727
Calling release
Destroying RandomGenerator
Calling __close
> do local rng = love.math.newRandomGenerator(); print(rng:random()) end
0.68980386685727
> collectgarbage()
Destroying RandomGenerator
0

Hopefully that demonstrates that <close> works as intended, and it can be mixed with release without issue.

Automatically call object:release() when an object is used as a to-be-closed variable.

Since both release and __gc already handle multiple calls, this just works.
Theoretically extra actions could be added to some objects, like File calling File:close(), but it already does that in its destructor, so that should be unnecessary.
@slime73 slime73 merged commit 81c26e4 into love2d:main Mar 30, 2024
8 checks passed
@bartbes bartbes deleted the lua54_to_be_closed_support branch March 30, 2024 12:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support to-be-closed variable in Lua 5.4 for LOVE objects.
2 participants