// The MIT License (MIT) // Copyright (c) 2013-2016 Rapptz, ThePhD and contributors // Permission is hereby granted, free of charge, to any person obtaining a copy of // this software and associated documentation files (the "Software"), to deal in // the Software without restriction, including without limitation the rights to // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of // the Software, and to permit persons to whom the Software is furnished to do so, // subject to the following conditions: // The above copyright notice and this permission notice shall be included in all // copies or substantial portions of the Software. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #ifndef SOL_STACK_CHECK_HPP #define SOL_STACK_CHECK_HPP #include "stack_core.hpp" #include "usertype_traits.hpp" #include "inheritance.hpp" #include #include #include namespace sol { namespace stack { namespace stack_detail { template inline bool check_metatable(lua_State* L, int index = -2) { luaL_getmetatable(L, &usertype_traits::metatable[0]); const type expectedmetatabletype = static_cast(lua_type(L, -1)); if (expectedmetatabletype != type::nil) { if (lua_rawequal(L, -1, index) == 1) { lua_pop(L, 2); return true; } } lua_pop(L, 1); return false; } template struct basic_check { template static bool check (lua_State* L, int index, Handler&& handler) { bool success = check_func(L, index) == 1; if (!success) { // expected type, actual type handler(L, index, expected, type_of(L, index)); } return success; } }; template struct check_types { template static bool check(types, std::index_sequence, lua_State* L, int firstargument, Handler&& handler) { if (!stack::check(L, firstargument + I0, handler)) return false; return check(types(), std::index_sequence(), L, firstargument - static_cast(is_transparent_argument>::value), std::forward(handler)); } template static bool check(types<>, std::index_sequence<>, lua_State*, int, Handler&&) { return true; } }; template <> struct check_types { template static bool check(types, std::index_sequence, lua_State*, int, Handler&&) { return true; } }; } // stack_detail template struct checker { template static bool check (lua_State* L, int index, Handler&& handler) { const type indextype = type_of(L, index); bool success = expected == indextype; if (!success) { // expected type, actual type handler(L, index, expected, indextype); } return success; } }; template struct checker { template static bool check (lua_State*, int, Handler&&) { return true; } }; template struct checker { template static bool check (lua_State* L, int index, Handler&& handler) { bool success = lua_isnoneornil(L, index); if (!success) { // expected type, actual type handler(L, index, expected, type_of(L, index)); } return success; } }; template struct checker : checker {}; template struct checker { template static bool check (lua_State*, int, Handler&&) { return true; } }; template struct checker { template static bool check (lua_State*, int, Handler&&) { return true; } }; template struct checker { template static bool check (lua_State* L, int index, Handler&& handler) { bool success = !lua_isnone(L, index); if (!success) { // expected type, actual type handler(L, index, type::none, type_of(L, index)); } return success; } }; template struct checker { template static bool check (lua_State* L, int index, Handler&& handler) { type t = type_of(L, index); bool success = t == type::userdata || t == type::lightuserdata; if (!success) { // expected type, actual type handler(L, index, type::lightuserdata, t); } return success; } }; template struct checker, type::userdata, C> : checker::value, C> {}; template struct checker : stack_detail::basic_check {}; template struct checker, type::function, C> : checker {}; template struct checker : checker {}; template struct checker { template static bool check(lua_State* L, int index, Handler&& handler) { type t = type_of(L, index); if (t == type::function) { return true; } if (t != type::userdata && t != type::table) { handler(L, index, t, type::function); return false; } // Do advanced check for call-style userdata? static const auto& callkey = name_of(meta_function::call); lua_getmetatable(L, index); if (lua_isnoneornil(L, -1)) { handler(L, index, t, type::function); lua_pop(L, 1); return false; } lua_getfield(L, -1, &callkey[0]); if (lua_isnoneornil(L, -1)) { handler(L, index, t, type::function); lua_pop(L, 2); return false; } // has call, is definitely a function lua_pop(L, 2); return true; } }; template struct checker { template static bool check(lua_State* L, int index, Handler&& handler) { type t = type_of(L, index); if (t == type::table) { return true; } if (t != type::userdata) { handler(L, index, t, type::function); return false; } return true; } }; template struct checker { template static bool check (lua_State* L, int index, Handler&& handler) { const type indextype = type_of(L, index); // Allow nil to be transformed to nullptr if (indextype == type::nil) { return true; } return checker{}.check(types(), L, indextype, index, std::forward(handler)); } }; template struct checker { template static bool check(lua_State* L, int index, Handler&& handler) { type t = type_of(L, index); bool success = t == type::userdata || t == type::lightuserdata; if (!success) { // expected type, actual type handler(L, index, type::lightuserdata, t); } return success; } }; template struct checker { template static bool check (types, lua_State* L, type indextype, int index, Handler&& handler) { if (indextype != type::userdata) { handler(L, index, type::userdata, indextype); return false; } if (meta::Or, std::is_same>::value) return true; if (lua_getmetatable(L, index) == 0) { return true; } if (stack_detail::check_metatable(L)) return true; if (stack_detail::check_metatable(L)) return true; if (stack_detail::check_metatable>(L)) return true; #ifndef SOL_NO_EXCEPTIONS lua_getfield(L, -1, &detail::base_class_check_key()[0]); void* basecastdata = lua_touserdata(L, -1); detail::throw_cast basecast = (detail::throw_cast)basecastdata; bool success = detail::catch_check(basecast); #elif !defined(SOL_NO_RTTI) lua_getfield(L, -1, &detail::base_class_check_key()[0]); if (stack::get(L) == type::nil) { lua_pop(L, 2); return false; } void* basecastdata = lua_touserdata(L, -1); detail::inheritance_check_function ic = (detail::inheritance_check_function)basecastdata; bool success = ic(typeid(T)); #else // Topkek lua_getfield(L, -1, &detail::base_class_check_key()[0]); if (stack::get(L) == type::nil) { lua_pop(L, 2); return false; } void* basecastdata = lua_touserdata(L, -1); detail::inheritance_check_function ic = (detail::inheritance_check_function)basecastdata; bool success = ic(detail::id_for::value); #endif // No Runtime Type Information || Exceptions lua_pop(L, 2); if (!success) { handler(L, index, type::userdata, indextype); return false; } return true; } template static bool check (lua_State* L, int index, Handler&& handler) { const type indextype = type_of(L, index); return check(types(), L, indextype, index, std::forward(handler)); } }; template struct checker::value>> { template static bool check(lua_State* L, int index, Handler&& handler) { return checker::type, type::userdata>{}.check(L, index, std::forward(handler)); } }; template struct checker, type::userdata, C> { template static bool check(lua_State* L, int index, Handler&& handler) { return checker{}.check(L, index, std::forward(handler)); } }; template struct checker, type::poly, C> { template static bool apply(std::index_sequence is, lua_State* L, int index, Handler&& handler) { index = index < 0 ? lua_absindex(L, index) - ( sizeof...(I) - 1 ) : index; return stack_detail::check_types{}.check(types(), is, L, index, handler); } template static bool check(lua_State* L, int index, Handler&& handler) { return apply(std::make_index_sequence(), L, index, std::forward(handler)); } }; template struct checker, type::poly, C> { template static bool check(lua_State* L, int index, Handler&& handler) { index = index < 0 ? lua_absindex(L, index) - 1 : index; return stack::check(L, index, handler) && stack::check(L, index + 1, handler); } }; template struct checker, type::poly, C> { template static bool check(lua_State* L, int index, Handler&& handler) { return stack::check(L, index, no_panic) || stack::check(L, index, std::forward(handler)); } }; } // stack } // sol #endif // SOL_STACK_CHECK_HPP