From e84cf66683156fdebed4e3ad335b03cc7dbc04a3 Mon Sep 17 00:00:00 2001 From: ThePhD Date: Sun, 25 Oct 2015 06:48:19 -0400 Subject: [PATCH] decay to std::string instead of `char*`, as its both safer (protects against early stack pops) and is handled better by g++'s new std::string conversion constructors Make sure we're only using C++11 features (no decltype(auto)) --- sol/function.hpp | 42 +++++++++++++++++++++--------------------- sol/function_types.hpp | 9 ++++++--- sol/proxy.hpp | 4 ++-- tests.cpp | 2 +- 4 files changed, 30 insertions(+), 27 deletions(-) diff --git a/sol/function.hpp b/sol/function.hpp index 0a9d1f10..bb6401d9 100644 --- a/sol/function.hpp +++ b/sol/function.hpp @@ -60,25 +60,25 @@ public: function_result& operator=(const function_result&) = default; function_result(function_result&& o) : L(o.L), index(o.index), returncount(o.returncount), error(o.error) { // 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; - o.error = call_error::runtime; + // return count being 0 is enough to keep things clean + // but will be thorough + o.L = nullptr; + o.index = 0; + o.returncount = 0; + o.error = call_error::runtime; } function_result& operator=(function_result&& o) { - L = o.L; - index = o.index; - returncount = o.returncount; - error = o.error; - // 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; - o.error = call_error::runtime; + L = o.L; + index = o.index; + returncount = o.returncount; + error = o.error; + // 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; + o.error = call_error::runtime; } bool valid() const { @@ -91,8 +91,8 @@ public: return get(tr, tr); } - operator const char* () const { - return get(); + operator std::string() const { + return get(); } template, const char*>>, Not, char>>, Not, std::string>>, Not, std::initializer_list>>> = 0> @@ -210,13 +210,13 @@ public: template auto operator()(types, Args&&... args) const - -> decltype(call(std::forward(args)...)) { + -> decltype(invoke(types(), types(), 0, std::declval())) { return call(std::forward(args)...); } template auto call(Args&&... args) const - -> decltype(invoke(types(), types(), 0, std::declval())) { + -> decltype(invoke(types(), types(), 0, std::declval())) { handler h(error_handler); push(); int pushcount = stack::push_args(state(), std::forward(args)...); diff --git a/sol/function_types.hpp b/sol/function_types.hpp index 747fcf48..88215587 100644 --- a/sol/function_types.hpp +++ b/sol/function_types.hpp @@ -168,7 +168,8 @@ struct static_function { template static int typed_call(types, types ta, function_type* fx, lua_State* L) { typedef typename return_type::type return_type; - decltype(auto) r = stack::call(L, 0, types(), ta, fx); + typedef decltype(stack::call(L, 0, types(), ta, fx)) ret_t; + ret_t r = stack::call(L, 0, types(), ta, fx); int nargs = static_cast(sizeof...(Args)); lua_pop(L, nargs); return stack::push(L, std::forward(r)); @@ -392,7 +393,8 @@ struct member_function : public base_function { template int operator()(types tr, types ta, lua_State* L) { - decltype(auto) r = stack::call(L, 0, tr, ta, fx); + typedef decltype(stack::call(L, 0, tr, ta, fx)) ret_t; + ret_t r = stack::call(L, 0, tr, ta, fx); int nargs = static_cast(sizeof...(Args)); lua_pop(L, nargs); return stack::push(L, std::forward(r)); @@ -458,7 +460,8 @@ struct usertype_function_core : public base_function { template int call(types tr, types ta, lua_State* L) { - decltype(auto) r = stack::call(L, 0, tr, ta, fx); + typedef decltype(stack::call(L, 0, tr, ta, fx)) ret_t; + ret_t r = stack::call(L, 0, tr, ta, fx); int nargs = static_cast(sizeof...(Args)); lua_pop(L, nargs); int pushcount = push(L, std::forward(r)); diff --git a/sol/proxy.hpp b/sol/proxy.hpp index 1a69dc57..fd78b30d 100644 --- a/sol/proxy.hpp +++ b/sol/proxy.hpp @@ -67,8 +67,8 @@ public: return tbl.template get( key ); } - operator const char* () const { - return get(); + operator std::string() const { + return get(); } template, const char*>>, Not, char>>, Not, std::string>>, Not, std::initializer_list>>> = 0> diff --git a/tests.cpp b/tests.cpp index 312037f5..3f62aa9a 100644 --- a/tests.cpp +++ b/tests.cpp @@ -1009,7 +1009,7 @@ TEST_CASE( "functions/sol::function_result", "Function result should be the beef // Some function; just using a lambda to be cheap auto doom = []() { // Bypasses handler function: puts information directly into lua error - throw std::exception( errormessage1 ); + throw std::runtime_error( errormessage1 ); }; auto luadoom = [&lua]() { // Does not bypass error function, will call it