decimad / luabind-deboostified

Create Lua bindings for your C++ code easily - my improvements
http://www.vrac.iastate.edu/vancegroup/docs/luabind/
Other
70 stars 27 forks source link

Multiple Return Values #41

Closed atom0s closed 1 year ago

atom0s commented 6 years ago

Is there a specific way of handling custom return values / multiple return values with this version of luabind? So far this appears safe, but I want to double check that I'm not missing a policy or specific method of doing this:

void Test(const luabind::object& t, lua_State* L)
{
    if (luabind::type(t) == LUA_TTABLE)
    {
        const auto a = luabind::object_cast<std::string>(t["a"]);
        const auto b = luabind::object_cast<uint32_t>(t["b"]);

        // Multiple returns..
        lua_pushstring(L, a.c_str());
        lua_pushnumber(L, b);
        return;
    }

    // Multiple returns..
    lua_pushnil(L);
    lua_pushnil(L);
}

// ...

    luabind::module(L)
        [
            luabind::def("Test", &Test)
        ];
local t = { ['a'] = 'hello world', ['b'] = 1234 };
local a, b = Test(t);
print(a);
print(b);
a, b = Test(t);
print(a);
print(b);

This all seems to work fine, the stack appears to be valid and not stale with left over values etc. but I couldn't find any info in the docs around the net for Luabind on how to handle multiple values or custom pushing like this. Generally I'm used to using luabind::object as the return, something like:

luabind::object Test(const luabind::object& t, lua_State* L)
{
    return luabind::object(L, 1234);
}

But I don't think there is any proper way to return multiple values with the object.

ltjax commented 6 years ago

You can do it by using multiple pure_out_value policies!

ltjax commented 6 years ago

I've just written a small blog article that has an example on how to do it using policies: https://schneide.blog/2018/09/17/luabind-deboostified-tips-and-tricks/