Lua stream example!

This commit is contained in:
ThePhD 2020-01-25 13:09:16 -05:00
parent 2beb13b9b2
commit 0c38fd1a57
No known key found for this signature in database
GPG Key ID: 1509DB1C0F702BFA
6 changed files with 178 additions and 58 deletions

View 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;
}

View File

@ -181,7 +181,7 @@ namespace sol { namespace stack {
return success;
#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)index;
(void)handler;
@ -223,11 +223,12 @@ namespace sol { namespace stack {
handler(L, index, type::userdata, indextype, "unrecognized userdata (not pushed by sol?)");
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
std::nullopt_t,
std::nullopt_t,
#endif
nullopt_t>) {
nullopt_t>) {
bool success = lua_isnil(L, index);
if (success) {
tracking.use(1);
@ -271,7 +272,7 @@ namespace sol { namespace stack {
}
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);
if (lua_getmetatable(L, index) == 0) {
return true;
@ -305,6 +306,33 @@ namespace sol { namespace stack {
}
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) {
if constexpr (meta::any_same_v<T, userdata_value> || meta::is_specialization_of_v<T, basic_userdata>) {
tracking.use(1);
@ -435,7 +463,9 @@ namespace sol { namespace stack {
template <typename U, typename Handler>
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);
if (indextype != type::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 has_derived = derive<T>::value || weak_derive<T>::value;
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);
#endif // make sure stack doesn't overflow
#endif // make sure stack doesn't overflow
auto pn = stack::pop_n(L, 1);
lua_pushstring(L, &detail::base_class_check_key()[0]);
lua_rawget(L, metatableindex);

View File

@ -84,7 +84,7 @@ namespace sol { namespace stack {
cp = dr.codepoint;
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);
f(er);
}
@ -153,6 +153,14 @@ namespace sol { namespace stack {
Real* mem = static_cast<Real*>(memory);
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 {
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) {
using Tu = meta::unqualified_t<X>;
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 (type_of(L, index) == type::userdata) {
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,
// 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
// so now I would get the error W4XXX unreachable
@ -316,7 +325,7 @@ namespace sol { namespace stack {
T cont;
std::size_t idx = 0;
#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
// Questionable in 5.4
for (lua_Integer i = 0;; i += lua_size<V>::value) {
@ -363,7 +372,7 @@ namespace sol { namespace stack {
#endif
continue;
}
push_back_at_end(meta::has_push_back<Tu>(), t, L, cont, idx);
++idx;
lua_pop(L, lua_size<V>::value);
@ -399,7 +408,7 @@ namespace sol { namespace stack {
++idx;
}
#endif
done:
done:
return cont;
}
@ -514,7 +523,7 @@ namespace sol { namespace stack {
++idx;
}
#endif
done:
done:
return cont;
}
@ -756,7 +765,7 @@ namespace sol { namespace stack {
struct unqualified_getter<meta_function> {
static meta_function get(lua_State* L, int index, record& tracking) {
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();
for (std::size_t i = 0; i < mfnames.size(); ++i)
if (mfnames[i] == name)
@ -874,8 +883,7 @@ namespace sol { namespace stack {
lua_getfield(L, -1, &detail::base_class_cast_key()[0]);
if (type_of(L, -1) != type::lua_nil) {
void* basecastdata = lua_touserdata(L, -1);
detail::inheritance_cast_function ic
= reinterpret_cast<detail::inheritance_cast_function>(basecastdata);
detail::inheritance_cast_function ic = reinterpret_cast<detail::inheritance_cast_function>(basecastdata);
// use the casting function to properly adjust the pointer for the desired T
udata = ic(udata, usertype_traits<T>::qualified_name());
}
@ -953,15 +961,14 @@ namespace sol { namespace stack {
template <typename... Args>
static R apply(std::index_sequence<>, lua_State*, int, record&, Args&&... args) {
// 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>
static R apply(std::index_sequence<I, Ix...>, lua_State* L, int index, record& tracking, Args&&... args) {
// Fuck you too, VC++
typedef std::tuple_element_t<I, std::tuple<Tn...>> T;
return apply(std::index_sequence<Ix...>(), L, index, tracking, std::forward<Args>(args)...,
stack::get<T>(L, index + tracking.used, tracking));
return apply(std::index_sequence<Ix...>(), L, index, tracking, std::forward<Args>(args)..., stack::get<T>(L, index + tracking.used, tracking));
}
static R get(lua_State* L, int index, record& tracking) {
@ -972,9 +979,8 @@ namespace sol { namespace stack {
template <typename A, typename B>
struct unqualified_getter<std::pair<A, B>> {
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))>{
stack::get<A>(L, index, tracking), stack::get<B>(L, index + tracking.used, tracking)
};
return std::pair<decltype(stack::get<A>(L, index)), decltype(stack::get<B>(L, index))> { stack::get<A>(L, index, tracking),
stack::get<B>(L, index + tracking.used, tracking) };
}
};
@ -992,9 +998,9 @@ namespace sol { namespace stack {
return V();
}
else {
//using T = std::variant_alternative_t<0, V>;
// using T = std::variant_alternative_t<0, V>;
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_CXX17_FEATURES
}} // namespace sol::stack
}} // namespace sol::stack
#endif // SOL_STACK_UNQUALIFIED_GET_HPP

View File

@ -296,6 +296,20 @@ namespace sol { namespace stack {
lua_pushnumber(L, std::forward<Args>(args)...);
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>) {
return stack_detail::msvc_is_ass_with_if_constexpr_push_enum(std::true_type(), L, std::forward<Args>(args)...);
}

View File

@ -20,8 +20,8 @@
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// This file was generated with a script.
// Generated 2020-01-25 04:59:37.808053 UTC
// This header was generated with sol v3.2.0 (revision c5863c8)
// Generated 2020-01-25 17:11:21.688498 UTC
// This header was generated with sol v3.2.0 (revision 2beb13b)
// https://github.com/ThePhD/sol2
#ifndef SOL_SINGLE_INCLUDE_FORWARD_HPP

View File

@ -20,8 +20,8 @@
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// This file was generated with a script.
// Generated 2020-01-25 04:59:35.996014 UTC
// This header was generated with sol v3.2.0 (revision c5863c8)
// Generated 2020-01-25 17:11:19.283779 UTC
// This header was generated with sol v3.2.0 (revision 2beb13b)
// https://github.com/ThePhD/sol2
#ifndef SOL_SINGLE_INCLUDE_HPP
@ -11060,7 +11060,7 @@ namespace sol { namespace stack {
return success;
#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)index;
(void)handler;
@ -11102,11 +11102,12 @@ namespace sol { namespace stack {
handler(L, index, type::userdata, indextype, "unrecognized userdata (not pushed by sol?)");
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
std::nullopt_t,
std::nullopt_t,
#endif
nullopt_t>) {
nullopt_t>) {
bool success = lua_isnil(L, index);
if (success) {
tracking.use(1);
@ -11150,7 +11151,7 @@ namespace sol { namespace stack {
}
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);
if (lua_getmetatable(L, index) == 0) {
return true;
@ -11184,6 +11185,30 @@ namespace sol { namespace stack {
}
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) {
if constexpr (meta::any_same_v<T, userdata_value> || meta::is_specialization_of_v<T, basic_userdata>) {
tracking.use(1);
@ -11314,7 +11339,9 @@ namespace sol { namespace stack {
template <typename U, typename Handler>
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);
if (indextype != type::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 has_derived = derive<T>::value || weak_derive<T>::value;
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);
#endif // make sure stack doesn't overflow
#endif // make sure stack doesn't overflow
auto pn = stack::pop_n(L, 1);
lua_pushstring(L, &detail::base_class_check_key()[0]);
lua_rawget(L, metatableindex);
@ -11932,7 +11959,7 @@ namespace sol { namespace stack {
cp = dr.codepoint;
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);
f(er);
}
@ -12001,6 +12028,14 @@ namespace sol { namespace stack {
Real* mem = static_cast<Real*>(memory);
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 {
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) {
using Tu = meta::unqualified_t<X>;
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 (type_of(L, index) == type::userdata) {
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,
// 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
// so now I would get the error W4XXX unreachable
@ -12164,7 +12200,7 @@ namespace sol { namespace stack {
T cont;
std::size_t idx = 0;
#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
// Questionable in 5.4
for (lua_Integer i = 0;; i += lua_size<V>::value) {
@ -12211,7 +12247,7 @@ namespace sol { namespace stack {
#endif
continue;
}
push_back_at_end(meta::has_push_back<Tu>(), t, L, cont, idx);
++idx;
lua_pop(L, lua_size<V>::value);
@ -12247,7 +12283,7 @@ namespace sol { namespace stack {
++idx;
}
#endif
done:
done:
return cont;
}
@ -12362,7 +12398,7 @@ namespace sol { namespace stack {
++idx;
}
#endif
done:
done:
return cont;
}
@ -12604,7 +12640,7 @@ namespace sol { namespace stack {
struct unqualified_getter<meta_function> {
static meta_function get(lua_State* L, int index, record& tracking) {
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();
for (std::size_t i = 0; i < mfnames.size(); ++i)
if (mfnames[i] == name)
@ -12722,8 +12758,7 @@ namespace sol { namespace stack {
lua_getfield(L, -1, &detail::base_class_cast_key()[0]);
if (type_of(L, -1) != type::lua_nil) {
void* basecastdata = lua_touserdata(L, -1);
detail::inheritance_cast_function ic
= reinterpret_cast<detail::inheritance_cast_function>(basecastdata);
detail::inheritance_cast_function ic = reinterpret_cast<detail::inheritance_cast_function>(basecastdata);
// use the casting function to properly adjust the pointer for the desired T
udata = ic(udata, usertype_traits<T>::qualified_name());
}
@ -12801,15 +12836,14 @@ namespace sol { namespace stack {
template <typename... Args>
static R apply(std::index_sequence<>, lua_State*, int, record&, Args&&... args) {
// 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>
static R apply(std::index_sequence<I, Ix...>, lua_State* L, int index, record& tracking, Args&&... args) {
// Fuck you too, VC++
typedef std::tuple_element_t<I, std::tuple<Tn...>> T;
return apply(std::index_sequence<Ix...>(), L, index, tracking, std::forward<Args>(args)...,
stack::get<T>(L, index + tracking.used, tracking));
return apply(std::index_sequence<Ix...>(), L, index, tracking, std::forward<Args>(args)..., stack::get<T>(L, index + tracking.used, tracking));
}
static R get(lua_State* L, int index, record& tracking) {
@ -12820,9 +12854,8 @@ namespace sol { namespace stack {
template <typename A, typename B>
struct unqualified_getter<std::pair<A, B>> {
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))>{
stack::get<A>(L, index, tracking), stack::get<B>(L, index + tracking.used, tracking)
};
return std::pair<decltype(stack::get<A>(L, index)), decltype(stack::get<B>(L, index))> { stack::get<A>(L, index, tracking),
stack::get<B>(L, index + tracking.used, tracking) };
}
};
@ -12840,9 +12873,9 @@ namespace sol { namespace stack {
return V();
}
else {
//using T = std::variant_alternative_t<0, V>;
// using T = std::variant_alternative_t<0, V>;
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_CXX17_FEATURES
}} // namespace sol::stack
}} // namespace sol::stack
// end of sol/stack_get_unqualified.hpp
@ -13383,6 +13416,20 @@ namespace sol { namespace stack {
lua_pushnumber(L, std::forward<Args>(args)...);
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>) {
return stack_detail::msvc_is_ass_with_if_constexpr_push_enum(std::true_type(), L, std::forward<Args>(args)...);
}