diff --git a/core/variant/variant_op.cpp b/core/variant/variant_op.cpp index 33c285dc6d0..aed83ac010d 100644 --- a/core/variant/variant_op.cpp +++ b/core/variant/variant_op.cpp @@ -900,6 +900,39 @@ void Variant::_register_variant_operators() { register_op(Variant::OP_NOT, Variant::INT, Variant::NIL); register_op(Variant::OP_NOT, Variant::FLOAT, Variant::NIL); register_op(Variant::OP_NOT, Variant::OBJECT, Variant::NIL); + register_op>(Variant::OP_NOT, Variant::STRING, Variant::NIL); + register_op>(Variant::OP_NOT, Variant::VECTOR2, Variant::NIL); + register_op>(Variant::OP_NOT, Variant::VECTOR2I, Variant::NIL); + register_op>(Variant::OP_NOT, Variant::RECT2, Variant::NIL); + register_op>(Variant::OP_NOT, Variant::RECT2I, Variant::NIL); + register_op>(Variant::OP_NOT, Variant::VECTOR3, Variant::NIL); + register_op>(Variant::OP_NOT, Variant::VECTOR3I, Variant::NIL); + register_op>(Variant::OP_NOT, Variant::TRANSFORM2D, Variant::NIL); + register_op>(Variant::OP_NOT, Variant::VECTOR4, Variant::NIL); + register_op>(Variant::OP_NOT, Variant::VECTOR4I, Variant::NIL); + register_op>(Variant::OP_NOT, Variant::PLANE, Variant::NIL); + register_op>(Variant::OP_NOT, Variant::QUATERNION, Variant::NIL); + register_op>(Variant::OP_NOT, Variant::AABB, Variant::NIL); + register_op>(Variant::OP_NOT, Variant::BASIS, Variant::NIL); + register_op>(Variant::OP_NOT, Variant::TRANSFORM3D, Variant::NIL); + register_op>(Variant::OP_NOT, Variant::PROJECTION, Variant::NIL); + register_op>(Variant::OP_NOT, Variant::COLOR, Variant::NIL); + register_op>(Variant::OP_NOT, Variant::STRING_NAME, Variant::NIL); + register_op>(Variant::OP_NOT, Variant::NODE_PATH, Variant::NIL); + register_op>(Variant::OP_NOT, Variant::RID, Variant::NIL); + register_op>(Variant::OP_NOT, Variant::CALLABLE, Variant::NIL); + register_op>(Variant::OP_NOT, Variant::SIGNAL, Variant::NIL); + register_op>(Variant::OP_NOT, Variant::DICTIONARY, Variant::NIL); + register_op>(Variant::OP_NOT, Variant::ARRAY, Variant::NIL); + register_op>(Variant::OP_NOT, Variant::PACKED_BYTE_ARRAY, Variant::NIL); + register_op>(Variant::OP_NOT, Variant::PACKED_INT32_ARRAY, Variant::NIL); + register_op>(Variant::OP_NOT, Variant::PACKED_INT64_ARRAY, Variant::NIL); + register_op>(Variant::OP_NOT, Variant::PACKED_FLOAT32_ARRAY, Variant::NIL); + register_op>(Variant::OP_NOT, Variant::PACKED_FLOAT64_ARRAY, Variant::NIL); + register_op>(Variant::OP_NOT, Variant::PACKED_STRING_ARRAY, Variant::NIL); + register_op>(Variant::OP_NOT, Variant::PACKED_VECTOR2_ARRAY, Variant::NIL); + register_op>(Variant::OP_NOT, Variant::PACKED_VECTOR3_ARRAY, Variant::NIL); + register_op>(Variant::OP_NOT, Variant::PACKED_COLOR_ARRAY, Variant::NIL); register_string_op(OperatorEvaluatorInStringFind, Variant::OP_IN); diff --git a/core/variant/variant_op.h b/core/variant/variant_op.h index d2163cf92dd..5db4d638067 100644 --- a/core/variant/variant_op.h +++ b/core/variant/variant_op.h @@ -805,14 +805,14 @@ class OperatorEvaluatorNot { public: static void evaluate(const Variant &p_left, const Variant &p_right, Variant *r_ret, bool &r_valid) { const A &a = *VariantGetInternalPtr::get_ptr(&p_left); - *r_ret = !a; + *r_ret = a == A(); r_valid = true; } static inline void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) { - *VariantGetInternalPtr::get_ptr(r_ret) = !*VariantGetInternalPtr::get_ptr(left); + *VariantGetInternalPtr::get_ptr(r_ret) = *VariantGetInternalPtr::get_ptr(left) == A(); } static void ptr_evaluate(const void *left, const void *right, void *r_ret) { - PtrToArg::encode(!PtrToArg::convert(left)); + PtrToArg::encode(PtrToArg::convert(left) == A(), r_ret); } static Variant::Type get_return_type() { return Variant::BOOL; } }; diff --git a/tests/core/variant/test_variant.h b/tests/core/variant/test_variant.h index 024fcf53c43..66b58620d28 100644 --- a/tests/core/variant/test_variant.h +++ b/tests/core/variant/test_variant.h @@ -1171,6 +1171,22 @@ TEST_CASE("[Variant] Utility functions") { } } +TEST_CASE("[Variant] Operator NOT") { + // Verify that operator NOT works for all types and is consistent with booleanize(). + for (int i = 0; i < Variant::VARIANT_MAX; i++) { + Variant value; + Callable::CallError err; + Variant::construct((Variant::Type)i, value, nullptr, 0, err); + + REQUIRE_EQ(err.error, Callable::CallError::CALL_OK); + + Variant result = Variant::evaluate(Variant::OP_NOT, value, Variant()); + + REQUIRE_EQ(result.get_type(), Variant::BOOL); + CHECK_EQ(!value.booleanize(), result.operator bool()); + } +} + } // namespace TestVariant #endif // TEST_VARIANT_H