mirror of
https://github.com/ThePhD/sol2.git
synced 2024-03-22 13:10:44 +08:00
Lua stream example!
This commit is contained in:
parent
2beb13b9b2
commit
0c38fd1a57
23
examples/source/lua_stream.cpp
Normal file
23
examples/source/lua_stream.cpp
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
#define SOL_CHECK_ARGUMENTS 1
|
||||||
|
#include <sol/sol.hpp>
|
||||||
|
|
||||||
|
#include <cstdio>
|
||||||
|
|
||||||
|
bool is_file_std_out(sol::table data) {
|
||||||
|
sol::object maybe_file = data["file"];
|
||||||
|
if (maybe_file.is<luaL_Stream&>()) {
|
||||||
|
luaL_Stream& filestream = data["file"];
|
||||||
|
return filestream.f == stdout;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
sol::state lua;
|
||||||
|
lua.open_libraries(sol::lib::base, sol::lib::io);
|
||||||
|
|
||||||
|
lua["is_std_out"] = &is_file_std_out;
|
||||||
|
lua.script("assert(is_std_out{ file = io.stdout })");
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
@ -181,7 +181,7 @@ namespace sol { namespace stack {
|
|||||||
return success;
|
return success;
|
||||||
#endif // Strings are Numbers
|
#endif // Strings are Numbers
|
||||||
}
|
}
|
||||||
else if constexpr(meta::any_same_v<T, type, this_state, this_main_state, this_environment, variadic_args>) {
|
else if constexpr (meta::any_same_v<T, type, this_state, this_main_state, this_environment, variadic_args>) {
|
||||||
(void)L;
|
(void)L;
|
||||||
(void)index;
|
(void)index;
|
||||||
(void)handler;
|
(void)handler;
|
||||||
@ -223,11 +223,12 @@ namespace sol { namespace stack {
|
|||||||
handler(L, index, type::userdata, indextype, "unrecognized userdata (not pushed by sol?)");
|
handler(L, index, type::userdata, indextype, "unrecognized userdata (not pushed by sol?)");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
else if constexpr (meta::any_same_v<T, lua_nil_t,
|
else if constexpr (meta::any_same_v<T,
|
||||||
|
lua_nil_t,
|
||||||
#if defined(SOL_CXX_17_FEATURES) && SOL_CXX_17_FEATURES
|
#if defined(SOL_CXX_17_FEATURES) && SOL_CXX_17_FEATURES
|
||||||
std::nullopt_t,
|
std::nullopt_t,
|
||||||
#endif
|
#endif
|
||||||
nullopt_t>) {
|
nullopt_t>) {
|
||||||
bool success = lua_isnil(L, index);
|
bool success = lua_isnil(L, index);
|
||||||
if (success) {
|
if (success) {
|
||||||
tracking.use(1);
|
tracking.use(1);
|
||||||
@ -271,7 +272,7 @@ namespace sol { namespace stack {
|
|||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
else if constexpr(meta::is_specialization_of_v<T, basic_environment>) {
|
else if constexpr (meta::is_specialization_of_v<T, basic_environment>) {
|
||||||
tracking.use(1);
|
tracking.use(1);
|
||||||
if (lua_getmetatable(L, index) == 0) {
|
if (lua_getmetatable(L, index) == 0) {
|
||||||
return true;
|
return true;
|
||||||
@ -305,6 +306,33 @@ namespace sol { namespace stack {
|
|||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
else if constexpr (std::is_same_v<T, luaL_Stream*> || std::is_same_v<T, luaL_Stream>) {
|
||||||
|
if (lua_getmetatable(L, index) == 0) {
|
||||||
|
type t = type_of(L, index);
|
||||||
|
handler(L, index, expected, t, "value is not a valid luaL_Stream (has no metatable/is not a valid value)");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
luaL_getmetatable(L, LUA_FILEHANDLE);
|
||||||
|
if (type_of(L, index) != type::table) {
|
||||||
|
type t = type_of(L, index);
|
||||||
|
lua_pop(L, 1);
|
||||||
|
handler(L,
|
||||||
|
index,
|
||||||
|
expected,
|
||||||
|
t,
|
||||||
|
"value is not a valid luaL_Stream (there is no metatable for luaL_Stream -- did you forget to "
|
||||||
|
"my_lua_state.open_libraries(sol::lib::state) or equivalent?)");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
int is_stream_table = lua_compare(L, -1, -2, LUA_OPEQ);
|
||||||
|
lua_pop(L, 2);
|
||||||
|
if (is_stream_table == 0) {
|
||||||
|
type t = type_of(L, index);
|
||||||
|
handler(L, index, expected, t, "value is not a valid luaL_Stream (incorrect metatable)");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
else if constexpr (expected == type::userdata) {
|
else if constexpr (expected == type::userdata) {
|
||||||
if constexpr (meta::any_same_v<T, userdata_value> || meta::is_specialization_of_v<T, basic_userdata>) {
|
if constexpr (meta::any_same_v<T, userdata_value> || meta::is_specialization_of_v<T, basic_userdata>) {
|
||||||
tracking.use(1);
|
tracking.use(1);
|
||||||
@ -435,7 +463,9 @@ namespace sol { namespace stack {
|
|||||||
|
|
||||||
template <typename U, typename Handler>
|
template <typename U, typename Handler>
|
||||||
static bool check(types<U>, lua_State* L, int index, type indextype, Handler&& handler, record& tracking) {
|
static bool check(types<U>, lua_State* L, int index, type indextype, Handler&& handler, record& tracking) {
|
||||||
if constexpr (std::is_same_v<T, lightuserdata_value> || std::is_same_v<T, userdata_value> || std::is_same_v<T, userdata> || std::is_same_v<T, lightuserdata>) {
|
if constexpr (
|
||||||
|
std::is_same_v<T,
|
||||||
|
lightuserdata_value> || std::is_same_v<T, userdata_value> || std::is_same_v<T, userdata> || std::is_same_v<T, lightuserdata>) {
|
||||||
tracking.use(1);
|
tracking.use(1);
|
||||||
if (indextype != type::userdata) {
|
if (indextype != type::userdata) {
|
||||||
handler(L, index, type::userdata, indextype, "value is not a valid userdata");
|
handler(L, index, type::userdata, indextype, "value is not a valid userdata");
|
||||||
@ -469,9 +499,9 @@ namespace sol { namespace stack {
|
|||||||
bool success = false;
|
bool success = false;
|
||||||
bool has_derived = derive<T>::value || weak_derive<T>::value;
|
bool has_derived = derive<T>::value || weak_derive<T>::value;
|
||||||
if (has_derived) {
|
if (has_derived) {
|
||||||
#if defined(SOL_SAFE_STACK_CHECK) && SOL_SAFE_STACK_CHECK
|
#if defined(SOL_SAFE_STACK_CHECK) && SOL_SAFE_STACK_CHECK
|
||||||
luaL_checkstack(L, 1, detail::not_enough_stack_space_string);
|
luaL_checkstack(L, 1, detail::not_enough_stack_space_string);
|
||||||
#endif // make sure stack doesn't overflow
|
#endif // make sure stack doesn't overflow
|
||||||
auto pn = stack::pop_n(L, 1);
|
auto pn = stack::pop_n(L, 1);
|
||||||
lua_pushstring(L, &detail::base_class_check_key()[0]);
|
lua_pushstring(L, &detail::base_class_check_key()[0]);
|
||||||
lua_rawget(L, metatableindex);
|
lua_rawget(L, metatableindex);
|
||||||
|
@ -84,7 +84,7 @@ namespace sol { namespace stack {
|
|||||||
cp = dr.codepoint;
|
cp = dr.codepoint;
|
||||||
strtarget = dr.next;
|
strtarget = dr.next;
|
||||||
}
|
}
|
||||||
if constexpr(std::is_same_v<Ch, char32_t>) {
|
if constexpr (std::is_same_v<Ch, char32_t>) {
|
||||||
auto er = unicode::code_point_to_utf32(cp);
|
auto er = unicode::code_point_to_utf32(cp);
|
||||||
f(er);
|
f(er);
|
||||||
}
|
}
|
||||||
@ -153,6 +153,14 @@ namespace sol { namespace stack {
|
|||||||
Real* mem = static_cast<Real*>(memory);
|
Real* mem = static_cast<Real*>(memory);
|
||||||
return *mem;
|
return *mem;
|
||||||
}
|
}
|
||||||
|
else if constexpr (std::is_same_v<T, luaL_Stream*>) {
|
||||||
|
luaL_Stream* pstream = static_cast<luaL_Stream*>(lua_touserdata(L, index));
|
||||||
|
return pstream;
|
||||||
|
}
|
||||||
|
else if constexpr (std::is_same_v<T, luaL_Stream>) {
|
||||||
|
luaL_Stream* pstream = static_cast<luaL_Stream*>(lua_touserdata(L, index));
|
||||||
|
return *pstream;
|
||||||
|
}
|
||||||
else {
|
else {
|
||||||
return stack_detail::unchecked_unqualified_get<detail::as_value_tag<T>>(L, index, tracking);
|
return stack_detail::unchecked_unqualified_get<detail::as_value_tag<T>>(L, index, tracking);
|
||||||
}
|
}
|
||||||
@ -164,7 +172,8 @@ namespace sol { namespace stack {
|
|||||||
static decltype(auto) get(lua_State* L, int index, record& tracking) {
|
static decltype(auto) get(lua_State* L, int index, record& tracking) {
|
||||||
using Tu = meta::unqualified_t<X>;
|
using Tu = meta::unqualified_t<X>;
|
||||||
static constexpr bool is_userdata_of_some_kind
|
static constexpr bool is_userdata_of_some_kind
|
||||||
= !std::is_reference_v<X> && is_container_v<Tu> && std::is_default_constructible_v<Tu> && !is_lua_primitive_v<Tu> && !is_transparent_argument_v<Tu>;
|
= !std::is_reference_v<
|
||||||
|
X> && is_container_v<Tu> && std::is_default_constructible_v<Tu> && !is_lua_primitive_v<Tu> && !is_transparent_argument_v<Tu>;
|
||||||
if constexpr (is_userdata_of_some_kind) {
|
if constexpr (is_userdata_of_some_kind) {
|
||||||
if (type_of(L, index) == type::userdata) {
|
if (type_of(L, index) == type::userdata) {
|
||||||
return static_cast<Tu>(stack_detail::unchecked_unqualified_get<Tu>(L, index, tracking));
|
return static_cast<Tu>(stack_detail::unchecked_unqualified_get<Tu>(L, index, tracking));
|
||||||
@ -280,7 +289,7 @@ namespace sol { namespace stack {
|
|||||||
|
|
||||||
// the W4 flag is really great,
|
// the W4 flag is really great,
|
||||||
// so great that it can tell my for loops (twice nested)
|
// so great that it can tell my for loops (twice nested)
|
||||||
// below never actually terminate
|
// below never actually terminate
|
||||||
// without hitting where the gotos have infested
|
// without hitting where the gotos have infested
|
||||||
|
|
||||||
// so now I would get the error W4XXX unreachable
|
// so now I would get the error W4XXX unreachable
|
||||||
@ -316,7 +325,7 @@ namespace sol { namespace stack {
|
|||||||
T cont;
|
T cont;
|
||||||
std::size_t idx = 0;
|
std::size_t idx = 0;
|
||||||
#if SOL_LUA_VERSION >= 503
|
#if SOL_LUA_VERSION >= 503
|
||||||
// This method is HIGHLY performant over regular table iteration
|
// This method is HIGHLY performant over regular table iteration
|
||||||
// thanks to the Lua API changes in 5.3
|
// thanks to the Lua API changes in 5.3
|
||||||
// Questionable in 5.4
|
// Questionable in 5.4
|
||||||
for (lua_Integer i = 0;; i += lua_size<V>::value) {
|
for (lua_Integer i = 0;; i += lua_size<V>::value) {
|
||||||
@ -363,7 +372,7 @@ namespace sol { namespace stack {
|
|||||||
#endif
|
#endif
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
push_back_at_end(meta::has_push_back<Tu>(), t, L, cont, idx);
|
push_back_at_end(meta::has_push_back<Tu>(), t, L, cont, idx);
|
||||||
++idx;
|
++idx;
|
||||||
lua_pop(L, lua_size<V>::value);
|
lua_pop(L, lua_size<V>::value);
|
||||||
@ -399,7 +408,7 @@ namespace sol { namespace stack {
|
|||||||
++idx;
|
++idx;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
done:
|
done:
|
||||||
return cont;
|
return cont;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -514,7 +523,7 @@ namespace sol { namespace stack {
|
|||||||
++idx;
|
++idx;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
done:
|
done:
|
||||||
return cont;
|
return cont;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -756,7 +765,7 @@ namespace sol { namespace stack {
|
|||||||
struct unqualified_getter<meta_function> {
|
struct unqualified_getter<meta_function> {
|
||||||
static meta_function get(lua_State* L, int index, record& tracking) {
|
static meta_function get(lua_State* L, int index, record& tracking) {
|
||||||
tracking.use(1);
|
tracking.use(1);
|
||||||
const char* name = unqualified_getter<const char*>{}.get(L, index, tracking);
|
const char* name = unqualified_getter<const char*> {}.get(L, index, tracking);
|
||||||
const auto& mfnames = meta_function_names();
|
const auto& mfnames = meta_function_names();
|
||||||
for (std::size_t i = 0; i < mfnames.size(); ++i)
|
for (std::size_t i = 0; i < mfnames.size(); ++i)
|
||||||
if (mfnames[i] == name)
|
if (mfnames[i] == name)
|
||||||
@ -874,8 +883,7 @@ namespace sol { namespace stack {
|
|||||||
lua_getfield(L, -1, &detail::base_class_cast_key()[0]);
|
lua_getfield(L, -1, &detail::base_class_cast_key()[0]);
|
||||||
if (type_of(L, -1) != type::lua_nil) {
|
if (type_of(L, -1) != type::lua_nil) {
|
||||||
void* basecastdata = lua_touserdata(L, -1);
|
void* basecastdata = lua_touserdata(L, -1);
|
||||||
detail::inheritance_cast_function ic
|
detail::inheritance_cast_function ic = reinterpret_cast<detail::inheritance_cast_function>(basecastdata);
|
||||||
= reinterpret_cast<detail::inheritance_cast_function>(basecastdata);
|
|
||||||
// use the casting function to properly adjust the pointer for the desired T
|
// use the casting function to properly adjust the pointer for the desired T
|
||||||
udata = ic(udata, usertype_traits<T>::qualified_name());
|
udata = ic(udata, usertype_traits<T>::qualified_name());
|
||||||
}
|
}
|
||||||
@ -953,15 +961,14 @@ namespace sol { namespace stack {
|
|||||||
template <typename... Args>
|
template <typename... Args>
|
||||||
static R apply(std::index_sequence<>, lua_State*, int, record&, Args&&... args) {
|
static R apply(std::index_sequence<>, lua_State*, int, record&, Args&&... args) {
|
||||||
// Fuck you too, VC++
|
// Fuck you too, VC++
|
||||||
return R{ std::forward<Args>(args)... };
|
return R { std::forward<Args>(args)... };
|
||||||
}
|
}
|
||||||
|
|
||||||
template <std::size_t I, std::size_t... Ix, typename... Args>
|
template <std::size_t I, std::size_t... Ix, typename... Args>
|
||||||
static R apply(std::index_sequence<I, Ix...>, lua_State* L, int index, record& tracking, Args&&... args) {
|
static R apply(std::index_sequence<I, Ix...>, lua_State* L, int index, record& tracking, Args&&... args) {
|
||||||
// Fuck you too, VC++
|
// Fuck you too, VC++
|
||||||
typedef std::tuple_element_t<I, std::tuple<Tn...>> T;
|
typedef std::tuple_element_t<I, std::tuple<Tn...>> T;
|
||||||
return apply(std::index_sequence<Ix...>(), L, index, tracking, std::forward<Args>(args)...,
|
return apply(std::index_sequence<Ix...>(), L, index, tracking, std::forward<Args>(args)..., stack::get<T>(L, index + tracking.used, tracking));
|
||||||
stack::get<T>(L, index + tracking.used, tracking));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static R get(lua_State* L, int index, record& tracking) {
|
static R get(lua_State* L, int index, record& tracking) {
|
||||||
@ -972,9 +979,8 @@ namespace sol { namespace stack {
|
|||||||
template <typename A, typename B>
|
template <typename A, typename B>
|
||||||
struct unqualified_getter<std::pair<A, B>> {
|
struct unqualified_getter<std::pair<A, B>> {
|
||||||
static decltype(auto) get(lua_State* L, int index, record& tracking) {
|
static decltype(auto) get(lua_State* L, int index, record& tracking) {
|
||||||
return std::pair<decltype(stack::get<A>(L, index)), decltype(stack::get<B>(L, index))>{
|
return std::pair<decltype(stack::get<A>(L, index)), decltype(stack::get<B>(L, index))> { stack::get<A>(L, index, tracking),
|
||||||
stack::get<A>(L, index, tracking), stack::get<B>(L, index + tracking.used, tracking)
|
stack::get<B>(L, index + tracking.used, tracking) };
|
||||||
};
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -992,9 +998,9 @@ namespace sol { namespace stack {
|
|||||||
return V();
|
return V();
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
//using T = std::variant_alternative_t<0, V>;
|
// using T = std::variant_alternative_t<0, V>;
|
||||||
std::abort();
|
std::abort();
|
||||||
//return V(std::in_place_index<0>, stack::get<T>(L, index, tracking));
|
// return V(std::in_place_index<0>, stack::get<T>(L, index, tracking));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1016,6 +1022,6 @@ namespace sol { namespace stack {
|
|||||||
#endif // SOL_STD_VARIANT
|
#endif // SOL_STD_VARIANT
|
||||||
#endif // SOL_CXX17_FEATURES
|
#endif // SOL_CXX17_FEATURES
|
||||||
|
|
||||||
}} // namespace sol::stack
|
}} // namespace sol::stack
|
||||||
|
|
||||||
#endif // SOL_STACK_UNQUALIFIED_GET_HPP
|
#endif // SOL_STACK_UNQUALIFIED_GET_HPP
|
||||||
|
@ -296,6 +296,20 @@ namespace sol { namespace stack {
|
|||||||
lua_pushnumber(L, std::forward<Args>(args)...);
|
lua_pushnumber(L, std::forward<Args>(args)...);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
else if constexpr (std::is_same<Tu, luaL_Stream*>) {
|
||||||
|
luaL_Stream* source(std::forward<Args>(args)...);
|
||||||
|
luaL_Stream* stream = static_cast<luaL_Stream*>(lua_newuserdata(L, sizeof(luaL_Stream)));
|
||||||
|
stream->f = source->f;
|
||||||
|
stream->closef = source->closef;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
else if constexpr (std::is_same<Tu, luaL_Stream>) {
|
||||||
|
luaL_Stream& source(std::forward<Args>(args)...);
|
||||||
|
luaL_Stream* stream = static_cast<luaL_Stream*>(lua_newuserdata(L, sizeof(luaL_Stream)));
|
||||||
|
stream->f = source.f;
|
||||||
|
stream->closef = source.closef;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
else if constexpr (std::is_enum_v<Tu>) {
|
else if constexpr (std::is_enum_v<Tu>) {
|
||||||
return stack_detail::msvc_is_ass_with_if_constexpr_push_enum(std::true_type(), L, std::forward<Args>(args)...);
|
return stack_detail::msvc_is_ass_with_if_constexpr_push_enum(std::true_type(), L, std::forward<Args>(args)...);
|
||||||
}
|
}
|
||||||
|
@ -20,8 +20,8 @@
|
|||||||
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
// This file was generated with a script.
|
// This file was generated with a script.
|
||||||
// Generated 2020-01-25 04:59:37.808053 UTC
|
// Generated 2020-01-25 17:11:21.688498 UTC
|
||||||
// This header was generated with sol v3.2.0 (revision c5863c8)
|
// This header was generated with sol v3.2.0 (revision 2beb13b)
|
||||||
// https://github.com/ThePhD/sol2
|
// https://github.com/ThePhD/sol2
|
||||||
|
|
||||||
#ifndef SOL_SINGLE_INCLUDE_FORWARD_HPP
|
#ifndef SOL_SINGLE_INCLUDE_FORWARD_HPP
|
||||||
|
@ -20,8 +20,8 @@
|
|||||||
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
// This file was generated with a script.
|
// This file was generated with a script.
|
||||||
// Generated 2020-01-25 04:59:35.996014 UTC
|
// Generated 2020-01-25 17:11:19.283779 UTC
|
||||||
// This header was generated with sol v3.2.0 (revision c5863c8)
|
// This header was generated with sol v3.2.0 (revision 2beb13b)
|
||||||
// https://github.com/ThePhD/sol2
|
// https://github.com/ThePhD/sol2
|
||||||
|
|
||||||
#ifndef SOL_SINGLE_INCLUDE_HPP
|
#ifndef SOL_SINGLE_INCLUDE_HPP
|
||||||
@ -11060,7 +11060,7 @@ namespace sol { namespace stack {
|
|||||||
return success;
|
return success;
|
||||||
#endif // Strings are Numbers
|
#endif // Strings are Numbers
|
||||||
}
|
}
|
||||||
else if constexpr(meta::any_same_v<T, type, this_state, this_main_state, this_environment, variadic_args>) {
|
else if constexpr (meta::any_same_v<T, type, this_state, this_main_state, this_environment, variadic_args>) {
|
||||||
(void)L;
|
(void)L;
|
||||||
(void)index;
|
(void)index;
|
||||||
(void)handler;
|
(void)handler;
|
||||||
@ -11102,11 +11102,12 @@ namespace sol { namespace stack {
|
|||||||
handler(L, index, type::userdata, indextype, "unrecognized userdata (not pushed by sol?)");
|
handler(L, index, type::userdata, indextype, "unrecognized userdata (not pushed by sol?)");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
else if constexpr (meta::any_same_v<T, lua_nil_t,
|
else if constexpr (meta::any_same_v<T,
|
||||||
|
lua_nil_t,
|
||||||
#if defined(SOL_CXX_17_FEATURES) && SOL_CXX_17_FEATURES
|
#if defined(SOL_CXX_17_FEATURES) && SOL_CXX_17_FEATURES
|
||||||
std::nullopt_t,
|
std::nullopt_t,
|
||||||
#endif
|
#endif
|
||||||
nullopt_t>) {
|
nullopt_t>) {
|
||||||
bool success = lua_isnil(L, index);
|
bool success = lua_isnil(L, index);
|
||||||
if (success) {
|
if (success) {
|
||||||
tracking.use(1);
|
tracking.use(1);
|
||||||
@ -11150,7 +11151,7 @@ namespace sol { namespace stack {
|
|||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
else if constexpr(meta::is_specialization_of_v<T, basic_environment>) {
|
else if constexpr (meta::is_specialization_of_v<T, basic_environment>) {
|
||||||
tracking.use(1);
|
tracking.use(1);
|
||||||
if (lua_getmetatable(L, index) == 0) {
|
if (lua_getmetatable(L, index) == 0) {
|
||||||
return true;
|
return true;
|
||||||
@ -11184,6 +11185,30 @@ namespace sol { namespace stack {
|
|||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
else if constexpr (std::is_same_v<T, luaL_Stream*> || std::is_same_v<T, luaL_Stream>) {
|
||||||
|
if (lua_getmetatable(L, index) == 0) {
|
||||||
|
handler(L, index, expected, t, "value is not a valid luaL_Stream (has no metatable/is not a valid value)");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
luaL_getmetatable(L, LUA_FILEHANDLE);
|
||||||
|
if (type_of(L, index) != type::table) {
|
||||||
|
lua_pop(L, 1);
|
||||||
|
handler(L,
|
||||||
|
index,
|
||||||
|
expected,
|
||||||
|
t,
|
||||||
|
"value is not a valid luaL_Stream (there is no metatable for luaL_Stream -- did you forget to "
|
||||||
|
"my_lua_state.open_libraries(sol::lib::state) or equivalent?)");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
int is_stream_table = lua_compare(L, -1, -2, LUA_OPEQ);
|
||||||
|
lua_pop(L, 2);
|
||||||
|
if (is_stream_table == 0) {
|
||||||
|
handler(L, index, expected, t, "value is not a valid luaL_Stream (incorrect metatable)");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
else if constexpr (expected == type::userdata) {
|
else if constexpr (expected == type::userdata) {
|
||||||
if constexpr (meta::any_same_v<T, userdata_value> || meta::is_specialization_of_v<T, basic_userdata>) {
|
if constexpr (meta::any_same_v<T, userdata_value> || meta::is_specialization_of_v<T, basic_userdata>) {
|
||||||
tracking.use(1);
|
tracking.use(1);
|
||||||
@ -11314,7 +11339,9 @@ namespace sol { namespace stack {
|
|||||||
|
|
||||||
template <typename U, typename Handler>
|
template <typename U, typename Handler>
|
||||||
static bool check(types<U>, lua_State* L, int index, type indextype, Handler&& handler, record& tracking) {
|
static bool check(types<U>, lua_State* L, int index, type indextype, Handler&& handler, record& tracking) {
|
||||||
if constexpr (std::is_same_v<T, lightuserdata_value> || std::is_same_v<T, userdata_value> || std::is_same_v<T, userdata> || std::is_same_v<T, lightuserdata>) {
|
if constexpr (
|
||||||
|
std::is_same_v<T,
|
||||||
|
lightuserdata_value> || std::is_same_v<T, userdata_value> || std::is_same_v<T, userdata> || std::is_same_v<T, lightuserdata>) {
|
||||||
tracking.use(1);
|
tracking.use(1);
|
||||||
if (indextype != type::userdata) {
|
if (indextype != type::userdata) {
|
||||||
handler(L, index, type::userdata, indextype, "value is not a valid userdata");
|
handler(L, index, type::userdata, indextype, "value is not a valid userdata");
|
||||||
@ -11348,9 +11375,9 @@ namespace sol { namespace stack {
|
|||||||
bool success = false;
|
bool success = false;
|
||||||
bool has_derived = derive<T>::value || weak_derive<T>::value;
|
bool has_derived = derive<T>::value || weak_derive<T>::value;
|
||||||
if (has_derived) {
|
if (has_derived) {
|
||||||
#if defined(SOL_SAFE_STACK_CHECK) && SOL_SAFE_STACK_CHECK
|
#if defined(SOL_SAFE_STACK_CHECK) && SOL_SAFE_STACK_CHECK
|
||||||
luaL_checkstack(L, 1, detail::not_enough_stack_space_string);
|
luaL_checkstack(L, 1, detail::not_enough_stack_space_string);
|
||||||
#endif // make sure stack doesn't overflow
|
#endif // make sure stack doesn't overflow
|
||||||
auto pn = stack::pop_n(L, 1);
|
auto pn = stack::pop_n(L, 1);
|
||||||
lua_pushstring(L, &detail::base_class_check_key()[0]);
|
lua_pushstring(L, &detail::base_class_check_key()[0]);
|
||||||
lua_rawget(L, metatableindex);
|
lua_rawget(L, metatableindex);
|
||||||
@ -11932,7 +11959,7 @@ namespace sol { namespace stack {
|
|||||||
cp = dr.codepoint;
|
cp = dr.codepoint;
|
||||||
strtarget = dr.next;
|
strtarget = dr.next;
|
||||||
}
|
}
|
||||||
if constexpr(std::is_same_v<Ch, char32_t>) {
|
if constexpr (std::is_same_v<Ch, char32_t>) {
|
||||||
auto er = unicode::code_point_to_utf32(cp);
|
auto er = unicode::code_point_to_utf32(cp);
|
||||||
f(er);
|
f(er);
|
||||||
}
|
}
|
||||||
@ -12001,6 +12028,14 @@ namespace sol { namespace stack {
|
|||||||
Real* mem = static_cast<Real*>(memory);
|
Real* mem = static_cast<Real*>(memory);
|
||||||
return *mem;
|
return *mem;
|
||||||
}
|
}
|
||||||
|
else if constexpr (std::is_same_v<T, luaL_Stream*>) {
|
||||||
|
luaL_Stream* pstream = static_cast<luaL_Stream*>(lua_touserdata(L, index));
|
||||||
|
return pstream;
|
||||||
|
}
|
||||||
|
else if constexpr (std::is_same_v<T, luaL_Stream>) {
|
||||||
|
luaL_Stream* pstream = static_cast<luaL_Stream*>(lua_touserdata(L, index));
|
||||||
|
return *pstream;
|
||||||
|
}
|
||||||
else {
|
else {
|
||||||
return stack_detail::unchecked_unqualified_get<detail::as_value_tag<T>>(L, index, tracking);
|
return stack_detail::unchecked_unqualified_get<detail::as_value_tag<T>>(L, index, tracking);
|
||||||
}
|
}
|
||||||
@ -12012,7 +12047,8 @@ namespace sol { namespace stack {
|
|||||||
static decltype(auto) get(lua_State* L, int index, record& tracking) {
|
static decltype(auto) get(lua_State* L, int index, record& tracking) {
|
||||||
using Tu = meta::unqualified_t<X>;
|
using Tu = meta::unqualified_t<X>;
|
||||||
static constexpr bool is_userdata_of_some_kind
|
static constexpr bool is_userdata_of_some_kind
|
||||||
= !std::is_reference_v<X> && is_container_v<Tu> && std::is_default_constructible_v<Tu> && !is_lua_primitive_v<Tu> && !is_transparent_argument_v<Tu>;
|
= !std::is_reference_v<
|
||||||
|
X> && is_container_v<Tu> && std::is_default_constructible_v<Tu> && !is_lua_primitive_v<Tu> && !is_transparent_argument_v<Tu>;
|
||||||
if constexpr (is_userdata_of_some_kind) {
|
if constexpr (is_userdata_of_some_kind) {
|
||||||
if (type_of(L, index) == type::userdata) {
|
if (type_of(L, index) == type::userdata) {
|
||||||
return static_cast<Tu>(stack_detail::unchecked_unqualified_get<Tu>(L, index, tracking));
|
return static_cast<Tu>(stack_detail::unchecked_unqualified_get<Tu>(L, index, tracking));
|
||||||
@ -12128,7 +12164,7 @@ namespace sol { namespace stack {
|
|||||||
|
|
||||||
// the W4 flag is really great,
|
// the W4 flag is really great,
|
||||||
// so great that it can tell my for loops (twice nested)
|
// so great that it can tell my for loops (twice nested)
|
||||||
// below never actually terminate
|
// below never actually terminate
|
||||||
// without hitting where the gotos have infested
|
// without hitting where the gotos have infested
|
||||||
|
|
||||||
// so now I would get the error W4XXX unreachable
|
// so now I would get the error W4XXX unreachable
|
||||||
@ -12164,7 +12200,7 @@ namespace sol { namespace stack {
|
|||||||
T cont;
|
T cont;
|
||||||
std::size_t idx = 0;
|
std::size_t idx = 0;
|
||||||
#if SOL_LUA_VERSION >= 503
|
#if SOL_LUA_VERSION >= 503
|
||||||
// This method is HIGHLY performant over regular table iteration
|
// This method is HIGHLY performant over regular table iteration
|
||||||
// thanks to the Lua API changes in 5.3
|
// thanks to the Lua API changes in 5.3
|
||||||
// Questionable in 5.4
|
// Questionable in 5.4
|
||||||
for (lua_Integer i = 0;; i += lua_size<V>::value) {
|
for (lua_Integer i = 0;; i += lua_size<V>::value) {
|
||||||
@ -12211,7 +12247,7 @@ namespace sol { namespace stack {
|
|||||||
#endif
|
#endif
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
push_back_at_end(meta::has_push_back<Tu>(), t, L, cont, idx);
|
push_back_at_end(meta::has_push_back<Tu>(), t, L, cont, idx);
|
||||||
++idx;
|
++idx;
|
||||||
lua_pop(L, lua_size<V>::value);
|
lua_pop(L, lua_size<V>::value);
|
||||||
@ -12247,7 +12283,7 @@ namespace sol { namespace stack {
|
|||||||
++idx;
|
++idx;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
done:
|
done:
|
||||||
return cont;
|
return cont;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -12362,7 +12398,7 @@ namespace sol { namespace stack {
|
|||||||
++idx;
|
++idx;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
done:
|
done:
|
||||||
return cont;
|
return cont;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -12604,7 +12640,7 @@ namespace sol { namespace stack {
|
|||||||
struct unqualified_getter<meta_function> {
|
struct unqualified_getter<meta_function> {
|
||||||
static meta_function get(lua_State* L, int index, record& tracking) {
|
static meta_function get(lua_State* L, int index, record& tracking) {
|
||||||
tracking.use(1);
|
tracking.use(1);
|
||||||
const char* name = unqualified_getter<const char*>{}.get(L, index, tracking);
|
const char* name = unqualified_getter<const char*> {}.get(L, index, tracking);
|
||||||
const auto& mfnames = meta_function_names();
|
const auto& mfnames = meta_function_names();
|
||||||
for (std::size_t i = 0; i < mfnames.size(); ++i)
|
for (std::size_t i = 0; i < mfnames.size(); ++i)
|
||||||
if (mfnames[i] == name)
|
if (mfnames[i] == name)
|
||||||
@ -12722,8 +12758,7 @@ namespace sol { namespace stack {
|
|||||||
lua_getfield(L, -1, &detail::base_class_cast_key()[0]);
|
lua_getfield(L, -1, &detail::base_class_cast_key()[0]);
|
||||||
if (type_of(L, -1) != type::lua_nil) {
|
if (type_of(L, -1) != type::lua_nil) {
|
||||||
void* basecastdata = lua_touserdata(L, -1);
|
void* basecastdata = lua_touserdata(L, -1);
|
||||||
detail::inheritance_cast_function ic
|
detail::inheritance_cast_function ic = reinterpret_cast<detail::inheritance_cast_function>(basecastdata);
|
||||||
= reinterpret_cast<detail::inheritance_cast_function>(basecastdata);
|
|
||||||
// use the casting function to properly adjust the pointer for the desired T
|
// use the casting function to properly adjust the pointer for the desired T
|
||||||
udata = ic(udata, usertype_traits<T>::qualified_name());
|
udata = ic(udata, usertype_traits<T>::qualified_name());
|
||||||
}
|
}
|
||||||
@ -12801,15 +12836,14 @@ namespace sol { namespace stack {
|
|||||||
template <typename... Args>
|
template <typename... Args>
|
||||||
static R apply(std::index_sequence<>, lua_State*, int, record&, Args&&... args) {
|
static R apply(std::index_sequence<>, lua_State*, int, record&, Args&&... args) {
|
||||||
// Fuck you too, VC++
|
// Fuck you too, VC++
|
||||||
return R{ std::forward<Args>(args)... };
|
return R { std::forward<Args>(args)... };
|
||||||
}
|
}
|
||||||
|
|
||||||
template <std::size_t I, std::size_t... Ix, typename... Args>
|
template <std::size_t I, std::size_t... Ix, typename... Args>
|
||||||
static R apply(std::index_sequence<I, Ix...>, lua_State* L, int index, record& tracking, Args&&... args) {
|
static R apply(std::index_sequence<I, Ix...>, lua_State* L, int index, record& tracking, Args&&... args) {
|
||||||
// Fuck you too, VC++
|
// Fuck you too, VC++
|
||||||
typedef std::tuple_element_t<I, std::tuple<Tn...>> T;
|
typedef std::tuple_element_t<I, std::tuple<Tn...>> T;
|
||||||
return apply(std::index_sequence<Ix...>(), L, index, tracking, std::forward<Args>(args)...,
|
return apply(std::index_sequence<Ix...>(), L, index, tracking, std::forward<Args>(args)..., stack::get<T>(L, index + tracking.used, tracking));
|
||||||
stack::get<T>(L, index + tracking.used, tracking));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static R get(lua_State* L, int index, record& tracking) {
|
static R get(lua_State* L, int index, record& tracking) {
|
||||||
@ -12820,9 +12854,8 @@ namespace sol { namespace stack {
|
|||||||
template <typename A, typename B>
|
template <typename A, typename B>
|
||||||
struct unqualified_getter<std::pair<A, B>> {
|
struct unqualified_getter<std::pair<A, B>> {
|
||||||
static decltype(auto) get(lua_State* L, int index, record& tracking) {
|
static decltype(auto) get(lua_State* L, int index, record& tracking) {
|
||||||
return std::pair<decltype(stack::get<A>(L, index)), decltype(stack::get<B>(L, index))>{
|
return std::pair<decltype(stack::get<A>(L, index)), decltype(stack::get<B>(L, index))> { stack::get<A>(L, index, tracking),
|
||||||
stack::get<A>(L, index, tracking), stack::get<B>(L, index + tracking.used, tracking)
|
stack::get<B>(L, index + tracking.used, tracking) };
|
||||||
};
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -12840,9 +12873,9 @@ namespace sol { namespace stack {
|
|||||||
return V();
|
return V();
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
//using T = std::variant_alternative_t<0, V>;
|
// using T = std::variant_alternative_t<0, V>;
|
||||||
std::abort();
|
std::abort();
|
||||||
//return V(std::in_place_index<0>, stack::get<T>(L, index, tracking));
|
// return V(std::in_place_index<0>, stack::get<T>(L, index, tracking));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -12864,7 +12897,7 @@ namespace sol { namespace stack {
|
|||||||
#endif // SOL_STD_VARIANT
|
#endif // SOL_STD_VARIANT
|
||||||
#endif // SOL_CXX17_FEATURES
|
#endif // SOL_CXX17_FEATURES
|
||||||
|
|
||||||
}} // namespace sol::stack
|
}} // namespace sol::stack
|
||||||
|
|
||||||
// end of sol/stack_get_unqualified.hpp
|
// end of sol/stack_get_unqualified.hpp
|
||||||
|
|
||||||
@ -13383,6 +13416,20 @@ namespace sol { namespace stack {
|
|||||||
lua_pushnumber(L, std::forward<Args>(args)...);
|
lua_pushnumber(L, std::forward<Args>(args)...);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
else if constexpr (std::is_same<Tu, luaL_Stream*>) {
|
||||||
|
luaL_Stream* source(std::forward<Args>(Args)...);
|
||||||
|
luaL_Stream* stream = static_cast<luaL_Stream*>(lua_newuserdata(L, sizeof(luaL_Stream)));
|
||||||
|
stream->f = source->f;
|
||||||
|
stream->closef = source->closef;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
else if constexpr (std::is_same<Tu, luaL_Stream>) {
|
||||||
|
luaL_Stream& source(std::forward<Args>(Args)...);
|
||||||
|
luaL_Stream* stream = static_cast<luaL_Stream*>(lua_newuserdata(L, sizeof(luaL_Stream)));
|
||||||
|
stream->f = source.f;
|
||||||
|
stream->closef = source.closef;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
else if constexpr (std::is_enum_v<Tu>) {
|
else if constexpr (std::is_enum_v<Tu>) {
|
||||||
return stack_detail::msvc_is_ass_with_if_constexpr_push_enum(std::true_type(), L, std::forward<Args>(args)...);
|
return stack_detail::msvc_is_ass_with_if_constexpr_push_enum(std::true_type(), L, std::forward<Args>(args)...);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user