diff --git a/single/sol/sol.hpp b/single/sol/sol.hpp new file mode 100644 index 00000000..31e9afcf --- /dev/null +++ b/single/sol/sol.hpp @@ -0,0 +1,10307 @@ +// 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. + +// This file was generated with a script. +// Generated 2016-06-22 17:39:19.028386 UTC +// This header was generated with sol v2.8.7 (revision 97c132d) +// https://github.com/ThePhD/sol2 + +#ifndef SOL_SINGLE_INCLUDE_HPP +#define SOL_SINGLE_INCLUDE_HPP + +// beginning of sol\state.hpp + +// beginning of sol\state_view.hpp + +// beginning of sol\error.hpp + +#include +#include + +namespace sol { + namespace detail { + struct direct_error_tag {}; + const auto direct_error = direct_error_tag{}; + } // detail + + class error : public std::runtime_error { + private: + // Because VC++ is a fuccboi + std::string w; + public: + error(const std::string& str) : error(detail::direct_error, "lua: error: " + str) {} + error(detail::direct_error_tag, const std::string& str) : std::runtime_error(""), w(str) {} + error(detail::direct_error_tag, std::string&& str) : std::runtime_error(""), w(std::move(str)) {} + + error(const error& e) = default; + error(error&& e) = default; + error& operator=(const error& e) = default; + error& operator=(error&& e) = default; + + virtual const char* what() const noexcept override { + return w.c_str(); + } + }; + +} // sol + +// end of sol\error.hpp + +// beginning of sol\table.hpp + +// beginning of sol\table_core.hpp + +// beginning of sol\proxy.hpp + +// beginning of sol\traits.hpp + +// beginning of sol\tuple.hpp + +#include +#include + +namespace sol { + namespace detail { + using swallow = std::initializer_list; + } // detail + + template + struct types { typedef std::make_index_sequence indices; static constexpr std::size_t size() { return sizeof...(Args); } }; + namespace meta { + namespace detail { + template + struct tuple_types_ { typedef types type; }; + + template + struct tuple_types_> { typedef types type; }; + } // detail + + template + using unqualified = std::remove_cv>; + + template + using unqualified_t = typename unqualified::type; + + template + using tuple_types = typename detail::tuple_types_::type; + + template + struct pop_front_type; + + template + using pop_front_type_t = typename pop_front_type::type; + + template + struct pop_front_type> { typedef void front_type; typedef types type; }; + + template + struct pop_front_type> { typedef Arg front_type; typedef types type; }; + + template + using tuple_element = std::tuple_element>; + + template + using tuple_element_t = std::tuple_element_t>; + + } // meta +} // sol + +// end of sol\tuple.hpp + +// beginning of sol\bind_traits.hpp + +namespace sol { + namespace meta { + namespace meta_detail { + + template + struct check_deducible_signature { + struct nat {}; + template + static auto test(int) -> decltype(&G::operator(), void()); + template + static auto test(...)->nat; + + using type = std::is_void(0))>; + }; + } // meta_detail + + template + struct has_deducible_signature : meta_detail::check_deducible_signature::type { }; + + namespace meta_detail { + + template + struct void_tuple_element : meta::tuple_element {}; + + template + struct void_tuple_element> { typedef void type; }; + + template + using void_tuple_element_t = typename void_tuple_element::type; + + template + struct basic_traits { + private: + typedef std::conditional_t::value, int, T>& first_type; + + public: + static const bool is_member_function = std::is_void::value; + static const bool has_c_var_arg = has_c_variadic; + static const std::size_t arity = sizeof...(Args); + static const std::size_t free_arity = sizeof...(Args)+static_cast(!std::is_void::value); + typedef types args_list; + typedef std::tuple args_tuple; + typedef T object_type; + typedef R return_type; + typedef tuple_types returns_list; + typedef R(function_type)(Args...); + typedef std::conditional_t::value, args_list, types> free_args_list; + typedef std::conditional_t::value, R(Args...), R(first_type, Args...)> free_function_type; + typedef std::conditional_t::value, R(*)(Args...), R(*)(first_type, Args...)> free_function_pointer_type; + typedef std::remove_pointer_t signature_type; + template + using arg_at = void_tuple_element_t; + }; + + template::value> + struct fx_traits : basic_traits {}; + + // Free Functions + template + struct fx_traits : basic_traits { + typedef R(*function_pointer_type)(Args...); + }; + + template + struct fx_traits : basic_traits { + typedef R(*function_pointer_type)(Args...); + }; + + template + struct fx_traits : basic_traits { + typedef R(*function_pointer_type)(Args..., ...); + }; + + template + struct fx_traits : basic_traits { + typedef R(*function_pointer_type)(Args..., ...); + }; + + // Member Functions + /* C-Style Variadics */ + template + struct fx_traits : basic_traits { + typedef R(T::* function_pointer_type)(Args...); + }; + + template + struct fx_traits : basic_traits { + typedef R(T::* function_pointer_type)(Args..., ...); + }; + + /* Const Volatile */ + template + struct fx_traits : basic_traits { + typedef R(T::* function_pointer_type)(Args...) const; + }; + + template + struct fx_traits : basic_traits { + typedef R(T::* function_pointer_type)(Args..., ...) const; + }; + + template + struct fx_traits : basic_traits { + typedef R(T::* function_pointer_type)(Args...) const volatile; + }; + + template + struct fx_traits : basic_traits { + typedef R(T::* function_pointer_type)(Args..., ...) const volatile; + }; + + /* Member Function Qualifiers */ + template + struct fx_traits : basic_traits { + typedef R(T::* function_pointer_type)(Args...) &; + }; + + template + struct fx_traits : basic_traits { + typedef R(T::* function_pointer_type)(Args..., ...) &; + }; + + template + struct fx_traits : basic_traits { + typedef R(T::* function_pointer_type)(Args...) const &; + }; + + template + struct fx_traits : basic_traits { + typedef R(T::* function_pointer_type)(Args..., ...) const &; + }; + + template + struct fx_traits : basic_traits { + typedef R(T::* function_pointer_type)(Args...) const volatile &; + }; + + template + struct fx_traits : basic_traits { + typedef R(T::* function_pointer_type)(Args..., ...) const volatile &; + }; + + template + struct fx_traits : basic_traits { + typedef R(T::* function_pointer_type)(Args...) && ; + }; + + template + struct fx_traits : basic_traits { + typedef R(T::* function_pointer_type)(Args..., ...) && ; + }; + + template + struct fx_traits : basic_traits { + typedef R(T::* function_pointer_type)(Args...) const &&; + }; + + template + struct fx_traits : basic_traits { + typedef R(T::* function_pointer_type)(Args..., ...) const &&; + }; + + template + struct fx_traits : basic_traits { + typedef R(T::* function_pointer_type)(Args...) const volatile &&; + }; + + template + struct fx_traits : basic_traits { + typedef R(T::* function_pointer_type)(Args..., ...) const volatile &&; + }; + + template + struct fx_traits : fx_traits::function_type, false> {}; + + template::value> + struct callable_traits : fx_traits> { + + }; + + template + struct callable_traits { + typedef R Arg; + typedef T object_type; + using signature_type = R(T::*); + static const bool is_member_function = false; + static const std::size_t arity = 1; + static const std::size_t free_arity = 2; + typedef std::tuple args_tuple; + typedef R return_type; + typedef types args_list; + typedef meta::tuple_types returns_list; + typedef R(function_type)(T&, R); + typedef R(*function_pointer_type)(T&, R); + typedef R(*free_function_pointer_type)(T&, R); + template + using arg_at = void_tuple_element_t; + }; + } // meta_detail + + template + struct bind_traits : meta_detail::callable_traits {}; + + template + using function_args_t = typename bind_traits::args_list; + + template + using function_signature_t = typename bind_traits::signature_type; + + template + using function_return_t = typename bind_traits::return_type; + + } // meta +} // sol + +// end of sol\bind_traits.hpp + +#include +#include +#include + +namespace sol { + template + using index_value = std::integral_constant; + + namespace meta { + template + struct identity { typedef T type; }; + + template + using identity_t = typename identity::type; + + template + struct is_tuple : std::false_type { }; + + template + struct is_tuple> : std::true_type { }; + + template + struct unwrapped { + typedef T type; + }; + + template + struct unwrapped> { + typedef T type; + }; + + template + using unwrapped_t = typename unwrapped::type; + + template + struct remove_member_pointer; + + template + struct remove_member_pointer { + typedef R type; + }; + + template + struct remove_member_pointer { + typedef R type; + }; + + template + using remove_member_pointer_t = remove_member_pointer; + + template class Templ, typename T> + struct is_specialization_of : std::false_type { }; + template class Templ> + struct is_specialization_of> : std::true_type { }; + + template + struct all_same : std::true_type { }; + + template + struct all_same : std::integral_constant ::value && all_same::value> { }; + + template + struct any_same : std::false_type { }; + + template + struct any_same : std::integral_constant ::value || any_same::value> { }; + + template + using invoke_t = typename T::type; + + template + using boolean = std::integral_constant; + + template + using neg = boolean; + + template + using condition = std::conditional_t; + + template + struct all : boolean {}; + + template + struct all : condition, boolean> {}; + + template + struct any : boolean {}; + + template + struct any : condition, any> {}; + + enum class enable_t { + _ + }; + + constexpr const auto enabler = enable_t::_; + + template + using enable = std::enable_if_t::value, enable_t>; + + template + using disable = std::enable_if_t>::value, enable_t>; + + template + struct find_in_pack_v : boolean { }; + + template + struct find_in_pack_v : any, find_in_pack_v> { }; + + namespace meta_detail { + template + struct index_in_pack : std::integral_constant { }; + + template + struct index_in_pack : std::conditional_t::value, std::integral_constant, index_in_pack> { }; + } + + template + struct index_in_pack : meta_detail::index_in_pack<0, T, Args...> { }; + + template + struct index_in : meta_detail::index_in_pack<0, T, List> { }; + + template + struct index_in> : meta_detail::index_in_pack<0, T, Args...> { }; + + template + struct at_in_pack {}; + + template + using at_in_pack_t = typename at_in_pack::type; + + template + struct at_in_pack : std::conditional> {}; + + template + struct at_in_pack<0, Arg, Args...> { typedef Arg type; }; + + namespace meta_detail { + template class Pred, typename... Ts> + struct count_for_pack : std::integral_constant {}; + template class Pred, typename T, typename... Ts> + struct count_for_pack : std::conditional_t < sizeof...(Ts) == 0 || Limit < 2, + std::integral_constant(Limit != 0 && Pred::value)>, + count_for_pack(Pred::value), Pred, Ts...> + > { }; + template class Pred, typename... Ts> + struct count_2_for_pack : std::integral_constant {}; + template class Pred, typename T, typename U, typename... Ts> + struct count_2_for_pack : std::conditional_t(Pred::value)>, + count_2_for_pack(Pred::value), Pred, Ts...> + > { }; + } // meta_detail + + template class Pred, typename... Ts> + struct count_for_pack : meta_detail::count_for_pack { }; + + template class Pred, typename List> + struct count_for; + + template class Pred, typename... Args> + struct count_for> : count_for_pack {}; + + template class Pred, typename... Ts> + struct count_for_to_pack : meta_detail::count_for_pack { }; + + template class Pred, typename... Ts> + struct count_2_for_pack : meta_detail::count_2_for_pack<0, Pred, Ts...> { }; + + template + struct return_type { + typedef std::tuple type; + }; + + template + struct return_type { + typedef T type; + }; + + template<> + struct return_type<> { + typedef void type; + }; + + template + using return_type_t = typename return_type::type; + + namespace meta_detail { + template struct always_true : std::true_type {}; + struct is_invokable_tester { + template + always_true()(std::declval()...))> static test(int); + template + std::false_type static test(...); + }; + } // meta_detail + + template + struct is_invokable; + template + struct is_invokable : decltype(meta_detail::is_invokable_tester::test(0)) {}; + + namespace meta_detail { + + template>::value> + struct is_callable : std::is_function> {}; + + template + struct is_callable { + using yes = char; + using no = struct { char s[2]; }; + + struct F { void operator()(); }; + struct Derived : T, F {}; + template struct Check; + + template + static no test(Check*); + + template + static yes test(...); + + static const bool value = sizeof(test(0)) == sizeof(yes); + }; + + } // meta_detail + + template + struct is_callable : boolean::value> {}; + + struct has_begin_end_impl { + template, + typename B = decltype(std::declval().begin()), + typename E = decltype(std::declval().end())> + static std::true_type test(int); + + template + static std::false_type test(...); + }; + + template + struct has_begin_end : decltype(has_begin_end_impl::test(0)) {}; + + struct has_key_value_pair_impl { + template, + typename V = typename U::value_type, + typename F = decltype(std::declval().first), + typename S = decltype(std::declval().second)> + static std::true_type test(int); + + template + static std::false_type test(...); + }; + + template + struct has_key_value_pair : decltype(has_key_value_pair_impl::test(0)) {}; + + template + using is_string_constructible = any, const char*>, std::is_same, char>, std::is_same, std::string>, std::is_same, std::initializer_list>>; + + template + using is_c_str = any< + std::is_same>, const char*>, + std::is_same>, char*>, + std::is_same, std::string> + >; + + template + struct is_move_only : all< + neg>, + neg>>, + std::is_move_constructible> + > {}; + + template + using is_not_move_only = neg>; + + namespace meta_detail { + template >> = meta::enabler> + decltype(auto) force_tuple(T&& x) { + return std::forward_as_tuple(std::forward(x)); + } + + template >> = meta::enabler> + decltype(auto) force_tuple(T&& x) { + return std::forward(x); + } + } // meta_detail + + template + decltype(auto) tuplefy(X&&... x) { + return std::tuple_cat(meta_detail::force_tuple(std::forward(x))...); + } + } // meta + namespace detail { + template + decltype(auto) forward_get(Tuple&& tuple) { + return std::forward>(std::get(tuple)); + } + + template + auto forward_tuple_impl(std::index_sequence, Tuple&& tuple) -> decltype(std::tuple(tuple))...>(forward_get(tuple)...)) { + return std::tuple(tuple))...>(std::move(std::get(tuple))...); + } + + template + auto forward_tuple(Tuple&& tuple) { + auto x = forward_tuple_impl(std::make_index_sequence>::value>(), std::forward(tuple)); + return x; + } + + template + auto unwrap(T&& item) -> decltype(std::forward(item)) { + return std::forward(item); + } + + template + T& unwrap(std::reference_wrapper arg) { + return arg.get(); + } + + template + decltype(auto) deref(T&& item) { + return std::forward(item); + } + + template + T& deref(T* item) { + return *item; + } + + template + decltype(auto) deref(std::unique_ptr& item) { + return *item; + } + + template + T& deref(std::shared_ptr& item) { + return *item; + } + + template + decltype(auto) deref(const std::unique_ptr& item) { + return *item; + } + + template + T& deref(const std::shared_ptr& item) { + return *item; + } + + template + inline T* ptr(T& val) { + return std::addressof(val); + } + + template + inline T* ptr(std::reference_wrapper val) { + return std::addressof(val.get()); + } + + template + inline T* ptr(T* val) { + return val; + } + } // detail +} // sol + +// end of sol\traits.hpp + +// beginning of sol\object.hpp + +// beginning of sol\reference.hpp + +// beginning of sol\types.hpp + +// beginning of sol\optional.hpp + +#if __cplusplus > 201402L +#include +#elif defined(SOL_USE_BOOST) +#include +#else +// beginning of Optional\optional.hpp + +# ifndef ___SOL2_OPTIONAL_HPP___ +# define ___SOL2_OPTIONAL_HPP___ + +# include +# include +# include +# include +# include +# include +# include + +# define TR2_OPTIONAL_REQUIRES(...) typename ::std::enable_if<__VA_ARGS__::value, bool>::type = false + +# if defined __GNUC__ // NOTE: GNUC is also defined for Clang +# if (__GNUC__ == 4) && (__GNUC_MINOR__ >= 8) +# define TR2_OPTIONAL_GCC_4_8_AND_HIGHER___ +# elif (__GNUC__ > 4) +# define TR2_OPTIONAL_GCC_4_8_AND_HIGHER___ +# endif +# +# if (__GNUC__ == 4) && (__GNUC_MINOR__ >= 7) +# define TR2_OPTIONAL_GCC_4_7_AND_HIGHER___ +# elif (__GNUC__ > 4) +# define TR2_OPTIONAL_GCC_4_7_AND_HIGHER___ +# endif +# +# if (__GNUC__ == 4) && (__GNUC_MINOR__ == 8) && (__GNUC_PATCHLEVEL__ >= 1) +# define TR2_OPTIONAL_GCC_4_8_1_AND_HIGHER___ +# elif (__GNUC__ == 4) && (__GNUC_MINOR__ >= 9) +# define TR2_OPTIONAL_GCC_4_8_1_AND_HIGHER___ +# elif (__GNUC__ > 4) +# define TR2_OPTIONAL_GCC_4_8_1_AND_HIGHER___ +# endif +# endif +# +# if defined __clang_major__ +# if (__clang_major__ == 3 && __clang_minor__ >= 5) +# define TR2_OPTIONAL_CLANG_3_5_AND_HIGHTER_ +# elif (__clang_major__ > 3) +# define TR2_OPTIONAL_CLANG_3_5_AND_HIGHTER_ +# endif +# if defined TR2_OPTIONAL_CLANG_3_5_AND_HIGHTER_ +# define TR2_OPTIONAL_CLANG_3_4_2_AND_HIGHER_ +# elif (__clang_major__ == 3 && __clang_minor__ == 4 && __clang_patchlevel__ >= 2) +# define TR2_OPTIONAL_CLANG_3_4_2_AND_HIGHER_ +# endif +# endif +# +# if defined _MSC_VER +# if (_MSC_VER >= 1900) +# define TR2_OPTIONAL_MSVC_2015_AND_HIGHER___ +# endif +# endif + +# if defined __clang__ +# if (__clang_major__ > 2) || (__clang_major__ == 2) && (__clang_minor__ >= 9) +# define OPTIONAL_HAS_THIS_RVALUE_REFS 1 +# else +# define OPTIONAL_HAS_THIS_RVALUE_REFS 0 +# endif +# elif defined TR2_OPTIONAL_GCC_4_8_1_AND_HIGHER___ +# define OPTIONAL_HAS_THIS_RVALUE_REFS 1 +# elif defined TR2_OPTIONAL_MSVC_2015_AND_HIGHER___ +# define OPTIONAL_HAS_THIS_RVALUE_REFS 1 +# else +# define OPTIONAL_HAS_THIS_RVALUE_REFS 0 +# endif + +# if defined TR2_OPTIONAL_GCC_4_8_1_AND_HIGHER___ +# define OPTIONAL_HAS_CONSTEXPR_INIT_LIST 1 +# define OPTIONAL_CONSTEXPR_INIT_LIST constexpr +# else +# define OPTIONAL_HAS_CONSTEXPR_INIT_LIST 0 +# define OPTIONAL_CONSTEXPR_INIT_LIST +# endif + +# if defined TR2_OPTIONAL_CLANG_3_5_AND_HIGHTER_ && (defined __cplusplus) && (__cplusplus != 201103L) +# define OPTIONAL_HAS_MOVE_ACCESSORS 1 +# else +# define OPTIONAL_HAS_MOVE_ACCESSORS 0 +# endif + +# // In C++11 constexpr implies const, so we need to make non-const members also non-constexpr +# if (defined __cplusplus) && (__cplusplus == 201103L) +# define OPTIONAL_MUTABLE_CONSTEXPR +# else +# define OPTIONAL_MUTABLE_CONSTEXPR constexpr +# endif + +namespace sol{ + +# if defined TR2_OPTIONAL_GCC_4_8_AND_HIGHER___ + // leave it: it is already there +# elif defined TR2_OPTIONAL_CLANG_3_4_2_AND_HIGHER_ + // leave it: it is already there +# elif defined TR2_OPTIONAL_MSVC_2015_AND_HIGHER___ + // leave it: it is already there +# elif defined TR2_OPTIONAL_DISABLE_EMULATION_OF_TYPE_TRAITS + // leave it: the user doesn't want it +# else + template + using is_trivially_destructible = std::has_trivial_destructor; +# endif + +# if (defined TR2_OPTIONAL_GCC_4_7_AND_HIGHER___) + // leave it; our metafunctions are already defined. +# elif defined TR2_OPTIONAL_CLANG_3_4_2_AND_HIGHER_ + // leave it; our metafunctions are already defined. +# elif defined TR2_OPTIONAL_MSVC_2015_AND_HIGHER___ + // leave it: it is already there +# elif defined TR2_OPTIONAL_DISABLE_EMULATION_OF_TYPE_TRAITS + // leave it: the user doesn't want it +# else + +template +struct is_nothrow_move_constructible +{ + constexpr static bool value = std::is_nothrow_constructible::value; +}; + +template +struct is_assignable +{ + template + constexpr static bool has_assign(...) { return false; } + + template () = std::declval(), true)) > + // the comma operator is necessary for the cases where operator= returns void + constexpr static bool has_assign(bool) { return true; } + + constexpr static bool value = has_assign(true); +}; + +template +struct is_nothrow_move_assignable +{ + template + struct has_nothrow_move_assign { + constexpr static bool value = false; + }; + + template + struct has_nothrow_move_assign { + constexpr static bool value = noexcept( std::declval() = std::declval() ); + }; + + constexpr static bool value = has_nothrow_move_assign::value>::value; +}; + +# endif + +template class optional; + +template class optional; + +template inline constexpr T&& constexpr_forward(typename ::std::remove_reference::type& t) noexcept +{ + return static_cast(t); +} + +template inline constexpr T&& constexpr_forward(typename ::std::remove_reference::type&& t) noexcept +{ + static_assert(!::std::is_lvalue_reference::value, "!!"); + return static_cast(t); +} + +template inline constexpr typename ::std::remove_reference::type&& constexpr_move(T&& t) noexcept +{ + return static_cast::type&&>(t); +} + +#if defined NDEBUG +# define TR2_OPTIONAL_ASSERTED_EXPRESSION(CHECK, EXPR) (EXPR) +#else +# define TR2_OPTIONAL_ASSERTED_EXPRESSION(CHECK, EXPR) ((CHECK) ? (EXPR) : ([]{assert(!#CHECK);}(), (EXPR))) +#endif + +namespace detail_ +{ + +template +struct has_overloaded_addressof +{ + template + constexpr static bool has_overload(...) { return false; } + + template ().operator&()) > + constexpr static bool has_overload(bool) { return true; } + + constexpr static bool value = has_overload(true); +}; + +template )> +constexpr T* static_addressof(T& ref) +{ + return &ref; +} + +template )> +T* static_addressof(T& ref) +{ + return std::addressof(ref); +} + +template +U convert(U v) { return v; } + +} // namespace detail + +constexpr struct trivial_init_t{} trivial_init{}; + +constexpr struct in_place_t{} in_place{}; + +struct nullopt_t +{ + struct init{}; + constexpr explicit nullopt_t(init){} +}; +constexpr nullopt_t nullopt{nullopt_t::init()}; + +class bad_optional_access : public ::std::logic_error { +public: + explicit bad_optional_access(const ::std::string& what_arg) : logic_error{what_arg} {} + explicit bad_optional_access(const char* what_arg) : logic_error{what_arg} {} +}; + +template +union storage_t +{ + unsigned char dummy_; + T value_; + + constexpr storage_t( trivial_init_t ) noexcept : dummy_() {}; + + template + constexpr storage_t( Args&&... args ) : value_(constexpr_forward(args)...) {} + + ~storage_t(){} +}; + +template +union constexpr_storage_t +{ + unsigned char dummy_; + T value_; + + constexpr constexpr_storage_t( trivial_init_t ) noexcept : dummy_() {}; + + template + constexpr constexpr_storage_t( Args&&... args ) : value_(constexpr_forward(args)...) {} + + ~constexpr_storage_t() = default; +}; + +template +struct optional_base +{ + bool init_; + storage_t storage_; + + constexpr optional_base() noexcept : init_(false), storage_(trivial_init) {}; + + explicit constexpr optional_base(const T& v) : init_(true), storage_(v) {} + + explicit constexpr optional_base(T&& v) : init_(true), storage_(constexpr_move(v)) {} + + template explicit optional_base(in_place_t, Args&&... args) + : init_(true), storage_(constexpr_forward(args)...) {} + + template >)> + explicit optional_base(in_place_t, std::initializer_list il, Args&&... args) + : init_(true), storage_(il, std::forward(args)...) {} + + ~optional_base() { if (init_) storage_.value_.T::~T(); } +}; + +template +struct constexpr_optional_base +{ + bool init_; + constexpr_storage_t storage_; + + constexpr constexpr_optional_base() noexcept : init_(false), storage_(trivial_init) {}; + + explicit constexpr constexpr_optional_base(const T& v) : init_(true), storage_(v) {} + + explicit constexpr constexpr_optional_base(T&& v) : init_(true), storage_(constexpr_move(v)) {} + + template explicit constexpr constexpr_optional_base(in_place_t, Args&&... args) + : init_(true), storage_(constexpr_forward(args)...) {} + + template >)> + OPTIONAL_CONSTEXPR_INIT_LIST explicit constexpr_optional_base(in_place_t, std::initializer_list il, Args&&... args) + : init_(true), storage_(il, std::forward(args)...) {} + + ~constexpr_optional_base() = default; +}; + +template +using OptionalBase = typename std::conditional< + ::std::is_trivially_destructible::value, + constexpr_optional_base, + optional_base +>::type; + +template +class optional : private OptionalBase +{ + static_assert( !std::is_same::type, nullopt_t>::value, "bad T" ); + static_assert( !std::is_same::type, in_place_t>::value, "bad T" ); + + constexpr bool initialized() const noexcept { return OptionalBase::init_; } + T* dataptr() { return std::addressof(OptionalBase::storage_.value_); } + constexpr const T* dataptr() const { return detail_::static_addressof(OptionalBase::storage_.value_); } + +# if OPTIONAL_HAS_THIS_RVALUE_REFS == 1 + constexpr const T& contained_val() const& { return OptionalBase::storage_.value_; } +# if OPTIONAL_HAS_MOVE_ACCESSORS == 1 + OPTIONAL_MUTABLE_CONSTEXPR T&& contained_val() && { return std::move(OptionalBase::storage_.value_); } + OPTIONAL_MUTABLE_CONSTEXPR T& contained_val() & { return OptionalBase::storage_.value_; } +# else + T& contained_val() & { return OptionalBase::storage_.value_; } + T&& contained_val() && { return std::move(OptionalBase::storage_.value_); } +# endif +# else + constexpr const T& contained_val() const { return OptionalBase::storage_.value_; } + T& contained_val() { return OptionalBase::storage_.value_; } +# endif + + void clear() noexcept { + if (initialized()) dataptr()->T::~T(); + OptionalBase::init_ = false; + } + + template + void initialize(Args&&... args) noexcept(noexcept(T(::std::forward(args)...))) + { + assert(!OptionalBase::init_); + ::new (static_cast(dataptr())) T(::std::forward(args)...); + OptionalBase::init_ = true; + } + + template + void initialize(::std::initializer_list il, Args&&... args) noexcept(noexcept(T(il, ::std::forward(args)...))) + { + assert(!OptionalBase::init_); + ::new (static_cast(dataptr())) T(il, ::std::forward(args)...); + OptionalBase::init_ = true; + } + +public: + typedef T value_type; + + // 20.5.5.1, constructors + constexpr optional() noexcept : OptionalBase() {}; + constexpr optional(nullopt_t) noexcept : OptionalBase() {}; + + optional(const optional& rhs) : optional() { + if (rhs) { + ::new (static_cast(dataptr())) T(*rhs); + OptionalBase::init_ = true; + } + } + + optional(const optional& rhs) + : OptionalBase() + { + if (rhs.initialized()) { + ::new (static_cast(dataptr())) T(*rhs); + OptionalBase::init_ = true; + } + } + + optional(optional&& rhs) noexcept(::std::is_nothrow_move_constructible::value) + : OptionalBase() + { + if (rhs.initialized()) { + ::new (static_cast(dataptr())) T(std::move(*rhs)); + OptionalBase::init_ = true; + } + } + + constexpr optional(const T& v) : OptionalBase(v) {} + + constexpr optional(T&& v) : OptionalBase(constexpr_move(v)) {} + + template + explicit constexpr optional(in_place_t, Args&&... args) + : OptionalBase(in_place_t{}, constexpr_forward(args)...) {} + + template >)> + OPTIONAL_CONSTEXPR_INIT_LIST explicit optional(in_place_t, std::initializer_list il, Args&&... args) + : OptionalBase(in_place_t{}, il, constexpr_forward(args)...) {} + + // 20.5.4.2, Destructor + ~optional() = default; + + // 20.5.4.3, assignment + optional& operator=(nullopt_t) noexcept + { + clear(); + return *this; + } + + optional& operator=(const optional& rhs) + { + if (initialized() == true && rhs.initialized() == false) clear(); + else if (initialized() == false && rhs.initialized() == true) initialize(*rhs); + else if (initialized() == true && rhs.initialized() == true) contained_val() = *rhs; + return *this; + } + + optional& operator=(optional&& rhs) + noexcept(::std::is_nothrow_move_assignable::value && ::std::is_nothrow_move_constructible::value) + { + if (initialized() == true && rhs.initialized() == false) clear(); + else if (initialized() == false && rhs.initialized() == true) initialize(std::move(*rhs)); + else if (initialized() == true && rhs.initialized() == true) contained_val() = std::move(*rhs); + return *this; + } + + template + auto operator=(U&& v) + -> typename ::std::enable_if + < + ::std::is_same::type, T>::value, + optional& + >::type + { + if (initialized()) { contained_val() = ::std::forward(v); } + else { initialize(::std::forward(v)); } + return *this; + } + + template + void emplace(Args&&... args) + { + clear(); + initialize(::std::forward(args)...); + } + + template + void emplace(::std::initializer_list il, Args&&... args) + { + clear(); + initialize(il, std::forward(args)...); + } + + // 20.5.4.4, Swap + void swap(optional& rhs) noexcept(::std::is_nothrow_move_constructible::value && noexcept(::std::swap(::std::declval(), ::std::declval()))) + { + if (initialized() == true && rhs.initialized() == false) { rhs.initialize(::std::move(**this)); clear(); } + else if (initialized() == false && rhs.initialized() == true) { initialize(::std::move(*rhs)); rhs.clear(); } + else if (initialized() == true && rhs.initialized() == true) { using ::std::swap; swap(**this, *rhs); } + } + + // 20.5.4.5, Observers + + explicit constexpr operator bool() const noexcept { return initialized(); } + + constexpr T const* operator ->() const { + return TR2_OPTIONAL_ASSERTED_EXPRESSION(initialized(), dataptr()); + } + +# if OPTIONAL_HAS_MOVE_ACCESSORS == 1 + + OPTIONAL_MUTABLE_CONSTEXPR T* operator ->() { + assert (initialized()); + return dataptr(); + } + + constexpr T const& operator *() const& { + return TR2_OPTIONAL_ASSERTED_EXPRESSION(initialized(), contained_val()); + } + + OPTIONAL_MUTABLE_CONSTEXPR T& operator *() & { + assert (initialized()); + return contained_val(); + } + + OPTIONAL_MUTABLE_CONSTEXPR T&& operator *() && { + assert (initialized()); + return constexpr_move(contained_val()); + } + + constexpr T const& value() const& { + return initialized() ? contained_val() : (throw bad_optional_access("bad optional access"), contained_val()); + } + + OPTIONAL_MUTABLE_CONSTEXPR T& value() & { + return initialized() ? contained_val() : (throw bad_optional_access("bad optional access"), contained_val()); + } + + OPTIONAL_MUTABLE_CONSTEXPR T&& value() && { + if (!initialized()) throw bad_optional_access("bad optional access"); + return ::std::move(contained_val()); + } + +# else + + T* operator ->() { + assert (initialized()); + return dataptr(); + } + + constexpr T const& operator *() const { + return TR2_OPTIONAL_ASSERTED_EXPRESSION(initialized(), contained_val()); + } + + T& operator *() { + assert (initialized()); + return contained_val(); + } + + constexpr T const& value() const { + return initialized() ? contained_val() : (throw bad_optional_access("bad optional access"), contained_val()); + } + + T& value() { + return initialized() ? contained_val() : (throw bad_optional_access("bad optional access"), contained_val()); + } + +# endif + +# if OPTIONAL_HAS_THIS_RVALUE_REFS == 1 + + template + constexpr T value_or(V&& v) const& + { + return *this ? **this : detail_::convert(constexpr_forward(v)); + } + +# if OPTIONAL_HAS_MOVE_ACCESSORS == 1 + + template + OPTIONAL_MUTABLE_CONSTEXPR T value_or(V&& v) && + { + return *this ? constexpr_move(const_cast&>(*this).contained_val()) : detail_::convert(constexpr_forward(v)); + } + +# else + + template + T value_or(V&& v) && + { + return *this ? constexpr_move(const_cast&>(*this).contained_val()) : detail_::convert(constexpr_forward(v)); + } + +# endif + +# else + + template + constexpr T value_or(V&& v) const + { + return *this ? **this : detail_::convert(constexpr_forward(v)); + } + +# endif + +}; + +template +class optional +{ + static_assert( !std::is_same::value, "bad T" ); + static_assert( !std::is_same::value, "bad T" ); + T* ref; + +public: + typedef T& value_type; + + // 20.5.5.1, construction/destruction + constexpr optional() noexcept : ref(nullptr) {} + + constexpr optional(nullopt_t) noexcept : ref(nullptr) {} + + constexpr optional(T& v) noexcept : ref(detail_::static_addressof(v)) {} + + optional(T&&) = delete; + + constexpr optional(const optional& rhs) noexcept : ref(rhs.ref) {} + + explicit constexpr optional(in_place_t, T& v) noexcept : ref(detail_::static_addressof(v)) {} + + explicit optional(in_place_t, T&&) = delete; + + ~optional() = default; + + // 20.5.5.2, mutation + optional& operator=(nullopt_t) noexcept { + ref = nullptr; + return *this; + } + + // optional& operator=(const optional& rhs) noexcept { + // ref = rhs.ref; + // return *this; + // } + + // optional& operator=(optional&& rhs) noexcept { + // ref = rhs.ref; + // return *this; + // } + + template + auto operator=(U&& rhs) noexcept + -> typename ::std::enable_if + < + ::std::is_same::type, optional>::value, + optional& + >::type + { + ref = rhs.ref; + return *this; + } + + template + auto operator=(U&& rhs) noexcept + -> typename ::std::enable_if + < + !::std::is_same::type, optional>::value, + optional& + >::type + = delete; + + void emplace(T& v) noexcept { + ref = detail_::static_addressof(v); + } + + void emplace(T&&) = delete; + + void swap(optional& rhs) noexcept + { + ::std::swap(ref, rhs.ref); + } + + // 20.5.5.3, observers + constexpr T* operator->() const { + return TR2_OPTIONAL_ASSERTED_EXPRESSION(ref, ref); + } + + constexpr T& operator*() const { + return TR2_OPTIONAL_ASSERTED_EXPRESSION(ref, *ref); + } + + constexpr T& value() const { + return ref ? *ref : (throw bad_optional_access("bad optional access"), *ref); + } + + explicit constexpr operator bool() const noexcept { + return ref != nullptr; + } + + template + constexpr typename ::std::decay::type value_or(V&& v) const + { + return *this ? **this : detail_::convert::type>(constexpr_forward(v)); + } +}; + +template +class optional +{ + static_assert( sizeof(T) == 0, "optional rvalue references disallowed" ); +}; + +template constexpr bool operator==(const optional& x, const optional& y) +{ + return bool(x) != bool(y) ? false : bool(x) == false ? true : *x == *y; +} + +template constexpr bool operator!=(const optional& x, const optional& y) +{ + return !(x == y); +} + +template constexpr bool operator<(const optional& x, const optional& y) +{ + return (!y) ? false : (!x) ? true : *x < *y; +} + +template constexpr bool operator>(const optional& x, const optional& y) +{ + return (y < x); +} + +template constexpr bool operator<=(const optional& x, const optional& y) +{ + return !(y < x); +} + +template constexpr bool operator>=(const optional& x, const optional& y) +{ + return !(x < y); +} + +template constexpr bool operator==(const optional& x, nullopt_t) noexcept +{ + return (!x); +} + +template constexpr bool operator==(nullopt_t, const optional& x) noexcept +{ + return (!x); +} + +template constexpr bool operator!=(const optional& x, nullopt_t) noexcept +{ + return bool(x); +} + +template constexpr bool operator!=(nullopt_t, const optional& x) noexcept +{ + return bool(x); +} + +template constexpr bool operator<(const optional&, nullopt_t) noexcept +{ + return false; +} + +template constexpr bool operator<(nullopt_t, const optional& x) noexcept +{ + return bool(x); +} + +template constexpr bool operator<=(const optional& x, nullopt_t) noexcept +{ + return (!x); +} + +template constexpr bool operator<=(nullopt_t, const optional&) noexcept +{ + return true; +} + +template constexpr bool operator>(const optional& x, nullopt_t) noexcept +{ + return bool(x); +} + +template constexpr bool operator>(nullopt_t, const optional&) noexcept +{ + return false; +} + +template constexpr bool operator>=(const optional&, nullopt_t) noexcept +{ + return true; +} + +template constexpr bool operator>=(nullopt_t, const optional& x) noexcept +{ + return (!x); +} + +template constexpr bool operator==(const optional& x, const T& v) +{ + return bool(x) ? *x == v : false; +} + +template constexpr bool operator==(const T& v, const optional& x) +{ + return bool(x) ? v == *x : false; +} + +template constexpr bool operator!=(const optional& x, const T& v) +{ + return bool(x) ? *x != v : true; +} + +template constexpr bool operator!=(const T& v, const optional& x) +{ + return bool(x) ? v != *x : true; +} + +template constexpr bool operator<(const optional& x, const T& v) +{ + return bool(x) ? *x < v : true; +} + +template constexpr bool operator>(const T& v, const optional& x) +{ + return bool(x) ? v > *x : true; +} + +template constexpr bool operator>(const optional& x, const T& v) +{ + return bool(x) ? *x > v : false; +} + +template constexpr bool operator<(const T& v, const optional& x) +{ + return bool(x) ? v < *x : false; +} + +template constexpr bool operator>=(const optional& x, const T& v) +{ + return bool(x) ? *x >= v : false; +} + +template constexpr bool operator<=(const T& v, const optional& x) +{ + return bool(x) ? v <= *x : false; +} + +template constexpr bool operator<=(const optional& x, const T& v) +{ + return bool(x) ? *x <= v : true; +} + +template constexpr bool operator>=(const T& v, const optional& x) +{ + return bool(x) ? v >= *x : true; +} + +template constexpr bool operator==(const optional& x, const T& v) +{ + return bool(x) ? *x == v : false; +} + +template constexpr bool operator==(const T& v, const optional& x) +{ + return bool(x) ? v == *x : false; +} + +template constexpr bool operator!=(const optional& x, const T& v) +{ + return bool(x) ? *x != v : true; +} + +template constexpr bool operator!=(const T& v, const optional& x) +{ + return bool(x) ? v != *x : true; +} + +template constexpr bool operator<(const optional& x, const T& v) +{ + return bool(x) ? *x < v : true; +} + +template constexpr bool operator>(const T& v, const optional& x) +{ + return bool(x) ? v > *x : true; +} + +template constexpr bool operator>(const optional& x, const T& v) +{ + return bool(x) ? *x > v : false; +} + +template constexpr bool operator<(const T& v, const optional& x) +{ + return bool(x) ? v < *x : false; +} + +template constexpr bool operator>=(const optional& x, const T& v) +{ + return bool(x) ? *x >= v : false; +} + +template constexpr bool operator<=(const T& v, const optional& x) +{ + return bool(x) ? v <= *x : false; +} + +template constexpr bool operator<=(const optional& x, const T& v) +{ + return bool(x) ? *x <= v : true; +} + +template constexpr bool operator>=(const T& v, const optional& x) +{ + return bool(x) ? v >= *x : true; +} + +template constexpr bool operator==(const optional& x, const T& v) +{ + return bool(x) ? *x == v : false; +} + +template constexpr bool operator==(const T& v, const optional& x) +{ + return bool(x) ? v == *x : false; +} + +template constexpr bool operator!=(const optional& x, const T& v) +{ + return bool(x) ? *x != v : true; +} + +template constexpr bool operator!=(const T& v, const optional& x) +{ + return bool(x) ? v != *x : true; +} + +template constexpr bool operator<(const optional& x, const T& v) +{ + return bool(x) ? *x < v : true; +} + +template constexpr bool operator>(const T& v, const optional& x) +{ + return bool(x) ? v > *x : true; +} + +template constexpr bool operator>(const optional& x, const T& v) +{ + return bool(x) ? *x > v : false; +} + +template constexpr bool operator<(const T& v, const optional& x) +{ + return bool(x) ? v < *x : false; +} + +template constexpr bool operator>=(const optional& x, const T& v) +{ + return bool(x) ? *x >= v : false; +} + +template constexpr bool operator<=(const T& v, const optional& x) +{ + return bool(x) ? v <= *x : false; +} + +template constexpr bool operator<=(const optional& x, const T& v) +{ + return bool(x) ? *x <= v : true; +} + +template constexpr bool operator>=(const T& v, const optional& x) +{ + return bool(x) ? v >= *x : true; +} + +template +void swap(optional& x, optional& y) noexcept(noexcept(x.swap(y))) +{ + x.swap(y); +} + +template +constexpr optional::type> make_optional(T&& v) +{ + return optional::type>(constexpr_forward(v)); +} + +template +constexpr optional make_optional(::std::reference_wrapper v) +{ + return optional(v.get()); +} + +} // namespace sol + +namespace std +{ + template + struct hash> + { + typedef typename hash::result_type result_type; + typedef sol::optional argument_type; + + constexpr result_type operator()(argument_type const& arg) const { + return arg ? std::hash{}(*arg) : result_type{}; + } + }; + + template + struct hash> + { + typedef typename hash::result_type result_type; + typedef sol::optional argument_type; + + constexpr result_type operator()(argument_type const& arg) const { + return arg ? std::hash{}(*arg) : result_type{}; + } + }; +} + +# undef TR2_OPTIONAL_REQUIRES +# undef TR2_OPTIONAL_ASSERTED_EXPRESSION + +# endif //___SOL2_OPTIONAL_HPP___ +// end of Optional\optional.hpp + +#endif // C++ 14 + +namespace sol { + +#if __cplusplus > 201402L + template + using optional = sol::optional; + using nullopt_t = std::nullopt_t; + constexpr nullopt_t nullopt = std::nullopt; +#elif defined(SOL_USE_BOOST) + template + using optional = boost::optional; + using nullopt_t = boost::none_t; + const nullopt_t nullopt = boost::none; +#else +#endif // C++ 14 +} + +// end of sol\optional.hpp + +// beginning of sol\compatibility.hpp + +// beginning of sol\compatibility\version.hpp + +#include + +#ifdef LUAJIT_VERSION +#ifndef SOL_LUAJIT +#define SOL_LUAJIT +#endif // sol luajit +#endif // luajit + +#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 502 +#define SOL_LUA_VERSION LUA_VERSION_NUM +#elif defined(LUA_VERSION_NUM) && LUA_VERSION_NUM == 501 +#define SOL_LUA_VERSION LUA_VERSION_NUM +#elif !defined(LUA_VERSION_NUM) +#define SOL_LUA_VERSION 500 +#else +#define SOL_LUA_VERSION 502 +#endif // Lua Version 502, 501 || luajit, 500 + +#ifdef _MSC_VER +#ifdef _DEBUG +#ifndef NDEBUG +#ifndef SOL_CHECK_ARGUMENTS +#endif // Check Arguments +#ifndef SOL_SAFE_USERTYPE +#define SOL_SAFE_USERTYPE +#endif // Safe Usertypes +#endif // NDEBUG +#endif // Debug + +#ifndef _CPPUNWIND +#ifndef SOL_NO_EXCEPTIONS +#define SOL_NO_EXCEPTIONS 1 +#endif +#endif // Automatic Exceptions + +#ifndef _CPPRTTI +#ifndef SOL_NO_RTTI +#define SOL_NO_RTTI 1 +#endif +#endif // Automatic RTTI + +#elif defined(__GNUC__) || defined(__clang__) + +#ifndef NDEBUG +#ifndef __OPTIMIZE__ +#ifndef SOL_CHECK_ARGUMENTS +#endif // Check Arguments +#ifndef SOL_SAFE_USERTYPE +#define SOL_SAFE_USERTYPE +#endif // Safe Usertypes +#endif // g++ optimizer flag +#endif // Not Debug + +#ifndef __EXCEPTIONS +#ifndef SOL_NO_EXCEPTIONS +#define SOL_NO_EXCEPTIONS 1 +#endif +#endif // No Exceptions + +#ifndef __GXX_RTTI +#ifndef SOL_NO_RTII +#define SOL_NO_RTTI 1 +#endif +#endif // No RTTI + +#endif // vc++ || clang++/g++ + +#ifndef SOL_SAFE_USERTYPE +#ifdef SOL_CHECK_ARGUMENTS +#define SOL_SAFE_USERTYPE +#endif // Turn on Safety for all +#endif // Safe Usertypes + +// end of sol\compatibility\version.hpp + +#ifndef SOL_NO_COMPAT + +#ifdef __cplusplus +extern "C" { +#endif +// beginning of sol\compatibility\5.1.0.h + +#ifndef SOL_5_1_0_H +#define SOL_5_1_0_H + +#if SOL_LUA_VERSION == 501 +/* Lua 5.1 */ + +#include +#include +#include + +/* LuaJIT doesn't define these unofficial macros ... */ +#if !defined(LUAI_INT32) +#include +#if INT_MAX-20 < 32760 +#define LUAI_INT32 long +#define LUAI_UINT32 unsigned long +#elif INT_MAX > 2147483640L +#define LUAI_INT32 int +#define LUAI_UINT32 unsigned int +#else +#error "could not detect suitable lua_Unsigned datatype" +#endif +#endif + +/* LuaJIT does not have the updated error codes for thread status/function returns */ +#ifndef LUA_ERRGCMM +#define LUA_ERRGCMM (LUA_ERRERR + 1) +#endif // LUA_ERRGCMM + +/* LuaJIT does not support continuation contexts / return error codes? */ +#ifndef LUA_KCONTEXT +#define LUA_KCONTEXT std::ptrdiff_t +typedef LUA_KCONTEXT lua_KContext; +typedef int(*lua_KFunction) (lua_State *L, int status, lua_KContext ctx); +#endif // LUA_KCONTEXT + +#define LUA_OPADD 0 +#define LUA_OPSUB 1 +#define LUA_OPMUL 2 +#define LUA_OPDIV 3 +#define LUA_OPMOD 4 +#define LUA_OPPOW 5 +#define LUA_OPUNM 6 +#define LUA_OPEQ 0 +#define LUA_OPLT 1 +#define LUA_OPLE 2 + +typedef LUAI_UINT32 lua_Unsigned; + +typedef struct luaL_Buffer_52 { + luaL_Buffer b; /* make incorrect code crash! */ + char *ptr; + size_t nelems; + size_t capacity; + lua_State *L2; +} luaL_Buffer_52; +#define luaL_Buffer luaL_Buffer_52 + +#define lua_tounsigned(L, i) lua_tounsignedx(L, i, NULL) + +#define lua_rawlen(L, i) lua_objlen(L, i) + +inline void lua_callk(lua_State *L, int nargs, int nresults, lua_KContext, lua_KFunction) { + // should probably warn the user of Lua 5.1 that continuation isn't supported... + lua_call(L, nargs, nresults); +} +inline int lua_pcallk(lua_State *L, int nargs, int nresults, int errfunc, lua_KContext, lua_KFunction) { + // should probably warn the user of Lua 5.1 that continuation isn't supported... + return lua_pcall(L, nargs, nresults, errfunc); +} +void lua_arith(lua_State *L, int op); +int lua_compare(lua_State *L, int idx1, int idx2, int op); +void lua_pushunsigned(lua_State *L, lua_Unsigned n); +lua_Unsigned luaL_checkunsigned(lua_State *L, int i); +lua_Unsigned lua_tounsignedx(lua_State *L, int i, int *isnum); +lua_Unsigned luaL_optunsigned(lua_State *L, int i, lua_Unsigned def); +lua_Integer lua_tointegerx(lua_State *L, int i, int *isnum); +void lua_len(lua_State *L, int i); +int luaL_len(lua_State *L, int i); +const char *luaL_tolstring(lua_State *L, int idx, size_t *len); +void luaL_requiref(lua_State *L, char const* modname, lua_CFunction openf, int glb); + +#define luaL_buffinit luaL_buffinit_52 +void luaL_buffinit(lua_State *L, luaL_Buffer_52 *B); + +#define luaL_prepbuffsize luaL_prepbuffsize_52 +char *luaL_prepbuffsize(luaL_Buffer_52 *B, size_t s); + +#define luaL_addlstring luaL_addlstring_52 +void luaL_addlstring(luaL_Buffer_52 *B, const char *s, size_t l); + +#define luaL_addvalue luaL_addvalue_52 +void luaL_addvalue(luaL_Buffer_52 *B); + +#define luaL_pushresult luaL_pushresult_52 +void luaL_pushresult(luaL_Buffer_52 *B); + +#undef luaL_buffinitsize +#define luaL_buffinitsize(L, B, s) \ + (luaL_buffinit(L, B), luaL_prepbuffsize(B, s)) + +#undef luaL_prepbuffer +#define luaL_prepbuffer(B) \ + luaL_prepbuffsize(B, LUAL_BUFFERSIZE) + +#undef luaL_addchar +#define luaL_addchar(B, c) \ + ((void)((B)->nelems < (B)->capacity || luaL_prepbuffsize(B, 1)), \ + ((B)->ptr[(B)->nelems++] = (c))) + +#undef luaL_addsize +#define luaL_addsize(B, s) \ + ((B)->nelems += (s)) + +#undef luaL_addstring +#define luaL_addstring(B, s) \ + luaL_addlstring(B, s, strlen(s)) + +#undef luaL_pushresultsize +#define luaL_pushresultsize(B, s) \ + (luaL_addsize(B, s), luaL_pushresult(B)) + +typedef struct kepler_lua_compat_get_string_view { + const char *s; + size_t size; +} kepler_lua_compat_get_string_view; + +inline const char* kepler_lua_compat_get_string(lua_State* L, void* ud, size_t* size) { + kepler_lua_compat_get_string_view* ls = (kepler_lua_compat_get_string_view*) ud; + (void)L; + if (ls->size == 0) return NULL; + *size = ls->size; + ls->size = 0; + return ls->s; +} + +inline int luaL_loadbufferx(lua_State* L, const char* buff, size_t size, const char* name, const char*) { + kepler_lua_compat_get_string_view ls; + ls.s = buff; + ls.size = size; + return lua_load(L, kepler_lua_compat_get_string, &ls, name/*, mode*/); +} + +#endif /* Lua 5.1 */ + +#endif // SOL_5_1_0_H// end of sol\compatibility\5.1.0.h + +// beginning of sol\compatibility\5.0.0.h + +#ifndef SOL_5_0_0_H +#define SOL_5_0_0_H + +#if SOL_LUA_VERSION < 501 +/* Lua 5.0 */ + +#define LUA_QL(x) "'" x "'" +#define LUA_QS LUA_QL("%s") + +#define luaL_Reg luaL_reg + +#define luaL_opt(L, f, n, d) \ + (lua_isnoneornil(L, n) ? (d) : f(L, n)) + +#define luaL_addchar(B,c) \ + ((void)((B)->p < ((B)->buffer+LUAL_BUFFERSIZE) || luaL_prepbuffer(B)), \ + (*(B)->p++ = (char)(c))) + +#endif // Lua 5.0 + +#endif // SOL_5_0_0_H +// end of sol\compatibility\5.0.0.h + +// beginning of sol\compatibility\5.x.x.h + +#ifndef SOL_5_X_X_H +#define SOL_5_X_X_H + +#if SOL_LUA_VERSION < 502 + +#define LUA_RIDX_GLOBALS LUA_GLOBALSINDEX + +#define LUA_OK 0 + +#define lua_pushglobaltable(L) \ + lua_pushvalue(L, LUA_GLOBALSINDEX) + +#define luaL_newlib(L, l) \ + (lua_newtable((L)),luaL_setfuncs((L), (l), 0)) + +void luaL_checkversion(lua_State *L); + +int lua_absindex(lua_State *L, int i); +void lua_copy(lua_State *L, int from, int to); +void lua_rawgetp(lua_State *L, int i, const void *p); +void lua_rawsetp(lua_State *L, int i, const void *p); +void *luaL_testudata(lua_State *L, int i, const char *tname); +lua_Number lua_tonumberx(lua_State *L, int i, int *isnum); +void lua_getuservalue(lua_State *L, int i); +void lua_setuservalue(lua_State *L, int i); +void luaL_setfuncs(lua_State *L, const luaL_Reg *l, int nup); +void luaL_setmetatable(lua_State *L, const char *tname); +int luaL_getsubtable(lua_State *L, int i, const char *name); +void luaL_traceback(lua_State *L, lua_State *L1, const char *msg, int level); +int luaL_fileresult(lua_State *L, int stat, const char *fname); + +#endif // Lua 5.1 and below + +#endif // SOL_5_X_X_H +// end of sol\compatibility\5.x.x.h + +// beginning of sol\compatibility\5.x.x.inl + +#ifndef SOL_5_X_X_INL +#define SOL_5_X_X_INL + +#if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM == 501 + +#include + +#define PACKAGE_KEY "_sol.package" + +inline int lua_absindex(lua_State *L, int i) { + if (i < 0 && i > LUA_REGISTRYINDEX) + i += lua_gettop(L) + 1; + return i; +} + +inline void lua_copy(lua_State *L, int from, int to) { + int abs_to = lua_absindex(L, to); + luaL_checkstack(L, 1, "not enough stack slots"); + lua_pushvalue(L, from); + lua_replace(L, abs_to); +} + +inline void lua_rawgetp(lua_State *L, int i, const void *p) { + int abs_i = lua_absindex(L, i); + lua_pushlightuserdata(L, (void*)p); + lua_rawget(L, abs_i); +} + +inline void lua_rawsetp(lua_State *L, int i, const void *p) { + int abs_i = lua_absindex(L, i); + luaL_checkstack(L, 1, "not enough stack slots"); + lua_pushlightuserdata(L, (void*)p); + lua_insert(L, -2); + lua_rawset(L, abs_i); +} + +inline void *luaL_testudata(lua_State *L, int i, const char *tname) { + void *p = lua_touserdata(L, i); + luaL_checkstack(L, 2, "not enough stack slots"); + if (p == NULL || !lua_getmetatable(L, i)) + return NULL; + else { + int res = 0; + luaL_getmetatable(L, tname); + res = lua_rawequal(L, -1, -2); + lua_pop(L, 2); + if (!res) + p = NULL; + } + return p; +} + +inline lua_Number lua_tonumberx(lua_State *L, int i, int *isnum) { + lua_Number n = lua_tonumber(L, i); + if (isnum != NULL) { + *isnum = (n != 0 || lua_isnumber(L, i)); + } + return n; +} + +inline static void push_package_table(lua_State *L) { + lua_pushliteral(L, PACKAGE_KEY); + lua_rawget(L, LUA_REGISTRYINDEX); + if (!lua_istable(L, -1)) { + lua_pop(L, 1); + /* try to get package table from globals */ + lua_pushliteral(L, "package"); + lua_rawget(L, LUA_GLOBALSINDEX); + if (lua_istable(L, -1)) { + lua_pushliteral(L, PACKAGE_KEY); + lua_pushvalue(L, -2); + lua_rawset(L, LUA_REGISTRYINDEX); + } + } +} + +inline void lua_getuservalue(lua_State *L, int i) { + luaL_checktype(L, i, LUA_TUSERDATA); + luaL_checkstack(L, 2, "not enough stack slots"); + lua_getfenv(L, i); + lua_pushvalue(L, LUA_GLOBALSINDEX); + if (lua_rawequal(L, -1, -2)) { + lua_pop(L, 1); + lua_pushnil(L); + lua_replace(L, -2); + } + else { + lua_pop(L, 1); + push_package_table(L); + if (lua_rawequal(L, -1, -2)) { + lua_pop(L, 1); + lua_pushnil(L); + lua_replace(L, -2); + } + else + lua_pop(L, 1); + } +} + +inline void lua_setuservalue(lua_State *L, int i) { + luaL_checktype(L, i, LUA_TUSERDATA); + if (lua_isnil(L, -1)) { + luaL_checkstack(L, 1, "not enough stack slots"); + lua_pushvalue(L, LUA_GLOBALSINDEX); + lua_replace(L, -2); + } + lua_setfenv(L, i); +} + +/* +** Adapted from Lua 5.2.0 +*/ +inline void luaL_setfuncs(lua_State *L, const luaL_Reg *l, int nup) { + luaL_checkstack(L, nup + 1, "too many upvalues"); + for (; l->name != NULL; l++) { /* fill the table with given functions */ + int i; + lua_pushstring(L, l->name); + for (i = 0; i < nup; i++) /* copy upvalues to the top */ + lua_pushvalue(L, -(nup + 1)); + lua_pushcclosure(L, l->func, nup); /* closure with those upvalues */ + lua_settable(L, -(nup + 3)); /* table must be below the upvalues, the name and the closure */ + } + lua_pop(L, nup); /* remove upvalues */ +} + +inline void luaL_setmetatable(lua_State *L, const char *tname) { + luaL_checkstack(L, 1, "not enough stack slots"); + luaL_getmetatable(L, tname); + lua_setmetatable(L, -2); +} + +inline int luaL_getsubtable(lua_State *L, int i, const char *name) { + int abs_i = lua_absindex(L, i); + luaL_checkstack(L, 3, "not enough stack slots"); + lua_pushstring(L, name); + lua_gettable(L, abs_i); + if (lua_istable(L, -1)) + return 1; + lua_pop(L, 1); + lua_newtable(L); + lua_pushstring(L, name); + lua_pushvalue(L, -2); + lua_settable(L, abs_i); + return 0; +} + +#ifndef SOL_LUAJIT +inline static int countlevels(lua_State *L) { + lua_Debug ar; + int li = 1, le = 1; + /* find an upper bound */ + while (lua_getstack(L, le, &ar)) { li = le; le *= 2; } + /* do a binary search */ + while (li < le) { + int m = (li + le) / 2; + if (lua_getstack(L, m, &ar)) li = m + 1; + else le = m; + } + return le - 1; +} + +inline static int findfield(lua_State *L, int objidx, int level) { + if (level == 0 || !lua_istable(L, -1)) + return 0; /* not found */ + lua_pushnil(L); /* start 'next' loop */ + while (lua_next(L, -2)) { /* for each pair in table */ + if (lua_type(L, -2) == LUA_TSTRING) { /* ignore non-string keys */ + if (lua_rawequal(L, objidx, -1)) { /* found object? */ + lua_pop(L, 1); /* remove value (but keep name) */ + return 1; + } + else if (findfield(L, objidx, level - 1)) { /* try recursively */ + lua_remove(L, -2); /* remove table (but keep name) */ + lua_pushliteral(L, "."); + lua_insert(L, -2); /* place '.' between the two names */ + lua_concat(L, 3); + return 1; + } + } + lua_pop(L, 1); /* remove value */ + } + return 0; /* not found */ +} + +inline static int pushglobalfuncname(lua_State *L, lua_Debug *ar) { + int top = lua_gettop(L); + lua_getinfo(L, "f", ar); /* push function */ + lua_pushvalue(L, LUA_GLOBALSINDEX); + if (findfield(L, top + 1, 2)) { + lua_copy(L, -1, top + 1); /* move name to proper place */ + lua_pop(L, 2); /* remove pushed values */ + return 1; + } + else { + lua_settop(L, top); /* remove function and global table */ + return 0; + } +} + +inline static void pushfuncname(lua_State *L, lua_Debug *ar) { + if (*ar->namewhat != '\0') /* is there a name? */ + lua_pushfstring(L, "function " LUA_QS, ar->name); + else if (*ar->what == 'm') /* main? */ + lua_pushliteral(L, "main chunk"); + else if (*ar->what == 'C') { + if (pushglobalfuncname(L, ar)) { + lua_pushfstring(L, "function " LUA_QS, lua_tostring(L, -1)); + lua_remove(L, -2); /* remove name */ + } + else + lua_pushliteral(L, "?"); + } + else + lua_pushfstring(L, "function <%s:%d>", ar->short_src, ar->linedefined); +} + +#define LEVELS1 12 /* size of the first part of the stack */ +#define LEVELS2 10 /* size of the second part of the stack */ + +inline void luaL_traceback(lua_State *L, lua_State *L1, + const char *msg, int level) { + lua_Debug ar; + int top = lua_gettop(L); + int numlevels = countlevels(L1); + int mark = (numlevels > LEVELS1 + LEVELS2) ? LEVELS1 : 0; + if (msg) lua_pushfstring(L, "%s\n", msg); + lua_pushliteral(L, "stack traceback:"); + while (lua_getstack(L1, level++, &ar)) { + if (level == mark) { /* too many levels? */ + lua_pushliteral(L, "\n\t..."); /* add a '...' */ + level = numlevels - LEVELS2; /* and skip to last ones */ + } + else { + lua_getinfo(L1, "Slnt", &ar); + lua_pushfstring(L, "\n\t%s:", ar.short_src); + if (ar.currentline > 0) + lua_pushfstring(L, "%d:", ar.currentline); + lua_pushliteral(L, " in "); + pushfuncname(L, &ar); + lua_concat(L, lua_gettop(L) - top); + } + } + lua_concat(L, lua_gettop(L) - top); +} +#endif + +inline const lua_Number *lua_version(lua_State *L) { + static const lua_Number version = LUA_VERSION_NUM; + if (L == NULL) return &version; + // TODO: wonky hacks to get at the inside of the incomplete type lua_State? + //else return L->l_G->version; + else return &version; +} + +inline static void luaL_checkversion_(lua_State *L, lua_Number ver) { + const lua_Number* v = lua_version(L); + if (v != lua_version(NULL)) + luaL_error(L, "multiple Lua VMs detected"); + else if (*v != ver) + luaL_error(L, "version mismatch: app. needs %f, Lua core provides %f", + ver, *v); + /* check conversions number -> integer types */ + lua_pushnumber(L, -(lua_Number)0x1234); + if (lua_tointeger(L, -1) != -0x1234 || + lua_tounsigned(L, -1) != (lua_Unsigned)-0x1234) + luaL_error(L, "bad conversion number->int;" + " must recompile Lua with proper settings"); + lua_pop(L, 1); +} + +inline void luaL_checkversion(lua_State* L) { + luaL_checkversion_(L, LUA_VERSION_NUM); +} + +#ifndef SOL_LUAJIT +inline int luaL_fileresult(lua_State *L, int stat, const char *fname) { + int en = errno; /* calls to Lua API may change this value */ + if (stat) { + lua_pushboolean(L, 1); + return 1; + } + else { + char buf[1024]; +#ifdef __GLIBC__ + strerror_r(en, buf, 1024); +#else + strerror_s(buf, 1024, en); +#endif + lua_pushnil(L); + if (fname) + lua_pushfstring(L, "%s: %s", fname, buf); + else + lua_pushstring(L, buf); + lua_pushnumber(L, (lua_Number)en); + return 3; + } +} +#endif // luajit +#endif // Lua 5.0 or Lua 5.1 + +#if SOL_LUA_VERSION == 501 + +typedef LUAI_INT32 LUA_INT32; + +/********************************************************************/ +/* extract of 5.2's luaconf.h */ +/* detects proper defines for faster unsigned<->number conversion */ +/* see copyright notice at the end of this file */ +/********************************************************************/ + +#if !defined(LUA_ANSI) && defined(_WIN32) && !defined(_WIN32_WCE) +#define LUA_WIN /* enable goodies for regular Windows platforms */ +#endif + +#if defined(LUA_NUMBER_DOUBLE) && !defined(LUA_ANSI) /* { */ + +/* Microsoft compiler on a Pentium (32 bit) ? */ +#if defined(LUA_WIN) && defined(_MSC_VER) && defined(_M_IX86) /* { */ + +#define LUA_MSASMTRICK +#define LUA_IEEEENDIAN 0 +#define LUA_NANTRICK + +/* pentium 32 bits? */ +#elif defined(__i386__) || defined(__i386) || defined(__X86__) /* }{ */ + +#define LUA_IEEE754TRICK +#define LUA_IEEELL +#define LUA_IEEEENDIAN 0 +#define LUA_NANTRICK + +/* pentium 64 bits? */ +#elif defined(__x86_64) /* }{ */ + +#define LUA_IEEE754TRICK +#define LUA_IEEEENDIAN 0 + +#elif defined(__POWERPC__) || defined(__ppc__) /* }{ */ + +#define LUA_IEEE754TRICK +#define LUA_IEEEENDIAN 1 + +#else /* }{ */ + +/* assume IEEE754 and a 32-bit integer type */ +#define LUA_IEEE754TRICK + +#endif /* } */ + +#endif /* } */ + +/********************************************************************/ +/* extract of 5.2's llimits.h */ +/* gives us lua_number2unsigned and lua_unsigned2number */ +/* see copyright notice just below this one here */ +/********************************************************************/ + +/********************************************************************* +* This file contains parts of Lua 5.2's source code: +* +* Copyright (C) 1994-2013 Lua.org, PUC-Rio. +* +* 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. +*********************************************************************/ + +#if defined(MS_ASMTRICK) || defined(LUA_MSASMTRICK) /* { */ +/* trick with Microsoft assembler for X86 */ + +#define lua_number2unsigned(i,n) \ + {__int64 l; __asm {__asm fld n __asm fistp l} i = (unsigned int)l;} + +#elif defined(LUA_IEEE754TRICK) /* }{ */ +/* the next trick should work on any machine using IEEE754 with +a 32-bit int type */ + +union compat52_luai_Cast { double l_d; LUA_INT32 l_p[2]; }; + +#if !defined(LUA_IEEEENDIAN) /* { */ +#define LUAI_EXTRAIEEE \ + static const union compat52_luai_Cast ieeeendian = {-(33.0 + 6755399441055744.0)}; +#define LUA_IEEEENDIANLOC (ieeeendian.l_p[1] == 33) +#else +#define LUA_IEEEENDIANLOC LUA_IEEEENDIAN +#define LUAI_EXTRAIEEE /* empty */ +#endif /* } */ + +#define lua_number2int32(i,n,t) \ + { LUAI_EXTRAIEEE \ + volatile union compat52_luai_Cast u; u.l_d = (n) + 6755399441055744.0; \ + (i) = (t)u.l_p[LUA_IEEEENDIANLOC]; } + +#define lua_number2unsigned(i,n) lua_number2int32(i, n, lua_Unsigned) + +#endif /* } */ + +/* the following definitions always work, but may be slow */ + +#if !defined(lua_number2unsigned) /* { */ +/* the following definition assures proper modulo behavior */ +#if defined(LUA_NUMBER_DOUBLE) || defined(LUA_NUMBER_FLOAT) +#include +#define SUPUNSIGNED ((lua_Number)(~(lua_Unsigned)0) + 1) +#define lua_number2unsigned(i,n) \ + ((i)=(lua_Unsigned)((n) - floor((n)/SUPUNSIGNED)*SUPUNSIGNED)) +#else +#define lua_number2unsigned(i,n) ((i)=(lua_Unsigned)(n)) +#endif +#endif /* } */ + +#if !defined(lua_unsigned2number) +/* on several machines, coercion from unsigned to double is slow, +so it may be worth to avoid */ +#define lua_unsigned2number(u) \ + (((u) <= (lua_Unsigned)INT_MAX) ? (lua_Number)(int)(u) : (lua_Number)(u)) +#endif + +/********************************************************************/ + +inline static void compat52_call_lua(lua_State *L, char const code[], size_t len, + int nargs, int nret) { + lua_rawgetp(L, LUA_REGISTRYINDEX, (void*)code); + if (lua_type(L, -1) != LUA_TFUNCTION) { + lua_pop(L, 1); + if (luaL_loadbuffer(L, code, len, "=none")) + lua_error(L); + lua_pushvalue(L, -1); + lua_rawsetp(L, LUA_REGISTRYINDEX, (void*)code); + } + lua_insert(L, -nargs - 1); + lua_call(L, nargs, nret); +} + +static const char compat52_arith_code[] = { + "local op,a,b=...\n" + "if op==0 then return a+b\n" + "elseif op==1 then return a-b\n" + "elseif op==2 then return a*b\n" + "elseif op==3 then return a/b\n" + "elseif op==4 then return a%b\n" + "elseif op==5 then return a^b\n" + "elseif op==6 then return -a\n" + "end\n" +}; + +inline void lua_arith(lua_State *L, int op) { + if (op < LUA_OPADD || op > LUA_OPUNM) + luaL_error(L, "invalid 'op' argument for lua_arith"); + luaL_checkstack(L, 5, "not enough stack slots"); + if (op == LUA_OPUNM) + lua_pushvalue(L, -1); + lua_pushnumber(L, op); + lua_insert(L, -3); + compat52_call_lua(L, compat52_arith_code, + sizeof(compat52_arith_code) - 1, 3, 1); +} + +static const char compat52_compare_code[] = { + "local a,b=...\n" + "return a<=b\n" +}; + +inline int lua_compare(lua_State *L, int idx1, int idx2, int op) { + int result = 0; + switch (op) { + case LUA_OPEQ: + return lua_equal(L, idx1, idx2); + case LUA_OPLT: + return lua_lessthan(L, idx1, idx2); + case LUA_OPLE: + luaL_checkstack(L, 5, "not enough stack slots"); + idx1 = lua_absindex(L, idx1); + idx2 = lua_absindex(L, idx2); + lua_pushvalue(L, idx1); + lua_pushvalue(L, idx2); + compat52_call_lua(L, compat52_compare_code, + sizeof(compat52_compare_code) - 1, 2, 1); + result = lua_toboolean(L, -1); + lua_pop(L, 1); + return result; + default: + luaL_error(L, "invalid 'op' argument for lua_compare"); + } + return 0; +} + +inline void lua_pushunsigned(lua_State *L, lua_Unsigned n) { + lua_pushnumber(L, lua_unsigned2number(n)); +} + +inline lua_Unsigned luaL_checkunsigned(lua_State *L, int i) { + lua_Unsigned result; + lua_Number n = lua_tonumber(L, i); + if (n == 0 && !lua_isnumber(L, i)) + luaL_checktype(L, i, LUA_TNUMBER); + lua_number2unsigned(result, n); + return result; +} + +inline lua_Unsigned lua_tounsignedx(lua_State *L, int i, int *isnum) { + lua_Unsigned result; + lua_Number n = lua_tonumberx(L, i, isnum); + lua_number2unsigned(result, n); + return result; +} + +inline lua_Unsigned luaL_optunsigned(lua_State *L, int i, lua_Unsigned def) { + return luaL_opt(L, luaL_checkunsigned, i, def); +} + +inline lua_Integer lua_tointegerx(lua_State *L, int i, int *isnum) { + lua_Integer n = lua_tointeger(L, i); + if (isnum != NULL) { + *isnum = (n != 0 || lua_isnumber(L, i)); + } + return n; +} + +inline void lua_len(lua_State *L, int i) { + switch (lua_type(L, i)) { + case LUA_TSTRING: /* fall through */ + case LUA_TTABLE: + if (!luaL_callmeta(L, i, "__len")) + lua_pushnumber(L, (int)lua_objlen(L, i)); + break; + case LUA_TUSERDATA: + if (luaL_callmeta(L, i, "__len")) + break; + /* maybe fall through */ + default: + luaL_error(L, "attempt to get length of a %s value", + lua_typename(L, lua_type(L, i))); + } +} + +inline int luaL_len(lua_State *L, int i) { + int res = 0, isnum = 0; + luaL_checkstack(L, 1, "not enough stack slots"); + lua_len(L, i); + res = (int)lua_tointegerx(L, -1, &isnum); + lua_pop(L, 1); + if (!isnum) + luaL_error(L, "object length is not a number"); + return res; +} + +inline const char *luaL_tolstring(lua_State *L, int idx, size_t *len) { + if (!luaL_callmeta(L, idx, "__tostring")) { + int t = lua_type(L, idx); + switch (t) { + case LUA_TNIL: + lua_pushliteral(L, "nil"); + break; + case LUA_TSTRING: + case LUA_TNUMBER: + lua_pushvalue(L, idx); + break; + case LUA_TBOOLEAN: + if (lua_toboolean(L, idx)) + lua_pushliteral(L, "true"); + else + lua_pushliteral(L, "false"); + break; + default: + lua_pushfstring(L, "%s: %p", lua_typename(L, t), + lua_topointer(L, idx)); + break; + } + } + return lua_tolstring(L, -1, len); +} + +inline void luaL_requiref(lua_State *L, char const* modname, + lua_CFunction openf, int glb) { + luaL_checkstack(L, 3, "not enough stack slots"); + lua_pushcfunction(L, openf); + lua_pushstring(L, modname); + lua_call(L, 1, 1); + lua_getglobal(L, "package"); + if (lua_istable(L, -1) == 0) { + lua_pop(L, 1); + lua_createtable(L, 0, 16); + lua_setglobal(L, "package"); + lua_getglobal(L, "package"); + } + lua_getfield(L, -1, "loaded"); + if (lua_istable(L, -1) == 0) { + lua_pop(L, 1); + lua_createtable(L, 0, 1); + lua_setfield(L, -2, "loaded"); + lua_getfield(L, -1, "loaded"); + } + lua_replace(L, -2); + lua_pushvalue(L, -2); + lua_setfield(L, -2, modname); + lua_pop(L, 1); + if (glb) { + lua_pushvalue(L, -1); + lua_setglobal(L, modname); + } +} + +inline void luaL_buffinit(lua_State *L, luaL_Buffer_52 *B) { + /* make it crash if used via pointer to a 5.1-style luaL_Buffer */ + B->b.p = NULL; + B->b.L = NULL; + B->b.lvl = 0; + /* reuse the buffer from the 5.1-style luaL_Buffer though! */ + B->ptr = B->b.buffer; + B->capacity = LUAL_BUFFERSIZE; + B->nelems = 0; + B->L2 = L; +} + +inline char *luaL_prepbuffsize(luaL_Buffer_52 *B, size_t s) { + if (B->capacity - B->nelems < s) { /* needs to grow */ + char* newptr = NULL; + size_t newcap = B->capacity * 2; + if (newcap - B->nelems < s) + newcap = B->nelems + s; + if (newcap < B->capacity) /* overflow */ + luaL_error(B->L2, "buffer too large"); + newptr = (char*)lua_newuserdata(B->L2, newcap); + memcpy(newptr, B->ptr, B->nelems); + if (B->ptr != B->b.buffer) + lua_replace(B->L2, -2); /* remove old buffer */ + B->ptr = newptr; + B->capacity = newcap; + } + return B->ptr + B->nelems; +} + +inline void luaL_addlstring(luaL_Buffer_52 *B, const char *s, size_t l) { + memcpy(luaL_prepbuffsize(B, l), s, l); + luaL_addsize(B, l); +} + +inline void luaL_addvalue(luaL_Buffer_52 *B) { + size_t len = 0; + const char *s = lua_tolstring(B->L2, -1, &len); + if (!s) + luaL_error(B->L2, "cannot convert value to string"); + if (B->ptr != B->b.buffer) + lua_insert(B->L2, -2); /* userdata buffer must be at stack top */ + luaL_addlstring(B, s, len); + lua_remove(B->L2, B->ptr != B->b.buffer ? -2 : -1); +} + +inline void luaL_pushresult(luaL_Buffer_52 *B) { + lua_pushlstring(B->L2, B->ptr, B->nelems); + if (B->ptr != B->b.buffer) + lua_replace(B->L2, -2); /* remove userdata buffer */ +} + +#endif /* SOL_LUA_VERSION == 501 */ + +#endif // SOL_5_X_X_INL +// end of sol\compatibility\5.x.x.inl + +#ifdef __cplusplus +} +#endif + +#endif // SOL_NO_COMPAT + +// end of sol\compatibility.hpp + +// beginning of sol\string_shim.hpp + +#pragma once + +namespace sol { + namespace string_detail { + struct string_shim { + std::size_t s; + const char* p; + + string_shim(const std::string& r) : string_shim(r.data(), r.size()) {} + string_shim(const char* p) : string_shim(p, std::char_traits::length(p)) {} + string_shim(const char* p, std::size_t s) : s(s), p(p) {} + + static int compare(const char* lhs_p, std::size_t lhs_sz, const char* rhs_p, std::size_t rhs_sz) { + int result = std::char_traits::compare(lhs_p, rhs_p, lhs_sz < rhs_sz ? lhs_sz : rhs_sz); + if (result != 0) + return result; + if (lhs_sz < rhs_sz) + return -1; + if (lhs_sz > rhs_sz) + return 1; + return 0; + } + + const char* c_str() const { + return p; + } + + const char* data() const { + return p; + } + + std::size_t size() const { + return s; + } + + bool operator==(const string_shim& r) const { + return compare(p, s, r.data(), r.size()) == 0; + } + + bool operator==(const char* r) const { + return compare(r, std::char_traits::length(r), p, s) == 0; + } + + bool operator==(const std::string& r) const { + return compare(r.data(), r.size(), p, s) == 0; + } + }; + } +}// end of sol\string_shim.hpp + +#include + +namespace sol { + namespace detail { +#ifdef SOL_NO_EXCEPTIONS + template + int static_trampoline(lua_State* L) { + return f(L); + } + + template + int trampoline(lua_State* L, Fx&& f, Args&&... args) { + return f(L, std::forward(args)...); + } + + inline int c_trampoline(lua_State* L, lua_CFunction f) { + return trampoline(L, f); + } +#else + template + int static_trampoline(lua_State* L) { + try { + return f(L); + } + catch (const char *s) { + lua_pushstring(L, s); + } + catch (const std::exception& e) { + lua_pushstring(L, e.what()); + } + catch (...) { + lua_pushstring(L, "caught (...) exception"); + } + return lua_error(L); + } + + template + int trampoline(lua_State* L, Fx&& f, Args&&... args) { + try { + return f(L, std::forward(args)...); + } + catch (const char *s) { + lua_pushstring(L, s); + } + catch (const std::exception& e) { + lua_pushstring(L, e.what()); + } + catch (...) { + lua_pushstring(L, "caught (...) exception"); + } + return lua_error(L); + } + + inline int c_trampoline(lua_State* L, lua_CFunction f) { + return trampoline(L, f); + } +#endif // Exceptions vs. No Exceptions + + template + struct unique_usertype {}; + + template + struct implicit_wrapper { + T& item; + implicit_wrapper(T* item) : item(*item) {} + implicit_wrapper(T& item) : item(item) {} + operator T& () { + return item; + } + operator T* () { + return std::addressof(item); + } + }; + } // detail + + struct nil_t {}; + const nil_t nil{}; + inline bool operator==(nil_t, nil_t) { return true; } + inline bool operator!=(nil_t, nil_t) { return false; } + + struct metatable_key_t {}; + const metatable_key_t metatable_key = {}; + + struct no_metatable_t {}; + const no_metatable_t no_metatable = {}; + + typedef std::remove_pointer_t lua_r_CFunction; + + template + struct unique_usertype_traits { + typedef T type; + typedef T actual_type; + static const bool value = false; + + template + static bool is_null(U&&) { + return false; + } + + template + static auto get(U&& value) { + return std::addressof(detail::deref(value)); + } + }; + + template + struct unique_usertype_traits> { + typedef T type; + typedef std::shared_ptr actual_type; + static const bool value = true; + + static bool is_null(const actual_type& value) { + return value == nullptr; + } + + static type* get(const actual_type& p) { + return p.get(); + } + }; + + template + struct unique_usertype_traits> { + typedef T type; + typedef std::unique_ptr actual_type; + static const bool value = true; + + static bool is_null(const actual_type& value) { + return value == nullptr; + } + + static type* get(const actual_type& p) { + return p.get(); + } + }; + + template + struct non_null {}; + + template + struct function_sig {}; + + struct up_value_index { + int index; + up_value_index(int idx) : index(lua_upvalueindex(idx)) {} + operator int() const { return index; } + }; + + struct absolute_index { + int index; + absolute_index(lua_State* L, int idx) : index(lua_absindex(L, idx)) {} + operator int() const { return index; } + }; + + struct lightuserdata_value { + void* value; + lightuserdata_value(void* data) : value(data) {} + operator void*() const { return value; } + }; + + struct userdata_value { + void* value; + userdata_value(void* data) : value(data) {} + operator void*() const { return value; } + }; + + template + struct light { + L* value; + + light(L& x) : value(std::addressof(x)) {} + light(L* x) : value(x) {} + light(void* x) : value(static_cast(x)) {} + operator L* () const { return value; } + operator L& () const { return *value; } + }; + + template + auto make_light(T& l) { + typedef meta::unwrapped_t>> L; + return light(l); + } + + template + struct user { + U value; + + user(U x) : value(std::forward(x)) {} + operator U* () { return std::addressof(value); } + operator U& () { return value; } + }; + + template + auto make_user(T&& u) { + typedef meta::unwrapped_t> U; + return user(std::forward(u)); + } + + template + struct metatable_registry_key { + T key; + + metatable_registry_key(T key) : key(std::forward(key)) {} + }; + + template + auto meta_registry_key(T&& key) { + typedef meta::unqualified_t K; + return metatable_registry_key(std::forward(key)); + } + + template + struct closure { + lua_CFunction c_function; + std::tuple upvalues; + closure(lua_CFunction f, Upvalues... upvalues) : c_function(f), upvalues(std::forward(upvalues)...) {} + }; + + template <> + struct closure<> { + lua_CFunction c_function; + int upvalues; + closure(lua_CFunction f, int upvalues = 0) : c_function(f), upvalues(upvalues) {} + }; + + typedef closure<> c_closure; + + template + closure make_closure(lua_CFunction f, Args&&... args) { + return closure(f, std::forward(args)...); + } + + struct this_state { + lua_State* L; + operator lua_State* () const { + return L; + } + lua_State* operator-> () const { + return L; + } + }; + + enum class call_syntax { + dot = 0, + colon = 1 + }; + + enum class call_status : int { + ok = LUA_OK, + yielded = LUA_YIELD, + runtime = LUA_ERRRUN, + memory = LUA_ERRMEM, + handler = LUA_ERRERR, + gc = LUA_ERRGCMM + }; + + enum class thread_status : int { + ok = LUA_OK, + yielded = LUA_YIELD, + runtime = LUA_ERRRUN, + memory = LUA_ERRMEM, + gc = LUA_ERRGCMM, + handler = LUA_ERRERR, + dead, + }; + + enum class load_status : int { + ok = LUA_OK, + syntax = LUA_ERRSYNTAX, + memory = LUA_ERRMEM, + gc = LUA_ERRGCMM, + file = LUA_ERRFILE, + }; + + enum class type : int { + none = LUA_TNONE, + nil = LUA_TNIL, + string = LUA_TSTRING, + number = LUA_TNUMBER, + thread = LUA_TTHREAD, + boolean = LUA_TBOOLEAN, + function = LUA_TFUNCTION, + userdata = LUA_TUSERDATA, + lightuserdata = LUA_TLIGHTUSERDATA, + table = LUA_TTABLE, + poly = none | nil | string | number | thread | + table | boolean | function | userdata | lightuserdata + }; + + enum class meta_function { + construct, + index, + new_index, + mode, + call, + metatable, + to_string, + length, + unary_minus, + addition, + subtraction, + multiplication, + division, + modulus, + power_of, + involution = power_of, + concatenation, + equal_to, + less_than, + less_than_or_equal_to, + garbage_collect, + call_function = call, + }; + + typedef meta_function meta_method; + + const std::array meta_variable_names = { { + "__index", + "__newindex", + } }; + + const std::array meta_function_names = { { + "new", + "__index", + "__newindex", + "__mode", + "__call", + "__metatable", + "__tostring", + "__len", + "__unm", + "__add", + "__sub", + "__mul", + "__div", + "__mod", + "__pow", + "__concat", + "__eq", + "__lt", + "__le", + "__gc", + } }; + + inline const std::string& name_of(meta_function mf) { + return meta_function_names[static_cast(mf)]; + } + + inline type type_of(lua_State* L, int index) { + return static_cast(lua_type(L, index)); + } + + inline int type_panic(lua_State* L, int index, type expected, type actual) { + return luaL_error(L, "stack index %d, expected %s, received %s", index, + expected == type::poly ? "anything" : lua_typename(L, static_cast(expected)), + expected == type::poly ? "anything" : lua_typename(L, static_cast(actual)) + ); + } + + // Specify this function as the handler for lua::check if you know there's nothing wrong + inline int no_panic(lua_State*, int, type, type) noexcept { + return 0; + } + + inline void type_error(lua_State* L, int expected, int actual) { + luaL_error(L, "expected %s, received %s", lua_typename(L, expected), lua_typename(L, actual)); + } + + inline void type_error(lua_State* L, type expected, type actual) { + type_error(L, static_cast(expected), static_cast(actual)); + } + + inline void type_assert(lua_State* L, int index, type expected, type actual) { + if (expected != type::poly && expected != actual) { + type_panic(L, index, expected, actual); + } + } + + inline void type_assert(lua_State* L, int index, type expected) { + type actual = type_of(L, index); + type_assert(L, index, expected, actual); + } + + inline std::string type_name(lua_State* L, type t) { + return lua_typename(L, static_cast(t)); + } + + class reference; + class stack_reference; + template + class usertype; + template + class basic_table_core; + template + using table_core = basic_table_core; + template + using stack_table_core = basic_table_core; + typedef table_core table; + typedef table_core global_table; + typedef stack_table_core stack_table; + typedef stack_table_core stack_global_table; + template + class basic_function; + template + class basic_protected_function; + using function = basic_function; + using protected_function = basic_protected_function; + using stack_function = basic_function; + using stack_protected_function = basic_protected_function; + template + class basic_object; + template + class basic_userdata; + template + class basic_lightuserdata; + struct variadic_args; + using object = basic_object; + using stack_object = basic_object; + using userdata = basic_userdata; + using stack_userdata = basic_userdata; + using lightuserdata = basic_lightuserdata; + using stack_lightuserdata = basic_lightuserdata; + class coroutine; + class thread; + struct variadic_args; + struct this_state; + + namespace detail { + template + struct lua_type_of : std::integral_constant {}; + + template <> + struct lua_type_of : std::integral_constant {}; + + template <> + struct lua_type_of : std::integral_constant {}; + + template <> + struct lua_type_of : std::integral_constant {}; + + template <> + struct lua_type_of : std::integral_constant {}; + + template + struct lua_type_of : std::integral_constant {}; + + template + struct lua_type_of : std::integral_constant {}; + + template + struct lua_type_of : std::integral_constant {}; + + template + struct lua_type_of : std::integral_constant {}; + + template <> + struct lua_type_of : std::integral_constant {}; + + template <> + struct lua_type_of : std::integral_constant {}; + + template <> + struct lua_type_of : std::integral_constant {}; + + template <> + struct lua_type_of : std::integral_constant {}; + + template <> + struct lua_type_of : std::integral_constant {}; + + template <> + struct lua_type_of : std::integral_constant {}; + + template <> + struct lua_type_of : std::integral_constant {}; + + template <> + struct lua_type_of : std::integral_constant {}; + + template <> + struct lua_type_of : std::integral_constant {}; + + template <> + struct lua_type_of : std::integral_constant { }; + + template <> + struct lua_type_of : std::integral_constant { }; + + template <> + struct lua_type_of : std::integral_constant { }; + + template + struct lua_type_of> : std::integral_constant { }; + + template <> + struct lua_type_of : std::integral_constant {}; + + template <> + struct lua_type_of : std::integral_constant {}; + + template + struct lua_type_of> : std::integral_constant {}; + + template + struct lua_type_of> : std::integral_constant {}; + + template + struct lua_type_of> : std::integral_constant {}; + + template <> + struct lua_type_of : std::integral_constant {}; + + template <> + struct lua_type_of : std::integral_constant {}; + + template + struct lua_type_of> : std::integral_constant {}; + + template + struct lua_type_of> : std::integral_constant {}; + + template + struct lua_type_of> : std::integral_constant {}; + + template + struct lua_type_of> : std::integral_constant {}; + + template <> + struct lua_type_of : std::integral_constant {}; + + template <> + struct lua_type_of> : std::integral_constant {}; + + template + struct lua_type_of> : std::integral_constant {}; + + template + struct lua_type_of> : std::integral_constant {}; + + template <> + struct lua_type_of : std::integral_constant {}; + + template <> + struct lua_type_of : std::integral_constant {}; + + template + struct lua_type_of> : std::integral_constant {}; + + template + struct lua_type_of> : std::integral_constant {}; + + template <> + struct lua_type_of : std::integral_constant {}; + + template + struct lua_type_of : std::integral_constant {}; + + template + struct lua_type_of::value>> : std::integral_constant {}; + + template + struct lua_type_of::value>> : std::integral_constant {}; + + template <> + struct lua_type_of : std::integral_constant {}; + + template <> + struct lua_type_of : std::integral_constant {}; + + template <> + struct lua_type_of : std::integral_constant {}; + } // detail + + template + struct lua_type_of : detail::lua_type_of {}; + + template + struct is_lua_primitive : std::integral_constant>::value + || std::is_base_of>::value + || std::is_base_of>::value + || meta::is_specialization_of>::value + || meta::is_specialization_of>::value + > { }; + + template + struct is_lua_primitive> : std::true_type { }; + template + struct is_lua_primitive> : std::true_type {}; + template + struct is_lua_primitive : std::true_type {}; + template <> + struct is_lua_primitive : std::true_type {}; + template <> + struct is_lua_primitive : std::true_type {}; + template + struct is_lua_primitive> : is_lua_primitive {}; + + template + struct is_proxy_primitive : is_lua_primitive { }; + + template + struct is_unique_usertype : std::integral_constant::value> {}; + + template + struct is_transparent_argument : std::false_type {}; + + template <> + struct is_transparent_argument : std::true_type {}; + + template <> + struct is_transparent_argument : std::true_type {}; + + template + struct lua_bind_traits : meta::bind_traits { + private: + typedef meta::bind_traits base_t; + public: + static const std::size_t true_arity = base_t::arity; + static const std::size_t arity = base_t::arity - meta::count_for::value; + static const std::size_t true_free_arity = base_t::free_arity; + static const std::size_t free_arity = base_t::free_arity - meta::count_for::value; + }; + + template + struct is_table : std::false_type {}; + + template + struct is_table> : std::true_type {}; + + template + inline type type_of() { + return lua_type_of>::value; + } +} // sol + +// end of sol\types.hpp + +// beginning of sol\stack_reference.hpp + +namespace sol { + class stack_reference { + private: + lua_State* L = nullptr; + int index = 0; + + protected: + int registry_index() const noexcept { + return LUA_NOREF; + } + + public: + stack_reference() noexcept = default; + stack_reference(nil_t) noexcept : stack_reference() {}; + stack_reference(lua_State* L, int i) noexcept : L(L), index(lua_absindex(L, i)) {} + stack_reference(stack_reference&& o) noexcept = default; + stack_reference& operator=(stack_reference&&) noexcept = default; + stack_reference(const stack_reference&) noexcept = default; + stack_reference& operator=(const stack_reference&) noexcept = default; + + int push() const noexcept { + lua_pushvalue(L, index); + return 1; + } + + void pop(int n = 1) const noexcept { + lua_pop(lua_state(), n); + } + + int stack_index() const noexcept { + return index; + } + + type get_type() const noexcept { + int result = lua_type(L, index); + return static_cast(result); + } + + lua_State* lua_state() const noexcept { + return L; + } + + bool valid() const noexcept { + type t = get_type(); + return t != type::nil && t != type::none; + } + }; + + inline bool operator== (const stack_reference& l, const stack_reference& r) { + return lua_compare(l.lua_state(), l.stack_index(), r.stack_index(), LUA_OPEQ) == 0; + } + + inline bool operator!= (const stack_reference& l, const stack_reference& r) { + return !operator==(l, r); + } +} // sol + +// end of sol\stack_reference.hpp + +namespace sol { + namespace stack { + template + struct push_popper { + T t; + push_popper(T x) : t(x) { t.push(); } + ~push_popper() { t.pop(); } + }; + template + struct push_popper { + push_popper(T) {} + ~push_popper() {} + }; + template + push_popper push_pop(T&& x) { + return push_popper(std::forward(x)); + } + } // stack + + namespace detail { + struct global_tag { } const global_{}; + } // detail + + class reference { + private: + lua_State* L = nullptr; // non-owning + int ref = LUA_NOREF; + + int copy() const noexcept { + if (ref == LUA_NOREF) + return LUA_NOREF; + push(); + return luaL_ref(L, LUA_REGISTRYINDEX); + } + + protected: + reference(lua_State* L, detail::global_tag) noexcept : L(L) { + lua_pushglobaltable(L); + ref = luaL_ref(L, LUA_REGISTRYINDEX); + } + + int stack_index() const noexcept { + return -1; + } + + public: + reference() noexcept = default; + reference(nil_t) noexcept : reference() {} + reference(const stack_reference& r) noexcept : reference(r.lua_state(), r.stack_index()) {} + reference(stack_reference&& r) noexcept : reference(r.lua_state(), r.stack_index()) {} + reference(lua_State* L, int index = -1) noexcept : L(L) { + lua_pushvalue(L, index); + ref = luaL_ref(L, LUA_REGISTRYINDEX); + } + + virtual ~reference() noexcept { + luaL_unref(L, LUA_REGISTRYINDEX, ref); + } + + reference(reference&& o) noexcept { + L = o.L; + ref = o.ref; + + o.L = nullptr; + o.ref = LUA_NOREF; + } + + reference& operator=(reference&& o) noexcept { + L = o.L; + ref = o.ref; + + o.L = nullptr; + o.ref = LUA_NOREF; + + return *this; + } + + reference(const reference& o) noexcept { + L = o.L; + ref = o.copy(); + } + + reference& operator=(const reference& o) noexcept { + L = o.L; + ref = o.copy(); + return *this; + } + + int push() const noexcept { + lua_rawgeti(L, LUA_REGISTRYINDEX, ref); + return 1; + } + + void pop(int n = 1) const noexcept { + lua_pop(lua_state(), n); + } + + int registry_index() const noexcept { + return ref; + } + + bool valid() const noexcept { + return !(ref == LUA_NOREF || ref == LUA_REFNIL); + } + + explicit operator bool() const noexcept { + return valid(); + } + + type get_type() const noexcept { + push(); + int result = lua_type(L, -1); + lua_pop(L, 1); + return static_cast(result); + } + + lua_State* lua_state() const noexcept { + return L; + } + }; + + inline bool operator== (const reference& l, const reference& r) { + auto ppl = stack::push_pop(l); + auto ppr = stack::push_pop(r); + return lua_compare(l.lua_state(), -1, -2, LUA_OPEQ) == 1; + } + + inline bool operator!= (const reference& l, const reference& r) { + return !operator==(l, r); + } +} // sol + +// end of sol\reference.hpp + +// beginning of sol\userdata.hpp + +namespace sol { + template + class basic_userdata : public base_t { + public: + basic_userdata() noexcept = default; + basic_userdata(const basic_userdata&) = default; + basic_userdata(basic_userdata&&) = default; + basic_userdata& operator=(const basic_userdata&) = default; + basic_userdata& operator=(basic_userdata&&) = default; + basic_userdata(const stack_reference& r) : basic_userdata(r.lua_state(), r.stack_index()) {} + basic_userdata(stack_reference&& r) : basic_userdata(r.lua_state(), r.stack_index()) {} + basic_userdata(lua_State* L, int index = -1) : base_t(L, index) { +#ifdef SOL_CHECK_ARGUMENTS + type_assert(L, index, type::userdata); +#endif // Safety + } + }; + + template + class basic_lightuserdata : public base_t { + public: + basic_lightuserdata() noexcept = default; + basic_lightuserdata(const basic_lightuserdata&) = default; + basic_lightuserdata(basic_lightuserdata&&) = default; + basic_lightuserdata& operator=(const basic_lightuserdata&) = default; + basic_lightuserdata& operator=(basic_lightuserdata&&) = default; + basic_lightuserdata(const stack_reference& r) : basic_lightuserdata(r.lua_state(), r.stack_index()) {} + basic_lightuserdata(stack_reference&& r) : basic_lightuserdata(r.lua_state(), r.stack_index()) {} + basic_lightuserdata(lua_State* L, int index = -1) : base_t(L, index) { +#ifdef SOL_CHECK_ARGUMENTS + type_assert(L, index, type::lightuserdata); +#endif // Safety + } + }; + +} // sol + +// end of sol\userdata.hpp + +// beginning of sol\stack.hpp + +// beginning of sol\stack_core.hpp + +// beginning of sol\tie.hpp + +namespace sol { + + template + struct tie_size : std::tuple_size {}; + + template + struct is_tieable : std::integral_constant::value > 0)> {}; + + template + struct tie_t : public std::tuple...> { + private: + typedef std::tuple...> base_t; + + template + void set(std::false_type, T&& target) { + std::get<0>(*this) = std::forward(target); + } + + template + void set(std::true_type, T&& target) { + typedef tie_size> value_size; + typedef tie_size> tie_size; + typedef std::conditional_t<(value_size::value < tie_size::value), value_size, tie_size> indices_size; + typedef std::make_index_sequence indices; + set(indices(), std::forward(target)); + } + + template + void set(std::index_sequence, T&& target) { + using std::get; + (void)detail::swallow{ 0, + (get(*this) = get(target), 0)... + , 0 }; + } + + public: + using base_t::base_t; + + template + tie_t& operator= (T&& value) { + typedef is_tieable> bondable; + set(bondable(), std::forward(value)); + return *this; + } + + }; + + template + struct tie_size<::sol::tie_t> : ::std::tuple_size<::std::tuple> { }; + + namespace adl_barrier_detail { + template + inline tie_t...> tie(Tn&&... argn) { + return tie_t...>(std::forward(argn)...); + } + } + + using namespace adl_barrier_detail; + +} // sol + +// end of sol\tie.hpp + +namespace sol { + namespace detail { + struct as_reference_tag {}; + + using special_destruct_func = void(*)(void*); + + template + inline void special_destruct(void* memory) { + T** pointerpointer = static_cast(memory); + special_destruct_func* dx = static_cast(static_cast(pointerpointer + 1)); + Real* target = static_cast(static_cast(dx + 1)); + target->~Real(); + } + + template + inline int unique_destruct(lua_State* L) { + void* memory = lua_touserdata(L, 1); + T** pointerpointer = static_cast(memory); + special_destruct_func& dx = *static_cast(static_cast(pointerpointer + 1)); + (dx)(memory); + return 0; + } + } // detail + + namespace stack { + + template + struct field_getter; + template + struct probe_field_getter; + template + struct field_setter; + template + struct getter; + template + struct popper; + template + struct pusher; + template::value, typename = void> + struct checker; + template + struct check_getter; + + struct probe { + bool success; + int levels; + + probe(bool s, int l) : success(s), levels(l) {} + + operator bool() const { return success; }; + }; + + namespace stack_detail { + template + struct strip { + typedef T type; + }; + template + struct strip> { + typedef T& type; + }; + template + struct strip> { + typedef T type; + }; + template + using strip_t = typename strip::type; + const bool default_check_arguments = +#ifdef SOL_CHECK_ARGUMENTS + true; +#else + false; +#endif + template + inline decltype(auto) unchecked_get(lua_State* L, int index = -1) { + return getter>{}.get(L, index); + } + } // stack_detail + + inline bool maybe_indexable(lua_State* L, int index = -1) { + type t = type_of(L, index); + return t == type::userdata || t == type::table; + } + + template + inline int push(lua_State* L, T&& t, Args&&... args) { + return pusher>{}.push(L, std::forward(t), std::forward(args)...); + } + + // overload allows to use a pusher of a specific type, but pass in any kind of args + template + inline int push(lua_State* L, Arg&& arg, Args&&... args) { + return pusher>{}.push(L, std::forward(arg), std::forward(args)...); + } + + template + inline int push_reference(lua_State* L, T&& t, Args&&... args) { + typedef meta::all< + std::is_lvalue_reference, + meta::neg>, + meta::neg> + > use_reference_tag; + return pusher>>{}.push(L, std::forward(t), std::forward(args)...); + } + + inline int multi_push(lua_State*) { + // do nothing + return 0; + } + + template + inline int multi_push(lua_State* L, T&& t, Args&&... args) { + int pushcount = push(L, std::forward(t)); + void(sol::detail::swallow{ (pushcount += sol::stack::push(L, std::forward(args)), 0)... }); + return pushcount; + } + + inline int multi_push_reference(lua_State*) { + // do nothing + return 0; + } + + template + inline int multi_push_reference(lua_State* L, T&& t, Args&&... args) { + int pushcount = push_reference(L, std::forward(t)); + void(sol::detail::swallow{ (pushcount += sol::stack::push_reference(L, std::forward(args)), 0)... }); + return pushcount; + } + + template + bool check(lua_State* L, int index, Handler&& handler) { + typedef meta::unqualified_t Tu; + checker c; + // VC++ has a bad warning here: shut it up + (void)c; + return c.check(L, index, std::forward(handler)); + } + + template + bool check(lua_State* L, int index = -1) { + auto handler = no_panic; + return check(L, index, handler); + } + + template + inline decltype(auto) check_get(lua_State* L, int index, Handler&& handler) { + return check_getter>{}.get(L, index, std::forward(handler)); + } + + template + inline decltype(auto) check_get(lua_State* L, int index = -1) { + auto handler = no_panic; + return check_get(L, index, handler); + } + + namespace stack_detail { + +#ifdef SOL_CHECK_ARGUMENTS + template + inline auto tagged_get(types, lua_State* L, int index = -1) -> decltype(stack_detail::unchecked_get(L, index)) { + auto op = check_get(L, index, type_panic); + return *op; + } +#else + template + inline decltype(auto) tagged_get(types, lua_State* L, int index = -1) { + return stack_detail::unchecked_get(L, index); + } +#endif + + template + inline decltype(auto) tagged_get(types>, lua_State* L, int index = -1) { + return stack_detail::unchecked_get>(L, index); + } + + template + inline int alloc_destroy(lua_State* L) { + void* rawdata = lua_touserdata(L, up_value_index(1)); + T* data = static_cast(rawdata); + std::allocator alloc; + alloc.destroy(data); + return 0; + } + + } // stack_detail + + template + inline decltype(auto) get(lua_State* L, int index = -1) { + return stack_detail::tagged_get(types(), L, index); + } + + template + inline decltype(auto) pop(lua_State* L) { + return popper>{}.pop(L); + } + + template + void get_field(lua_State* L, Key&& key) { + field_getter, global, raw>{}.get(L, std::forward(key)); + } + + template + void get_field(lua_State* L, Key&& key, int tableindex) { + field_getter, global, raw>{}.get(L, std::forward(key), tableindex); + } + + template + void raw_get_field(lua_State* L, Key&& key) { + get_field(L, std::forward(key)); + } + + template + void raw_get_field(lua_State* L, Key&& key, int tableindex) { + get_field(L, std::forward(key), tableindex); + } + + template + probe probe_get_field(lua_State* L, Key&& key) { + return probe_field_getter, global, raw>{}.get(L, std::forward(key)); + } + + template + probe probe_get_field(lua_State* L, Key&& key, int tableindex) { + return probe_field_getter, global, raw>{}.get(L, std::forward(key), tableindex); + } + + template + probe probe_raw_get_field(lua_State* L, Key&& key) { + return probe_get_field(L, std::forward(key)); + } + + template + probe probe_raw_get_field(lua_State* L, Key&& key, int tableindex) { + return probe_get_field(L, std::forward(key), tableindex); + } + + template + void set_field(lua_State* L, Key&& key, Value&& value) { + field_setter, global, raw>{}.set(L, std::forward(key), std::forward(value)); + } + + template + void set_field(lua_State* L, Key&& key, Value&& value, int tableindex) { + field_setter, global, raw>{}.set(L, std::forward(key), std::forward(value), tableindex); + } + + template + void raw_set_field(lua_State* L, Key&& key, Value&& value) { + set_field(L, std::forward(key), std::forward(value)); + } + + template + void raw_set_field(lua_State* L, Key&& key, Value&& value, int tableindex) { + set_field(L, std::forward(key), std::forward(value), tableindex); + } + } // stack +} // sol + +// end of sol\stack_core.hpp + +// beginning of sol\stack_check.hpp + +// beginning of sol\usertype_traits.hpp + +// beginning of sol\demangle.hpp + +#include +#include + +#if defined(__GNUC__) || defined(__clang__) +#include +#endif + +namespace sol { + namespace detail { +#ifdef _MSC_VER +#ifndef SOL_NO_RTTI + inline std::string get_type_name(const std::type_info& id) { + std::string realname = id.name(); + const static std::array removals = { { "struct ", "class " } }; + for (std::size_t r = 0; r < removals.size(); ++r) { + auto found = realname.find(removals[r]); + while (found != std::string::npos) { + realname.erase(found, removals[r].size()); + found = realname.find(removals[r]); + } + } + return realname; + } +#endif // No RTII + template + inline std::string ctti_get_type_name() { + std::string name = __FUNCSIG__; + std::size_t start = name.find("get_type_name"); + if (start == std::string::npos) + start = 0; + else + start += 13; + if (start < name.size() - 1) + start += 1; + std::size_t end = name.find_last_of('>'); + if (end == std::string::npos) + end = name.size(); + name = name.substr(start, end - start); + if (name.find("struct", 0) == 0) + name.replace(0, 6, "", 0); + if (name.find("class", 0) == 0) + name.replace(0, 5, "", 0); + while (!name.empty() && std::isblank(name.front())) name.erase(name.begin(), ++name.begin()); + while (!name.empty() && std::isblank(name.back())) name.erase(--name.end(), name.end()); + return name; + } +#elif defined(__GNUC__) || defined(__clang__) + template + inline std::string ctti_get_type_name() { + std::string name = __PRETTY_FUNCTION__; + std::size_t start = name.find_last_of('['); + start = name.find_first_of('=', start); + std::size_t end = name.find_last_of(']'); + if (end == std::string::npos) + end = name.size(); + if (start == std::string::npos) + start = 0; + if (start < name.size() - 1) + start += 1; + name = name.substr(start, end - start); + start = name.find(";"); + if (start != std::string::npos) { + name.erase(start, name.length()); + } + while (!name.empty() && std::isblank(name.front())) name.erase(name.begin(), ++name.begin()); + while (!name.empty() && std::isblank(name.back())) name.erase(--name.end(), name.end()); + return name; + } +#ifndef SOL_NO_RTTI +#if defined(__clang__) + inline std::string get_type_name(const std::type_info& id) { + int status; + char* unmangled = abi::__cxa_demangle(id.name(), 0, 0, &status); + std::string realname = unmangled; + std::free(unmangled); + return realname; + } +#elif defined(__GNUC__) + inline std::string get_type_name(const std::type_info& id) { + int status; + char* unmangled = abi::__cxa_demangle(id.name(), 0, 0, &status); + std::string realname = unmangled; + std::free(unmangled); + return realname; + } +#endif // g++ || clang++ +#endif // No RTII +#else +#error Compiler not supported for demangling +#endif // compilers + + template + inline std::string demangle_once() { +#ifndef SOL_NO_RTTI + std::string realname = get_type_name(typeid(T)); +#else + std::string realname = ctti_get_type_name(); +#endif // No Runtime Type Information + return realname; + } + + template + inline std::string short_demangle_once() { + std::string realname = ctti_get_type_name(); + std::size_t idx = realname.find_last_of(":`'\"{}[]|-)(*^&!@#$%`~", std::string::npos, 23); + if (idx != std::string::npos) { + realname.erase(0, realname.length() < idx ? realname.length() : idx + 1); + } + return realname; + } + + template + inline std::string demangle() { + static const std::string d = demangle_once(); + return d; + } + + template + inline std::string short_demangle() { + static const std::string d = short_demangle_once(); + return d; + } + } // detail +} // sol + +// end of sol\demangle.hpp + +namespace sol { + + template + struct usertype_traits { + static const std::string name; + static const std::string qualified_name; + static const std::string metatable; + static const std::string variable_metatable; + static const std::string gc_table; + }; + + template + const std::string usertype_traits::name = detail::short_demangle(); + + template + const std::string usertype_traits::qualified_name = detail::demangle(); + + template + const std::string usertype_traits::metatable = std::string("sol.").append(detail::demangle()); + + template + const std::string usertype_traits::variable_metatable = std::string("sol.").append(detail::demangle()).append(".variables"); + + template + const std::string usertype_traits::gc_table = std::string("sol.").append(detail::demangle().append(".\xE2\x99\xBB")); + +} + +// end of sol\usertype_traits.hpp + +// beginning of sol\inheritance.hpp + +#if defined(SOL_NO_RTTI) && defined(SOL_NO_EXCEPTIONS) +#include +#endif // No Runtime Type Information and No Exceptions + +namespace sol { + template + struct base_list { }; + template + using bases = base_list; + + typedef bases<> base_classes_tag; + const auto base_classes = base_classes_tag(); + + namespace detail { + + template + struct has_derived { + static bool value; + }; + + template + bool has_derived::value = false; + +#if defined(SOL_NO_RTTI) && defined(SOL_NO_EXCEPTIONS) + inline std::size_t unique_id() { + static std::atomic x(0); + return ++x; + } + + template + struct id_for { + static const std::size_t value; + }; + + template + const std::size_t id_for::value = unique_id(); +#endif // No Runtime Type Information / No Exceptions + + inline decltype(auto) base_class_check_key() { + static const auto& key = u8"♡o。.(✿ฺ。 ✿ฺ)"; + return key; + } + + inline decltype(auto) base_class_cast_key() { + static const auto& key = u8"(◕‿◕✿)"; + return key; + } + +#ifndef SOL_NO_EXCEPTIONS + + template + void throw_as(void* p) { + throw static_cast(p); + } + + using throw_cast = decltype(&throw_as); + + template + inline T* catch_cast(void* p, throw_cast f) { + try { + f(static_cast(p)); + } + catch (T* ptr) { + return ptr; + } + catch (...) { + return static_cast(p); + } + return static_cast(p); + } + + template + inline bool catch_check(throw_cast f) { + try { + f(nullptr); + } + catch (T*) { + return true; + } + catch (...) { + return false; + } + return false; + } + +#elif !defined(SOL_NO_RTTI) + template + struct inheritance { + static bool type_check(types<>, const std::type_info&) { + return false; + } + + template + static bool type_check(types, const std::type_info& ti) { + return ti != typeid(Base) || type_check(types(), ti); + } + + static bool type_check(const std::type_info& ti) { + return ti != typeid(T) || type_check(types(), ti); + } + + static void* type_cast(types<>, T*, const std::type_info& ti) { + return nullptr; + } + + template + static void* type_cast(types, T* data, const std::type_info& ti) { + // Make sure to convert to T first, and then dynamic cast to the proper type + return ti != typeid(Base) ? type_cast(types(), data, ti) : static_cast(dynamic_cast(static_cast(data))); + } + + static void* type_cast(void* voiddata, const std::type_info& ti) { + T* data = static_cast(voiddata); + return static_cast(ti != typeid(T) ? type_cast(types(), data, ti) : data); + } + }; + + using inheritance_check_function = decltype(&inheritance::type_check); + using inheritance_cast_function = decltype(&inheritance::type_cast); +#else + template + struct inheritance { + static bool type_check(types<>, std::size_t) { + return false; + } + + template + static bool type_check(types, std::size_t ti) { + return ti != id_for::value || type_check(types(), ti); + } + + static bool type_check(std::size_t ti) { + return ti != id_for::value || type_check(types(), ti); + } + + static void* type_cast(types<>, T*, std::size_t) { + return nullptr; + } + + template + static void* type_cast(types, T* data, std::size_t ti) { + // Make sure to convert to T first, and then dynamic cast to the proper type + return ti != id_for::value ? type_cast(types(), data, ti) : static_cast(static_cast(data)); + } + + static void* type_cast(void* voiddata, std::size_t ti) { + T* data = static_cast(voiddata); + return static_cast(ti != id_for::value ? type_cast(types(), data, ti) : data); + } + }; + + using inheritance_check_function = decltype(&inheritance::type_check); + using inheritance_cast_function = decltype(&inheritance::type_cast); +#endif // No Exceptions and/or No Runtime Type Information + + } // detail +} // sol + +// end of sol\inheritance.hpp + +#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::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 { + template + static bool check(lua_State* L, int index, Handler&& handler) { + 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> : 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) { + type t = type_of(L, index); + if (t == type::nil || t == type::none || t == type::function) { + // allow for nil to be returned + 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(types, lua_State* L, type indextype, int index, Handler&& handler) { + 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; + } + if (stack_detail::check_metatable(L)) + return true; + if (stack_detail::check_metatable(L)) + return true; + if (stack_detail::check_metatable>(L)) + return true; + bool success = false; +#ifndef SOL_NO_EXCEPTIONS + lua_getfield(L, -1, &detail::base_class_check_key()[0]); + if (type_of(L, -1) != type::nil) { + void* basecastdata = lua_touserdata(L, -1); + detail::throw_cast basecast = (detail::throw_cast)basecastdata; + success = detail::catch_check(basecast); + } +#elif !defined(SOL_NO_RTTI) + lua_getfield(L, -1, &detail::base_class_check_key()[0]); + if (type_of(L, -1) != type::nil) { + void* basecastdata = lua_touserdata(L, -1); + detail::inheritance_check_function ic = (detail::inheritance_check_function)basecastdata; + success = ic(typeid(T)); + } +#else + // Topkek + lua_getfield(L, -1, &detail::base_class_check_key()[0]); + if (type_of(L, -1) != type::nil) { + void* basecastdata = lua_touserdata(L, -1); + detail::inheritance_check_function ic = (detail::inheritance_check_function)basecastdata; + 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 + +// end of sol\stack_check.hpp + +// beginning of sol\stack_get.hpp + +// beginning of sol\overload.hpp + +namespace sol { + template + struct overload_set { + std::tuple set; + template >> = meta::enabler> + overload_set (Arg&& arg, Args&&... args) : set(std::forward(arg), std::forward(args)...) {} + overload_set(const overload_set&) = default; + overload_set(overload_set&&) = default; + overload_set& operator=(const overload_set&) = default; + overload_set& operator=(overload_set&&) = default; + }; + + template + decltype(auto) overload(Args&&... args) { + return overload_set(std::forward(args)...); + } +} + +// end of sol\overload.hpp + +namespace sol { + namespace stack { + + template + struct getter { + static T& get(lua_State* L, int index = -1) { + return getter{}.get(L, index); + } + }; + + template + struct getter::value>> { + static T get(lua_State* L, int index = -1) { + return static_cast(lua_tonumber(L, index)); + } + }; + + template + struct getter, std::is_signed>::value>> { + static T get(lua_State* L, int index = -1) { + return static_cast(lua_tointeger(L, index)); + } + }; + + template + struct getter, std::is_unsigned>::value>> { + static T get(lua_State* L, int index = -1) { + return static_cast(lua_tointeger(L, index)); + } + }; + + template + struct getter::value>> { + static T get(lua_State* L, int index = -1) { + return static_cast(lua_tointegerx(L, index, nullptr)); + } + }; + + template + struct getter::value || std::is_base_of::value>> { + static T get(lua_State* L, int index = -1) { + return T(L, index); + } + }; + + template<> + struct getter { + static userdata_value get(lua_State* L, int index = -1) { + return userdata_value(lua_touserdata(L, index)); + } + }; + + template<> + struct getter { + static lightuserdata_value get(lua_State* L, int index = -1) { + return lightuserdata_value(lua_touserdata(L, index)); + } + }; + + template + struct getter> { + static light get(lua_State* L, int index = -1) { + return light(static_cast(lua_touserdata(L, index))); + } + }; + + template + struct getter> { + static T& get(lua_State* L, int index = -1) { + return *static_cast(lua_touserdata(L, index)); + } + }; + + template + struct getter> { + static T* get(lua_State* L, int index = -1) { + return static_cast(lua_touserdata(L, index)); + } + }; + + template<> + struct getter { + static type get(lua_State *L, int index) { + return static_cast(lua_type(L, index)); + } + }; + + template<> + struct getter { + static bool get(lua_State* L, int index) { + return lua_toboolean(L, index) != 0; + } + }; + + template<> + struct getter { + static std::string get(lua_State* L, int index = -1) { + std::size_t len; + auto str = lua_tolstring(L, index, &len); + return{ str, len }; + } + }; + + template <> + struct getter { + string_detail::string_shim get(lua_State* L, int index) { + size_t len; + const char* p = lua_tolstring(L, index, &len); + return string_detail::string_shim(p, len); + } + }; + + template<> + struct getter { + static const char* get(lua_State* L, int index = -1) { + return lua_tostring(L, index); + } + }; + + template<> + struct getter { + static meta_function get(lua_State *L, int index) { + const char* name = getter{}.get(L, index); + for (std::size_t i = 0; i < meta_function_names.size(); ++i) + if (meta_function_names[i] == name) + return static_cast(i); + return meta_function::construct; + } + }; + + template<> + struct getter { + static char get(lua_State* L, int index = -1) { + size_t len; + auto str = lua_tolstring(L, index, &len); + return len > 0 ? str[0] : '\0'; + } + }; + +#if 0 + + template<> + struct getter { + static std::wstring get(lua_State* L, int index = -1) { + return{}; + } + }; + + template<> + struct getter { + static std::u16string get(lua_State* L, int index = -1) { + return{}; + } + }; + + template<> + struct getter { + static std::u32string get(lua_State* L, int index = -1) { + return{}; + } + }; + + template<> + struct getter { + static wchar_t get(lua_State* L, int index = -1) { + auto str = getter{}.get(L, index); + return str.size() > 0 ? str[0] : '\0'; + } + }; + + template<> + struct getter { + static char get(lua_State* L, int index = -1) { + auto str = getter{}.get(L, index); + return str.size() > 0 ? str[0] : '\0'; + } + }; + + template<> + struct getter { + static char32_t get(lua_State* L, int index = -1) { + auto str = getter{}.get(L, index); + return str.size() > 0 ? str[0] : '\0'; + } + }; + +#endif // For a distant future + + template<> + struct getter { + static nil_t get(lua_State*, int = -1) { + return nil; + } + }; + + template<> + struct pusher { + static std::nullptr_t get(lua_State*, int = -1) { + return nullptr; + } + }; + + template<> + struct getter { + static nullopt_t get(lua_State*, int = -1) { + return nullopt; + } + }; + + template<> + struct getter { + static this_state get(lua_State* L, int = -1) { + return this_state{ L }; + } + }; + + template<> + struct getter { + static lua_CFunction get(lua_State* L, int index = -1) { + return lua_tocfunction(L, index); + } + }; + + template<> + struct getter { + static c_closure get(lua_State* L, int index = -1) { + return c_closure(lua_tocfunction(L, index), -1); + } + }; + + template<> + struct getter { + static error get(lua_State* L, int index = -1) { + size_t sz = 0; + const char* err = lua_tolstring(L, index, &sz); + if (err == nullptr) { + return error(detail::direct_error, ""); + } + return error(detail::direct_error, std::string(err, sz)); + } + }; + + template<> + struct getter { + static void* get(lua_State* L, int index = -1) { + return lua_touserdata(L, index); + } + }; + + template + struct getter { + static T* get_no_nil(lua_State* L, int index = -1) { + void** pudata = static_cast(lua_touserdata(L, index)); + void* udata = *pudata; + return get_no_nil_from(L, udata, index); + } + + static T* get_no_nil_from(lua_State* L, void* udata, int index = -1) { +#ifndef SOL_NO_EXCEPTIONS + if (detail::has_derived::value && luaL_getmetafield(L, index, &detail::base_class_check_key()[0]) != 0) { + void* basecastdata = lua_touserdata(L, -1); + detail::throw_cast basecast = (detail::throw_cast)basecastdata; + // use the casting function to properly adjust the pointer for the desired T + udata = detail::catch_cast(udata, basecast); + lua_pop(L, 1); + } +#elif !defined(SOL_NO_RTTI) + if (detail::has_derived::value && luaL_getmetafield(L, index, &detail::base_class_cast_key()[0]) != 0) { + void* basecastdata = lua_touserdata(L, -1); + detail::inheritance_cast_function ic = (detail::inheritance_cast_function)basecastdata; + // use the casting function to properly adjust the pointer for the desired T + udata = ic(udata, typeid(T)); + lua_pop(L, 1); + } +#else + // Lol, you motherfucker + if (detail::has_derived::value && luaL_getmetafield(L, index, &detail::base_class_cast_key()[0]) != 0) { + void* basecastdata = lua_touserdata(L, -1); + detail::inheritance_cast_function ic = (detail::inheritance_cast_function)basecastdata; + // use the casting function to properly adjust the pointer for the desired T + udata = ic(udata, detail::id_for::value); + lua_pop(L, 1); + } +#endif // No Runtime Type Information || Exceptions + T* obj = static_cast(udata); + return obj; + } + + static T* get(lua_State* L, int index = -1) { + type t = type_of(L, index); + if (t == type::nil) + return nullptr; + return get_no_nil(L, index); + } + }; + + template + struct getter> { + static T* get(lua_State* L, int index = -1) { + return getter::get_no_nil(L, index); + } + }; + + template + struct getter { + static T& get(lua_State* L, int index = -1) { + return *getter::get_no_nil(L, index); + } + }; + + template + struct getter::value>> { + typedef typename unique_usertype_traits::type P; + typedef typename unique_usertype_traits::actual_type Real; + + static Real& get(lua_State* L, int index = -1) { + P** pref = static_cast(lua_touserdata(L, index)); + detail::special_destruct_func* fx = static_cast(static_cast(pref + 1)); + Real* mem = static_cast(static_cast(fx + 1)); + return *mem; + } + }; + + template + struct getter> { + static T& get(lua_State* L, int index = -1) { + return getter{}.get(L, index); + } + }; + + template + struct getter> { + template + static decltype(auto) apply(std::index_sequence, lua_State* L, int index = -1) { + index = index < 0 ? lua_absindex(L, index) - (sizeof...(I)-1) : index; + return std::tuple(L, index + I))...>(stack::get(L, index + I)...); + } + + static decltype(auto) get(lua_State* L, int index = -1) { + return apply(std::make_index_sequence(), L, index); + } + }; + + template + struct getter> { + static decltype(auto) get(lua_State* L, int index = -1) { + index = index < 0 ? lua_absindex(L, index) - 1 : index; + return std::pair(L, index)), decltype(stack::get(L, index))>(stack::get(L, index), stack::get(L, index + 1)); + } + }; + + } // stack +} // sol + +// end of sol\stack_get.hpp + +// beginning of sol\stack_check_get.hpp + +namespace sol { + namespace stack { + template + struct check_getter { + typedef stack_detail::strip_t U; + typedef std::conditional_t::value, U, U&> R; + + template + static optional get(lua_State* L, int index, Handler&& handler) { + if (!check(L, index, std::forward(handler))) + return nullopt; + return stack_detail::unchecked_get(L, index); + } + }; + + template + struct check_getter> { + template + static decltype(auto) get(lua_State* L, int index, Handler&&) { + return check_get(L, index, no_panic); + } + }; + + template + struct check_getter::value && lua_type_of::value == type::number>> { + template + static optional get(lua_State* L, int index, Handler&& handler) { + int isnum = 0; + lua_Integer value = lua_tointegerx(L, index, &isnum); + if (isnum == 0) { + handler(L, index, type::number, type_of(L, index)); + return nullopt; + } + return static_cast(value); + } + }; + + template + struct check_getter::value && !meta::any_same::value>> { + template + static optional get(lua_State* L, int index, Handler&& handler) { + int isnum = 0; + lua_Integer value = lua_tointegerx(L, index, &isnum); + if (isnum == 0) { + handler(L, index, type::number, type_of(L, index)); + return nullopt; + } + return static_cast(value); + } + }; + + template + struct check_getter::value>> { + template + static optional get(lua_State* L, int index, Handler&& handler) { + int isnum = 0; + lua_Number value = lua_tonumberx(L, index, &isnum); + if (isnum == 0) { + handler(L, index, type::number, type_of(L, index)); + return nullopt; + } + return static_cast(value); + } + }; + + template + struct getter> { + static decltype(auto) get(lua_State* L, int index) { + return check_get(L, index); + } + }; + } // stack +} // sol + +// end of sol\stack_check_get.hpp + +// beginning of sol\stack_push.hpp + +// beginning of sol\raii.hpp + +namespace sol { + namespace detail { + struct default_construct { + template + static void construct(T&& obj, Args&&... args) { + std::allocator> alloc{}; + alloc.construct(obj, std::forward(args)...); + } + + template + void operator()(T&& obj, Args&&... args) const { + construct(std::forward(obj), std::forward(args)...); + } + }; + + struct default_destruct { + template + static void destroy(T&& obj) { + std::allocator> alloc{}; + alloc.destroy(obj); + } + + template + void operator()(T&& obj) const { + destroy(std::forward(obj)); + } + }; + } // detail + + template + struct constructor_list {}; + + template + using constructors = constructor_list; + + const auto default_constructor = constructors>{}; + + struct no_construction {}; + const auto no_constructor = no_construction{}; + + struct call_construction {}; + const auto call_constructor = call_construction{}; + + template + struct constructor_wrapper { + std::tuple set; + template + constructor_wrapper(Args&&... args) : set(std::forward(args)...) {} + }; + + template + inline auto initializers(Functions&&... functions) { + return constructor_wrapper...>(std::forward(functions)...); + } + + template + struct destructor_wrapper { + Function fx; + destructor_wrapper(Function f) : fx(std::move(f)) {} + }; + + template <> + struct destructor_wrapper {}; + + const destructor_wrapper default_destructor{}; + + template + inline auto destructor(Fx&& fx) { + return destructor_wrapper>(std::forward(fx)); + } + +} // sol + +// end of sol\raii.hpp + +namespace sol { + namespace stack { + template + struct pusher { + template + static int push_keyed(lua_State* L, K&& k, Args&&... args) { + // Basically, we store all user-data like this: + // If it's a movable/copyable value (no std::ref(x)), then we store the pointer to the new + // data in the first sizeof(T*) bytes, and then however many bytes it takes to + // do the actual object. Things that are std::ref or plain T* are stored as + // just the sizeof(T*), and nothing else. + T** pointerpointer = static_cast(lua_newuserdata(L, sizeof(T*) + sizeof(T))); + T*& referencereference = *pointerpointer; + T* allocationtarget = reinterpret_cast(pointerpointer + 1); + referencereference = allocationtarget; + std::allocator alloc{}; + alloc.construct(allocationtarget, std::forward(args)...); + luaL_newmetatable(L, &k[0]); + lua_setmetatable(L, -2); + return 1; + } + + template + static int push(lua_State* L, Args&&... args) { + return push_keyed(L, usertype_traits::metatable, std::forward(args)...); + } + }; + + template + struct pusher { + template + static int push_keyed(lua_State* L, K&& k, T* obj) { + if (obj == nullptr) + return stack::push(L, nil); + T** pref = static_cast(lua_newuserdata(L, sizeof(T*))); + *pref = obj; + luaL_getmetatable(L, &k[0]); + lua_setmetatable(L, -2); + return 1; + } + + static int push(lua_State* L, T* obj) { + return push_keyed(L, usertype_traits::metatable, obj); + } + }; + + template <> + struct pusher { + template + static int push(lua_State* L, T&& obj) { + return stack::push(L, detail::ptr(obj)); + } + }; + + template + struct pusher::value>> { + typedef typename unique_usertype_traits::type P; + typedef typename unique_usertype_traits::actual_type Real; + + template >> = meta::enabler> + static int push(lua_State* L, Arg&& arg) { + if (unique_usertype_traits::is_null(arg)) + return stack::push(L, nil); + return push_deep(L, std::forward(arg)); + } + + template + static int push(lua_State* L, Arg0&& arg0, Arg0&& arg1, Args&&... args) { + return push_deep(L, std::forward(arg0), std::forward(arg1), std::forward(args)...); + } + + template + static int push_deep(lua_State* L, Args&&... args) { + P** pref = static_cast(lua_newuserdata(L, sizeof(P*) + sizeof(detail::special_destruct_func) + sizeof(Real))); + detail::special_destruct_func* fx = static_cast(static_cast(pref + 1)); + Real* mem = static_cast(static_cast(fx + 1)); + *fx = detail::special_destruct; + detail::default_construct::construct(mem, std::forward(args)...); + *pref = unique_usertype_traits::get(*mem); + if (luaL_newmetatable(L, &usertype_traits>::metatable[0]) == 1) { + set_field(L, "__gc", detail::unique_destruct

); + } + lua_setmetatable(L, -2); + return 1; + } + }; + + template + struct pusher> { + static int push(lua_State* L, const std::reference_wrapper& t) { + return stack::push(L, std::addressof(detail::deref(t.get()))); + } + }; + + template + struct pusher::value>> { + static int push(lua_State* L, const T& value) { + lua_pushnumber(L, value); + return 1; + } + }; + + template + struct pusher, std::is_signed>::value>> { + static int push(lua_State* L, const T& value) { + lua_pushinteger(L, static_cast(value)); + return 1; + } + }; + + template + struct pusher::value>> { + static int push(lua_State* L, const T& value) { + if (std::is_same::value) { + return stack::push(L, static_cast(value)); + } + return stack::push(L, static_cast>(value)); + } + }; + + template + struct pusher, std::is_unsigned>::value>> { + static int push(lua_State* L, const T& value) { + lua_pushinteger(L, static_cast(value)); + return 1; + } + }; + + template + struct pusher, meta::neg>, meta::neg, std::is_base_of>>>::value>> { + static int push(lua_State* L, const T& cont) { + lua_createtable(L, static_cast(cont.size()), 0); + int tableindex = lua_gettop(L); + unsigned index = 1; + for (auto&& i : cont) { + set_field(L, index++, i, tableindex); + } + return 1; + } + }; + + template + struct pusher, meta::has_key_value_pair, meta::neg, std::is_base_of>>>::value>> { + static int push(lua_State* L, const T& cont) { + lua_createtable(L, static_cast(cont.size()), 0); + int tableindex = lua_gettop(L); + for (auto&& pair : cont) { + set_field(L, pair.first, pair.second, tableindex); + } + return 1; + } + }; + + template + struct pusher::value || std::is_base_of::value>> { + static int push(lua_State*, T& ref) { + return ref.push(); + } + + static int push(lua_State*, T&& ref) { + return ref.push(); + } + }; + + template<> + struct pusher { + static int push(lua_State* L, bool b) { + lua_pushboolean(L, b); + return 1; + } + }; + + template<> + struct pusher { + static int push(lua_State* L, nil_t) { + lua_pushnil(L); + return 1; + } + }; + + template<> + struct pusher { + static int push(lua_State* L, metatable_key_t) { + lua_pushlstring(L, "__mt", 4); + return 1; + } + }; + + template<> + struct pusher> { + static int push(lua_State* L, lua_CFunction func, int n = 0) { + lua_pushcclosure(L, func, n); + return 1; + } + }; + + template<> + struct pusher { + static int push(lua_State* L, lua_CFunction func, int n = 0) { + lua_pushcclosure(L, func, n); + return 1; + } + }; + + template<> + struct pusher { + static int push(lua_State* L, c_closure cc) { + lua_pushcclosure(L, cc.c_function, cc.upvalues); + return 1; + } + }; + + template + struct pusher> { + template + static int push(std::index_sequence, lua_State* L, T&& c) { + int pushcount = multi_push(L, detail::forward_get(c.upvalues)...); + return stack::push(L, c_closure(c.c_function, pushcount)); + } + + template + static int push(lua_State* L, T&& c) { + return push(std::make_index_sequence<1 + sizeof...(Args)>(), L, std::forward(c)); + } + }; + + template<> + struct pusher { + static int push(lua_State* L, void* userdata) { + lua_pushlightuserdata(L, userdata); + return 1; + } + }; + + template<> + struct pusher { + static int push(lua_State* L, lightuserdata_value userdata) { + lua_pushlightuserdata(L, userdata); + return 1; + } + }; + + template + struct pusher> { + static int push(lua_State* L, light l) { + lua_pushlightuserdata(L, static_cast(l.value)); + return 1; + } + }; + + template + struct pusher> { + template + static int push_with(lua_State* L, Args&&... args) { + // A dumb pusher + void* rawdata = lua_newuserdata(L, sizeof(T)); + std::allocator alloc; + alloc.construct(static_cast(rawdata), std::forward(args)...); + if (with_meta) { + lua_CFunction cdel = stack_detail::alloc_destroy; + // Make sure we have a plain GC set for this data + lua_createtable(L, 0, 1); + lua_pushlightuserdata(L, rawdata); + lua_pushcclosure(L, cdel, 1); + lua_setfield(L, -2, "__gc"); + lua_setmetatable(L, -2); + } + return 1; + } + + static int push(lua_State* L, const user& u) { + return push_with(L, u.value); + } + + static int push(lua_State* L, user&& u) { + return push_with(L, std::move(u.value)); + } + + static int push(lua_State* L, no_metatable_t, const user& u) { + return push_with(L, u.value); + } + + static int push(lua_State* L, no_metatable_t, user&& u) { + return push_with(L, std::move(u.value)); + } + }; + + template<> + struct pusher { + static int push(lua_State* L, userdata_value data) { + void** ud = static_cast(lua_newuserdata(L, sizeof(void*))); + *ud = data.value; + return 1; + } + }; + + template<> + struct pusher { + static int push(lua_State* L, const char* str) { + lua_pushlstring(L, str, std::char_traits::length(str)); + return 1; + } + }; + + template + struct pusher { + static int push(lua_State* L, const char(&str)[N]) { + lua_pushlstring(L, str, N - 1); + return 1; + } + }; + + template <> + struct pusher { + static int push(lua_State* L, char c) { + const char str[2] = { c, '\0' }; + return stack::push(L, str); + } + }; + + template<> + struct pusher { + static int push(lua_State* L, const std::string& str) { + lua_pushlstring(L, str.c_str(), str.size()); + return 1; + } + }; + + template<> + struct pusher { + static int push(lua_State* L, meta_function m) { + const std::string& str = name_of(m); + lua_pushlstring(L, str.c_str(), str.size()); + return 1; + } + }; + +#if 0 + + template<> + struct pusher { + static int push(lua_State* L, const wchar_t* wstr) { + return push(L, wstr, wstr + std::char_traits::length(wstr)); + } + static int push(lua_State* L, const wchar_t* wstrb, const wchar_t* wstre) { + std::string str{}; + return stack::push(L, str); + } + }; + + template<> + struct pusher { + static int push(lua_State* L, const char16_t* u16str) { + return push(L, u16str, u16str + std::char_traits::length(u16str)); + } + static int push(lua_State* L, const char16_t* u16strb, const char16_t* u16stre) { + std::string str{}; + return stack::push(L, str); + } + }; + + template<> + struct pusher { + static int push(lua_State* L, const char32_t* u32str) { + return push(L, u32str, u32str + std::char_traits::length(u32str)); + } + static int push(lua_State* L, const char32_t* u32strb, const char32_t* u32stre) { + std::string str{}; + return stack::push(L, str); + } + }; + + template + struct pusher { + static int push(lua_State* L, const wchar_t(&str)[N]) { + return stack::push(L, str, str + N - 1); + } + }; + + template + struct pusher { + static int push(lua_State* L, const char16_t(&str)[N]) { + return stack::push(L, str, str + N - 1); + } + }; + + template + struct pusher { + static int push(lua_State* L, const char32_t(&str)[N]) { + return stack::push(L, str, str + N - 1); + } + }; + + template <> + struct pusher { + static int push(lua_State* L, wchar_t c) { + const wchar_t str[2] = { c, '\0' }; + return stack::push(L, str); + } + }; + + template <> + struct pusher { + static int push(lua_State* L, char16_t c) { + const char16_t str[2] = { c, '\0' }; + return stack::push(L, str); + } + }; + + template <> + struct pusher { + static int push(lua_State* L, char32_t c) { + const char32_t str[2] = { c, '\0' }; + return stack::push(L, str); + } + }; + + template<> + struct pusher { + static int push(lua_State* L, const std::wstring& wstr) { + return stack::push(L, wstr.data(), wstr.data() + wstr.size()); + } + }; + + template<> + struct pusher { + static int push(lua_State* L, const std::u16string& u16str) { + return stack::push(L, u16str.data(), u16str.data() + u16str.size()); + } + }; + + template<> + struct pusher { + static int push(lua_State* L, const std::u32string& u32str) { + return stack::push(L, u32str.data(), u32str.data() + u32str.size()); + } + }; + +#endif // Bad conversions + + template + struct pusher> { + template + static int push(std::index_sequence, lua_State* L, T&& t) { + int pushcount = 0; + (void)detail::swallow{ 0, (pushcount += stack::push(L, + detail::forward_get(t) + ), 0)... }; + return pushcount; + } + + template + static int push(lua_State* L, T&& t) { + return push(std::index_sequence_for(), L, std::forward(t)); + } + }; + + template + struct pusher> { + template + static int push(lua_State* L, T&& t) { + int pushcount = stack::push(L, detail::forward_get<0>(t)); + pushcount += stack::push(L, detail::forward_get<1>(t)); + return pushcount; + } + }; + + template + struct pusher> { + template + static int push(lua_State* L, T&& t) { + if (t == nullopt) { + return stack::push(L, nullopt); + } + return stack::push(L, t.value()); + } + }; + + template<> + struct pusher { + static int push(lua_State* L, nullopt_t) { + return stack::push(L, nil); + } + }; + + template<> + struct pusher { + static int push(lua_State*, const this_state&) { + return 0; + } + }; + } // stack +} // sol + +// end of sol\stack_push.hpp + +// beginning of sol\stack_pop.hpp + +namespace sol { + namespace stack { + template + struct popper { + inline static decltype(auto) pop(lua_State* L) { + decltype(auto) r = get(L); + lua_pop(L, 1); + return r; + } + }; + + template + struct popper> { + inline static decltype(auto) pop(lua_State* L) { + decltype(auto) r = get>(L); + lua_pop(L, static_cast(sizeof...(Args))); + return r; + } + }; + + template + struct popper> { + inline static decltype(auto) pop(lua_State* L) { + decltype(auto) r = get>(L); + lua_pop(L, 2); + return r; + } + }; + + template + struct popper>::value>> { + static_assert(meta::neg>>::value, "You cannot pop something that derives from stack_reference: it will not remain on the stack and thusly will go out of scope!"); + }; + } // stack +} // sol + +// end of sol\stack_pop.hpp + +// beginning of sol\stack_field.hpp + +namespace sol { + namespace stack { + template + struct field_getter { + template + void get(lua_State* L, Key&& key, int tableindex = -2) { + push(L, std::forward(key)); + lua_gettable(L, tableindex); + } + }; + + template + struct field_getter { + void get(lua_State* L, metatable_key_t, int tableindex = -1) { + if (lua_getmetatable(L, tableindex) == 0) + push(L, nil); + } + }; + + template + struct field_getter::value>> { + template + void get(lua_State* L, Key&& key, int = -1) { + lua_getglobal(L, &key[0]); + } + }; + + template + struct field_getter::value>> { + template + void get(lua_State* L, Key&& key, int tableindex = -1) { + lua_getfield(L, tableindex, &key[0]); + } + }; + +#if SOL_LUA_VERSION >= 503 + template + struct field_getter::value>> { + template + void get(lua_State* L, Key&& key, int tableindex = -1) { + lua_geti(L, tableindex, static_cast(key)); + } + }; +#endif // Lua 5.3.x + +#if SOL_LUA_VERSION >= 502 + template + struct field_getter { + void get(lua_State* L, void* key, int tableindex = -1) { + lua_rawgetp(L, tableindex, key); + } + }; +#endif // Lua 5.3.x + + template + struct field_getter::value>> { + template + void get(lua_State* L, Key&& key, int tableindex = -1) { + lua_rawgeti(L, tableindex, static_cast(key)); + } + }; + + template + struct field_getter, b, raw, C> { + template + void apply(std::index_sequence<0, I...>, lua_State* L, Keys&& keys, int tableindex) { + get_field(L, detail::forward_get<0>(keys), tableindex); + void(detail::swallow{ (get_field(L, detail::forward_get(keys)), 0)... }); + reference saved(L, -1); + lua_pop(L, static_cast(sizeof...(I))); + saved.push(); + } + + template + void get(lua_State* L, Keys&& keys) { + apply(std::make_index_sequence(), L, std::forward(keys), lua_absindex(L, -1)); + } + + template + void get(lua_State* L, Keys&& keys, int tableindex) { + apply(std::make_index_sequence(), L, std::forward(keys), tableindex); + } + }; + + template + struct field_getter, b, raw, C> { + template + void get(lua_State* L, Keys&& keys, int tableindex) { + get_field(L, detail::forward_get<0>(keys), tableindex); + get_field(L, detail::forward_get<1>(keys)); + reference saved(L, -1); + lua_pop(L, static_cast(2)); + saved.push(); + } + + template + void get(lua_State* L, Keys&& keys) { + get_field(L, detail::forward_get<0>(keys)); + get_field(L, detail::forward_get<1>(keys)); + reference saved(L, -1); + lua_pop(L, static_cast(2)); + saved.push(); + } + }; + + template + struct field_setter { + template + void set(lua_State* L, Key&& key, Value&& value, int tableindex = -3) { + push(L, std::forward(key)); + push(L, std::forward(value)); + lua_settable(L, tableindex); + } + }; + + template + struct field_setter { + template + void set(lua_State* L, Key&& key, Value&& value, int tableindex = -3) { + push(L, std::forward(key)); + push(L, std::forward(value)); + lua_rawset(L, tableindex); + } + }; + + template + struct field_setter { + template + void set(lua_State* L, metatable_key_t, Value&& value, int tableindex = -2) { + push(L, std::forward(value)); + lua_setmetatable(L, tableindex); + } + }; + + template + struct field_setter::value>> { + template + void set(lua_State* L, Key&& key, Value&& value, int = -2) { + push(L, std::forward(value)); + lua_setglobal(L, &key[0]); + } + }; + + template + struct field_setter::value>> { + template + void set(lua_State* L, Key&& key, Value&& value, int tableindex = -2) { + push(L, std::forward(value)); + lua_setfield(L, tableindex, &key[0]); + } + }; + +#if SOL_LUA_VERSION >= 503 + template + struct field_setter::value>> { + template + void set(lua_State* L, Key&& key, Value&& value, int tableindex = -2) { + push(L, std::forward(value)); + lua_seti(L, tableindex, static_cast(key)); + } + }; +#endif // Lua 5.3.x + + template + struct field_setter::value>> { + template + void set(lua_State* L, Key&& key, Value&& value, int tableindex = -2) { + push(L, std::forward(value)); + lua_rawseti(L, tableindex, static_cast(key)); + } + }; + +#if SOL_LUA_VERSION >= 502 + template + struct field_setter { + template + void set(lua_State* L, void* key, Value&& value, int tableindex = -2) { + push(L, std::forward(value)); + lua_rawsetp(L, tableindex, key); + } + }; +#endif // Lua 5.2.x + + template + struct field_setter, b, raw, C> { + template + void apply(std::index_sequence, lua_State* L, Key&& keys, Value&& value, int tableindex) { + I < 1 ? + set_field(L, detail::forward_get(keys), std::forward(value), tableindex) : + set_field(L, detail::forward_get(keys), std::forward(value)); + } + + template + void apply(std::index_sequence, lua_State* L, Keys&& keys, Value&& value, int tableindex) { + I0 < 1 ? get_field(L, detail::forward_get(keys), tableindex) : get_field(L, detail::forward_get(keys), -1); + apply(std::index_sequence(), L, std::forward(keys), std::forward(value), -1); + } + + template + void set(lua_State* L, Keys&& keys, Value&& value, int tableindex = -3) { + apply(std::make_index_sequence(), L, std::forward(keys), std::forward(value), tableindex); + } + }; + + template + struct field_setter, b, raw, C> { + template + void set(lua_State* L, Keys&& keys, Value&& value, int tableindex = -1) { + get_field(L, detail::forward_get<0>(keys), tableindex); + set_field(L, detail::forward_get<1>(keys), std::forward(value)); + } + }; + } // stack +} // sol + +// end of sol\stack_field.hpp + +// beginning of sol\stack_probe.hpp + +namespace sol { + namespace stack { + template + struct probe_field_getter { + template + probe get(lua_State* L, Key&& key, int tableindex = -2) { + if (!b && !maybe_indexable(L, tableindex)) { + return probe(false, 0); + } + get_field(L, std::forward(key), tableindex); + return probe(!check(L), 1); + } + }; + + template + struct probe_field_getter, b, raw, C> { + template + probe get(lua_State* L, Keys&& keys, int tableindex = -2) { + if (!b && !maybe_indexable(L, tableindex)) { + return probe(false, 0); + } + get_field(L, std::get<0>(keys), tableindex); + if (!maybe_indexable(L)) { + return probe(false, 1); + } + get_field(L, std::get<1>(keys), tableindex); + return probe(!check(L), 2); + } + }; + + template + struct probe_field_getter, b, raw, C> { + template + probe apply(std::index_sequence, int sofar, lua_State* L, Keys&& keys, int tableindex) { + get_field < I < 1 && b, raw>(L, std::get(keys), tableindex); + return probe(!check(L), sofar); + } + + template + probe apply(std::index_sequence, int sofar, lua_State* L, Keys&& keys, int tableindex) { + get_field < I < 1 && b, raw>(L, std::get(keys), tableindex); + if (!maybe_indexable(L)) { + return probe(false, sofar); + } + return apply(std::index_sequence(), sofar + 1, L, std::forward(keys), -1); + } + + template + probe get(lua_State* L, Keys&& keys, int tableindex = -2) { + if (!b && !maybe_indexable(L, tableindex)) { + return probe(false, 0); + } + return apply(std::index_sequence_for(), 1, L, std::forward(keys), tableindex); + } + }; + } // stack +} // sol + +// end of sol\stack_probe.hpp + +#include + +namespace sol { + namespace stack { + namespace stack_detail { + template + inline int push_as_upvalues(lua_State* L, T& item) { + typedef std::decay_t TValue; + const static std::size_t itemsize = sizeof(TValue); + const static std::size_t voidsize = sizeof(void*); + const static std::size_t voidsizem1 = voidsize - 1; + const static std::size_t data_t_count = (sizeof(TValue) + voidsizem1) / voidsize; + typedef std::array data_t; + + data_t data{ {} }; + std::memcpy(&data[0], std::addressof(item), itemsize); + int pushcount = 0; + for (auto&& v : data) { + pushcount += push(L, lightuserdata_value(v)); + } + return pushcount; + } + + template + inline std::pair get_as_upvalues(lua_State* L, int index = 1) { + const static std::size_t data_t_count = (sizeof(T) + (sizeof(void*) - 1)) / sizeof(void*); + typedef std::array data_t; + data_t voiddata{ {} }; + for (std::size_t i = 0, d = 0; d < sizeof(T); ++i, d += sizeof(void*)) { + voiddata[i] = get(L, up_value_index(index++)); + } + return std::pair(*reinterpret_cast(static_cast(voiddata.data())), index); + } + + template ::value>> + inline decltype(auto) call(types, types ta, std::index_sequence tai, lua_State* L, int start, Fx&& fx, FxArgs&&... args) { +#ifndef _MSC_VER + static_assert(meta::all...>::value, "One of the arguments being bound is a move-only type, and it is not being taken by reference: this will break your code. Please take a reference and std::move it manually if this was your intention."); +#endif // This compiler make me so fucking sad + check_types{}.check(ta, tai, L, start, type_panic); + return fx(std::forward(args)..., stack_detail::unchecked_get(L, start + I - meta::count_for_to_pack::value)...); + } + + template + inline void call(types, types ta, std::index_sequence tai, lua_State* L, int start, Fx&& fx, FxArgs&&... args) { +#ifndef _MSC_VER + static_assert(meta::all...>::value, "One of the arguments being bound is a move-only type, and it is not being taken by reference: this will break your code. Please take a reference and std::move it manually if this was your intention."); +#endif // This compiler make me so fucking sad + check_types{}.check(ta, tai, L, start, type_panic); + fx(std::forward(args)..., stack_detail::unchecked_get(L, start + I - meta::count_for_to_pack::value)...); + } + } // stack_detail + + template + int set_ref(lua_State* L, T&& arg, int tableindex = -2) { + push(L, std::forward(arg)); + return luaL_ref(L, tableindex); + } + + inline void remove(lua_State* L, int index, int count) { + if (count < 1) + return; + int top = lua_gettop(L); + if (index == -1 || top == index) { + // Slice them right off the top + lua_pop(L, static_cast(count)); + return; + } + + // Remove each item one at a time using stack operations + // Probably slower, maybe, haven't benchmarked, + // but necessary + if (index < 0) { + index = lua_gettop(L) + (index + 1); + } + int last = index + count; + for (int i = index; i < last; ++i) { + lua_remove(L, i); + } + } + + template ::value>> + inline decltype(auto) call(types tr, types ta, lua_State* L, int start, Fx&& fx, FxArgs&&... args) { + typedef std::make_index_sequence args_indices; + return stack_detail::call(tr, ta, args_indices(), L, start, std::forward(fx), std::forward(args)...); + } + + template ::value>> + inline decltype(auto) call(types tr, types ta, lua_State* L, Fx&& fx, FxArgs&&... args) { + return call(tr, ta, L, 1, std::forward(fx), std::forward(args)...); + } + + template + inline void call(types tr, types ta, lua_State* L, int start, Fx&& fx, FxArgs&&... args) { + typedef std::make_index_sequence args_indices; + stack_detail::call(tr, ta, args_indices(), L, start, std::forward(fx), std::forward(args)...); + } + + template + inline void call(types tr, types ta, lua_State* L, Fx&& fx, FxArgs&&... args) { + call(tr, ta, L, 1, std::forward(fx), std::forward(args)...); + } + + template ::value>> + inline decltype(auto) call_from_top(types tr, types ta, lua_State* L, Fx&& fx, FxArgs&&... args) { + return call(tr, ta, L, static_cast(lua_gettop(L) - sizeof...(Args)), std::forward(fx), std::forward(args)...); + } + + template + inline void call_from_top(types tr, types ta, lua_State* L, Fx&& fx, FxArgs&&... args) { + call(tr, ta, L, static_cast(lua_gettop(L) - sizeof...(Args)), std::forward(fx), std::forward(args)...); + } + + template + inline int call_into_lua(types tr, types ta, lua_State* L, int start, Fx&& fx, FxArgs&&... fxargs) { + call(tr, ta, L, start, std::forward(fx), std::forward(fxargs)...); + int nargs = static_cast(sizeof...(Args)) + additionalpop - meta::count_for_pack::value; + lua_pop(L, nargs); + return 0; + } + + template>::value>> + inline int call_into_lua(types, types ta, lua_State* L, int start, Fx&& fx, FxArgs&&... fxargs) { + decltype(auto) r = call(types>(), ta, L, start, std::forward(fx), std::forward(fxargs)...); + int nargs = static_cast(sizeof...(Args)) + additionalpop - meta::count_for_pack::value; + lua_pop(L, nargs); + return push_reference(L, std::forward(r)); + } + + template + inline int call_lua(lua_State* L, int start, Fx&& fx, FxArgs&&... fxargs) { + typedef lua_bind_traits> traits_type; + typedef typename traits_type::args_list args_list; + typedef typename traits_type::returns_list returns_list; + return call_into_lua(returns_list(), args_list(), L, start, std::forward(fx), std::forward(fxargs)...); + } + + inline call_syntax get_call_syntax(lua_State* L, const std::string& key, int index = -2) { + luaL_getmetatable(L, key.c_str()); + if (lua_compare(L, -1, index, LUA_OPEQ) == 1) { + lua_pop(L, 1); + return call_syntax::colon; + } + lua_pop(L, 1); + return call_syntax::dot; + } + + inline void script(lua_State* L, const std::string& code) { + if (luaL_dostring(L, code.c_str())) { + lua_error(L); + } + } + + inline void script_file(lua_State* L, const std::string& filename) { + if (luaL_dofile(L, filename.c_str())) { + lua_error(L); + } + } + + inline void luajit_exception_handler(lua_State* L, int(*handler)(lua_State*, lua_CFunction) = detail::c_trampoline) { +#ifdef SOL_LUAJIT + lua_pushlightuserdata(L, (void*)handler); + luaJIT_setmode(L, -1, LUAJIT_MODE_WRAPCFUNC | LUAJIT_MODE_ON); + lua_pop(L, 1); +#else + (void)L; + (void)handler; +#endif + } + + inline void luajit_exception_off(lua_State* L) { +#ifdef SOL_LUAJIT + luaJIT_setmode(L, -1, LUAJIT_MODE_WRAPCFUNC | LUAJIT_MODE_OFF); +#else + (void)L; +#endif + } + } // stack +} // sol + +// end of sol\stack.hpp + +// beginning of sol\variadic_args.hpp + +// beginning of sol\stack_proxy.hpp + +// beginning of sol\function.hpp + +// beginning of sol\function_result.hpp + +// beginning of sol\proxy_base.hpp + +namespace sol { + template + struct proxy_base { + operator std::string() const { + const Super& super = *static_cast(static_cast(this)); + return super.template get(); + } + + template>, is_proxy_primitive>> = meta::enabler> + operator T () const { + const Super& super = *static_cast(static_cast(this)); + return super.template get(); + } + + template>, meta::neg>>> = meta::enabler> + operator T& () const { + const Super& super = *static_cast(static_cast(this)); + return super.template get(); + } + }; +} // sol + +// end of sol\proxy_base.hpp + +#include + +namespace sol { + struct function_result : public proxy_base { + private: + lua_State* L; + int index; + int returncount; + + public: + function_result() = default; + function_result(lua_State* L, int index = -1, int returncount = 0) : L(L), index(index), returncount(returncount) { + + } + function_result(const function_result&) = default; + function_result& operator=(const function_result&) = default; + function_result(function_result&& o) : L(o.L), index(o.index), returncount(o.returncount) { + // Must be manual, otherwise destructor will screw us + // return count being 0 is enough to keep things clean + // but will be thorough + o.L = nullptr; + o.index = 0; + o.returncount = 0; + } + function_result& operator=(function_result&& o) { + L = o.L; + index = o.index; + returncount = o.returncount; + // Must be manual, otherwise destructor will screw us + // return count being 0 is enough to keep things clean + // but will be thorough + o.L = nullptr; + o.index = 0; + o.returncount = 0; + return *this; + } + + template + decltype(auto) get() const { + return stack::get(L, index); + } + + call_status status() const noexcept { + return call_status::ok; + } + + bool valid() const noexcept { + return status() == call_status::ok || status() == call_status::yielded; + } + + lua_State* lua_state() const { return L; }; + int stack_index() const { return index; }; + + ~function_result() { + lua_pop(L, returncount); + } + }; +} // sol + +// end of sol\function_result.hpp + +// beginning of sol\function_types.hpp + +// beginning of sol\function_types_core.hpp + +// beginning of sol\wrapper.hpp + +namespace sol { + + template + struct wrapper { + typedef lua_bind_traits traits_type; + typedef typename traits_type::args_list args_list; + typedef typename traits_type::args_list free_args_list; + typedef typename traits_type::returns_list returns_list; + + template + static decltype(auto) call(F& f, Args&&... args) { + return f(std::forward(args)...); + } + + struct caller { + template + decltype(auto) operator()(F& fx, Args&&... args) const { + return call(fx, std::forward(args)...); + } + }; + }; + + template + struct wrapper>>::value>> { + typedef lua_bind_traits traits_type; + typedef typename traits_type::args_list args_list; + typedef typename traits_type::args_list free_args_list; + typedef typename traits_type::returns_list returns_list; + + template + static decltype(auto) invoke(Args&&... args) { + return fx(std::forward(args)...); + } + + template + static decltype(auto) call(F& fx, Args&&... args) { + return fx(std::forward(args)...); + } + + struct caller { + template + decltype(auto) operator()(F& fx, Args&&... args) const { + return call(fx, std::forward(args)...); + } + }; + + template + struct invoker { + template + decltype(auto) operator()(Args&&... args) const { + return invoke(std::forward(args)...); + } + }; + }; + + template + struct wrapper>::value>> { + typedef lua_bind_traits traits_type; + typedef typename traits_type::object_type object_type; + typedef typename traits_type::return_type return_type; + typedef typename traits_type::args_list args_list; + typedef types free_args_list; + typedef typename traits_type::returns_list returns_list; + + template + static decltype(auto) invoke(object_type& mem, Args&&... args) { + return (mem.*fx)(std::forward(args)...); + } + + static decltype(auto) call(F& fx, object_type& mem) { + return (mem.*fx); + } + + template + static void call(F& fx, object_type& mem, Arg&& arg, Args&&...) { + (mem.*fx) = std::forward(arg); + } + + struct caller { + template + decltype(auto) operator()(F& fx, object_type& mem, Args&&... args) const { + return call(fx, mem, std::forward(args)...); + } + }; + + template + struct invoker { + template + decltype(auto) operator()(Args&&... args) const { + return invoke(std::forward(args)...); + } + }; + }; + + template + struct member_function_wrapper { + typedef O object_type; + typedef lua_bind_traits traits_type; + typedef typename traits_type::args_list args_list; + typedef types free_args_list; + typedef meta::tuple_types returns_list; + + template + static R invoke(O& mem, Args&&... args) { + return (mem.*fx)(std::forward(args)...); + } + + template + static R call(F& fx, O& mem, Args&&... args) { + return (mem.*fx)(std::forward(args)...); + } + + struct caller { + template + decltype(auto) operator()(F& fx, O& mem, Args&&... args) const { + return call(fx, mem, std::forward(args)...); + } + }; + + template + struct invoker { + template + decltype(auto) operator()(O& mem, Args&&... args) const { + return invoke(mem, std::forward(args)...); + } + }; + }; + + template + struct wrapper : public member_function_wrapper { + + }; + + template + struct wrapper : public member_function_wrapper { + + }; + + template + struct wrapper : public member_function_wrapper { + + }; + + template + struct wrapper : public member_function_wrapper { + + }; + + template + struct wrapper : public member_function_wrapper { + + }; + + template + struct wrapper : public member_function_wrapper { + + }; + + template + struct wrapper : public member_function_wrapper { + + }; + + template + struct wrapper : public member_function_wrapper { + + }; + + template + struct wrapper : public member_function_wrapper { + + }; + + template + struct wrapper : public member_function_wrapper { + + }; + + template + struct wrapper : public member_function_wrapper { + + }; + + template + struct wrapper : public member_function_wrapper { + + }; + + template + struct wrapper : public member_function_wrapper { + + }; + + template + struct wrapper : public member_function_wrapper { + + }; + + template + struct wrapper : public member_function_wrapper { + + }; + +} // sol + +// end of sol\wrapper.hpp + +namespace sol { + namespace function_detail { + inline decltype(auto) cleanup_key() { + const auto& name = u8"sol.ƒ.♲.🗑.(/¯◡ ‿ ◡)/¯ ~ ┻━┻ (ノ◕ヮ◕)ノ*:・゚✧"; + return name; + } + + struct base_function { + virtual int operator()(lua_State* L) { + return luaL_error(L, "sol: failure to call specialized wrapped C++ function from Lua"); + } + + virtual ~base_function() {} + }; + + static int base_call(lua_State* L, void* inheritancedata) { + if (inheritancedata == nullptr) { + return luaL_error(L, "sol: call from Lua to C++ function has null data"); + } + + base_function* pfx = static_cast(inheritancedata); + base_function& fx = *pfx; + return detail::trampoline(L, fx); + } + + static int base_gc(lua_State* L, void* udata) { + if (udata == nullptr) { + return luaL_error(L, "sol: call from lua to C++ gc function with null data"); + } + + base_function* ptr = static_cast(udata); + std::default_delete dx{}; + dx(ptr); + return 0; + } + + template + static void func_gc(std::true_type, lua_State*) { + + } + + template + static void func_gc(std::false_type, lua_State* L) { + for (std::size_t i = 0; i < limit; ++i) { + void* value = stack::get(L, up_value_index(static_cast(i + 1))); + if (value == nullptr) + continue; + base_function* obj = static_cast(value); + std::allocator alloc{}; + alloc.destroy(obj); + alloc.deallocate(obj, 1); + } + } + + inline int call(lua_State* L) { + void* ludata = stack::get(L, up_value_index(1)); + void** pinheritancedata = static_cast(ludata); + return base_call(L, *pinheritancedata); + } + + inline int gc(lua_State* L) { + void* udata = stack::get(L, 1); + void** pudata = static_cast(udata); + return base_gc(L, *pudata); + } + + template + inline int usertype_call(lua_State* L) { + // Zero-based template parameter, but upvalues start at 1 + return base_call(L, stack::get(L, up_value_index(static_cast(I + 1)))); + } + + template + inline int usertype_gc(lua_State* L) { + func_gc(meta::boolean<(I < 1)>(), L); + return 0; + } + + inline void free_function_cleanup(lua_State* L) { + const static char* metatablename = &cleanup_key()[0]; + int metapushed = luaL_newmetatable(L, metatablename); + if (metapushed == 1) { + stack::set_field(L, "__gc", function_detail::gc); + } + } + } // function_detail +} // sol + +// end of sol\function_types_core.hpp + +// beginning of sol\function_types_templated.hpp + +// beginning of sol\call.hpp + +// beginning of sol\property.hpp + +namespace sol { + + struct no_prop { }; + + template + struct property_wrapper { + typedef std::integral_constant::value> can_read; + typedef std::integral_constant::value> can_write; + typedef std::conditional_t Read; + typedef std::conditional_t Write; + Read read; + Write write; + + template + property_wrapper(Rx&& r, Wx&& w) : read(std::forward(r)), write(std::forward(w)) {} + }; + + namespace property_detail { + template + inline decltype(auto) property(std::true_type, R&& read, W&& write) { + return property_wrapper, std::decay_t>(std::forward(read), std::forward(write)); + } + template + inline decltype(auto) property(std::false_type, W&& write, R&& read) { + return property_wrapper, std::decay_t>(std::forward(read), std::forward(write)); + } + template + inline decltype(auto) property(std::true_type, R&& read) { + return property_wrapper, void>(std::forward(read), no_prop()); + } + template + inline decltype(auto) property(std::false_type, W&& write) { + return property_wrapper>(no_prop(), std::forward(write)); + } + } // property_detail + + template + inline decltype(auto) property(F&& f, G&& g) { + using namespace sol; + typedef lua_bind_traits> left_traits; + typedef lua_bind_traits> right_traits; + return property_detail::property(meta::boolean<(left_traits::arity < right_traits::arity)>(), std::forward(f), std::forward(g)); + } + + template + inline decltype(auto) property(F&& f) { + using namespace sol; + typedef lua_bind_traits> left_traits; + return property_detail::property(meta::boolean<(left_traits::arity == 0)>(), std::forward(f)); + } + + // Allow someone to make a member variable readonly (const) + template + auto readonly(R T::* v) { + typedef const R C; + return static_cast(v); + } + +} // sol + +// end of sol\property.hpp + +namespace sol { + namespace call_detail { + + template + inline decltype(auto) pick(std::integral_constant, F&& f) { + return std::forward(f); + } + + template + inline auto& pick(std::true_type, property_wrapper& f) { + return f.read; + } + + template + inline auto& pick(std::false_type, property_wrapper& f) { + return f.write; + } + + template + struct void_call; + + template + struct void_call> { + static void call(Args...) {} + }; + + template + struct constructor_match { + T* obj; + + constructor_match(T* obj) : obj(obj) {} + + template + int operator()(types, index_value, types r, types a, lua_State* L, int, int start) const { + detail::default_construct func{}; + return stack::call_into_lua(r, a, L, start, func, obj); + } + }; + + template + inline int destruct(lua_State* L) { + T* obj = stack::get>(L, 1); + std::allocator alloc{}; + alloc.destroy(obj); + return 0; + } + + namespace overload_detail { + template + inline int overload_match_arity(sol::types<>, std::index_sequence<>, std::index_sequence, Match&&, lua_State* L, int, int, Args&&...) { + return luaL_error(L, "sol: no matching function call takes this number of arguments and the specified types"); + } + + template + inline int overload_match_arity(sol::types, std::index_sequence, std::index_sequence, Match&& matchfx, lua_State* L, int fxarity, int start, Args&&... args) { + typedef lua_bind_traits> traits; + typedef meta::tuple_types return_types; + typedef typename traits::free_args_list args_list; + typedef typename args_list::indices args_indices; + // compile-time eliminate any functions that we know ahead of time are of improper arity + if (meta::find_in_pack_v, index_value...>::value) { + return overload_match_arity(types(), std::index_sequence(), std::index_sequence(), std::forward(matchfx), L, fxarity, start, std::forward(args)...); + } + if (traits::free_arity != fxarity) { + return overload_match_arity(types(), std::index_sequence(), std::index_sequence(), std::forward(matchfx), L, fxarity, start, std::forward(args)...); + } + if (!stack::stack_detail::check_types().check(args_list(), args_indices(), L, start, no_panic)) { + return overload_match_arity(types(), std::index_sequence(), std::index_sequence(), std::forward(matchfx), L, fxarity, start, std::forward(args)...); + } + return matchfx(types(), index_value(), return_types(), args_list(), L, fxarity, start, std::forward(args)...); + } + } // overload_detail + + template + inline int overload_match_arity(Match&& matchfx, lua_State* L, int fxarity, int start, Args&&... args) { + return overload_detail::overload_match_arity(types(), std::make_index_sequence(), std::index_sequence<>(), std::forward(matchfx), L, fxarity, start, std::forward(args)...); + } + + template + inline int overload_match(Match&& matchfx, lua_State* L, int start, Args&&... args) { + int fxarity = lua_gettop(L) - (start - 1); + return overload_match_arity(std::forward(matchfx), L, fxarity, start, std::forward(args)...); + } + + template + inline int construct(Match&& matchfx, lua_State* L, int fxarity, int start, Args&&... args) { + // use same overload resolution matching as all other parts of the framework + return overload_match_arity::call)...>(std::forward(matchfx), L, fxarity, start, std::forward(args)...); + } + + template + inline int construct(lua_State* L) { + static const auto& meta = usertype_traits::metatable; + int argcount = lua_gettop(L); + call_syntax syntax = argcount > 0 ? stack::get_call_syntax(L, meta, 1) : call_syntax::dot; + argcount -= static_cast(syntax); + + T** pointerpointer = reinterpret_cast(lua_newuserdata(L, sizeof(T*) + sizeof(T))); + T*& referencepointer = *pointerpointer; + T* obj = reinterpret_cast(pointerpointer + 1); + referencepointer = obj; + reference userdataref(L, -1); + userdataref.pop(); + + construct(constructor_match(obj), L, argcount, 1 + static_cast(syntax)); + + userdataref.push(); + luaL_getmetatable(L, &meta[0]); + if (type_of(L, -1) == type::nil) { + lua_pop(L, 1); + return luaL_error(L, "sol: unable to get usertype metatable"); + } + + lua_setmetatable(L, -2); + return 1; + } + + template + struct agnostic_lua_call_wrapper { + static int var_call(std::true_type, lua_State* L, F& f) { + typedef wrapper> wrap; + typedef typename wrap::returns_list returns_list; + typedef typename wrap::free_args_list args_list; + typedef typename wrap::caller caller; + return stack::call_into_lua(returns_list(), args_list(), L, is_index ? 2 : 3, caller(), f); + } + + static int var_call(std::false_type, lua_State* L, F& f) { + typedef wrapper> wrap; + typedef typename wrap::free_args_list args_list; + typedef typename wrap::returns_list returns_list; + typedef typename wrap::caller caller; + return stack::call_into_lua(returns_list(), args_list(), L, 1, caller(), f); + } + + static int call(lua_State* L, F& f) { + return var_call(std::integral_constant(), L, f); + } + }; + + template + struct agnostic_lua_call_wrapper { + static int call(lua_State* L, lua_r_CFunction f) { + return f(L); + } + }; + + template + struct agnostic_lua_call_wrapper { + static int call(lua_State* L, lua_CFunction f) { + return f(L); + } + }; + + template + struct agnostic_lua_call_wrapper { + static int call(lua_State* L, no_prop&) { + return luaL_error(L, is_index ? "sol: cannot read from a writeonly property" : "sol: cannot write to a readonly property"); + } + }; + + template + struct agnostic_lua_call_wrapper { + static int call(lua_State* L, no_construction&) { + return luaL_error(L, "sol: cannot call this constructor (tagged as non-constructible)"); + } + }; + + template + struct agnostic_lua_call_wrapper, is_index, is_variable, C> { + static int call(lua_State*, bases&) { + // Uh. How did you even call this, lul + return 0; + } + }; + + template + struct lua_call_wrapper : agnostic_lua_call_wrapper {}; + + template + struct lua_call_wrapper::value>> { + static int call(lua_State* L, F& f) { + typedef wrapper> wrap; + typedef typename wrap::returns_list returns_list; + typedef typename wrap::args_list args_list; + typedef typename wrap::caller caller; + typedef typename wrap::object_type object_type; + +#ifdef SOL_SAFE_USERTYPE + object_type* o = static_cast(stack::get(L, 1)); + if (o == nullptr) { + return luaL_error(L, "sol: received null for 'self' argument (use ':' for accessing member functions, make sure member variables are preceeded by the actual object with '.' syntax)"); + } + return stack::call_into_lua(returns_list(), args_list(), L, is_variable ? 3 : 2, caller(), f, *o); +#else + object_type& o = static_cast(stack::get(L, 1)); + return stack::call_into_lua(returns_list(), args_list(), L, is_variable ? 3 : 2, caller(), f, o); +#endif // Safety + } + }; + + template + struct lua_call_wrapper::value>> { + typedef sol::lua_bind_traits traits_type; + + static int call_assign(std::true_type, lua_State* L, F& f) { + typedef wrapper> wrap; + typedef typename wrap::args_list args_list; + typedef typename wrap::object_type object_type; + typedef typename wrap::caller caller; +#ifdef SOL_SAFE_USERTYPE + object_type* o = static_cast(stack::get(L, 1)); + if (o == nullptr) { + if (is_variable) { + return luaL_error(L, "sol: received nil for 'self' argument (bad '.' access?)"); + } + return luaL_error(L, "sol: received nil for 'self' argument (pass 'self' as first argument)"); + } + return stack::call_into_lua(types(), args_list(), L, is_variable ? 3 : 2, caller(), f, *o); +#else + object_type& o = static_cast(stack::get(L, 1)); + return stack::call_into_lua(types(), args_list(), L, is_variable ? 3 : 2, caller(), f, o); +#endif // Safety + } + + static int call_assign(std::false_type, lua_State* L, F&) { + return luaL_error(L, "sol: cannot write to this variable: copy assignment/constructor not available"); + } + + static int call_const(std::false_type, lua_State* L, F& f) { + typedef typename traits_type::return_type R; + return call_assign(std::is_assignable>, R>(), L, f); + } + + static int call_const(std::true_type, lua_State* L, F&) { + return luaL_error(L, "sol: cannot write to a readonly (const) variable"); + } + + static int call(lua_State* L, F& f) { + return call_const(std::is_const(), L, f); + } + }; + + template + struct lua_call_wrapper::value>> { + typedef sol::lua_bind_traits traits_type; + + static int call(lua_State* L, F& f) { + typedef wrapper> wrap; + typedef typename wrap::object_type object_type; + typedef typename wrap::returns_list returns_list; + typedef typename wrap::caller caller; +#ifdef SOL_SAFE_USERTYPE + object_type* o = static_cast(stack::get(L, 1)); + if (o == nullptr) { + if (is_variable) { + return luaL_error(L, "sol: 'self' argument is nil (bad '.' access?)"); + } + return luaL_error(L, "sol: 'self' argument is nil (pass 'self' as first argument)"); + } + return stack::call_into_lua(returns_list(), types<>(), L, is_variable ? 3 : 2, caller(), f, *o); +#else + object_type& o = static_cast(stack::get(L, 1)); + return stack::call_into_lua(returns_list(), types<>(), L, is_variable ? 3 : 2, caller(), f, o); +#endif // Safety + } + }; + + template + struct lua_call_wrapper, is_index, is_variable, C> { + typedef sol::constructor_list F; + + static int call(lua_State* L, F&) { + const auto& metakey = usertype_traits::metatable; + int argcount = lua_gettop(L); + call_syntax syntax = argcount > 0 ? stack::get_call_syntax(L, metakey, 1) : call_syntax::dot; + argcount -= static_cast(syntax); + + T** pointerpointer = reinterpret_cast(lua_newuserdata(L, sizeof(T*) + sizeof(T))); + reference userdataref(L, -1); + T*& referencepointer = *pointerpointer; + T* obj = reinterpret_cast(pointerpointer + 1); + referencepointer = obj; + + construct(constructor_match(obj), L, argcount, 1 + static_cast(syntax)); + + userdataref.push(); + luaL_getmetatable(L, &metakey[0]); + if (type_of(L, -1) == type::nil) { + lua_pop(L, 1); + return luaL_error(L, "sol: unable to get usertype metatable"); + } + + lua_setmetatable(L, -2); + return 1; + } + }; + + template + struct lua_call_wrapper, is_index, is_variable, C> { + typedef sol::constructor_wrapper F; + + struct onmatch { + template + int operator()(types, index_value, types r, types a, lua_State* L, int, int start, F& f) { + const auto& metakey = usertype_traits::metatable; + T** pointerpointer = reinterpret_cast(lua_newuserdata(L, sizeof(T*) + sizeof(T))); + reference userdataref(L, -1); + T*& referencepointer = *pointerpointer; + T* obj = reinterpret_cast(pointerpointer + 1); + referencepointer = obj; + + auto& func = std::get(f.set); + stack::call_into_lua<1, false>(r, a, L, start, func, detail::implicit_wrapper(obj)); + + userdataref.push(); + luaL_getmetatable(L, &metakey[0]); + if (type_of(L, -1) == type::nil) { + lua_pop(L, 1); + std::string err = "sol: unable to get usertype metatable for "; + err += usertype_traits::name; + return luaL_error(L, err.c_str()); + } + lua_setmetatable(L, -2); + + return 1; + } + }; + + static int call(lua_State* L, F& f) { + call_syntax syntax = stack::get_call_syntax(L, usertype_traits::metatable); + int syntaxval = static_cast(syntax); + int argcount = lua_gettop(L) - syntaxval; + return construct>...>(onmatch(), L, argcount, 1 + syntaxval, f); + } + + }; + + template + struct lua_call_wrapper, is_index, is_variable, std::enable_if_t::value>> { + typedef sol::destructor_wrapper F; + + static int call(lua_State* L, F&) { + return destruct(L); + } + }; + + template + struct lua_call_wrapper, is_index, is_variable, std::enable_if_t::value>> { + typedef sol::destructor_wrapper F; + + static int call(lua_State* L, F& f) { + T* obj = stack::get>(L); + f.fx(detail::implicit_wrapper(obj)); + return 0; + } + }; + + template + struct lua_call_wrapper, is_index, is_variable, C> { + typedef overload_set F; + + struct on_match { + template + int operator()(types, index_value, types, types, lua_State* L, int, int, F& fx) { + auto& f = std::get(fx.set); + return lua_call_wrapper{}.call(L, f); + } + }; + + static int call(lua_State* L, F& fx) { + return overload_match_arity(on_match(), L, lua_gettop(L), 1, fx); + } + }; + + template + int call_wrapped(lua_State* L, Fx&& fx) { + return lua_call_wrapper, is_index, is_variable>{}.call(L, std::forward(fx)); + } + + template + struct is_var_bind : std::false_type {}; + + template + struct is_var_bind::value>> : std::true_type {}; + + template <> + struct is_var_bind : std::true_type {}; + + template + struct is_var_bind> : std::true_type {}; + + } // call_detail + + template + struct is_variable_binding : call_detail::is_var_bind> {}; + + template + struct is_function_binding : meta::neg> {}; + +} // sol + +// end of sol\call.hpp + +namespace sol { + namespace function_detail { + template + inline int call_wrapper_variable(std::false_type, lua_State* L) { + typedef meta::bind_traits> traits_type; + typedef typename traits_type::args_list args_list; + typedef meta::tuple_types return_type; + return stack::call_into_lua(return_type(), args_list(), L, 1, fx); + } + + template + inline int call_set_assignable(std::false_type, T&&, lua_State* L) { + lua_pop(L, 2); + return luaL_error(L, "cannot write to this type: copy assignment/constructor not available"); + } + + template + inline int call_set_assignable(std::true_type, lua_State* L, T&& mem) { + (mem.*variable) = stack::get(L, 2); + lua_pop(L, 2); + return 0; + } + + template + inline int call_set_variable(std::false_type, lua_State* L, T&&) { + lua_pop(L, 2); + return luaL_error(L, "cannot write to a const variable"); + } + + template + inline int call_set_variable(std::true_type, lua_State* L, T&& mem) { + return call_set_assignable(std::is_assignable, R>(), L, std::forward(mem)); + } + + template + inline int call_wrapper_variable(std::true_type, lua_State* L) { + typedef meta::bind_traits> traits_type; + typedef typename traits_type::object_type T; + typedef typename traits_type::return_type R; + auto& mem = stack::get(L, 1); + switch (lua_gettop(L)) { + case 1: { + decltype(auto) r = (mem.*variable); + lua_pop(L, 1); + stack::push_reference(L, std::forward(r)); + return 1; } + case 2: + return call_set_variable(meta::neg>(), L, mem); + default: + return luaL_error(L, "incorrect number of arguments to member variable function call"); + } + } + + template + inline int call_wrapper_function(std::false_type, lua_State* L) { + return call_wrapper_variable(std::is_member_object_pointer(), L); + } + + template + inline int call_wrapper_function(std::true_type, lua_State* L) { + typedef meta::bind_traits> traits_type; + typedef typename traits_type::object_type T; + typedef typename traits_type::args_list args_list; + typedef typename traits_type::return_type return_type; + typedef meta::tuple_types return_type_list; + auto mfx = [&](auto&&... args) -> typename traits_type::return_type { + auto& member = stack::get(L, 1); + return (member.*fx)(std::forward(args)...); + }; + int n = stack::call_into_lua<1>(return_type_list(), args_list(), L, 2, mfx); + return n; + } + + template + int call_wrapper_entry(lua_State* L) { + return call_wrapper_function(std::is_member_function_pointer>(), L); + } + + template + struct c_call_matcher { + template + int operator()(types, index_value, types, types, lua_State* L, int, int) const { + typedef meta::at_in_pack_t target; + return target::call(L); + } + }; + + } // function_detail + + template + inline int c_call(lua_State* L) { +#ifdef __clang__ + return detail::trampoline(L, function_detail::call_wrapper_entry); +#else + return detail::static_trampoline<(&function_detail::call_wrapper_entry)>(L); +#endif // fuck you clang :c + } + + template + struct wrap { + typedef F type; + + static int call(lua_State* L) { + return c_call(L); + } + }; + + template + inline int c_call(lua_State* L) { + if (sizeof...(Fxs) < 2) { + return meta::at_in_pack_t<0, Fxs...>::call(L); + } + else { + return call_detail::overload_match_arity(function_detail::c_call_matcher(), L, lua_gettop(L), 1); + } + } + +} // sol + +// end of sol\function_types_templated.hpp + +// beginning of sol\function_types_basic.hpp + +namespace sol { + namespace function_detail { + template + struct upvalue_free_function { + typedef std::remove_pointer_t> function_type; + typedef lua_bind_traits traits_type; + + static int real_call(lua_State* L) { + auto udata = stack::stack_detail::get_as_upvalues(L); + function_type* fx = udata.first; + int r = stack::call_into_lua(meta::tuple_types(), typename traits_type::args_list(), L, 1, fx); + return r; + } + + static int call(lua_State* L) { + return detail::static_trampoline<(&real_call)>(L); + } + + int operator()(lua_State* L) { + return call(L); + } + }; + + template + struct upvalue_member_function { + typedef std::remove_pointer_t> function_type; + typedef lua_bind_traits traits_type; + + static int real_call(lua_State* L) { + // Layout: + // idx 1...n: verbatim data of member function pointer + // idx n + 1: is the object's void pointer + // We don't need to store the size, because the other side is templated + // with the same member function pointer type + auto memberdata = stack::stack_detail::get_as_upvalues(L, 1); + auto objdata = stack::stack_detail::get_as_upvalues(L, memberdata.second); + function_type& memfx = memberdata.first; + auto& item = *objdata.first; + auto fx = [&item, &memfx](auto&&... args) -> typename traits_type::return_type { + return (item.*memfx)(std::forward(args)...); + }; + return stack::call_into_lua(meta::tuple_types(), typename traits_type::args_list(), L, 1, fx); + } + + static int call(lua_State* L) { + return detail::static_trampoline<(&real_call)>(L); + } + + int operator()(lua_State* L) { + return call(L); + } + }; + + template + int set_assignable(std::false_type, lua_State* L, M&, V&) { + lua_pop(L, N); + return luaL_error(L, "sol: cannot write to this type: copy assignment/constructor not available"); + } + + template + int set_assignable(std::true_type, lua_State* L, M& mem, V& var) { + (mem.*var) = stack::get(L, N); + lua_pop(L, N); + return 0; + } + + template + int set_variable(std::true_type, lua_State* L, M& mem, V& var) { + return set_assignable(std::is_assignable, R>(), L, mem, var); + } + + template + int set_variable(std::false_type, lua_State* L, M&, V&) { + lua_pop(L, N); + return luaL_error(L, "sol: cannot write to a const variable"); + } + + template + struct upvalue_member_variable { + typedef std::remove_pointer_t> function_type; + typedef lua_bind_traits traits_type; + + static int real_call(lua_State* L) { + // Layout: + // idx 1...n: verbatim data of member variable pointer + // idx n + 1: is the object's void pointer + // We don't need to store the size, because the other side is templated + // with the same member function pointer type + auto memberdata = stack::stack_detail::get_as_upvalues(L, 1); + auto objdata = stack::stack_detail::get_as_upvalues(L, memberdata.second); + auto& mem = *objdata.first; + function_type& var = memberdata.first; + switch (lua_gettop(L)) { + case 0: + stack::push(L, (mem.*var)); + return 1; + case 1: + set_variable<1, typename traits_type::return_type>(meta::neg>(), L, mem, var); + return 0; + default: + return luaL_error(L, "sol: incorrect number of arguments to member variable function"); + } + } + + static int call(lua_State* L) { + return detail::static_trampoline<(&real_call)>(L); + } + + int operator()(lua_State* L) { + return call(L); + } + }; + + template + struct upvalue_this_member_function { + typedef std::remove_pointer_t> function_type; + typedef lua_bind_traits traits_type; + + static int real_call(lua_State* L) { + // Layout: + // idx 1...n: verbatim data of member variable pointer + auto memberdata = stack::stack_detail::get_as_upvalues(L, 1); + function_type& memfx = memberdata.first; + auto fx = [&L, &memfx](auto&&... args) -> typename traits_type::return_type { + auto& item = stack::get(L, 1); + return (item.*memfx)(std::forward(args)...); + }; + int n = stack::call_into_lua<1>(meta::tuple_types(), typename traits_type::args_list(), L, 2, fx); + return n; + } + + static int call(lua_State* L) { + return detail::static_trampoline<(&real_call)>(L); + } + + int operator()(lua_State* L) { + return call(L); + } + }; + + template + struct upvalue_this_member_variable { + typedef std::remove_pointer_t> function_type; + typedef lua_bind_traits traits_type; + + static int real_call(lua_State* L) { + // Layout: + // idx 1...n: verbatim data of member variable pointer + auto memberdata = stack::stack_detail::get_as_upvalues(L, 1); + auto& mem = stack::get(L, 1); + function_type& var = memberdata.first; + switch (lua_gettop(L)) { + case 1: + lua_pop(L, 1); + stack::push(L, (mem.*var)); + return 1; + case 2: + set_variable<2, typename traits_type::return_type>(meta::neg>(), L, mem, var); + return 0; + default: + return luaL_error(L, "sol: incorrect number of arguments to member variable function"); + } + } + + static int call(lua_State* L) { + return detail::static_trampoline<(&real_call)>(L); + } + + int operator()(lua_State* L) { + return call(L); + } + }; + } // function_detail +} // sol + +// end of sol\function_types_basic.hpp + +// beginning of sol\function_types_member.hpp + +namespace sol { +namespace function_detail { +template +struct free_function : public base_function { + typedef meta::unwrapped_t> Function; + typedef meta::function_return_t return_type; + typedef meta::function_args_t args_lists; + Function fx; + + template + free_function(Args&&... args): fx(std::forward(args)...) {} + + int call(lua_State* L) { + return stack::call_into_lua(meta::tuple_types(), args_lists(), L, 1, fx); + } + + virtual int operator()(lua_State* L) override { + auto f = [&](lua_State* L) -> int { return this->call(L);}; + return detail::trampoline(L, f); + } +}; + +template +struct functor_function : public base_function { + typedef meta::unwrapped_t> Function; + typedef decltype(&Function::operator()) function_type; + typedef meta::function_return_t return_type; + typedef meta::function_args_t args_lists; + Function fx; + + template + functor_function(Args&&... args): fx(std::forward(args)...) {} + + int call(lua_State* L) { + return stack::call_into_lua(meta::tuple_types(), args_lists(), L, 1, fx); + } + + virtual int operator()(lua_State* L) override { + auto f = [&](lua_State* L) -> int { return this->call(L);}; + return detail::trampoline(L, f); + } +}; + +template +struct member_function : public base_function { + typedef std::remove_pointer_t> function_type; + typedef meta::function_return_t return_type; + typedef meta::function_args_t args_lists; + struct functor { + function_type invocation; + T member; + + template + functor(F&& f, Args&&... args): invocation(std::forward(f)), member(std::forward(args)...) {} + + template + return_type operator()(Args&&... args) { + auto& mem = detail::unwrap(detail::deref(member)); + return (mem.*invocation)(std::forward(args)...); + } + } fx; + + template + member_function(F&& f, Args&&... args) : fx(std::forward(f), std::forward(args)...) {} + + int call(lua_State* L) { + return stack::call_into_lua(meta::tuple_types(), args_lists(), L, 1, fx); + } + + virtual int operator()(lua_State* L) override { + auto f = [&](lua_State* L) -> int { return this->call(L);}; + return detail::trampoline(L, f); + } +}; + +template +struct member_variable : public base_function { + typedef std::remove_pointer_t> function_type; + typedef typename meta::bind_traits::return_type return_type; + typedef typename meta::bind_traits::args_list args_lists; + function_type var; + T member; + typedef std::add_lvalue_reference_t>> M; + + template + member_variable(V&& v, Args&&... args): var(std::forward(v)), member(std::forward(args)...) {} + + int set_assignable(std::false_type, lua_State* L, M) { + lua_pop(L, 1); + return luaL_error(L, "sol: cannot write to this type: copy assignment/constructor not available"); + } + + int set_assignable(std::true_type, lua_State* L, M mem) { + (mem.*var) = stack::get(L, 1); + lua_pop(L, 1); + return 0; + } + + int set_variable(std::true_type, lua_State* L, M mem) { + return set_assignable(std::is_assignable, return_type>(), L, mem); + } + + int set_variable(std::false_type, lua_State* L, M) { + lua_pop(L, 1); + return luaL_error(L, "sol: cannot write to a const variable"); + } + + int call(lua_State* L) { + M mem = detail::unwrap(detail::deref(member)); + switch (lua_gettop(L)) { + case 0: + stack::push(L, (mem.*var)); + return 1; + case 1: + return set_variable(meta::neg>(), L, mem); + default: + return luaL_error(L, "sol: incorrect number of arguments to member variable function"); + } + } + + virtual int operator()(lua_State* L) override { + auto f = [&](lua_State* L) -> int { return this->call(L);}; + return detail::trampoline(L, f); + } +}; +} // function_detail +} // sol + +// end of sol\function_types_member.hpp + +// beginning of sol\function_types_overloaded.hpp + +namespace sol { + namespace function_detail { + template + struct overloaded_function : base_function { + typedef std::tuple overload_list; + typedef std::make_index_sequence indices; + overload_list overloads; + + overloaded_function(overload_list set) + : overloads(std::move(set)) {} + + overloaded_function(Functions... fxs) + : overloads(fxs...) { + + } + + template + int call(types, index_value, types r, types a, lua_State* L, int, int start) { + auto& func = std::get(overloads); + return stack::call_into_lua<0, false>(r, a, L, start, func); + } + + virtual int operator()(lua_State* L) override { + auto mfx = [&](auto&&... args) { return this->call(std::forward(args)...); }; + return call_detail::overload_match(mfx, L, 1); + } + }; + } // function_detail +} // sol + +// end of sol\function_types_overloaded.hpp + +// beginning of sol\resolve.hpp + +namespace sol { + namespace detail { + template(Args...)>> + inline auto resolve_i(types, F&&)->R(meta::unqualified_t::*)(Args...) { + using Sig = R(Args...); + typedef meta::unqualified_t Fu; + return static_cast(&Fu::operator()); + } + + template> + inline auto resolve_f(std::true_type, F&& f) + -> decltype(resolve_i(types>(), std::forward(f))) { + return resolve_i(types>(), std::forward(f)); + } + + template + inline void resolve_f(std::false_type, F&&) { + static_assert(meta::has_deducible_signature::value, + "Cannot use no-template-parameter call with an overloaded functor: specify the signature"); + } + + template> + inline auto resolve_i(types<>, F&& f) -> decltype(resolve_f(meta::has_deducible_signature(), std::forward(f))) { + return resolve_f(meta::has_deducible_signature {}, std::forward(f)); + } + + template> + inline auto resolve_i(types, F&& f) -> decltype(resolve_i(types(), std::forward(f))) { + return resolve_i(types(), std::forward(f)); + } + + template + inline Sig C::* resolve_v(std::false_type, Sig C::* mem_func_ptr) { + return mem_func_ptr; + } + + template + inline Sig C::* resolve_v(std::true_type, Sig C::* mem_variable_ptr) { + return mem_variable_ptr; + } + } // detail + + template + inline auto resolve(R fun_ptr(Args...))->R(*)(Args...) { + return fun_ptr; + } + + template + inline Sig* resolve(Sig* fun_ptr) { + return fun_ptr; + } + + template + inline auto resolve(R(C::*mem_ptr)(Args...))->R(C::*)(Args...) { + return mem_ptr; + } + + template + inline Sig C::* resolve(Sig C::* mem_ptr) { + return detail::resolve_v(std::is_member_object_pointer(), mem_ptr); + } + + template + inline auto resolve(F&& f) -> decltype(detail::resolve_i(types(), std::forward(f))) { + return detail::resolve_i(types(), std::forward(f)); + } +} // sol + +// end of sol\resolve.hpp + +namespace sol { + template + struct function_arguments { + std::tuple params; + template + function_arguments(Args&&... args) : params(std::forward(args)...) {} + }; + + template , typename... Args> + function_arguments function_args(Args&&... args) { + return function_arguments(std::forward(args)...); + } + + namespace stack { + template + struct pusher> { + template + static void select_convertible(std::false_type, types, lua_State* L, Fx&& fx, Args&&... args) { + typedef std::remove_pointer_t> clean_fx; + std::unique_ptr sptr = std::make_unique>(std::forward(fx), std::forward(args)...); + set_fx(L, std::move(sptr)); + } + + template + static void select_convertible(std::true_type, types, lua_State* L, Fx&& fx, Args&&... args) { + using fx_ptr_t = R(*)(A...); + fx_ptr_t fxptr = detail::unwrap(std::forward(fx)); + select_function(std::true_type(), L, fxptr, std::forward(args)...); + } + + template + static void select_convertible(types t, lua_State* L, Fx&& fx, Args&&... args) { + typedef std::decay_t>> raw_fx_t; + typedef R(*fx_ptr_t)(A...); + typedef std::is_convertible is_convertible; + select_convertible(is_convertible(), t, L, std::forward(fx), std::forward(args)...); + } + + template + static void select_convertible(types<>, lua_State* L, Fx&& fx, Args&&... args) { + typedef meta::function_signature_t>> Sig; + select_convertible(types(), L, std::forward(fx), std::forward(args)...); + } + + template + static void select_reference_member_variable(std::false_type, lua_State* L, Fx&& fx, T&& obj, Args&&... args) { + typedef std::remove_pointer_t> clean_fx; + std::unique_ptr sptr = std::make_unique, clean_fx>>(std::forward(fx), std::forward(obj), std::forward(args)...); + set_fx(L, std::move(sptr)); + } + + template + static void select_reference_member_variable(std::true_type, lua_State* L, Fx&& fx, T&& obj, Args&&... args) { + typedef std::decay_t dFx; + dFx memfxptr(std::forward(fx)); + auto userptr = detail::ptr(std::forward(obj), std::forward(args)...); + lua_CFunction freefunc = &function_detail::upvalue_member_variable, meta::unqualified_t>::call; + + int upvalues = stack::stack_detail::push_as_upvalues(L, memfxptr); + upvalues += stack::push(L, lightuserdata_value(static_cast(userptr))); + stack::push(L, c_closure(freefunc, upvalues)); + } + + template + static void select_member_variable(std::false_type, lua_State* L, Fx&& fx, Args&&... args) { + select_convertible(types(), L, std::forward(fx), std::forward(args)...); + } + + template + static void select_member_variable(std::true_type, lua_State* L, Fx&& fx, T&& obj, Args&&... args) { + typedef meta::boolean>::value || std::is_pointer::value> is_reference; + select_reference_member_variable(is_reference(), L, std::forward(fx), std::forward(obj), std::forward(args)...); + } + + template + static void select_member_variable(std::true_type, lua_State* L, Fx&& fx) { + typedef typename meta::bind_traits>::object_type C; + lua_CFunction freefunc = &function_detail::upvalue_this_member_variable::call; + int upvalues = stack::stack_detail::push_as_upvalues(L, fx); + stack::push(L, c_closure(freefunc, upvalues)); + } + + template + static void select_reference_member_function(std::false_type, lua_State* L, Fx&& fx, T&& obj, Args&&... args) { + typedef std::remove_pointer_t> clean_fx; + std::unique_ptr sptr = std::make_unique>, clean_fx>>(std::forward(fx), std::forward(obj), std::forward(args)...); + set_fx(L, std::move(sptr)); + } + + template + static void select_reference_member_function(std::true_type, lua_State* L, Fx&& fx, T&& obj, Args&&... args) { + typedef std::decay_t dFx; + dFx memfxptr(std::forward(fx)); + auto userptr = detail::ptr(std::forward(obj), std::forward(args)...); + lua_CFunction freefunc = &function_detail::upvalue_member_function, meta::unqualified_t>::call; + + int upvalues = stack::stack_detail::push_as_upvalues(L, memfxptr); + upvalues += stack::push(L, lightuserdata_value(static_cast(userptr))); + stack::push(L, c_closure(freefunc, upvalues)); + } + + template + static void select_member_function(std::false_type, lua_State* L, Fx&& fx, Args&&... args) { + select_member_variable(std::is_member_object_pointer>(), L, std::forward(fx), std::forward(args)...); + } + + template + static void select_member_function(std::true_type, lua_State* L, Fx&& fx, T&& obj, Args&&... args) { + typedef meta::boolean>::value || std::is_pointer::value> is_reference; + select_reference_member_function(is_reference(), L, std::forward(fx), std::forward(obj), std::forward(args)...); + } + + template + static void select_member_function(std::true_type, lua_State* L, Fx&& fx) { + typedef typename meta::bind_traits>::object_type C; + lua_CFunction freefunc = &function_detail::upvalue_this_member_function::call; + int upvalues = stack::stack_detail::push_as_upvalues(L, fx); + stack::push(L, c_closure(freefunc, upvalues)); + } + + template + static void select_function(std::false_type, lua_State* L, Fx&& fx, Args&&... args) { + select_member_function(std::is_member_function_pointer>(), L, std::forward(fx), std::forward(args)...); + } + + template + static void select_function(std::true_type, lua_State* L, Fx&& fx, Args&&... args) { + std::decay_t target(std::forward(fx), std::forward(args)...); + lua_CFunction freefunc = &function_detail::upvalue_free_function::call; + + int upvalues = stack::stack_detail::push_as_upvalues(L, target); + stack::push(L, c_closure(freefunc, upvalues)); + } + + static void select_function(std::true_type, lua_State* L, lua_CFunction f) { + stack::push(L, f); + } + + template + static void select(lua_State* L, Fx&& fx, Args&&... args) { + select_function(std::is_function>(), L, std::forward(fx), std::forward(args)...); + } + + static void set_fx(lua_State* L, std::unique_ptr luafunc) { + function_detail::base_function* target = luafunc.release(); + void* targetdata = static_cast(target); + lua_CFunction freefunc = function_detail::call; + + stack::push(L, userdata_value(targetdata)); + function_detail::free_function_cleanup(L); + lua_setmetatable(L, -2); + stack::push(L, c_closure(freefunc, 1)); + } + + template + static int push(lua_State* L, Args&&... args) { + // Set will always place one thing (function) on the stack + select(L, std::forward(args)...); + return 1; + } + }; + + template + struct pusher> { + template + static int push_func(std::index_sequence, lua_State* L, FP&& fp) { + return stack::push(L, detail::forward_get(fp.params)...); + } + + template + static int push(lua_State* L, FP&& fp) { + return push_func(std::make_index_sequence(), L, std::forward(fp)); + } + }; + + template + struct pusher> { + static int push(lua_State* L, std::function fx) { + return pusher>{}.push(L, std::move(fx)); + } + }; + + template + struct pusher::value>> { + template + static int push(lua_State* L, F&& f) { + return pusher>{}.push(L, std::forward(f)); + } + }; + + template + struct pusher, meta::neg>, meta::neg>>>::value>> { + template + static int push(lua_State* L, F&& f) { + return pusher>{}.push(L, std::forward(f)); + } + }; + + template + struct pusher> { + static int push(lua_State* L, overload_set&& set) { + pusher>{}.set_fx(L, std::make_unique>(std::move(set.set))); + return 1; + } + + static int push(lua_State* L, const overload_set& set) { + pusher>{}.set_fx(L, std::make_unique>(set.set)); + return 1; + } + }; + + } // stack +} // sol + +// end of sol\function_types.hpp + +namespace sol { + template + class basic_function : public base_t { + private: + void luacall(std::ptrdiff_t argcount, std::ptrdiff_t resultcount) const { + lua_callk(base_t::lua_state(), static_cast(argcount), static_cast(resultcount), 0, nullptr); + } + + template + auto invoke(types, std::index_sequence, std::ptrdiff_t n) const { + luacall(n, sizeof...(Ret)); + return stack::pop>(base_t::lua_state()); + } + + template + Ret invoke(types, std::index_sequence, std::ptrdiff_t n) const { + luacall(n, 1); + return stack::pop(base_t::lua_state()); + } + + template + void invoke(types, std::index_sequence, std::ptrdiff_t n) const { + luacall(n, 0); + } + + function_result invoke(types<>, std::index_sequence<>, std::ptrdiff_t n) const { + int stacksize = lua_gettop(base_t::lua_state()); + int firstreturn = (std::max)(1, stacksize - static_cast(n)); + luacall(n, LUA_MULTRET); + int poststacksize = lua_gettop(base_t::lua_state()); + int returncount = poststacksize - (firstreturn - 1); + return function_result(base_t::lua_state(), firstreturn, returncount); + } + + public: + basic_function() = default; + basic_function(const basic_function&) = default; + basic_function& operator=(const basic_function&) = default; + basic_function(basic_function&&) = default; + basic_function& operator=(basic_function&&) = default; + basic_function(const stack_reference& r) : basic_function(r.lua_state(), r.stack_index()) {} + basic_function(stack_reference&& r) : basic_function(r.lua_state(), r.stack_index()) {} + basic_function(lua_State* L, int index = -1) : base_t(L, index) { +#ifdef SOL_CHECK_ARGUMENTS + stack::check(L, index, type_panic); +#endif // Safety + } + + template + function_result operator()(Args&&... args) const { + return call<>(std::forward(args)...); + } + + template + decltype(auto) operator()(types, Args&&... args) const { + return call(std::forward(args)...); + } + + template + decltype(auto) call(Args&&... args) const { + base_t::push(); + int pushcount = stack::multi_push_reference(base_t::lua_state(), std::forward(args)...); + return invoke(types(), std::make_index_sequence(), pushcount); + } + }; + + namespace stack { + template + struct getter> { + typedef meta::bind_traits fx_t; + typedef typename fx_t::args_list args_lists; + typedef meta::tuple_types return_types; + + template + static std::function get_std_func(types, types, lua_State* L, int index = -1) { + sol::function f(L, index); + auto fx = [f, L, index](Args&&... args) -> meta::return_type_t { + return f.call(std::forward(args)...); + }; + return std::move(fx); + } + + template + static std::function get_std_func(types, types, lua_State* L, int index = -1) { + sol::function f(L, index); + auto fx = [f, L, index](FxArgs&&... args) -> void { + f(std::forward(args)...); + }; + return std::move(fx); + } + + template + static std::function get_std_func(types<>, types t, lua_State* L, int index = -1) { + return get_std_func(types(), t, L, index); + } + + static std::function get(lua_State* L, int index) { + type t = type_of(L, index); + if (t == type::none || t == type::nil) { + return nullptr; + } + return get_std_func(return_types(), args_lists(), L, index); + } + }; + } // stack +} // sol + +// end of sol\function.hpp + +// beginning of sol\protected_function.hpp + +// beginning of sol\protected_function_result.hpp + +namespace sol { + struct protected_function_result : public proxy_base { + private: + lua_State* L; + int index; + int returncount; + int popcount; + call_status err; + + template + decltype(auto) tagged_get(types>) const { + if (!valid()) { + return sol::optional(nullopt); + } + return stack::get>(L, index); + } + + template + decltype(auto) tagged_get(types) const { +#ifdef SOL_CHECK_ARGUMENTS + if (!valid()) { + type_panic(L, index, type_of(L, index), type::none); + } +#endif // Check Argument Safety + return stack::get(L, index); + } + + optional tagged_get(types>) const { + if (valid()) { + return nullopt; + } + return error(detail::direct_error, stack::get(L, index)); + } + + error tagged_get(types) const { +#ifdef SOL_CHECK_ARGUMENTS + if (valid()) { + type_panic(L, index, type_of(L, index), type::none); + } +#endif // Check Argument Safety + return error(detail::direct_error, stack::get(L, index)); + } + + public: + protected_function_result() = default; + protected_function_result(lua_State* L, int index = -1, int returncount = 0, int popcount = 0, call_status err = call_status::ok) noexcept : L(L), index(index), returncount(returncount), popcount(popcount), err(err) { + + } + protected_function_result(const protected_function_result&) = default; + protected_function_result& operator=(const protected_function_result&) = default; + protected_function_result(protected_function_result&& o) noexcept : L(o.L), index(o.index), returncount(o.returncount), popcount(o.popcount), err(o.err) { + // Must be manual, otherwise destructor will screw us + // return count being 0 is enough to keep things clean + // but we will be thorough + o.L = nullptr; + o.index = 0; + o.returncount = 0; + o.popcount = 0; + o.err = call_status::runtime; + } + protected_function_result& operator=(protected_function_result&& o) noexcept { + L = o.L; + index = o.index; + returncount = o.returncount; + popcount = o.popcount; + err = o.err; + // Must be manual, otherwise destructor will screw us + // return count being 0 is enough to keep things clean + // but we will be thorough + o.L = nullptr; + o.index = 0; + o.returncount = 0; + o.popcount = 0; + o.err = call_status::runtime; + return *this; + } + + call_status status() const noexcept { + return err; + } + + bool valid() const noexcept { + return status() == call_status::ok || status() == call_status::yielded; + } + + template + T get() const { + return tagged_get(types>()); + } + + lua_State* lua_state() const noexcept { return L; }; + int stack_index() const noexcept { return index; }; + + ~protected_function_result() { + stack::remove(L, index, popcount); + } + }; +} // sol + +// end of sol\protected_function_result.hpp + +#include + +namespace sol { + template + class basic_protected_function : public base_t { + private: + static reference& handler_storage() { + static sol::reference h; + return h; + } + + public: + static const reference& get_default_handler() { + return handler_storage(); + } + + static void set_default_handler(reference& ref) { + handler_storage() = ref; + } + + private: + struct handler { + const reference& target; + int stackindex; + handler(const reference& target) : target(target), stackindex(0) { + if (target.valid()) { + stackindex = lua_gettop(target.lua_state()) + 1; + target.push(); + } + } + bool valid() const { return stackindex > 0; } + ~handler() { + if (valid()) { + lua_remove(target.lua_state(), stackindex); + } + } + }; + + int luacall(std::ptrdiff_t argcount, std::ptrdiff_t resultcount, handler& h) const { + return lua_pcallk(base_t::lua_state(), static_cast(argcount), static_cast(resultcount), h.stackindex, 0, nullptr); + } + + template + auto invoke(types, std::index_sequence, std::ptrdiff_t n, handler& h) const { + luacall(n, sizeof...(Ret), h); + return stack::pop>(base_t::lua_state()); + } + + template + Ret invoke(types, std::index_sequence, std::ptrdiff_t n, handler& h) const { + luacall(n, 1, h); + return stack::pop(base_t::lua_state()); + } + + template + void invoke(types, std::index_sequence, std::ptrdiff_t n, handler& h) const { + luacall(n, 0, h); + } + + protected_function_result invoke(types<>, std::index_sequence<>, std::ptrdiff_t n, handler& h) const { + int stacksize = lua_gettop(base_t::lua_state()); + int firstreturn = (std::max)(1, stacksize - static_cast(n) - 1); + int returncount = 0; + call_status code = call_status::ok; +#ifndef SOL_NO_EXCEPTIONS + auto onexcept = [&](const char* error) { + h.stackindex = 0; + if (h.target.valid()) { + h.target.push(); + stack::push(base_t::lua_state(), error); + lua_call(base_t::lua_state(), 1, 1); + } + else { + stack::push(base_t::lua_state(), error); + } + }; + try { +#endif // No Exceptions + code = static_cast(luacall(n, LUA_MULTRET, h)); + int poststacksize = lua_gettop(base_t::lua_state()); + returncount = poststacksize - (stacksize - 1); +#ifndef SOL_NO_EXCEPTIONS + } + // Handle C++ errors thrown from C++ functions bound inside of lua + catch (const char* error) { + onexcept(error); + firstreturn = lua_gettop(base_t::lua_state()); + return protected_function_result(base_t::lua_state(), firstreturn, 0, 1, call_status::runtime); + } + catch (const std::exception& error) { + onexcept(error.what()); + firstreturn = lua_gettop(base_t::lua_state()); + return protected_function_result(base_t::lua_state(), firstreturn, 0, 1, call_status::runtime); + } + catch (...) { + onexcept("caught (...) unknown error during protected_function call"); + firstreturn = lua_gettop(base_t::lua_state()); + return protected_function_result(base_t::lua_state(), firstreturn, 0, 1, call_status::runtime); + } +#endif // No Exceptions + return protected_function_result(base_t::lua_state(), firstreturn, returncount, returncount, code); + } + + public: + reference error_handler; + + basic_protected_function() = default; + basic_protected_function(const basic_protected_function&) = default; + basic_protected_function& operator=(const basic_protected_function&) = default; + basic_protected_function(basic_protected_function&&) = default; + basic_protected_function& operator=(basic_protected_function&&) = default; + basic_protected_function(const basic_function& b) : base_t(b) {} + basic_protected_function(basic_function&& b) : base_t(std::move(b)) {} + basic_protected_function(const stack_reference& r) : basic_protected_function(r.lua_state(), r.stack_index()) {} + basic_protected_function(stack_reference&& r) : basic_protected_function(r.lua_state(), r.stack_index()) {} + basic_protected_function(lua_State* L, int index = -1) : base_t(L, index), error_handler(get_default_handler()) { +#ifdef SOL_CHECK_ARGUMENTS + stack::check(L, index, type_panic); +#endif // Safety + } + + template + protected_function_result operator()(Args&&... args) const { + return call<>(std::forward(args)...); + } + + template + decltype(auto) operator()(types, Args&&... args) const { + return call(std::forward(args)...); + } + + template + decltype(auto) call(Args&&... args) const { + handler h(error_handler); + base_t::push(); + int pushcount = stack::multi_push_reference(base_t::lua_state(), std::forward(args)...); + return invoke(types(), std::make_index_sequence(), pushcount, h); + } + }; +} // sol + +// end of sol\protected_function.hpp + +namespace sol { + struct stack_proxy : public proxy_base { + private: + lua_State* L; + int index; + + public: + stack_proxy() : L(nullptr), index(0) {} + stack_proxy(lua_State* L, int index) : L(L), index(index) {} + + template + decltype(auto) get() const { + return stack::get(L, stack_index()); + } + + int push() const { + lua_pushvalue(L, index); + return 1; + } + + lua_State* lua_state() const { return L; } + int stack_index() const { return index; } + + template + decltype(auto) call(Args&&... args) { + return get().template call(std::forward(args)...); + } + + template + decltype(auto) operator()(Args&&... args) { + return call<>(std::forward(args)...); + } + }; + + namespace stack { + template <> + struct getter { + static stack_proxy get(lua_State* L, int index = -1) { + return stack_proxy(L, index); + } + }; + + template <> + struct pusher { + static int push(lua_State*, const stack_proxy& ref) { + return ref.push(); + } + }; + } // stack + + template <> + struct tie_size : std::integral_constant {}; + + template + stack_proxy get(const function_result& fr) { + return stack_proxy(fr.lua_state(), static_cast(fr.stack_index() + I)); + } + + template <> + struct tie_size : std::integral_constant {}; + + template + stack_proxy get(const protected_function_result& fr) { + return stack_proxy(fr.lua_state(), static_cast(fr.stack_index() + I)); + } +} // sol + +// end of sol\stack_proxy.hpp + +#include +#include + +namespace sol { + template + struct va_iterator : std::iterator, std::ptrdiff_t, std::conditional_t, std::conditional_t> { + typedef std::iterator, std::ptrdiff_t, std::conditional_t, std::conditional_t> base_t; + typedef typename base_t::reference reference; + typedef typename base_t::pointer pointer; + typedef typename base_t::value_type value_type; + typedef typename base_t::difference_type difference_type; + typedef typename base_t::iterator_category iterator_category; + lua_State* L; + int index; + int stacktop; + stack_proxy sp; + + va_iterator() : L(nullptr), index((std::numeric_limits::max)()), stacktop((std::numeric_limits::max)()) {} + va_iterator(lua_State* L, int index, int stacktop) : L(L), index(index), stacktop(stacktop), sp(L, index) {} + + reference operator*() { + return stack_proxy(L, index); + } + + pointer operator->() { + sp = stack_proxy(L, index); + return &sp; + } + + va_iterator& operator++ () { + ++index; + return *this; + } + + va_iterator operator++ (int) { + auto r = *this; + this->operator ++(); + return r; + } + + va_iterator& operator-- () { + --index; + return *this; + } + + va_iterator operator-- (int) { + auto r = *this; + this->operator --(); + return r; + } + + va_iterator& operator+= (difference_type idx) { + index += static_cast(idx); + return *this; + } + + va_iterator& operator-= (difference_type idx) { + index -= static_cast(idx); + return *this; + } + + difference_type operator- (const va_iterator& r) const { + return index - r.index; + } + + va_iterator operator+ (difference_type idx) const { + va_iterator r = *this; + r += idx; + return r; + } + + reference operator[](difference_type idx) { + return stack_proxy(L, index + static_cast(idx)); + } + + bool operator==(const va_iterator& r) const { + if (stacktop == (std::numeric_limits::max)()) { + return r.index == r.stacktop; + } + else if (r.stacktop == (std::numeric_limits::max)()) { + return index == stacktop; + } + return index == r.index; + } + + bool operator != (const va_iterator& r) const { + return !(this->operator==(r)); + } + + bool operator < (const va_iterator& r) const { + return index < r.index; + } + + bool operator > (const va_iterator& r) const { + return index > r.index; + } + + bool operator <= (const va_iterator& r) const { + return index <= r.index; + } + + bool operator >= (const va_iterator& r) const { + return index >= r.index; + } + }; + + template + inline va_iterator operator+(typename va_iterator::difference_type n, const va_iterator& r) { + return r + n; + } + + struct variadic_args { + private: + lua_State* L; + int index; + int stacktop; + + public: + typedef stack_proxy reference_type; + typedef stack_proxy value_type; + typedef stack_proxy* pointer; + typedef std::ptrdiff_t difference_type; + typedef std::size_t size_type; + typedef va_iterator iterator; + typedef va_iterator const_iterator; + typedef std::reverse_iterator reverse_iterator; + typedef std::reverse_iterator const_reverse_iterator; + + variadic_args() = default; + variadic_args(lua_State* L, int index = -1) : L(L), index(lua_absindex(L, index)), stacktop(lua_gettop(L)) {} + variadic_args(const variadic_args&) = default; + variadic_args& operator=(const variadic_args&) = default; + variadic_args(variadic_args&& o) : L(o.L), index(o.index), stacktop(o.stacktop) { + // Must be manual, otherwise destructor will screw us + // return count being 0 is enough to keep things clean + // but will be thorough + o.L = nullptr; + o.index = 0; + o.stacktop = 0; + } + variadic_args& operator=(variadic_args&& o) { + L = o.L; + index = o.index; + stacktop = o.stacktop; + // Must be manual, otherwise destructor will screw us + // return count being 0 is enough to keep things clean + // but will be thorough + o.L = nullptr; + o.index = 0; + o.stacktop = 0; + return *this; + } + + iterator begin() { return iterator(L, index, stacktop + 1); } + iterator end() { return iterator(L, stacktop + 1, stacktop + 1); } + const_iterator begin() const { return const_iterator(L, index, stacktop + 1); } + const_iterator end() const { return const_iterator(L, stacktop + 1, stacktop + 1); } + const_iterator cbegin() const { return begin(); } + const_iterator cend() const { return end(); } + + reverse_iterator rbegin() { return std::reverse_iterator(begin()); } + reverse_iterator rend() { return std::reverse_iterator(end()); } + const_reverse_iterator rbegin() const { return std::reverse_iterator(begin()); } + const_reverse_iterator rend() const { return std::reverse_iterator(end()); } + const_reverse_iterator crbegin() const { return std::reverse_iterator(cbegin()); } + const_reverse_iterator crend() const { return std::reverse_iterator(cend()); } + + int push() const { + int pushcount = 0; + for (int i = index; i <= stacktop; ++i) { + lua_pushvalue(L, i); + pushcount += 1; + } + return pushcount; + } + + template + decltype(auto) get(difference_type start = 0) const { + return stack::get(L, index + static_cast(start)); + } + + stack_proxy operator[](difference_type start) const { + return stack_proxy(L, index + static_cast(start)); + } + + lua_State* lua_state() const { return L; }; + int stack_index() const { return index; }; + int leftover_count() const { return stacktop - (index - 1); } + int top() const { return stacktop; } + }; + + namespace stack { + template <> + struct getter { + static variadic_args get(lua_State* L, int index = -1) { + return variadic_args(L, index); + } + }; + + template <> + struct pusher { + static int push(lua_State*, const variadic_args& ref) { + return ref.push(); + } + }; + } // stack +} // sol + +// end of sol\variadic_args.hpp + +namespace sol { + template + class basic_object : public base_t { + private: + template + decltype(auto) as_stack(std::true_type) const { + return stack::get(base_t::lua_state(), base_t::stack_index()); + } + + template + decltype(auto) as_stack(std::false_type) const { + base_t::push(); + return stack::pop(base_t::lua_state()); + } + + template + bool is_stack(std::true_type) const { + return stack::check(base_t::lua_state(), base_t::stack_index(), no_panic); + } + + template + bool is_stack(std::false_type) const { + auto pp = stack::push_pop(*this); + return stack::check(base_t::lua_state(), -1, no_panic); + } + + public: + basic_object() noexcept = default; + template , basic_object>>, std::is_base_of>> = meta::enabler> + basic_object(T&& r) : base_t(std::forward(r)) {} + basic_object(nil_t r) : base_t(r) {} + basic_object(const basic_object&) = default; + basic_object(basic_object&&) = default; + basic_object& operator=(const basic_object&) = default; + basic_object& operator=(basic_object&&) = default; + basic_object(const stack_reference& r) noexcept : basic_object(r.lua_state(), r.stack_index()) {} + basic_object(stack_reference&& r) noexcept : basic_object(r.lua_state(), r.stack_index()) {} + basic_object(lua_State* L, int index = -1) noexcept : base_t(L, index) {} + + template + decltype(auto) as() const { + return as_stack(std::is_same()); + } + + template + bool is() const { + if (!base_t::valid()) + return false; + return is_stack(std::is_same()); + } + }; + + template ::value, typename T> + R make_reference(lua_State* L, T&& value) { + int backpedal = stack::push(L, std::forward(value)); + R r = stack::get(L, -backpedal); + if (should_pop) { + lua_pop(L, backpedal); + } + return r; + } + + template ::value, typename... Args> + object make_reference(lua_State* L, Args&&... args) { + int backpedal = stack::push(L, std::forward(args)...); + object r = stack::get(L, -backpedal); + lua_pop(L, backpedal); + return r; + } + + template + object make_object(lua_State* L, T&& value) { + return make_reference(L, std::forward(value)); + } + + template + object make_object(lua_State* L, Args&&... args) { + return make_reference(L, std::forward(args)...); + } + + inline bool operator==(const object& lhs, const nil_t&) { + return !lhs.valid(); + } + + inline bool operator==(const nil_t&, const object& rhs) { + return !rhs.valid(); + } + + inline bool operator!=(const object& lhs, const nil_t&) { + return lhs.valid(); + } + + inline bool operator!=(const nil_t&, const object& rhs) { + return rhs.valid(); + } +} // sol + +// end of sol\object.hpp + +namespace sol { + template + struct proxy : public proxy_base> { + private: + typedef meta::condition, Key, std::tuple>, Key&, meta::unqualified_t>>> key_type; + + template + decltype(auto) tuple_get(std::index_sequence) const { + return tbl.template traverse_get(std::get(key)...); + } + + template + void tuple_set(std::index_sequence, T&& value) const { + tbl.traverse_set(std::get(key)..., std::forward(value)); + } + + public: + Table tbl; + key_type key; + + template + proxy(Table table, T&& key) : tbl(table), key(std::forward(key)) {} + + template + proxy& set(T&& item) { + tuple_set(std::make_index_sequence>::value>(), std::forward(item)); + return *this; + } + + template + proxy& set_function(Args&&... args) { + tbl.set_function(key, std::forward(args)...); + return *this; + } + + template>> = meta::enabler> + proxy& operator=(U&& other) { + return set_function(std::forward(other)); + } + + template>> = meta::enabler> + proxy& operator=(U&& other) { + return set(std::forward(other)); + } + + template + decltype(auto) get() const { + return tuple_get(std::make_index_sequence>::value>()); + } + + template + decltype(auto) get_or(T&& otherwise) const { + typedef decltype(get()) U; + sol::optional option = get>(); + if (option) { + return static_cast(option.value()); + } + return static_cast(std::forward(otherwise)); + } + + template + decltype(auto) get_or(D&& otherwise) const { + sol::optional option = get>(); + if (option) { + return static_cast(option.value()); + } + return static_cast(std::forward(otherwise)); + } + + template + decltype(auto) operator[](K&& k) const { + auto keys = meta::tuplefy(key, std::forward(k)); + return proxy(tbl, std::move(keys)); + } + + template + decltype(auto) call(Args&&... args) { + return get().template call(std::forward(args)...); + } + + template + decltype(auto) operator()(Args&&... args) { + return call<>(std::forward(args)...); + } + + bool valid() const { + stack::push_pop(tbl); + auto p = stack::probe_get_field, global_table>::value>(tbl.lua_state(), key, lua_gettop(tbl.lua_state())); + lua_pop(tbl.lua_state(), p.levels); + return p; + } + }; + + template + inline bool operator==(T&& left, const proxy& right) { + return left == right.template get>(); + } + + template + inline bool operator==(const proxy& right, T&& left) { + return right.template get>() == left; + } + + template + inline bool operator!=(T&& left, const proxy& right) { + return right.template get>() != left; + } + + template + inline bool operator!=(const proxy& right, T&& left) { + return right.template get>() != left; + } + + template + inline bool operator==(nil_t, const proxy& right) { + return !right.valid(); + } + + template + inline bool operator==(const proxy& right, nil_t) { + return !right.valid(); + } + + template + inline bool operator!=(nil_t, const proxy& right) { + return right.valid(); + } + + template + inline bool operator!=(const proxy& right, nil_t) { + return right.valid(); + } + + namespace stack { + template + struct pusher> { + static int push(lua_State*, const proxy& p) { + sol::reference r = p; + return r.push(); + } + }; + } // stack +} // sol + +// end of sol\proxy.hpp + +// beginning of sol\usertype.hpp + +// beginning of sol\usertype_metatable.hpp + +// beginning of sol\deprecate.hpp + +#ifndef SOL_DEPRECATED + #ifdef _MSC_VER + #define SOL_DEPRECATED __declspec(deprecated) + #elif __GNUC__ + #define SOL_DEPRECATED __attribute__((deprecated)) + #else + #define SOL_DEPRECATED [[deprecated]] + #endif // compilers +#endif // SOL_DEPRECATED + +namespace sol { + namespace detail { + template + struct SOL_DEPRECATED deprecate_type { + using type = T; + }; + } // detail +} // sol + +// end of sol\deprecate.hpp + +namespace sol { + + namespace usertype_detail { + inline bool is_indexer(string_detail::string_shim s) { + return s == name_of(meta_function::index) || s == name_of(meta_function::new_index); + } + + inline bool is_indexer(meta_function mf) { + return mf == meta_function::index || mf == meta_function::new_index; + } + + inline bool is_indexer(call_construction) { + return false; + } + + inline bool is_indexer(base_classes_tag) { + return false; + } + + inline auto make_shim(string_detail::string_shim s) { + return s; + } + + inline auto make_shim(call_construction) { + return string_detail::string_shim(name_of(meta_function::call_function)); + } + + inline auto make_shim(meta_function mf) { + return string_detail::string_shim(name_of(mf)); + } + + inline auto make_shim(base_classes_tag) { + return string_detail::string_shim(detail::base_class_cast_key()); + } + + template + inline luaL_Reg make_reg(N&& n, lua_CFunction f) { + luaL_Reg l{ make_shim(std::forward(n)).c_str(), f }; + return l; + } + + struct registrar { + virtual int push_um(lua_State* L) = 0; + virtual ~registrar() {} + }; + + template + inline int indexing_fail(lua_State* L) { + string_detail::string_shim accessor = stack::get(L, is_index ? -1 : -2); + if (is_index) + return luaL_error(L, "sol: attempt to index (get) nil value \"%s\" on userdata (bad (misspelled?) key name or does not exist)", accessor.data()); + else + return luaL_error(L, "sol: attempt to index (set) nil value \"%s\" on userdata (bad (misspelled?) key name or does not exist)", accessor.data()); + } + + } // usertype_detail + + template + struct usertype_metatable : usertype_detail::registrar { + typedef std::make_index_sequence::value> indices; + typedef std::make_index_sequence::value / 2> half_indices; + typedef luaL_Reg regs_t[std::tuple_size::value / 2 + 1]; + template + struct check_binding : is_variable_binding>> {}; + Tuple functions; + lua_CFunction indexfunc; + lua_CFunction newindexfunc; + lua_CFunction destructfunc; + lua_CFunction callconstructfunc; + void* baseclasscheck; + void* baseclasscast; + bool mustindex; + bool secondarymeta; + + template + static inline lua_CFunction make_func(lua_CFunction& f) { + return f; + } + + template + static inline lua_CFunction make_func(F&&) { + return call; + } + + template + static bool contains_variable(std::index_sequence) { + typedef meta::any...> has_variables; + return has_variables::value; + } + + template + bool contains_index(std::index_sequence) const { + bool idx = false; + (void)detail::swallow{ 0, ((idx &= usertype_detail::is_indexer(std::get(functions))), 0) ... }; + return idx; + } + + template + int make_regs(regs_t& l, int index ) { + if (destructfunc != nullptr) { + l[index] = { name_of(meta_function::garbage_collect).c_str(), destructfunc }; + ++index; + } + return index; + } + + template + int make_regs(regs_t& l, int index, sol::call_construction, F&&, Args&&... args) { + callconstructfunc = call; + secondarymeta = true; + int endindex = make_regs(l, index, std::forward(args)...); + return endindex; + } + + template + int make_regs(regs_t& l, int index, base_classes_tag, bases, Args&&... args) { + int endindex = make_regs(l, index, std::forward(args)...); + if (sizeof...(Bases) < 1) { + (void)detail::swallow{ 0, ((detail::has_derived::value = false), 0)... }; + return endindex; + } + (void)detail::swallow{ 0, ((detail::has_derived::value = true), 0)... }; +#ifndef SOL_NO_EXCEPTIONS + static_assert(sizeof(void*) <= sizeof(detail::throw_cast), "The size of this data pointer is too small to fit the inheritance checking function: file a bug report."); + baseclasscheck = baseclasscast = (void*)&detail::throw_as; +#elif !defined(SOL_NO_RTTI) + static_assert(sizeof(void*) <= sizeof(detail::inheritance_check_function), "The size of this data pointer is too small to fit the inheritance checking function: file a bug report."); + static_assert(sizeof(void*) <= sizeof(detail::inheritance_cast_function), "The size of this data pointer is too small to fit the inheritance checking function: file a bug report."); + baseclasscheck = (void*)&detail::inheritance::type_check; + baseclasscast = (void*)&detail::inheritance::type_cast; +#else + static_assert(sizeof(void*) <= sizeof(detail::inheritance_check_function), "The size of this data pointer is too small to fit the inheritance checking function: file a bug report."); + static_assert(sizeof(void*) <= sizeof(detail::inheritance_cast_function), "The size of this data pointer is too small to fit the inheritance checking function: file a bug report."); + baseclasscheck = (void*)&detail::inheritance::type_check; + baseclasscast = (void*)&detail::inheritance::type_cast; +#endif // No Runtime Type Information vs. Throw-Style Inheritance + return endindex; + } + + template , base_classes_tag, call_construction>::value>> + int make_regs(regs_t& l, int index, N&& n, F&& f, Args&&... args) { + string_detail::string_shim shimname = usertype_detail::make_shim(n); + // Returnable scope + // That would be a neat keyword for C++ + // returnable { ... }; + [&]() { + if (shimname == name_of(meta_function::garbage_collect)) { + destructfunc = call; + return; + } + else if (shimname == name_of(meta_function::index)) { + indexfunc = call; + mustindex = true; + return; + } + else if (shimname == name_of(meta_function::new_index)) { + newindexfunc = call; + mustindex = true; + return; + } + l[index] = usertype_detail::make_reg(std::forward(n), make_func(std::forward(f))); + ++index; + }(); + return make_regs(l, index, std::forward(args)...); + } + + usertype_metatable(Tuple t) : functions(std::move(t)), + indexfunc(usertype_detail::indexing_fail), newindexfunc(usertype_detail::indexing_fail), + destructfunc(nullptr), callconstructfunc(nullptr), baseclasscheck(nullptr), baseclasscast(nullptr), + mustindex(contains_variable(half_indices()) || contains_index(half_indices())), secondarymeta(false) { + } + + template + int find_call(std::integral_constant, std::index_sequence<>, lua_State* L, const sol::string_detail::string_shim&) { + if (is_index) + return indexfunc(L); + else + return newindexfunc(L); + } + + template + int find_call(std::integral_constant idx, std::index_sequence, lua_State* L, const sol::string_detail::string_shim& accessor) { + string_detail::string_shim name = usertype_detail::make_shim(std::get(functions)); + if (accessor == name) { + if (is_variable_binding(functions))>::value) { + return real_call_with(L, *this); + } + return stack::push(L, c_closure(call, stack::push(L, light(*this)))); + } + return find_call(idx, std::index_sequence(), L, accessor); + } + + template + static int real_call(lua_State* L) { + usertype_metatable& f = stack::get>(L, up_value_index(1)); + return real_call_with(L, f); + } + + template + static int real_call_with(lua_State* L, usertype_metatable& um) { + auto& f = call_detail::pick(std::integral_constant(), std::get(um.functions)); + return call_detail::call_wrapped(L, f); + } + + static int real_index_call(lua_State* L) { + usertype_metatable& f = stack::get>(L, up_value_index(1)); + if (stack::get(L, -1) == type::string) { + string_detail::string_shim accessor = stack::get(L, -1); + return f.find_call(std::true_type(), std::make_index_sequence::value>(), L, accessor); + } + return f.indexfunc(L); + } + + static int real_new_index_call(lua_State* L) { + usertype_metatable& f = stack::get>(L, up_value_index(1)); + if (stack::get(L, -2) == type::string) { + string_detail::string_shim accessor = stack::get(L, -2); + return f.find_call(std::false_type(), std::make_index_sequence::value>(), L, accessor); + } + return f.newindexfunc(L); + } + + template + static int call(lua_State* L) { + return detail::static_trampoline<(&real_call)>(L); + } + + template + static int call_with(lua_State* L) { + return detail::static_trampoline<(&real_call_with)>(L); + } + + static int index_call(lua_State* L) { + return detail::static_trampoline<(&real_index_call)>(L); + } + + static int new_index_call(lua_State* L) { + return detail::static_trampoline<(&real_new_index_call)>(L); + } + + virtual int push_um(lua_State* L) override { + return stack::push(L, std::move(*this)); + } + + ~usertype_metatable() override { + + } + }; + + namespace stack { + + template + struct pusher> { + typedef usertype_metatable umt_t; + typedef typename umt_t::regs_t regs_t; + + static usertype_metatable& make_cleanup(lua_State* L, usertype_metatable&& umx) { + // Make sure userdata's memory is properly in lua first, + // otherwise all the light userdata we make later will become invalid + + // Create the top level thing that will act as our deleter later on + const char* gcmetakey = &usertype_traits::gc_table[0]; + stack::set_field(L, gcmetakey, make_user(std::move(umx))); + stack::get_field(L, gcmetakey); + return stack::pop>>(L); + } + + template + static int push(std::index_sequence, lua_State* L, usertype_metatable&& umx) { + + usertype_metatable& um = make_cleanup(L, std::move(umx)); + + // Now use um + const bool& mustindex = um.mustindex; + stack_reference t; + for (std::size_t i = 0; i < 3; ++i) { + // Pointer types, AKA "references" from C++ + const char* metakey = nullptr; + switch (i) { + case 0: + metakey = &usertype_traits::metatable[0]; + break; + case 1: + metakey = &usertype_traits>::metatable[0]; + break; + case 2: + default: + metakey = &usertype_traits::metatable[0]; + break; + } + luaL_newmetatable(L, metakey); + t = stack_reference(L, -1); + stack::push(L, make_light(um)); + regs_t l{}; + int lastreg = um.make_regs(l, 0, std::get(um.functions)... ); + bool hasdestructor = lastreg > 0 && name_of(meta_function::garbage_collect) == l[lastreg - 1].name; + if (i < 2 && hasdestructor) { + l[lastreg - 1] = { nullptr, nullptr }; + } + l[lastreg] = { nullptr, nullptr }; + luaL_setfuncs(L, l, 1); + + if (um.baseclasscheck != nullptr) { + stack::set_field(L, detail::base_class_check_key(), um.baseclasscheck, t.stack_index()); + } + if (um.baseclasscast != nullptr) { + stack::set_field(L, detail::base_class_cast_key(), um.baseclasscast, t.stack_index()); + } + + if (mustindex) { + // Basic index pushing: specialize + // index and newindex to give variables and stuff + stack::set_field(L, meta_function::index, make_closure(umt_t::index_call, make_light(um)), t.stack_index()); + stack::set_field(L, meta_function::new_index, make_closure(umt_t::new_index_call, make_light(um)), t.stack_index()); + } + else { + // If there's only functions, we can use the fast index version + stack::set_field(L, meta_function::index, t, t.stack_index()); + } + // metatable on the metatable + // for call constructor purposes and such + lua_createtable(L, 0, 1); + stack_reference metabehind(L, -1); + if (um.callconstructfunc != nullptr) { + stack::set_field(L, sol::meta_function::call_function, make_closure(um.callconstructfunc, make_light(um)), metabehind.stack_index()); + } + stack::set_field(L, metatable_key, metabehind, t.stack_index()); + metabehind.pop(); + // We want to just leave the table + // in the registry only, otherwise we return it + if (i < 2) { + t.pop(); + } + } + + return 1; + } + + static int push(lua_State* L, usertype_metatable&& um) { + typedef typename umt_t::indices indices; + return push(indices(), L, std::move(um)); + } + }; + + } // stack + +} // sol + +// end of sol\usertype_metatable.hpp + +namespace sol { +namespace usertype_detail { +struct add_destructor_tag {}; +struct check_destructor_tag {}; +struct verified_tag {} const verified {}; + +template +struct is_constructor : std::false_type {}; + +template +struct is_constructor> : std::true_type {}; + +template +struct is_constructor> : std::true_type {}; + +template <> +struct is_constructor : std::true_type {}; + +template +using has_constructor = meta::any>...>; + +template +struct is_destructor : std::false_type {}; + +template +struct is_destructor> : std::true_type {}; + +template +using has_destructor = meta::any>...>; +} // usertype_detail + +template +class usertype { +private: + std::unique_ptr metatableregister; + + template + usertype(usertype_detail::verified_tag, Args&&... args) : metatableregister( std::make_unique...>>>(std::make_tuple(std::forward(args)...)) ) {} + + template + usertype(usertype_detail::add_destructor_tag, Args&&... args) : usertype(usertype_detail::verified, std::forward(args)..., "__gc", default_destructor) {} + + template + usertype(usertype_detail::check_destructor_tag, Args&&... args) : usertype(meta::condition, meta::neg>>, usertype_detail::add_destructor_tag, usertype_detail::verified_tag>(), std::forward(args)...) {} + +public: + + template + usertype(Args&&... args) : usertype(meta::condition, meta::neg>>, decltype(default_constructor), usertype_detail::check_destructor_tag>(), std::forward(args)...) {} + + template + usertype(constructors constructorlist, Args&&... args) : usertype(usertype_detail::check_destructor_tag(), std::forward(args)..., "new", constructorlist) {} + + template + usertype(constructor_wrapper constructorlist, Args&&... args) : usertype(usertype_detail::check_destructor_tag(), std::forward(args)..., "new", constructorlist) {} + + int push(lua_State* L) { + return metatableregister->push_um(L); + } +}; + +namespace stack { +template +struct pusher> { + static int push(lua_State* L, usertype& user) { + return user.push(L); + } +}; +} // stack +} // sol + +// end of sol\usertype.hpp + +// beginning of sol\table_iterator.hpp + +namespace sol { + + template + class basic_table_iterator : public std::iterator> { + private: + typedef std::iterator> base_t; + public: + typedef object key_type; + typedef object mapped_type; + typedef base_t::value_type value_type; + typedef base_t::iterator_category iterator_category; + typedef base_t::difference_type difference_type; + typedef base_t::pointer pointer; + typedef base_t::reference reference; + typedef const value_type& const_reference; + + private: + std::pair kvp; + reference_type ref; + int tableidx = 0; + int keyidx = 0; + std::ptrdiff_t idx = 0; + + public: + + basic_table_iterator() : keyidx(-1), idx(-1) { + + } + + basic_table_iterator(reference_type x) : ref(std::move(x)) { + ref.push(); + tableidx = lua_gettop(ref.lua_state()); + stack::push(ref.lua_state(), nil); + this->operator++(); + if (idx == -1) { + return; + } + --idx; + } + + basic_table_iterator& operator++() { + if (idx == -1) + return *this; + + if (lua_next(ref.lua_state(), tableidx) == 0) { + idx = -1; + keyidx = -1; + return *this; + } + ++idx; + kvp.first = object(ref.lua_state(), -2); + kvp.second = object(ref.lua_state(), -1); + lua_pop(ref.lua_state(), 1); + // leave key on the stack + keyidx = lua_gettop(ref.lua_state()); + return *this; + } + + basic_table_iterator operator++(int) { + auto saved = *this; + this->operator++(); + return saved; + } + + reference operator*() { + return kvp; + } + + const_reference operator*() const { + return kvp; + } + + bool operator== (const basic_table_iterator& right) const { + return idx == right.idx; + } + + bool operator!= (const basic_table_iterator& right) const { + return idx != right.idx; + } + + ~basic_table_iterator() { + if (keyidx != -1) { + stack::remove(ref.lua_state(), keyidx, 1); + } + if (ref.valid()) { + stack::remove(ref.lua_state(), tableidx, 1); + } + } + }; + +} // sol + +// end of sol\table_iterator.hpp + +namespace sol { + namespace detail { + template + struct clean { lua_State* L; clean(lua_State* L) : L(L) {} ~clean() { lua_pop(L, static_cast(n)); } }; + struct ref_clean { lua_State* L; int& n; ref_clean(lua_State* L, int& n) : L(L), n(n) {} ~ref_clean() { lua_pop(L, static_cast(n)); } }; + inline int fail_on_newindex(lua_State* L) { + return luaL_error(L, "sol: cannot modify the elements of an enumeration table"); + } + } + + template + class basic_table_core : public base_t { + friend class state; + friend class state_view; + + template + using is_global = meta::all, meta::is_c_str...>; + + template + void for_each(std::true_type, Fx&& fx) const { + auto pp = stack::push_pop(*this); + stack::push(base_t::lua_state(), nil); + while (lua_next(base_t::lua_state(), -2)) { + sol::object key(base_t::lua_state(), -2); + sol::object value(base_t::lua_state(), -1); + std::pair keyvalue(key, value); + fx(keyvalue); + lua_pop(base_t::lua_state(), 1); + } + } + + template + void for_each(std::false_type, Fx&& fx) const { + auto pp = stack::push_pop(*this); + stack::push(base_t::lua_state(), nil); + while (lua_next(base_t::lua_state(), -2)) { + sol::object key(base_t::lua_state(), -2); + sol::object value(base_t::lua_state(), -1); + fx(key, value); + lua_pop(base_t::lua_state(), 1); + } + } + + template + auto tuple_get(types, std::index_sequence<0, 1, I...>, Keys&& keys) const + -> decltype(stack::pop>(nullptr)) { + typedef decltype(stack::pop>(nullptr)) Tup; + return Tup( + traverse_get_optional(meta::is_specialization_of>(), detail::forward_get<0>(keys)), + traverse_get_optional(meta::is_specialization_of>(), detail::forward_get<1>(keys)), + traverse_get_optional(meta::is_specialization_of>(), detail::forward_get(keys))... + ); + } + + template + decltype(auto) tuple_get(types, std::index_sequence, Keys&& keys) const { + return traverse_get_optional(meta::is_specialization_of>(), detail::forward_get(keys)); + } + + template + void tuple_set(std::index_sequence, Pairs&& pairs) { + auto pp = stack::push_pop(pairs))...>::value)>(*this); + void(detail::swallow{ (stack::set_field(base_t::lua_state(), + detail::forward_get(pairs), + detail::forward_get(pairs), + lua_gettop(base_t::lua_state()) + ), 0)... }); + } + + template + decltype(auto) traverse_get_deep(Key&& key) const { + stack::get_field(base_t::lua_state(), std::forward(key)); + return stack::get(base_t::lua_state()); + } + + template + decltype(auto) traverse_get_deep(Key&& key, Keys&&... keys) const { + stack::get_field(base_t::lua_state(), std::forward(key)); + return traverse_get_deep(std::forward(keys)...); + } + + template + decltype(auto) traverse_get_deep_optional(int& popcount, Key&& key) const { + typedef decltype(stack::get(base_t::lua_state())) R; + auto p = stack::probe_get_field(base_t::lua_state(), std::forward(key), lua_gettop(base_t::lua_state())); + popcount += p.levels; + if (!p.success) + return R(nullopt); + return stack::get(base_t::lua_state()); + } + + template + decltype(auto) traverse_get_deep_optional(int& popcount, Key&& key, Keys&&... keys) const { + auto p = I > 0 ? stack::probe_get_field(base_t::lua_state(), std::forward(key), -1) : stack::probe_get_field(base_t::lua_state(), std::forward(key), lua_gettop(base_t::lua_state())); + popcount += p.levels; + if (!p.success) + return T(nullopt); + return traverse_get_deep_optional(popcount, std::forward(keys)...); + } + + template + decltype(auto) traverse_get_optional(std::false_type, Keys&&... keys) const { + detail::clean c(base_t::lua_state()); + return traverse_get_deep(std::forward(keys)...); + } + + template + decltype(auto) traverse_get_optional(std::true_type, Keys&&... keys) const { + int popcount = 0; + detail::ref_clean c(base_t::lua_state(), popcount); + return traverse_get_deep_optional(popcount, std::forward(keys)...); + } + + template + void traverse_set_deep(Key&& key, Value&& value) const { + stack::set_field(base_t::lua_state(), std::forward(key), std::forward(value)); + } + + template + void traverse_set_deep(Key&& key, Keys&&... keys) const { + stack::get_field(base_t::lua_state(), std::forward(key)); + traverse_set_deep(std::forward(keys)...); + } + + basic_table_core(lua_State* L, detail::global_tag t) noexcept : reference(L, t) { } + + public: + typedef basic_table_iterator iterator; + typedef iterator const_iterator; + + basic_table_core() noexcept : base_t() { } + template , basic_table_core>>, std::is_base_of>> = meta::enabler> + basic_table_core(T&& r) noexcept : base_t(std::forward(r)) { +#ifdef SOL_CHECK_ARGUMENTS + if (!is_table>::value) { + auto pp = stack::push_pop(*this); + stack::check(base_t::lua_state(), -1, type_panic); + } +#endif // Safety + } + basic_table_core(const basic_table_core&) = default; + basic_table_core(basic_table_core&&) = default; + basic_table_core& operator=(const basic_table_core&) = default; + basic_table_core& operator=(basic_table_core&&) = default; + basic_table_core(const stack_reference& r) : basic_table_core(r.lua_state(), r.stack_index()) {} + basic_table_core(stack_reference&& r) : basic_table_core(r.lua_state(), r.stack_index()) {} + basic_table_core(lua_State* L, int index = -1) : base_t(L, index) { +#ifdef SOL_CHECK_ARGUMENTS + stack::check(L, index, type_panic); +#endif // Safety + } + + iterator begin() const { + return iterator(*this); + } + + iterator end() const { + return iterator(); + } + + const_iterator cbegin() const { + return begin(); + } + + const_iterator cend() const { + return end(); + } + + template + decltype(auto) get(Keys&&... keys) const { + static_assert(sizeof...(Keys) == sizeof...(Ret), "number of keys and number of return types do not match"); + auto pp = stack::push_pop::value>(*this); + return tuple_get(types(), std::make_index_sequence(), std::forward_as_tuple(std::forward(keys)...)); + } + + template + decltype(auto) get_or(Key&& key, T&& otherwise) const { + typedef decltype(get("")) U; + sol::optional option = get>(std::forward(key)); + if (option) { + return static_cast(option.value()); + } + return static_cast(std::forward(otherwise)); + } + + template + decltype(auto) get_or(Key&& key, D&& otherwise) const { + sol::optional option = get>(std::forward(key)); + if (option) { + return static_cast(option.value()); + } + return static_cast(std::forward(otherwise)); + } + + template + decltype(auto) traverse_get(Keys&&... keys) const { + auto pp = stack::push_pop::value>(*this); + return traverse_get_optional(meta::is_specialization_of>(), std::forward(keys)...); + } + + template + basic_table_core& traverse_set(Keys&&... keys) { + auto pp = stack::push_pop::value>(*this); + traverse_set_deep(std::forward(keys)...); + lua_pop(base_t::lua_state(), static_cast(sizeof...(Keys)-2)); + return *this; + } + + template + basic_table_core& set(Args&&... args) { + tuple_set(std::make_index_sequence(), std::forward_as_tuple(std::forward(args)...)); + return *this; + } + + template + basic_table_core& set_usertype(usertype& user) { + return set_usertype(usertype_traits::name, user); + } + + template + basic_table_core& set_usertype(Key&& key, usertype& user) { + return set(std::forward(key), user); + } + + template + basic_table_core& new_usertype(const std::string& name, Args&&... args) { + usertype utype(std::forward(args)...); + set_usertype(name, utype); + return *this; + } + + template + basic_table_core& new_usertype(const std::string& name, Args&&... args) { + constructors> ctor{}; + return new_usertype(name, ctor, std::forward(args)...); + } + + template + basic_table_core& new_usertype(const std::string& name, constructors ctor, Args&&... args) { + usertype utype(ctor, std::forward(args)...); + set_usertype(name, utype); + return *this; + } + + template + basic_table_core& new_enum(const std::string& name, Args&&... args) { + if (read_only) { + table idx = create_with(std::forward(args)...); + table x = create_with( + meta_function::new_index, detail::fail_on_newindex, + meta_function::index, idx + ); + table target = create_named(name); + target[metatable_key] = x; + } + else { + create_named(name, std::forward(args)...); + } + return *this; + } + + template + void for_each(Fx&& fx) const { + typedef meta::is_invokable)> is_paired; + for_each(is_paired(), std::forward(fx)); + } + + size_t size() const { + auto pp = stack::push_pop(*this); + return lua_rawlen(base_t::lua_state(), -1); + } + + bool empty() const { + return cbegin() == cend(); + } + + template + proxy operator[](T&& key) & { + return proxy(*this, std::forward(key)); + } + + template + proxy operator[](T&& key) const & { + return proxy(*this, std::forward(key)); + } + + template + proxy operator[](T&& key) && { + return proxy(*this, std::forward(key)); + } + + template + basic_table_core& set_function(Key&& key, Args&&... args) { + set_fx(types(), std::forward(key), std::forward(args)...); + return *this; + } + + template + basic_table_core& set_function(Key&& key, Args&&... args) { + set_fx(types<>(), std::forward(key), std::forward(args)...); + return *this; + } + + template + basic_table_core& add(Args&&... args) { + auto pp = stack::push_pop(*this); + (void)detail::swallow{0, + (stack::set_ref(base_t::lua_state(), std::forward(args)), 0)... + }; + return *this; + } + + private: + template> + void set_fx(types, Key&& key, Fx&& fx) { + set_resolved_function(std::forward(key), std::forward(fx)); + } + + template>> = meta::enabler> + void set_fx(types<>, Key&& key, Fx&& fx) { + set(std::forward(key), std::forward(fx)); + } + + template>> = meta::enabler> + void set_fx(types<>, Key&& key, Fx&& fx, Args&&... args) { + set(std::forward(key), function_args(std::forward(fx), std::forward(args)...)); + } + + template + void set_resolved_function(Key&& key, Args&&... args) { + set(std::forward(key), function_args>(std::forward(args)...)); + } + + public: + static inline table create(lua_State* L, int narr = 0, int nrec = 0) { + lua_createtable(L, narr, nrec); + table result(L); + lua_pop(L, 1); + return result; + } + + template + static inline table create(lua_State* L, int narr, int nrec, Key&& key, Value&& value, Args&&... args) { + lua_createtable(L, narr, nrec); + table result(L); + result.set(std::forward(key), std::forward(value), std::forward(args)...); + lua_pop(L, 1); + return result; + } + + template + static inline table create_with(lua_State* L, Args&&... args) { + static const int narr = static_cast(meta::count_2_for_pack::value); + return create(L, narr, static_cast((sizeof...(Args) / 2) - narr), std::forward(args)...); + } + + table create(int narr = 0, int nrec = 0) { + return create(base_t::lua_state(), narr, nrec); + } + + template + table create(int narr, int nrec, Key&& key, Value&& value, Args&&... args) { + return create(base_t::lua_state(), narr, nrec, std::forward(key), std::forward(value), std::forward(args)...); + } + + template + table create(Name&& name, int narr = 0, int nrec = 0) { + table x = create(base_t::lua_state(), narr, nrec); + this->set(std::forward(name), x); + return x; + } + + template + table create(Name&& name, int narr, int nrec, Key&& key, Value&& value, Args&&... args) { + table x = create(base_t::lua_state(), narr, nrec, std::forward(key), std::forward(value), std::forward(args)...); + this->set(std::forward(name), x); + return x; + } + + template + table create_with(Args&&... args) { + return create_with(base_t::lua_state(), std::forward(args)...); + } + + template + table create_named(Name&& name, Args&&... args) { + static const int narr = static_cast(meta::count_2_for_pack::value); + return create(std::forward(name), narr, sizeof...(Args) / 2 - narr, std::forward(args)...); + } + }; +} // sol + +// end of sol\table_core.hpp + +namespace sol { + typedef table_core table; +} // sol + +// end of sol\table.hpp + +// beginning of sol\load_result.hpp + +namespace sol { + struct load_result : public proxy_base { + private: + lua_State* L; + int index; + int returncount; + int popcount; + load_status err; + + template + decltype(auto) tagged_get(types>) const { + if (!valid()) { + return sol::optional(nullopt); + } + return stack::get>(L, index); + } + + template + decltype(auto) tagged_get(types) const { +#ifdef SOL_CHECK_ARGUMENTS + if (!valid()) { + type_panic(L, index, type_of(L, index), type::none); + } +#endif // Check Argument Safety + return stack::get(L, index); + } + + sol::optional tagged_get(types>) const { + if (valid()) { + return nullopt; + } + return sol::error(detail::direct_error, stack::get(L, index)); + } + + sol::error tagged_get(types) const { +#ifdef SOL_CHECK_ARGUMENTS + if (valid()) { + type_panic(L, index, type_of(L, index), type::none); + } +#endif // Check Argument Safety + return sol::error(detail::direct_error, stack::get(L, index)); + } + + public: + load_result() = default; + load_result(lua_State* L, int index = -1, int returncount = 0, int popcount = 0, load_status err = load_status::ok) noexcept : L(L), index(index), returncount(returncount), popcount(popcount), err(err) { + + } + load_result(const load_result&) = default; + load_result& operator=(const load_result&) = default; + load_result(load_result&& o) noexcept : L(o.L), index(o.index), returncount(o.returncount), popcount(o.popcount), err(o.err) { + // Must be manual, otherwise destructor will screw us + // return count being 0 is enough to keep things clean + // but we will be thorough + o.L = nullptr; + o.index = 0; + o.returncount = 0; + o.popcount = 0; + o.err = load_status::syntax; + } + load_result& operator=(load_result&& o) noexcept { + L = o.L; + index = o.index; + returncount = o.returncount; + popcount = o.popcount; + err = o.err; + // Must be manual, otherwise destructor will screw us + // return count being 0 is enough to keep things clean + // but we will be thorough + o.L = nullptr; + o.index = 0; + o.returncount = 0; + o.popcount = 0; + o.err = load_status::syntax; + return *this; + } + + load_status status() const noexcept { + return err; + } + + bool valid() const noexcept { + return status() == load_status::ok; + } + + template + T get() const { + return tagged_get(types>()); + } + + template + decltype(auto) call(Args&&... args) { + return get().template call(std::forward(args)...); + } + + template + decltype(auto) operator()(Args&&... args) { + return call<>(std::forward(args)...); + } + + lua_State* lua_state() const noexcept { return L; }; + int stack_index() const noexcept { return index; }; + + ~load_result() { + stack::remove(L, index, popcount); + } + }; +} // sol + +// end of sol\load_result.hpp + +namespace sol { + enum class lib : char { + base, + package, + coroutine, + string, + os, + math, + table, + debug, + bit32, + io, + ffi, + jit, + count + }; + + class state_view { + private: + lua_State* L; + table reg; + global_table global; + + optional is_loaded_package(const std::string& key) { + auto loaded = reg.traverse_get>("_LOADED", key); + bool is53mod = loaded && !(loaded->is() && !loaded->as()); + if (is53mod) + return loaded; +#if SOL_LUA_VERSION <= 501 + auto loaded51 = global.traverse_get>("package", "loaded", key); + bool is51mod = loaded51 && !(loaded51->is() && !loaded51->as()); + if (is51mod) + return loaded51; +#endif + return nullopt; + } + + template + void ensure_package(const std::string& key, T&& sr) { +#if SOL_LUA_VERSION <= 501 + auto pkg = global["package"]; + if (!pkg.valid()) { + pkg = create_table_with("loaded", create_table_with(key, sr)); + } + else { + auto ld = pkg["loaded"]; + if (!ld.valid()) { + ld = create_table_with(key, sr); + } + else { + ld[key] = sr; + } + } +#endif + auto loaded = reg["_LOADED"]; + if (!loaded.valid()) { + loaded = create_table_with(key, sr); + } + else { + loaded[key] = sr; + } + } + + template + object require_core(const std::string& key, Fx&& action, bool create_global = true) { + optional loaded = is_loaded_package(key); + if (loaded) + return std::move(*loaded); + action(); + auto sr = stack::get(L); + if (create_global) + set(key, sr); + ensure_package(key, sr); + return stack::pop(L); + } + + public: + typedef global_table::iterator iterator; + typedef global_table::const_iterator const_iterator; + + state_view(lua_State* L) : + L(L), + reg(L, LUA_REGISTRYINDEX), + global(L, detail::global_) { + + } + + lua_State* lua_state() const { + return L; + } + + template + void open_libraries(Args&&... args) { + static_assert(meta::all_same::value, "all types must be libraries"); + if (sizeof...(args) == 0) { + luaL_openlibs(L); + return; + } + + lib libraries[1 + sizeof...(args)] = { lib::count, std::forward(args)... }; + + for (auto&& library : libraries) { + switch (library) { +#if SOL_LUA_VERSION <= 501 && defined(SOL_LUAJIT) + case lib::coroutine: +#endif // luajit opens coroutine base stuff + case lib::base: + luaL_requiref(L, "base", luaopen_base, 1); + lua_pop(L, 1); + break; + case lib::package: + luaL_requiref(L, "package", luaopen_package, 1); + lua_pop(L, 1); + break; +#if !defined(SOL_LUAJIT) + case lib::coroutine: +#if SOL_LUA_VERSION > 501 + luaL_requiref(L, "coroutine", luaopen_coroutine, 1); + lua_pop(L, 1); +#endif // Lua 5.2+ only + break; +#endif // Not LuaJIT + case lib::string: + luaL_requiref(L, "string", luaopen_string, 1); + lua_pop(L, 1); + break; + case lib::table: + luaL_requiref(L, "table", luaopen_table, 1); + lua_pop(L, 1); + break; + case lib::math: + luaL_requiref(L, "math", luaopen_math, 1); + lua_pop(L, 1); + break; + case lib::bit32: +#ifdef SOL_LUAJIT + luaL_requiref(L, "bit32", luaopen_bit, 1); + lua_pop(L, 1); +#elif SOL_LUA_VERSION == 502 + luaL_requiref(L, "bit32", luaopen_bit32, 1); + lua_pop(L, 1); +#else +#endif // Lua 5.2 only (deprecated in 5.3 (503)) + break; + case lib::io: + luaL_requiref(L, "io", luaopen_io, 1); + lua_pop(L, 1); + break; + case lib::os: + luaL_requiref(L, "os", luaopen_os, 1); + lua_pop(L, 1); + break; + case lib::debug: + luaL_requiref(L, "debug", luaopen_debug, 1); + lua_pop(L, 1); + break; + case lib::ffi: +#ifdef SOL_LUAJIT + luaL_requiref(L, "ffi", luaopen_ffi, 1); + lua_pop(L, 1); +#endif + break; + case lib::jit: +#ifdef SOL_LUAJIT + luaL_requiref(L, "jit", luaopen_jit, 1); + lua_pop(L, 1); +#endif + break; + case lib::count: + default: + break; + } + } + } + + object require(const std::string& key, lua_CFunction open_function, bool create_global = true) { + luaL_requiref(L, key.c_str(), open_function, create_global ? 1 : 0); + return stack::pop(L); + } + + object require_script(const std::string& key, const std::string& code, bool create_global = true) { + return require_core(key, [this, &code]() {stack::script(L, code); }, create_global); + } + + object require_file(const std::string& key, const std::string& filename, bool create_global = true) { + return require_core(key, [this, &filename]() {stack::script_file(L, filename); }, create_global); + } + + function_result script(const std::string& code) { + int index = (::std::max)(lua_gettop(L), 1); + stack::script(L, code); + int returns = lua_gettop(L) - (index - 1); + return function_result(L, index, returns); + } + + function_result script_file(const std::string& filename) { + int index = (::std::max)(lua_gettop(L), 1); + stack::script_file(L, filename); + int returns = lua_gettop(L) - index; + return function_result(L, index, returns); + } + + load_result load(const std::string& code) { + load_status x = static_cast(luaL_loadstring(L, code.c_str())); + return load_result(L, -1, 1, 1, x); + } + + load_result load_file(const std::string& filename) { + load_status x = static_cast(luaL_loadfile(L, filename.c_str())); + return load_result(L, -1, 1, 1, x); + } + + load_result load_buffer(const char *buff, size_t size, const char *name, const char* mode = nullptr) { + load_status x = static_cast(luaL_loadbufferx(L, buff, size, name, mode)); + return load_result(L, -1, 1, 1, x); + } + + iterator begin() const { + return global.begin(); + } + + iterator end() const { + return global.end(); + } + + const_iterator cbegin() const { + return global.cbegin(); + } + + const_iterator cend() const { + return global.cend(); + } + + global_table globals() const { + return global; + } + + table registry() const { + return reg; + } + + operator lua_State* () const { + return lua_state(); + } + + void set_panic(lua_CFunction panic) { + lua_atpanic(L, panic); + } + + template + decltype(auto) get(Keys&&... keys) const { + return global.get(std::forward(keys)...); + } + + template + decltype(auto) get_or(Key&& key, T&& otherwise) const { + return global.get_or(std::forward(key), std::forward(otherwise)); + } + + template + decltype(auto) get_or(Key&& key, D&& otherwise) const { + return global.get_or(std::forward(key), std::forward(otherwise)); + } + + template + state_view& set(Args&&... args) { + global.set(std::forward(args)...); + return *this; + } + + template + decltype(auto) traverse_get(Keys&&... keys) const { + return global.traverse_get(std::forward(keys)...); + } + + template + state_view& traverse_set(Args&&... args) { + global.traverse_set(std::forward(args)...); + return *this; + } + + template + state_view& set_usertype(usertype& user) { + return set_usertype(usertype_traits::name, user); + } + + template + state_view& set_usertype(Key&& key, usertype& user) { + global.set_usertype(std::forward(key), user); + return *this; + } + + template + state_view& new_usertype(const std::string& name, Args&&... args) { + global.new_usertype(name, std::forward(args)...); + return *this; + } + + template + state_view& new_usertype(const std::string& name, Args&&... args) { + global.new_usertype(name, std::forward(args)...); + return *this; + } + + template + state_view& new_enum(const std::string& name, Args&&... args) { + global.new_enum(name, std::forward(args)...); + return *this; + } + + template + state_view& new_usertype(const std::string& name, constructors ctor, Args&&... args) { + global.new_usertype(name, ctor, std::forward(args)...); + return *this; + } + + template + void for_each(Fx&& fx) { + global.for_each(std::forward(fx)); + } + + template + proxy operator[](T&& key) { + return global[std::forward(key)]; + } + + template + proxy operator[](T&& key) const { + return global[std::forward(key)]; + } + + template + state_view& set_function(Key&& key, Args&&... args) { + global.set_function(std::forward(key), std::forward(args)...); + return *this; + } + + template + state_view& set_function(Key&& key, Args&&... args) { + global.set_function(std::forward(key), std::forward(args)...); + return *this; + } + + template + table create_table(Name&& name, int narr = 0, int nrec = 0) { + return global.create(std::forward(name), narr, nrec); + } + + template + table create_table(Name&& name, int narr, int nrec, Key&& key, Value&& value, Args&&... args) { + return global.create(std::forward(name), narr, nrec, std::forward(key), std::forward(value), std::forward(args)...); + } + + template + table create_named_table(Name&& name, Args&&... args) { + table x = global.create_with(std::forward(args)...); + global.set(std::forward(name), x); + return x; + } + + table create_table(int narr = 0, int nrec = 0) { + return create_table(lua_state(), narr, nrec); + } + + template + table create_table(int narr, int nrec, Key&& key, Value&& value, Args&&... args) { + return create_table(lua_state(), narr, nrec, std::forward(key), std::forward(value), std::forward(args)...); + } + + template + table create_table_with(Args&&... args) { + return create_table_with(lua_state(), std::forward(args)...); + } + + static inline table create_table(lua_State* L, int narr = 0, int nrec = 0) { + return global_table::create(L, narr, nrec); + } + + template + static inline table create_table(lua_State* L, int narr, int nrec, Key&& key, Value&& value, Args&&... args) { + return global_table::create(L, narr, nrec, std::forward(key), std::forward(value), std::forward(args)...); + } + + template + static inline table create_table_with(lua_State* L, Args&&... args) { + return global_table::create_with(L, std::forward(args)...); + } + }; +} // sol + +// end of sol\state_view.hpp + +namespace sol { + inline int default_at_panic(lua_State* L) { +#ifdef SOL_NO_EXCEPTIONS + (void)L; + return -1; +#else + const char* message = lua_tostring(L, -1); + std::string err = message ? message : "An unexpected error occurred and forced the lua state to call atpanic"; + throw error(err); +#endif + } + + class state : private std::unique_ptr, public state_view { + private: + typedef std::unique_ptr unique_base; + public: + state(lua_CFunction panic = default_at_panic) : unique_base(luaL_newstate(), lua_close), + state_view(unique_base::get()) { + set_panic(panic); + stack::luajit_exception_handler(unique_base::get()); + } + + state(lua_CFunction panic, lua_Alloc alfunc, void* alpointer = nullptr) : unique_base(lua_newstate(alfunc, alpointer), lua_close), + state_view(unique_base::get()) { + set_panic(panic); + stack::luajit_exception_handler(unique_base::get()); + } + + using state_view::get; + }; +} // sol + +// end of sol\state.hpp + +// beginning of sol\coroutine.hpp + +// beginning of sol\thread.hpp + +namespace sol { + class thread : public reference { + public: + thread() noexcept = default; + thread(const thread&) = default; + thread(thread&&) = default; + thread(const stack_reference& r) : thread(r.lua_state(), r.stack_index()) {}; + thread(stack_reference&& r) : thread(r.lua_state(), r.stack_index()) {}; + thread& operator=(const thread&) = default; + thread& operator=(thread&&) = default; + thread(lua_State* L, int index = -1) : reference(L, index) { +#ifdef SOL_CHECK_ARGUMENTS + type_assert(L, index, type::thread); +#endif // Safety + } + + state_view state() const { + return state_view(this->thread_state()); + } + + lua_State* thread_state() const { + auto pp = stack::push_pop(*this); + lua_State* lthread = lua_tothread(lua_state(), -1); + return lthread; + } + + thread_status status() const { + lua_State* lthread = thread_state(); + thread_status lstat = static_cast(lua_status(lthread)); + if (lstat != thread_status::ok && lua_gettop(lthread) == 0) { + // No thing on the thread's stack means its dead + return thread_status::dead; + } + return lstat; + } + + thread create() { + return create(lua_state()); + } + + static thread create(lua_State* L) { + lua_newthread(L); + thread result(L); + lua_pop(L, 1); + return result; + } + }; +} // sol + +// end of sol\thread.hpp + +namespace sol { + class coroutine : public reference { + private: + call_status stats = call_status::yielded; + + void luacall(std::ptrdiff_t argcount, std::ptrdiff_t) { +#if SOL_LUA_VERSION < 502 + stats = static_cast(lua_resume(lua_state(), static_cast(argcount))); +#else + stats = static_cast(lua_resume(lua_state(), nullptr, static_cast(argcount))); +#endif // Lua 5.1 compat + } + + template + auto invoke(types, std::index_sequence, std::ptrdiff_t n) { + luacall(n, sizeof...(Ret)); + return stack::pop>(lua_state()); + } + + template + Ret invoke(types, std::index_sequence, std::ptrdiff_t n) { + luacall(n, 1); + return stack::pop(lua_state()); + } + + template + void invoke(types, std::index_sequence, std::ptrdiff_t n) { + luacall(n, 0); + } + + protected_function_result invoke(types<>, std::index_sequence<>, std::ptrdiff_t n) { + int stacksize = lua_gettop(lua_state()); + int firstreturn = (std::max)(1, stacksize - static_cast(n)); + luacall(n, LUA_MULTRET); + int poststacksize = lua_gettop(lua_state()); + int returncount = poststacksize - (firstreturn - 1); + return protected_function_result(lua_state(), firstreturn, returncount, returncount, status()); + } + + public: + coroutine() noexcept = default; + coroutine(const coroutine&) noexcept = default; + coroutine& operator=(const coroutine&) noexcept = default; + coroutine(lua_State* L, int index = -1) : reference(L, index) { +#ifdef SOL_CHECK_ARGUMENTS + stack::check(L, index, type_panic); +#endif // Safety + } + + call_status status() const noexcept { + return stats; + } + + bool error() const noexcept { + call_status cs = status(); + return cs != call_status::ok && cs != call_status::yielded; + } + + bool runnable() const noexcept { + return valid() + && (status() == call_status::yielded); + } + + explicit operator bool() const noexcept { + return runnable(); + } + + template + protected_function_result operator()(Args&&... args) { + return call<>(std::forward(args)...); + } + + template + decltype(auto) operator()(types, Args&&... args) { + return call(std::forward(args)...); + } + + template + decltype(auto) call(Args&&... args) { + push(); + int pushcount = stack::multi_push(lua_state(), std::forward(args)...); + return invoke(types(), std::make_index_sequence(), pushcount); + } + }; +} // sol + +// end of sol\coroutine.hpp + +#endif // SOL_SINGLE_INCLUDE_HPP