mirror of
https://github.com/ThePhD/sol2.git
synced 2024-03-22 13:10:44 +08:00
Solve's @starwing's problem with not having the ability to check if a function call succeeded or not.
This commit is contained in:
parent
93d532094e
commit
93fe7443f0
|
@ -38,6 +38,7 @@ private:
|
||||||
lua_State* L;
|
lua_State* L;
|
||||||
int index;
|
int index;
|
||||||
int returncount;
|
int returncount;
|
||||||
|
int error;
|
||||||
|
|
||||||
template <typename T, std::size_t I>
|
template <typename T, std::size_t I>
|
||||||
stack::get_return<T> get(types<T>, indices<I>) const {
|
stack::get_return<T> get(types<T>, indices<I>) const {
|
||||||
|
@ -52,7 +53,7 @@ private:
|
||||||
|
|
||||||
public:
|
public:
|
||||||
function_result() = default;
|
function_result() = default;
|
||||||
function_result(lua_State* L, int index = -1, int returncount = 0): L(L), index(index), returncount(returncount) {
|
function_result(lua_State* L, int index = -1, int returncount = 0, int code = LUA_OK): L(L), index(index), returncount(returncount), error(code) {
|
||||||
|
|
||||||
}
|
}
|
||||||
function_result(const function_result&) = default;
|
function_result(const function_result&) = default;
|
||||||
|
@ -60,6 +61,10 @@ public:
|
||||||
function_result(function_result&&) = default;
|
function_result(function_result&&) = default;
|
||||||
function_result& operator=(function_result&&) = default;
|
function_result& operator=(function_result&&) = default;
|
||||||
|
|
||||||
|
bool valid() const {
|
||||||
|
return error == LUA_OK;
|
||||||
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
T get() const {
|
T get() const {
|
||||||
tuple_types<Unqualified<T>> tr;
|
tuple_types<Unqualified<T>> tr;
|
||||||
|
@ -82,8 +87,12 @@ public:
|
||||||
|
|
||||||
class function : public reference {
|
class function : public reference {
|
||||||
private:
|
private:
|
||||||
|
int luacodecall(std::ptrdiff_t argcount, std::ptrdiff_t resultcount) const {
|
||||||
|
return lua_pcallk(state(), static_cast<int>(argcount), static_cast<int>(resultcount), 0, 0, nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
void luacall(std::ptrdiff_t argcount, std::ptrdiff_t resultcount) const {
|
void luacall(std::ptrdiff_t argcount, std::ptrdiff_t resultcount) const {
|
||||||
lua_call(state(), static_cast<int>(argcount), static_cast<int>(resultcount));
|
lua_callk(state(), static_cast<int>(argcount), static_cast<int>(resultcount), 0, nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<std::size_t... I, typename... Ret>
|
template<std::size_t... I, typename... Ret>
|
||||||
|
@ -111,10 +120,10 @@ private:
|
||||||
function_result invoke(indices<>, types<>, std::ptrdiff_t n) const {
|
function_result invoke(indices<>, types<>, std::ptrdiff_t n) const {
|
||||||
const int stacksize = lua_gettop(state());
|
const int stacksize = lua_gettop(state());
|
||||||
const int firstreturn = std::max(0, stacksize - static_cast<int>(n) - 1);
|
const int firstreturn = std::max(0, stacksize - static_cast<int>(n) - 1);
|
||||||
luacall(n, LUA_MULTRET);
|
int code = luacodecall(n, LUA_MULTRET);
|
||||||
const int poststacksize = lua_gettop(state());
|
const int poststacksize = lua_gettop(state());
|
||||||
const int returncount = poststacksize - firstreturn;
|
const int returncount = poststacksize - firstreturn;
|
||||||
return function_result(state(), firstreturn + 1, returncount);
|
return function_result(state(), firstreturn + 1, returncount, code);
|
||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
Loading…
Reference in New Issue
Block a user