diff --git a/CHANGELOG.md b/CHANGELOG.md index 3e0e113..f4dcb5c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -40,6 +40,7 @@ - Fixed `LuaJIT core/library version mismatch` errors in LuaJIT builds - `LuaScriptResourceFormatLoader::_load` now respects the cache mode, fixing "Another resource is loaded from path 'res://...' (possible cyclic resource inclusion)." errors - Error messages from Lua code using the wrong stack index +- 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` ## [0.5.0](https://github.com/gilzoide/lua-gdextension/releases/tag/0.5.0) diff --git a/src/luaopen/variant.cpp b/src/luaopen/variant.cpp index 7d4fd6d..99f03db 100644 --- a/src/luaopen/variant.cpp +++ b/src/luaopen/variant.cpp @@ -34,6 +34,7 @@ #include "../utils/MethodBindByName.hpp" #include "../utils/convert_godot_lua.hpp" #include "../utils/convert_godot_std.hpp" +#include "../utils/function_wrapper.hpp" #include "../utils/string_names.hpp" using namespace godot; @@ -132,11 +133,12 @@ std::tuple variant__pairs(sol::this_state state, const return ObjectIterator::object_pairs(state, variant); } -VariantType variant_get_type(const Variant& variant) { - return VariantType(variant.get_type()); +VariantType variant_get_type(const sol::stack_object& self) { + return VariantType(to_variant(self).get_type()); } -bool variant_is(const Variant& variant, const sol::stack_object& type) { +bool variant_is(const sol::stack_object& self, const sol::stack_object& type) { + Variant variant = to_variant(self); if (type.get_type() == sol::type::nil) { return variant.get_type() == Variant::NIL; } @@ -186,15 +188,15 @@ extern "C" int luaopen_godot_variant(lua_State *L) { Variant(double v), Variant(const char *v) >(), - "booleanize", &Variant::booleanize, - "duplicate", &Variant::duplicate, + "booleanize", wrap_function(L, +[](const Variant& v) { return v.booleanize(); }), + "duplicate", wrap_function(L, +[](const Variant& self) { return self.duplicate(); }), "call", &variant_call, "pcall", &variant_pcall, "get_type", &variant_get_type, - "get_type_name", &get_type_name, - "hash", &Variant::hash, - "recursive_hash", &Variant::recursive_hash, - "hash_compare", &Variant::hash_compare, + "get_type_name", wrap_function(L, &get_type_name), + "hash", wrap_function(L, +[](const Variant& self) { return self.hash(); }), + "recursive_hash", wrap_function(L, +[](const Variant& self, int recursion_count) { return self.recursive_hash(recursion_count); }), + "hash_compare", wrap_function(L, +[](const Variant& self, const Variant& other) { return self.hash_compare(other); }), "is", &variant_is, // comparison sol::meta_function::equal_to, &evaluate_binary_operator, diff --git a/test/lua_tests/variant_static_method.lua b/test/lua_tests/variant_static_method.lua index eac5423..1418bde 100644 --- a/test/lua_tests/variant_static_method.lua +++ b/test/lua_tests/variant_static_method.lua @@ -2,3 +2,12 @@ local b = assert(Basis:from_euler(Vector3()), "Static method call failed") local size = assert(String:humanize_size(42), "Static method call failed") assert(not pcall(function() Basis:from_euler("invalid argument") end), "Static method call with invalid argument succeeded") + +assert(Variant.get_type(1) == int) +assert(Variant.get_type(1.1) == float) +assert(Variant.get_type(Vector2()) == Vector2) + +assert(Variant.is("some string", String)) + +assert(Variant.booleanize(1)) +assert(not Variant.booleanize(0))