sol2 icon indicating copy to clipboard operation
sol2 copied to clipboard

why when bind function f(int, std::tuple<int, int>) doesn't work?

Open etorth opened this issue 2 years ago • 1 comments

code compiled with lua 5.4 and sol 3.3.0

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

    lua.set_function("func1", [](std::tuple<int, int> t)
    {
        std::cout << std::get<0>(t) << ","
                  << std::get<1>(t) << ","

                  << std::endl;
    });

    lua.set_function("func2", [](std::tuple<int, int> t, int k)
    {
        std::cout << std::get<0>(t) << ","
                  << std::get<1>(t) << ","
                  << k              << ","

                  << std::endl;
    });

    lua.set_function("func3", [](int i, std::tuple<int, int> t)
    {
        std::cout << i              << ","
                  << std::get<0>(t) << ","
                  << std::get<1>(t) << ","

                  << std::endl;
    });

    lua.script(R"#(
        func1(   1, 2   )
        func2(   1, 2, 3)
        func3(0, 1, 2   )
    )#");

    return 0;
}

run result:

1,2,
1,2,3,
[sol3] An error occurred and has been passed to an error handler: sol: runtime error: stack index 4, expected number, received no value: not a numeric type that fits exactly an integer (number maybe has significant decimals) (bad argument into 'void(int, std::tuple<int, int>)')
stack traceback:
        [C]: in function 'func3'
        [string "..."]:4: in main chunk

as you can see, func1 and func2 works while func3 not. Looks param location matters, is this expected?

Thanks, Etorth

etorth avatar Apr 14 '23 21:04 etorth

Seems like the Lua stack usage tracking gets screwed up. Whenever the tuple is encountered the sol::stack::multi_check is called with the tracking and index that both already account for the processed stack items. Works fine when the tuple is a first item because when it's encountered both values are 0.

If only to know the original author's idea it could be an easy fix. Unfortunately a seemingly simple fix may actually break a lot of unrelated stuff.

devgs avatar May 16 '23 05:05 devgs