Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
20 changes: 11 additions & 9 deletions src/luaopen/variant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -132,11 +133,12 @@ std::tuple<sol::object, sol::object> 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;
}
Expand Down Expand Up @@ -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<Variant::OP_EQUAL>,
Expand Down
9 changes: 9 additions & 0 deletions test/lua_tests/variant_static_method.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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))