// https://github.com/vinniefalco/LuaBridge // Copyright 2022, Stefan Frings // SPDX-License-Identifier: MIT #pragma once #include #include #include namespace luabridge { template struct Stack> { static void push(lua_State* L, std::pair const& pair) { lua_createtable(L, 2, 0); lua_pushinteger(L, static_cast(1)); Stack::push(L, pair.first); lua_settable(L, -3); lua_pushinteger(L, static_cast(2)); Stack::push(L, pair.second); lua_settable(L, -3); } static std::pair get(lua_State* L, int index) { if (!lua_istable(L, index)) { luaL_error(L, "#%d argument must be a table", index); } std::size_t const tableSize = static_cast(get_length(L, index)); if (tableSize != 2) { luaL_error(L, "pair size must be 2"); } std::pair pair; int const absindex = lua_absindex(L, index); lua_pushnil(L); { int const next = lua_next(L, absindex); assert(next != 0); pair.first = Stack::get(L, -1); lua_pop(L, 1); } { int const next = lua_next(L, absindex); assert(next != 0); pair.second = Stack::get(L, -1); lua_pop(L, 1); } { int const next = lua_next(L, absindex); assert(next == 0); } return pair; } static bool isInstance(lua_State* L, int index) { return lua_istable(L, index) && get_length(L, index) == 2; } }; } // namespace luabridge