// The MIT License (MIT) // Copyright (c) 2013-2017 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) { const auto& metakey = usertype_traits::metatable(); luaL_getmetatable(L, &metakey[0]); const type expectedmetatabletype = static_cast(lua_type(L, -1)); if (expectedmetatabletype != type::lua_nil) { if (lua_rawequal(L, -1, index) == 1) { lua_pop(L, 1 + static_cast(poptable)); return true; } } lua_pop(L, 1); return false; } template struct basic_check { template static bool check(lua_State* L, int index, Handler&& handler, record& tracking) { tracking.use(1); bool success = check_func(L, index) == 1; if (!success) { // expected type, actual type handler(L, index, expected, type_of(L, index)); } return success; } }; } // stack_detail template struct checker { template static bool check(lua_State* L, int index, Handler&& handler, record& tracking) { tracking.use(1); 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::value>> { template static bool check(lua_State* L, int index, Handler&& handler, record& tracking) { tracking.use(1); bool success = lua_isinteger(L, index) == 1; if (!success) { // expected type, actual type handler(L, index, type::number, type_of(L, index)); } return success; } }; template struct checker::value>> { template static bool check(lua_State* L, int index, Handler&& handler, record& tracking) { tracking.use(1); bool success = lua_isnumber(L, index) == 1; if (!success) { // expected type, actual type handler(L, index, type::number, type_of(L, index)); } return success; } }; template struct checker { template static bool check(lua_State* L, int index, Handler&& handler, record& tracking) { bool success = lua_isnil(L, index); if (success) { tracking.use(1); return success; } tracking.use(0); success = lua_isnone(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&&, record& tracking) { tracking.use(0); return true; } }; template struct checker { template static bool check(lua_State*, int, Handler&&, record& tracking) { tracking.use(0); return true; } }; template struct checker { template static bool check(lua_State*, int, Handler&&, record& tracking) { tracking.use(0); return true; } }; template struct checker { template static bool check(lua_State*, int, Handler&&, record& tracking) { tracking.use(0); return true; } }; template struct checker { template static bool check(lua_State* L, int index, Handler&& handler, record& tracking) { tracking.use(1); 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, record& tracking) { tracking.use(1); 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(lua_State* L, int index, Handler&& handler, record& tracking) { tracking.use(1); type t = type_of(L, index); bool success = t == type::userdata; if (!success) { // expected type, actual type handler(L, index, type::userdata, t); } return success; } }; template struct checker, type::userdata, C> { template static bool check(lua_State* L, int index, Handler&& handler, record& tracking) { return stack::check(L, index, std::forward(handler), tracking); } }; template struct checker, type::userdata, C> : checker, type::lightuserdata, C> {}; 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, record& tracking) { tracking.use(1); type t = type_of(L, index); if (t == type::lua_nil || t == type::none || t == type::function) { // allow for lua_nil to be returned return true; } if (t != type::userdata && t != type::table) { handler(L, index, type::function, t); return false; } // Do advanced check for call-style userdata? static const auto& callkey = to_string(meta_function::call); if (lua_getmetatable(L, index) == 0) { // No metatable, no __call key possible handler(L, index, type::function, t); return false; } if (lua_isnoneornil(L, -1)) { lua_pop(L, 1); handler(L, index, type::function, t); return false; } lua_getfield(L, -1, &callkey[0]); if (lua_isnoneornil(L, -1)) { lua_pop(L, 2); handler(L, index, type::function, t); 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, record& tracking) { tracking.use(1); type t = type_of(L, index); if (t == type::table) { return true; } if (t != type::userdata) { handler(L, index, type::table, t); return false; } return true; } }; template struct checker { template static bool check(lua_State* L, int index, Handler&& handler, record& tracking) { tracking.use(1); if (lua_getmetatable(L, index) == 0) { return true; } type t = type_of(L, -1); if (t == type::table || t == type::none || t == type::nil) { lua_pop(L, 1); return true; } if (t != type::userdata) { lua_pop(L, 1); handler(L, index, expected, t); return false; } return true; } }; template struct checker { template static bool check(lua_State* L, int index, Handler&& handler, record& tracking) { tracking.use(1); if (lua_getmetatable(L, index) == 0) { return true; } type t = type_of(L, -1); if (t == type::table || t == type::none || t == type::nil) { lua_pop(L, 1); return true; } if (t != type::userdata) { lua_pop(L, 1); handler(L, index, type::table, t); return false; } return true; } }; template struct checker, type::poly, C> { template static bool check(lua_State* L, int index, Handler&& handler, record& tracking) { tracking.use(1); if (lua_getmetatable(L, index) == 0) { return true; } type t = type_of(L, -1); if (t == type::table || t == type::none || t == type::nil) { lua_pop(L, 1); return true; } if (t != type::userdata) { lua_pop(L, 1); handler(L, index, type::table, t); return false; } return true; } }; template struct checker, type::userdata, C> { template static bool check(types, lua_State* L, type indextype, int index, Handler&& handler, record& tracking) { tracking.use(1); if (indextype != type::userdata) { handler(L, index, type::userdata, indextype); return false; } if (meta::any, std::is_same, std::is_same, std::is_same>::value) return true; if (lua_getmetatable(L, index) == 0) { return true; } int metatableindex = lua_gettop(L); if (stack_detail::check_metatable(L, metatableindex)) return true; if (stack_detail::check_metatable(L, metatableindex)) return true; if (stack_detail::check_metatable>(L, metatableindex)) return true; bool success = false; if (detail::has_derived::value) { auto pn = stack::pop_n(L, 1); lua_pushstring(L, &detail::base_class_check_key()[0]); lua_rawget(L, metatableindex); if (type_of(L, -1) != type::lua_nil) { void* basecastdata = lua_touserdata(L, -1); detail::inheritance_check_function ic = (detail::inheritance_check_function)basecastdata; success = ic(detail::id_for::value); } } if (!success) { lua_pop(L, 1); handler(L, index, type::userdata, indextype); return false; } lua_pop(L, 1); return true; } }; template struct checker { template static bool check(lua_State* L, int index, Handler&& handler, record& tracking) { const type indextype = type_of(L, index); return checker, type::userdata, C>{}.check(types(), L, indextype, index, std::forward(handler), tracking); } }; template struct checker { template static bool check(lua_State* L, int index, Handler&& handler, record& tracking) { const type indextype = type_of(L, index); // Allow lua_nil to be transformed to nullptr if (indextype == type::lua_nil) { tracking.use(1); return true; } return checker, type::userdata, C>{}.check(L, index, std::forward(handler), tracking); } }; template struct checker::value>> { typedef typename unique_usertype_traits::type T; template static bool check(lua_State* L, int index, Handler&& handler, record& tracking) { const type indextype = type_of(L, index); tracking.use(1); if (indextype != type::userdata) { handler(L, index, type::userdata, indextype); return false; } if (lua_getmetatable(L, index) == 0) { return true; } int metatableindex = lua_gettop(L); if (stack_detail::check_metatable>(L, metatableindex)) return true; lua_pop(L, 1); handler(L, index, type::userdata, indextype); return false; } }; template struct checker, type::userdata, C> { template static bool check(lua_State* L, int index, Handler&& handler, record& tracking) { return checker{}.check(L, index, std::forward(handler), tracking); } }; template struct checker, type::poly, C> { template static bool check(lua_State* L, int index, Handler&& handler, record& tracking) { return stack::multi_check(L, index, std::forward(handler), tracking); } }; template struct checker, type::poly, C> { template static bool check(lua_State* L, int index, Handler&& handler, record& tracking) { return stack::multi_check(L, index, std::forward(handler), tracking); } }; template struct checker, type::poly, C> { template static bool check(lua_State* L, int index, Handler&&, record& tracking) { type t = type_of(L, index); if (t == type::none) { tracking.use(0); return true; } if (t == type::lua_nil) { tracking.use(1); return true; } return stack::check(L, index, no_panic, tracking); } }; } // stack } // sol #endif // SOL_STACK_CHECK_HPP