ThePhD / sol2

Sol3 (sol2 v3.0) - a C++ <-> Lua API wrapper with advanced features and top notch performance - is here, and it's great! Documentation:
http://sol2.rtfd.io/
MIT License
4.12k stars 500 forks source link

Extra argument when using sol::environment with sol::variadic_args in call #1458

Open ricosolana opened 1 year ago

ricosolana commented 1 year ago

When calling a c-bound function from Lua under a sol::this_environment with sol::variadic_args, the final parameter appears duplicated when passed to the function.

#include <sol/sol.hpp>

int main() {
    sol::state state;
    state.open_libraries();

    sol::environment env(state, sol::create, state.globals());

    /*
    // This works fine
    state["OnState"] = [](sol::this_state ts, sol::variadic_args args) {
        sol::state_view view(ts);
        auto&& typefx(view["type"]);
        for (int i = 0; i < args.size(); i++) {
            std::string s = typefx(args[i]);
            std::cout << i << " " << s << "\n";
        }
        assert(args.size() == 1);
    };

    state.script("OnState(function() end)", env);
    */

    // A lambda signature with the variadic_args and this_environment swapped does appear to work as intended
    // [&](sol::variadic_args args, sol::this_environment te)

    // This does not work fine
    state["OnEnv"] = [](sol::this_environment ts, sol::variadic_args args) {
        sol::environment& view(ts);
        auto&& typefx(view["type"]);
        for (int i = 0; i < args.size(); i++) {
            std::string s = typefx(args[i]);
            std::cout << i << " " << s << "\n";
        }
        assert(args.size() == 1);
    };

    state.script("OnEnv(function() end)", env);

    return 0;
}

This does not happen however when this_environment is the last parameter (the variadic_args in this case contains the correct size).

As shown in the commented-out portion, this does not happen with sol::state.