mirror of
https://github.com/ThePhD/sol2.git
synced 2024-03-22 13:10:44 +08:00
The solution technically works, but there's some stack corruption going on somewhere that I can quite track down, even when calling a void function with no parameters. I'll have to look into it...
This commit is contained in:
parent
cea7e9be64
commit
f67b21b525
125
sol/function.hpp
125
sol/function.hpp
|
@ -1,41 +1,84 @@
|
||||||
#ifndef SOL_FUNCTION_HPP
|
// The MIT License (MIT)
|
||||||
#define SOL_FUNCTION_HPP
|
|
||||||
|
// Copyright (c) 2013 Danny Y., Rapptz
|
||||||
#include "reference.hpp"
|
|
||||||
#include "tuple.hpp"
|
// Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||||
#include "stack.hpp"
|
// this software and associated documentation files (the "Software"), to deal in
|
||||||
|
// the Software without restriction, including without limitation the rights to
|
||||||
namespace sol {
|
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||||
class function : virtual public reference {
|
// the Software, and to permit persons to whom the Software is furnished to do so,
|
||||||
private:
|
// subject to the following conditions:
|
||||||
template<typename... Args>
|
|
||||||
void push_args(Args&&... args) {
|
// The above copyright notice and this permission notice shall be included in all
|
||||||
auto L = state();
|
// copies or substantial portions of the Software.
|
||||||
using swallow = char [];
|
|
||||||
void( swallow{ (stack::push(L, std::forward<Args>(args)), '\0')... } );
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
}
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||||
public:
|
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||||
function() : reference() {}
|
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||||
function(lua_State* L, int index = -1) : reference(L, index) {
|
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||||
type_assert(L, index, type::function);
|
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
}
|
|
||||||
|
#ifndef SOL_FUNCTION_HPP
|
||||||
template<typename T, typename... Args>
|
#define SOL_FUNCTION_HPP
|
||||||
T invoke(Args&&... args) {
|
|
||||||
push();
|
#include "reference.hpp"
|
||||||
push_args(std::forward<Args>(args)...);
|
#include "tuple.hpp"
|
||||||
lua_pcall(state(), sizeof...(Args), 1, 0);
|
#include "stack.hpp"
|
||||||
return stack::pop<T>(state());
|
|
||||||
}
|
namespace sol {
|
||||||
|
class function : virtual public reference {
|
||||||
template<typename... Args>
|
private:
|
||||||
void invoke(Args&&... args) {
|
template<typename... Args>
|
||||||
push();
|
void push_args(Args&&... args) const {
|
||||||
push_args(std::forward<Args>(args)...);
|
auto L = state();
|
||||||
lua_pcall(state(), sizeof...(Args), 0, 0);
|
using swallow = char[ ];
|
||||||
}
|
void(swallow{ (stack::push(L, std::forward<Args>(args)), '\0')... });
|
||||||
};
|
}
|
||||||
} // sol
|
|
||||||
|
template<typename... Ret>
|
||||||
|
struct invoker {
|
||||||
#endif // SOL_FUNCTION_HPP
|
template<typename... Args>
|
||||||
|
static std::tuple<Ret...> call( const function& ref, Args&&... args ) {
|
||||||
|
ref.push( );
|
||||||
|
ref.push_args( std::forward<Args>( args )... );
|
||||||
|
lua_pcall( ref.state( ), sizeof...( Args ), sizeof...( Ret ), 0 );
|
||||||
|
return std::make_tuple( stack::pop<Ret>( ref.state( ) )... );
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template<>
|
||||||
|
struct invoker<> {
|
||||||
|
template<typename... Args>
|
||||||
|
static void call( const function& ref, Args&&... args ) {
|
||||||
|
ref.push( );
|
||||||
|
ref.push_args( std::forward<Args>( args )... );
|
||||||
|
lua_pcall( ref.state( ), sizeof...( Args ), 0, 0 );
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
struct invoker<T> {
|
||||||
|
template<typename... Args>
|
||||||
|
static T call( const function& ref, Args&&... args ) {
|
||||||
|
ref.push( );
|
||||||
|
ref.push_args( std::forward<Args>( args )... );
|
||||||
|
lua_pcall( ref.state( ), sizeof...( Args ), 1, 0 );
|
||||||
|
return stack::pop<T>( ref.state( ) );
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
public:
|
||||||
|
function() : reference() {}
|
||||||
|
function(lua_State* L, int index = -1) : reference(L, index) {
|
||||||
|
type_assert(L, index, type::function);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename... Ret, typename... Args>
|
||||||
|
auto invoke( Args&&... args ) {
|
||||||
|
return invoker<Ret...>::call( *this, std::forward<Args>( args )... );
|
||||||
|
}
|
||||||
|
};
|
||||||
|
} // sol
|
||||||
|
|
||||||
|
#endif // SOL_FUNCTION_HPP
|
||||||
|
|
102
sol/functional.hpp
Normal file
102
sol/functional.hpp
Normal file
|
@ -0,0 +1,102 @@
|
||||||
|
// The MIT License (MIT)
|
||||||
|
|
||||||
|
// Copyright (c) 2013 Danny Y., Rapptz
|
||||||
|
|
||||||
|
// 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.
|
||||||
|
|
||||||
|
#include "tuple.hpp"
|
||||||
|
|
||||||
|
#ifndef SOL_FUNCTIONAL_HPP
|
||||||
|
#define SOL_FUNCTIONAL_HPP
|
||||||
|
|
||||||
|
namespace sol {
|
||||||
|
namespace detail {
|
||||||
|
|
||||||
|
template<typename TFuncSignature>
|
||||||
|
struct function_traits;
|
||||||
|
|
||||||
|
template<typename T, typename R, typename... Tn>
|
||||||
|
struct function_traits<R( T::* )( Tn... )> {
|
||||||
|
static const std::size_t arity = sizeof...( Tn );
|
||||||
|
static const bool is_member_function = true;
|
||||||
|
typedef std::tuple<Tn...> arg_tuple_t;
|
||||||
|
typedef types<Tn...> args_t;
|
||||||
|
typedef R( T::* func_t )( Tn... );
|
||||||
|
typedef R( T::* func_pointer_t )( Tn... );
|
||||||
|
typedef R return_t;
|
||||||
|
template <std::size_t i>
|
||||||
|
using arg_n = std::tuple_element<i, arg_tuple_t>;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename T, typename R, typename... Tn>
|
||||||
|
struct function_traits<R( T::* )( Tn... ) const> {
|
||||||
|
static const std::size_t arity = sizeof...( Tn );
|
||||||
|
static const bool is_member_function = true;
|
||||||
|
typedef std::tuple<Tn...> arg_tuple_t;
|
||||||
|
typedef types<Tn...> args_t;
|
||||||
|
typedef R( T::* func_t )( Tn... );
|
||||||
|
typedef R( T::* func_pointer_t )( Tn... );
|
||||||
|
typedef R return_t;
|
||||||
|
template <std::size_t i>
|
||||||
|
using arg_n = std::tuple_element<i, arg_tuple_t>;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename R, typename... Tn>
|
||||||
|
struct function_traits<R( Tn... )> {
|
||||||
|
static const std::size_t arity = sizeof...( Tn );
|
||||||
|
static const bool is_member_function = false;
|
||||||
|
typedef std::tuple<Tn...> arg_tuple_t;
|
||||||
|
typedef types<Tn...> args_t;
|
||||||
|
typedef R( func_t )( Tn... );
|
||||||
|
typedef R( *func_pointer_t )( Tn... );
|
||||||
|
typedef R return_t;
|
||||||
|
template <std::size_t i>
|
||||||
|
using arg_n = std::tuple_element<i, arg_tuple_t>;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename R, typename... Tn>
|
||||||
|
struct function_traits<R( *)( Tn... )> {
|
||||||
|
static const std::size_t arity = sizeof...( Tn );
|
||||||
|
static const bool is_member_function = false;
|
||||||
|
typedef std::tuple<Tn...> arg_tuple_t;
|
||||||
|
typedef types<Tn...> args_t;
|
||||||
|
typedef R( func_t )( Tn... );
|
||||||
|
typedef R( *func_pointer_t )( Tn... );
|
||||||
|
typedef R return_t;
|
||||||
|
template <std::size_t i>
|
||||||
|
using arg_n = std::tuple_element<i, arg_tuple_t>;
|
||||||
|
};
|
||||||
|
|
||||||
|
using std::get;
|
||||||
|
|
||||||
|
template<typename Function, typename Tuple, size_t... Indices>
|
||||||
|
inline auto call( Function f, const Tuple& t, indices<Indices...> ) -> decltype( f( get<Indices>( t )... ) ) {
|
||||||
|
return f( get<Indices>( t )... );
|
||||||
|
}
|
||||||
|
|
||||||
|
} // detail
|
||||||
|
|
||||||
|
template<typename Function, typename... Args>
|
||||||
|
inline auto call( Function f, const std::tuple<Args...>& t ) -> decltype( detail::call( f, t, detail::build_indices<sizeof...( Args )>{} ) ) {
|
||||||
|
return call( f, t, detail::build_indices<sizeof...( Args )>{} );
|
||||||
|
}
|
||||||
|
|
||||||
|
} // sol
|
||||||
|
|
||||||
|
|
||||||
|
#endif // SOL_FUNCTIONAL_HPP
|
138
sol/lua_function.hpp
Normal file
138
sol/lua_function.hpp
Normal file
|
@ -0,0 +1,138 @@
|
||||||
|
// The MIT License (MIT)
|
||||||
|
|
||||||
|
// Copyright (c) 2013 Danny Y., Rapptz
|
||||||
|
|
||||||
|
// Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||||
|
// this software and associated documentation files (the "Software"), to deal in
|
||||||
|
// the Software without restriction, including without limitation the rights to
|
||||||
|
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||||
|
// the Software, and to permit persons to whom the Software is furnished to do so,
|
||||||
|
// subject to the following conditions:
|
||||||
|
|
||||||
|
// The above copyright notice and this permission notice shall be included in all
|
||||||
|
// copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||||
|
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||||
|
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||||
|
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||||
|
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
|
#ifndef SOL_LUA_FUNC_HPP
|
||||||
|
#define SOL_LUA_FUNC_HPP
|
||||||
|
|
||||||
|
#include "functional.hpp"
|
||||||
|
#include "stack.hpp"
|
||||||
|
|
||||||
|
namespace sol {
|
||||||
|
namespace detail {
|
||||||
|
|
||||||
|
struct lua_func {
|
||||||
|
virtual int operator () ( lua_State* L ) {
|
||||||
|
throw sol_error( "Failure to call specialized wrapped C++ function from lua" );
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual ~lua_func( ) {};
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename TFx>
|
||||||
|
struct lambda_lua_func : public lua_func {
|
||||||
|
typedef decltype( &TFx::operator() ) fx_t;
|
||||||
|
typedef function_traits<fx_t> fx_traits;
|
||||||
|
TFx fx;
|
||||||
|
|
||||||
|
template <typename... TFxn>
|
||||||
|
lambda_lua_func( TFxn&&... fxn ) : fx( std::forward<TFxn>( fxn )... ) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual int operator () ( lua_State* L ) override {
|
||||||
|
return ( *this )( tuple_types<fx_traits::return_t>( ), fx_traits::args_t( ), L );
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename... Tn>
|
||||||
|
int operator()( types<void>, types<Tn...> t, lua_State* L ) {
|
||||||
|
auto r = stack::pop_call( L, fx, t );
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename... TRn, typename... Tn>
|
||||||
|
int operator()( types<TRn...>, types<Tn...> t, lua_State* L ) {
|
||||||
|
auto r = stack::pop_call( L, fx, t );
|
||||||
|
stack::push( L, r );
|
||||||
|
return sizeof...( TRn );
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename TFx, typename T = TFx, bool is_member_pointer = std::is_member_function_pointer<TFx>::value>
|
||||||
|
struct explicit_lua_func : public lua_func {
|
||||||
|
typedef typename std::remove_pointer<typename std::decay<TFx>::type>::type fx_t;
|
||||||
|
typedef function_traits<fx_t> fx_traits;
|
||||||
|
TFx fx;
|
||||||
|
|
||||||
|
template <typename... TFxn>
|
||||||
|
explicit_lua_func( TFxn&&... fxn ) : fx( std::forward<TFxn>( fxn )... ) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual int operator () ( lua_State* L ) override {
|
||||||
|
return ( *this )( tuple_types<fx_traits::return_t>( ), fx_traits::args_t( ), L );
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename... Tn>
|
||||||
|
int operator () ( types<void>, types<Tn...> t, lua_State* L ) {
|
||||||
|
auto r = stack::pop_call( L, fx, t );
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename... TRn, typename... Tn>
|
||||||
|
int operator () ( types<TRn...>, types<Tn...> t, lua_State* L ) {
|
||||||
|
auto r = stack::pop_call( L, fx, t );
|
||||||
|
stack::push( L, r );
|
||||||
|
return sizeof...( TRn );
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename TFx, typename T>
|
||||||
|
struct explicit_lua_func<TFx, T, true> : public lua_func {
|
||||||
|
typedef typename std::remove_pointer<typename std::decay<TFx>::type>::type fx_t;
|
||||||
|
typedef function_traits<fx_t> fx_traits;
|
||||||
|
T* member;
|
||||||
|
TFx fx;
|
||||||
|
|
||||||
|
template <typename... TFxn>
|
||||||
|
explicit_lua_func( T* m, TFxn&&... fxn ) : member(m), fx( std::forward<TFxn>( fxn )... ) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual int operator () ( lua_State* L ) override {
|
||||||
|
return ( *this )( tuple_types<fx_traits::return_t>( ), fx_traits::args_t( ), L );
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename... Tn>
|
||||||
|
int operator () ( types<void>, types<Tn...>, lua_State* L ) {
|
||||||
|
auto r = stack::pop_call( L, fx, t );
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename... TRn, typename... Tn>
|
||||||
|
int operator () ( types<TRn...>, types<Tn...> t, lua_State* L ) {
|
||||||
|
auto r = stack::pop_call( L, fx, t );
|
||||||
|
stack::push( L, r );
|
||||||
|
return sizeof...( TRn );
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
int lua_cfun( lua_State* L ) {
|
||||||
|
void* bridgedata = lua_touserdata( L, lua_upvalueindex( 1 ) );
|
||||||
|
auto* fx = static_cast<lua_func*>( bridgedata );
|
||||||
|
int r = fx->operator()( L );
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // detail
|
||||||
|
} // sol
|
||||||
|
|
||||||
|
#endif // SOL_LUA_FUNC_HPP
|
|
@ -23,6 +23,7 @@
|
||||||
#define SOL_STACK_HPP
|
#define SOL_STACK_HPP
|
||||||
|
|
||||||
#include "reference.hpp"
|
#include "reference.hpp"
|
||||||
|
#include "tuple.hpp"
|
||||||
#include <utility>
|
#include <utility>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
|
|
||||||
|
@ -35,6 +36,19 @@ using DisableIf = typename std::enable_if<!T::value, R>::type;
|
||||||
|
|
||||||
namespace stack {
|
namespace stack {
|
||||||
namespace detail {
|
namespace detail {
|
||||||
|
template<class T, class F, class... Vs>
|
||||||
|
auto ltr_pop( T&& extra, F f, types<>, Vs&&... vs )
|
||||||
|
-> decltype( f( std::forward<Vs>( vs )... ) ) {
|
||||||
|
return f( std::forward<Vs>( vs )... );
|
||||||
|
}
|
||||||
|
|
||||||
|
// take head, produce value from it, pass after other values
|
||||||
|
template<class F, class Head, class... Tail, class... Vs>
|
||||||
|
auto ltr_pop( lua_State* L, F f, types<Head, Tail...>, Vs&&... vs )
|
||||||
|
-> decltype( ltr_pop( L, f, types<Tail...>{}, std::forward<Vs>( vs )..., stack::pop<Head>( L ) ) ) {
|
||||||
|
return ltr_pop( L, f, types<Tail...>{}, std::forward<Vs>( vs )..., stack::pop<Head>( L ) );
|
||||||
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
inline T get_unsigned(lua_State* L, std::true_type, int index = -1) {
|
inline T get_unsigned(lua_State* L, std::true_type, int index = -1) {
|
||||||
return lua_tounsigned(L, index);
|
return lua_tounsigned(L, index);
|
||||||
|
@ -150,6 +164,25 @@ inline void push(lua_State* L, const char (&str)[N]) {
|
||||||
inline void push(lua_State* L, const std::string& str) {
|
inline void push(lua_State* L, const std::string& str) {
|
||||||
lua_pushlstring(L, str.c_str(), str.size());
|
lua_pushlstring(L, str.c_str(), str.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
namespace detail {
|
||||||
|
template<typename T, std::size_t... I>
|
||||||
|
inline void push( lua_State* L, indices<I...>, const T& tuplen ) {
|
||||||
|
using swallow = char [];
|
||||||
|
void( swallow{ ( push( L, std::get<I>( tuplen ) ), '\0' )... } );
|
||||||
|
}
|
||||||
|
} // detail
|
||||||
|
|
||||||
|
template<typename... Args>
|
||||||
|
inline void push(lua_State* L, const std::tuple<Args...>& tuplen) {
|
||||||
|
detail::push(L, sol::detail::build_indices<sizeof...( Args )>(), tuplen);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename... Args, typename TFx>
|
||||||
|
inline auto pop_call( lua_State* L, TFx&& fx, types<Args...> t )->decltype( detail::ltr_pop( L, fx, t ) ) {
|
||||||
|
return detail::ltr_pop( L, fx, t );
|
||||||
|
}
|
||||||
|
|
||||||
} // stack
|
} // stack
|
||||||
} // sol
|
} // sol
|
||||||
|
|
||||||
|
|
378
sol/state.hpp
378
sol/state.hpp
|
@ -1,186 +1,194 @@
|
||||||
// The MIT License (MIT)
|
// The MIT License (MIT)
|
||||||
|
|
||||||
// Copyright (c) 2013 Danny Y., Rapptz
|
// Copyright (c) 2013 Danny Y., Rapptz
|
||||||
|
|
||||||
// Permission is hereby granted, free of charge, to any person obtaining a copy of
|
// 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
|
// this software and associated documentation files (the "Software"), to deal in
|
||||||
// the Software without restriction, including without limitation the rights to
|
// the Software without restriction, including without limitation the rights to
|
||||||
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
// 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,
|
// the Software, and to permit persons to whom the Software is furnished to do so,
|
||||||
// subject to the following conditions:
|
// subject to the following conditions:
|
||||||
|
|
||||||
// The above copyright notice and this permission notice shall be included in all
|
// The above copyright notice and this permission notice shall be included in all
|
||||||
// copies or substantial portions of the Software.
|
// copies or substantial portions of the Software.
|
||||||
|
|
||||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||||
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
// 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
|
// 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
|
// 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.
|
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
#ifndef SOL_STATE_HPP
|
#ifndef SOL_STATE_HPP
|
||||||
#define SOL_STATE_HPP
|
#define SOL_STATE_HPP
|
||||||
|
|
||||||
#include "error.hpp"
|
#include "error.hpp"
|
||||||
#include "table.hpp"
|
#include "table.hpp"
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
namespace sol {
|
namespace sol {
|
||||||
namespace detail {
|
namespace detail {
|
||||||
template<class T, class...>
|
template<class T, class...>
|
||||||
struct are_same : std::true_type {};
|
struct are_same : std::true_type {};
|
||||||
|
|
||||||
template<class T, class U, class... Args>
|
template<class T, class U, class... Args>
|
||||||
struct are_same<T, U, Args...> : std::integral_constant<bool, std::is_same<T,U>::value && are_same<T, Args...>::value> {};
|
struct are_same<T, U, Args...> : std::integral_constant<bool, std::is_same<T,U>::value && are_same<T, Args...>::value> {};
|
||||||
|
|
||||||
int atpanic(lua_State* L) {
|
int atpanic(lua_State* L) {
|
||||||
throw sol_error(lua_tostring(L, -1));
|
throw sol_error(lua_tostring(L, -1));
|
||||||
}
|
}
|
||||||
} // detail
|
} // detail
|
||||||
|
|
||||||
enum class lib : char {
|
enum class lib : char {
|
||||||
base,
|
base,
|
||||||
package,
|
package,
|
||||||
coroutine,
|
coroutine,
|
||||||
string,
|
string,
|
||||||
os,
|
os,
|
||||||
math,
|
math,
|
||||||
table,
|
table,
|
||||||
debug,
|
debug,
|
||||||
bit32,
|
bit32,
|
||||||
io,
|
io,
|
||||||
count
|
count
|
||||||
};
|
};
|
||||||
|
|
||||||
class state {
|
class state {
|
||||||
private:
|
private:
|
||||||
std::unique_ptr<lua_State, void(*)(lua_State*)> L;
|
std::unique_ptr<lua_State, void(*)(lua_State*)> L;
|
||||||
table reg;
|
table reg;
|
||||||
table global;
|
table global;
|
||||||
public:
|
std::unordered_map<std::string, std::unique_ptr<detail::lua_func>> funcs;
|
||||||
state():
|
|
||||||
L(luaL_newstate(), lua_close),
|
public:
|
||||||
reg(L.get(), LUA_REGISTRYINDEX),
|
state():
|
||||||
global(reg.get<table>(LUA_RIDX_GLOBALS)) {
|
L(luaL_newstate(), lua_close),
|
||||||
lua_atpanic(L.get(), detail::atpanic);
|
reg(L.get(), LUA_REGISTRYINDEX),
|
||||||
}
|
global(reg.get<table>(LUA_RIDX_GLOBALS)) {
|
||||||
|
lua_atpanic(L.get(), detail::atpanic);
|
||||||
state(const std::string& filename):
|
}
|
||||||
L(luaL_newstate(), lua_close),
|
|
||||||
reg(L.get(), LUA_REGISTRYINDEX),
|
state(const std::string& filename):
|
||||||
global(reg.get<table>(LUA_RIDX_GLOBALS)) {
|
L(luaL_newstate(), lua_close),
|
||||||
lua_atpanic(L.get(), detail::atpanic);
|
reg(L.get(), LUA_REGISTRYINDEX),
|
||||||
open_file(filename);
|
global(reg.get<table>(LUA_RIDX_GLOBALS)) {
|
||||||
}
|
lua_atpanic(L.get(), detail::atpanic);
|
||||||
|
open_file(filename);
|
||||||
template<typename... Args>
|
}
|
||||||
void open_libraries(Args&&... args) {
|
|
||||||
static_assert(detail::are_same<lib, Args...>{}, "all types must be libraries");
|
template<typename... Args>
|
||||||
if(sizeof...(args) == 0) {
|
void open_libraries(Args&&... args) {
|
||||||
luaL_openlibs(L.get());
|
static_assert(detail::are_same<lib, Args...>{}, "all types must be libraries");
|
||||||
return;
|
if(sizeof...(args) == 0) {
|
||||||
}
|
luaL_openlibs(L.get());
|
||||||
|
return;
|
||||||
lib libraries[1 + sizeof...(args)] = { lib::count, std::forward<Args>(args)... };
|
}
|
||||||
|
|
||||||
for(auto&& library : libraries) {
|
lib libraries[1 + sizeof...(args)] = { lib::count, std::forward<Args>(args)... };
|
||||||
switch(library) {
|
|
||||||
case lib::base:
|
for(auto&& library : libraries) {
|
||||||
luaL_requiref(L.get(), "base", luaopen_base, 1);
|
switch(library) {
|
||||||
break;
|
case lib::base:
|
||||||
case lib::package:
|
luaL_requiref(L.get(), "base", luaopen_base, 1);
|
||||||
luaL_requiref(L.get(), "package", luaopen_package, 1);
|
break;
|
||||||
break;
|
case lib::package:
|
||||||
case lib::coroutine:
|
luaL_requiref(L.get(), "package", luaopen_package, 1);
|
||||||
luaL_requiref(L.get(), "coroutine", luaopen_coroutine, 1);
|
break;
|
||||||
break;
|
case lib::coroutine:
|
||||||
case lib::string:
|
luaL_requiref(L.get(), "coroutine", luaopen_coroutine, 1);
|
||||||
luaL_requiref(L.get(), "string", luaopen_string, 1);
|
break;
|
||||||
break;
|
case lib::string:
|
||||||
case lib::table:
|
luaL_requiref(L.get(), "string", luaopen_string, 1);
|
||||||
luaL_requiref(L.get(), "table", luaopen_table, 1);
|
break;
|
||||||
break;
|
case lib::table:
|
||||||
case lib::math:
|
luaL_requiref(L.get(), "table", luaopen_table, 1);
|
||||||
luaL_requiref(L.get(), "math", luaopen_math, 1);
|
break;
|
||||||
break;
|
case lib::math:
|
||||||
case lib::bit32:
|
luaL_requiref(L.get(), "math", luaopen_math, 1);
|
||||||
luaL_requiref(L.get(), "bit32", luaopen_bit32, 1);
|
break;
|
||||||
break;
|
case lib::bit32:
|
||||||
case lib::io:
|
luaL_requiref(L.get(), "bit32", luaopen_bit32, 1);
|
||||||
luaL_requiref(L.get(), "io", luaopen_io, 1);
|
break;
|
||||||
break;
|
case lib::io:
|
||||||
case lib::os:
|
luaL_requiref(L.get(), "io", luaopen_io, 1);
|
||||||
luaL_requiref(L.get(), "os", luaopen_os, 1);
|
break;
|
||||||
break;
|
case lib::os:
|
||||||
case lib::debug:
|
luaL_requiref(L.get(), "os", luaopen_os, 1);
|
||||||
luaL_requiref(L.get(), "debug", luaopen_debug, 1);
|
break;
|
||||||
break;
|
case lib::debug:
|
||||||
case lib::count:
|
luaL_requiref(L.get(), "debug", luaopen_debug, 1);
|
||||||
break;
|
break;
|
||||||
}
|
case lib::count:
|
||||||
}
|
break;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
void script(const std::string& code) {
|
}
|
||||||
if(luaL_dostring(L.get(), code.c_str())) {
|
|
||||||
lua_error(L.get());
|
void script(const std::string& code) {
|
||||||
}
|
if(luaL_dostring(L.get(), code.c_str())) {
|
||||||
}
|
lua_error(L.get());
|
||||||
|
}
|
||||||
void open_file(const std::string& filename) {
|
}
|
||||||
if(luaL_dofile(L.get(), filename.c_str())) {
|
|
||||||
lua_error(L.get());
|
void open_file(const std::string& filename) {
|
||||||
}
|
if(luaL_dofile(L.get(), filename.c_str())) {
|
||||||
}
|
lua_error(L.get());
|
||||||
|
}
|
||||||
template<typename T, typename U>
|
}
|
||||||
T get(U&& key) const {
|
|
||||||
return global.get<T>(std::forward<U>(key));
|
template<typename T, typename U>
|
||||||
}
|
T get(U&& key) const {
|
||||||
|
return global.get<T>(std::forward<U>(key));
|
||||||
template<typename T, typename U>
|
}
|
||||||
state& set(T&& key, U&& value) {
|
|
||||||
global.set(std::forward<T>(key), std::forward<U>(value));
|
template<typename T, typename U>
|
||||||
return *this;
|
state& set(T&& key, U&& value) {
|
||||||
}
|
global.set(std::forward<T>(key), std::forward<U>(value));
|
||||||
|
return *this;
|
||||||
template<typename T>
|
}
|
||||||
table create_table(T&& key, int narr = 0, int nrec = 0) {
|
|
||||||
if(narr == 0 && nrec == 0) {
|
template<typename T, typename TFx>
|
||||||
lua_newtable(L.get());
|
state& set_function( T&& key, TFx&& fx ) {
|
||||||
}
|
global.set_function( std::forward<T>( key ), std::forward<TFx>( fx ) );
|
||||||
else {
|
return *this;
|
||||||
lua_createtable(L.get(), narr, nrec);
|
}
|
||||||
}
|
|
||||||
|
template<typename T>
|
||||||
table result(L.get());
|
table create_table(T&& key, int narr = 0, int nrec = 0) {
|
||||||
lua_pop(L.get(), 1);
|
if(narr == 0 && nrec == 0) {
|
||||||
global.set(std::forward<T>(key), result);
|
lua_newtable(L.get());
|
||||||
return result;
|
}
|
||||||
}
|
else {
|
||||||
|
lua_createtable(L.get(), narr, nrec);
|
||||||
table create_table(int narr = 0, int nrec = 0) {
|
}
|
||||||
if(narr == 0 && nrec == 0) {
|
|
||||||
lua_newtable(L.get());
|
table result(L.get());
|
||||||
}
|
lua_pop(L.get(), 1);
|
||||||
else {
|
global.set(std::forward<T>(key), result);
|
||||||
lua_createtable(L.get(), narr, nrec);
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
table result(L.get());
|
table create_table(int narr = 0, int nrec = 0) {
|
||||||
lua_pop(L.get(), 1);
|
if(narr == 0 && nrec == 0) {
|
||||||
return result;
|
lua_newtable(L.get());
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
table global_table() const {
|
lua_createtable(L.get(), narr, nrec);
|
||||||
return global;
|
}
|
||||||
}
|
|
||||||
|
table result(L.get());
|
||||||
table registry() const {
|
lua_pop(L.get(), 1);
|
||||||
return reg;
|
return result;
|
||||||
}
|
}
|
||||||
};
|
|
||||||
} // sol
|
table global_table() const {
|
||||||
|
return global;
|
||||||
|
}
|
||||||
|
|
||||||
|
table registry() const {
|
||||||
|
return reg;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
} // sol
|
||||||
|
|
||||||
#endif // SOL_STATE_HPP
|
#endif // SOL_STATE_HPP
|
|
@ -23,9 +23,14 @@
|
||||||
#define SOL_TABLE_HPP
|
#define SOL_TABLE_HPP
|
||||||
|
|
||||||
#include "stack.hpp"
|
#include "stack.hpp"
|
||||||
|
#include "lua_function.hpp"
|
||||||
|
#include <unordered_map>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
namespace sol {
|
namespace sol {
|
||||||
class table : virtual public reference {
|
class table : virtual public reference {
|
||||||
|
private:
|
||||||
|
std::unordered_map<std::string, std::shared_ptr<detail::lua_func>> funcs;
|
||||||
public:
|
public:
|
||||||
table() noexcept: reference{} {}
|
table() noexcept: reference{} {}
|
||||||
table(lua_State* L, int index = -1): reference(L, index) {
|
table(lua_State* L, int index = -1): reference(L, index) {
|
||||||
|
@ -53,10 +58,55 @@ public:
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename T, typename TFx>
|
||||||
|
table& set_function( T&& key, TFx&& fx ) {
|
||||||
|
typedef typename std::remove_pointer<typename std::decay<TFx>::type>::type clean_fx;
|
||||||
|
const static bool isfunction = std::is_function<clean_fx>::value;
|
||||||
|
return set_function( std::integral_constant<bool, !isfunction>( ),
|
||||||
|
std::forward<T>( key ), std::forward<TFx>( fx ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T, typename TFx>
|
||||||
|
table& set_function( std::true_type, T&& key, TFx&& fx ) {
|
||||||
|
typedef typename std::decay<TFx>::type clean_fx;
|
||||||
|
std::string fkey( key );
|
||||||
|
lua_CFunction freefunc = &detail::lua_cfun;
|
||||||
|
auto hint = funcs.find( fkey );
|
||||||
|
detail::lua_func* target = nullptr;
|
||||||
|
if ( hint == funcs.end( ) ) {
|
||||||
|
std::shared_ptr<detail::lua_func> sptr( new detail::lambda_lua_func<clean_fx>( std::forward<TFx>( fx ) ) );
|
||||||
|
hint = funcs.emplace_hint( hint, fkey, std::move( sptr ) );
|
||||||
|
}
|
||||||
|
target = hint->second.get( );
|
||||||
|
lua_pushlightuserdata( state( ), static_cast<void*>( target ) );
|
||||||
|
lua_pushcclosure( state( ), freefunc, 1 );
|
||||||
|
lua_setglobal( state( ), fkey.c_str( ) );
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T, typename TFx>
|
||||||
|
table& set_function( std::false_type, T&& key, TFx&& fx ) {
|
||||||
|
typedef typename std::decay<TFx>::type clean_fx;
|
||||||
|
std::string fkey( key );
|
||||||
|
lua_CFunction freefunc = &detail::lua_cfun;
|
||||||
|
auto hint = funcs.find( fkey );
|
||||||
|
detail::lua_func* target = nullptr;
|
||||||
|
if ( hint == funcs.end( ) ) {
|
||||||
|
std::shared_ptr<detail::lua_func> sptr( new detail::explicit_lua_func<TFx>( std::forward<TFx>( fx ) ) );
|
||||||
|
hint = funcs.emplace_hint( hint, fkey, std::move( sptr ) );
|
||||||
|
}
|
||||||
|
target = hint->second.get( );
|
||||||
|
lua_pushlightuserdata( state( ), static_cast<void*>( target ) );
|
||||||
|
lua_pushcclosure( state( ), freefunc, 1 );
|
||||||
|
lua_setglobal( state( ), fkey.c_str( ) );
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
size_t size() const {
|
size_t size() const {
|
||||||
push();
|
push();
|
||||||
return lua_rawlen(state(), -1);
|
return lua_rawlen(state(), -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
} // sol
|
} // sol
|
||||||
|
|
||||||
|
|
|
@ -26,28 +26,25 @@
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
|
|
||||||
namespace sol {
|
namespace sol {
|
||||||
namespace detail {
|
|
||||||
template<size_t... Ns>
|
template<size_t... Ns>
|
||||||
struct indices {};
|
struct indices { };
|
||||||
|
|
||||||
template<size_t N, size_t... Ns>
|
template<size_t N, size_t... Ns>
|
||||||
struct build_indices : build_indices<N-1, N-1, Ns...> {};
|
struct build_indices : build_indices<N - 1, N - 1, Ns...> { };
|
||||||
|
|
||||||
template<size_t... Ns>
|
template<size_t... Ns>
|
||||||
struct build_indices<0, Ns...> : indices<Ns...> {};
|
struct build_indices<0, Ns...> : indices<Ns...>{ };
|
||||||
|
|
||||||
using std::get;
|
template<typename... Args>
|
||||||
|
struct types : build_indices<sizeof...( Args )> { };
|
||||||
|
|
||||||
template<typename Function, typename Tuple, size_t... Indices>
|
template<typename... Args>
|
||||||
inline auto call(Function f, const Tuple& t, indices<Indices...>) -> decltype(f(get<Indices>(t)...)) {
|
struct tuple_types : types<Args...>, std::false_type { };
|
||||||
return f(get<Indices>(t)...);
|
|
||||||
}
|
template <typename... Args>
|
||||||
} // detail
|
struct tuple_types<std::tuple<Args...>> : types<Args...>, std::true_type{ };
|
||||||
|
|
||||||
template<typename Function, typename... Args>
|
|
||||||
inline auto call(Function f, const std::tuple<Args...>& t) -> decltype(detail::call(f, t, detail::build_indices<sizeof...(Args)>{})) {
|
|
||||||
return call(f, t, detail::build_indices<sizeof...(Args)>{});
|
|
||||||
}
|
|
||||||
} // sol
|
} // sol
|
||||||
|
|
||||||
#endif // SOL_TUPLE_HPP
|
#endif // SOL_TUPLE_HPP
|
|
@ -28,7 +28,9 @@
|
||||||
|
|
||||||
namespace sol {
|
namespace sol {
|
||||||
struct nil_t {};
|
struct nil_t {};
|
||||||
const nil_t nil{};
|
const nil_t nil{ };
|
||||||
|
struct Void_t { };
|
||||||
|
const Void_t Void{};
|
||||||
|
|
||||||
enum class type : int {
|
enum class type : int {
|
||||||
none = LUA_TNONE,
|
none = LUA_TNONE,
|
||||||
|
|
Loading…
Reference in New Issue
Block a user