Skip to content

Commit bb3b11f

Browse files
authored
Merge pull request #152 from gilzoide/feature/wrap-Variant-methods
Wrap most Variant methods to avoid errors when passing Lua primitives
2 parents d4ab30a + 07eea54 commit bb3b11f

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
- Fixed `LuaJIT core/library version mismatch` errors in LuaJIT builds
4141
- `LuaScriptResourceFormatLoader::_load` now respects the cache mode, fixing "Another resource is loaded from path 'res://...' (possible cyclic resource inclusion)." errors
4242
- Error messages from Lua code using the wrong stack index
43+
- Crashes when passing Lua primitives to `typeof`, `Variant.is`, `Variant.get_type`, `Variant.booleanize`, `Variant.duplicate`, `Variant.get_type_name`, `Variant.hash`, `Variant.recursive_hash` and `Variant.hash_compare`
4344

4445

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

src/luaopen/variant.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "../utils/MethodBindByName.hpp"
3535
#include "../utils/convert_godot_lua.hpp"
3636
#include "../utils/convert_godot_std.hpp"
37+
#include "../utils/function_wrapper.hpp"
3738
#include "../utils/string_names.hpp"
3839

3940
using namespace godot;
@@ -132,11 +133,12 @@ std::tuple<sol::object, sol::object> variant__pairs(sol::this_state state, const
132133
return ObjectIterator::object_pairs(state, variant);
133134
}
134135

135-
VariantType variant_get_type(const Variant& variant) {
136-
return VariantType(variant.get_type());
136+
VariantType variant_get_type(const sol::stack_object& self) {
137+
return VariantType(to_variant(self).get_type());
137138
}
138139

139-
bool variant_is(const Variant& variant, const sol::stack_object& type) {
140+
bool variant_is(const sol::stack_object& self, const sol::stack_object& type) {
141+
Variant variant = to_variant(self);
140142
if (type.get_type() == sol::type::nil) {
141143
return variant.get_type() == Variant::NIL;
142144
}
@@ -186,15 +188,15 @@ extern "C" int luaopen_godot_variant(lua_State *L) {
186188
Variant(double v),
187189
Variant(const char *v)
188190
>(),
189-
"booleanize", &Variant::booleanize,
190-
"duplicate", &Variant::duplicate,
191+
"booleanize", wrap_function(L, +[](const Variant& v) { return v.booleanize(); }),
192+
"duplicate", wrap_function(L, +[](const Variant& self) { return self.duplicate(); }),
191193
"call", &variant_call,
192194
"pcall", &variant_pcall,
193195
"get_type", &variant_get_type,
194-
"get_type_name", &get_type_name,
195-
"hash", &Variant::hash,
196-
"recursive_hash", &Variant::recursive_hash,
197-
"hash_compare", &Variant::hash_compare,
196+
"get_type_name", wrap_function(L, &get_type_name),
197+
"hash", wrap_function(L, +[](const Variant& self) { return self.hash(); }),
198+
"recursive_hash", wrap_function(L, +[](const Variant& self, int recursion_count) { return self.recursive_hash(recursion_count); }),
199+
"hash_compare", wrap_function(L, +[](const Variant& self, const Variant& other) { return self.hash_compare(other); }),
198200
"is", &variant_is,
199201
// comparison
200202
sol::meta_function::equal_to, &evaluate_binary_operator<Variant::OP_EQUAL>,

test/lua_tests/variant_static_method.lua

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,12 @@ local b = assert(Basis:from_euler(Vector3()), "Static method call failed")
22
local size = assert(String:humanize_size(42), "Static method call failed")
33

44
assert(not pcall(function() Basis:from_euler("invalid argument") end), "Static method call with invalid argument succeeded")
5+
6+
assert(Variant.get_type(1) == int)
7+
assert(Variant.get_type(1.1) == float)
8+
assert(Variant.get_type(Vector2()) == Vector2)
9+
10+
assert(Variant.is("some string", String))
11+
12+
assert(Variant.booleanize(1))
13+
assert(not Variant.booleanize(0))

0 commit comments

Comments
 (0)