Skip to content

Allow reversed proxy metamethods#261

Open
kunitoki wants to merge 2 commits into
masterfrom
dev/allow_reversed_proxy_metamethods
Open

Allow reversed proxy metamethods#261
kunitoki wants to merge 2 commits into
masterfrom
dev/allow_reversed_proxy_metamethods

Conversation

@kunitoki

Copy link
Copy Markdown
Owner

This pull request introduces support for reversed proxy functions for binary operator metamethods in LuaBridge, allowing operator overloading to work seamlessly for both obj OP value and value OP obj forms (e.g., vec * 3 and 3 * vec). It also enforces that reversed functions can only be registered for valid binary operator metamethods, preventing misuse. Comprehensive tests are added to cover these new capabilities and restrictions.

Core Library Enhancements

  • Added is_reversed_proxy_function_v and is_const_reversed_proxy_function_v traits in FuncTraits.h to detect reversed proxy functions, where the class type is the second argument, as needed for binary operator overloads with swapped operands.
  • Introduced is_binary_operator_metamethod utility and reversed_overload_type_checker to identify and correctly handle binary operator metamethods and their reversed forms in CFunctions.h [1] [2].
  • Updated member function registration logic to support reversed proxy functions for binary operator metamethods only, with static assertions and runtime checks to enforce correct usage [1] [2] [3] [4].

Test Suite Additions

  • Added a new Vec2 class with overloaded arithmetic operators to thoroughly test both normal and reversed binary operator metamethods.
  • Implemented extensive tests for reversed operator support (__add, __sub, __mul, __div) and for cases where only the reversed operator is provided.
  • Added tests to ensure that reversed functions are forbidden outside of binary operator metamethods, with appropriate exception handling.

Type Trait and Arity Logic

  • Updated is_const_function logic to correctly recognize constness for reversed proxy functions.

These changes significantly improve LuaBridge's operator overloading capabilities, ensuring both correctness and ease of use for C++ classes exposed to Lua.

@kunitoki kunitoki self-assigned this Jul 16, 2026
@kunitoki kunitoki added the enhancement New feature or request label Jul 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant