Skip to content

Commit fe56080

Browse files
authored
Merge pull request #134 from gilzoide/bugfix/invalid-object-handling
Fix handling of freed objects
2 parents 5762b30 + cc0c8a6 commit fe56080

File tree

8 files changed

+82
-2
lines changed

8 files changed

+82
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
MyScript.exported_int_valued_dict = export({ type = Dictionary[Variant][int] })
1414
MyScript.exported_texture_property = export({ type = Texture })
1515
```
16+
- `is_instance_valid` utility function when opening `GODOT_UTILITY_FUNCTIONS` library
1617

1718
### Changed
1819
- `LuaScriptInstance`'s data table is passed as `self` to methods instead of their owner `Object`
@@ -26,6 +27,7 @@
2627
MyScript.exported_dictionary = export(Dictionary)
2728
```
2829
- Convert null Object Variants (`<Object#null>`) to `nil` when passing them to Lua
30+
- Convert freed Object Variants (`<Freed Object>`) to `nil` when passing them to Lua
2931

3032

3133
## [0.5.0](https://github.com/gilzoide/lua-gdextension/releases/tag/0.5.0)

src/luaopen/utility_functions.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
*/
2222
#include "../generated/utility_functions.hpp"
2323
#include "../utils/VariantArguments.hpp"
24+
#include "../utils/extra_utility_functions.hpp"
2425
#include "../utils/function_wrapper.hpp"
2526
#include "../utils/string_literal.hpp"
2627
#include "../utils/string_names.hpp"
@@ -119,6 +120,7 @@ extern "C" int luaopen_godot_utility_functions(lua_State *L) {
119120
sol::state_view state = L;
120121

121122
register_utility_functions(state);
123+
state.set("is_instance_valid", wrap_function(L, &is_instance_valid));
122124

123125
// In Lua, `print` separates passed values with "\t", so we bind it to Godot's `printt`
124126
state.do_string("print = printt");

src/utils/convert_godot_lua.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "DictionaryIterator.hpp"
3333
#include "VariantArguments.hpp"
3434
#include "convert_godot_std.hpp"
35+
#include "extra_utility_functions.hpp"
3536
#include "load_fileaccess.hpp"
3637
#include "stack_top_checker.hpp"
3738

@@ -184,7 +185,7 @@ sol::stack_object lua_push(lua_State *lua_state, const Variant& value) {
184185
break;
185186

186187
case Variant::OBJECT:
187-
if ((Object *) value == nullptr) {
188+
if (!is_instance_valid(value)) {
188189
sol::stack::push(lua_state, sol::nil);
189190
break;
190191
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Copyright (C) 2025 Gil Barbosa Reis.
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy of
5+
* this software and associated documentation files (the “Software”), to deal in
6+
* the Software without restriction, including without limitation the rights to
7+
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8+
* of the Software, and to permit persons to whom the Software is furnished to do
9+
* so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in all
12+
* copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
* SOFTWARE.
21+
*/
22+
#include "extra_utility_functions.hpp"
23+
24+
#include <godot_cpp/variant/utility_functions.hpp>
25+
26+
namespace luagdextension {
27+
28+
bool is_instance_valid(const Variant& v) {
29+
return v.get_type() == Variant::Type::OBJECT
30+
&& UtilityFunctions::is_instance_id_valid(v.operator ObjectID());
31+
}
32+
33+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Copyright (C) 2025 Gil Barbosa Reis.
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy of
5+
* this software and associated documentation files (the “Software”), to deal in
6+
* the Software without restriction, including without limitation the rights to
7+
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8+
* of the Software, and to permit persons to whom the Software is furnished to do
9+
* so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in all
12+
* copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
* SOFTWARE.
21+
*/
22+
#ifndef __UTILS_EXTRA_UTILITY_FUNCTIONS_HPP__
23+
#define __UTILS_EXTRA_UTILITY_FUNCTIONS_HPP__
24+
25+
#include <godot_cpp/variant/variant.hpp>
26+
27+
using namespace godot;
28+
29+
namespace luagdextension {
30+
31+
bool is_instance_valid(const Variant& v);
32+
33+
}
34+
35+
#endif // __UTILS_EXTRA_UTILITY_FUNCTIONS_HPP__

test/lua_tests/invalid_objects.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
assert(instance_from_id(0) == nil)
2+
3+
assert(not is_instance_valid(nil))
4+
local node = Node:new()
5+
assert(is_instance_valid(node))
6+
node:free()
7+
assert(not is_instance_valid(node))
8+
File renamed without changes.

test/lua_tests/null_object.lua

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)