Fix slight error when error handler was called and the result wasn't properly moved.

This commit is contained in:
ThePhD 2017-03-31 22:39:16 -04:00
parent 91aff613bc
commit fb06c8a754
4 changed files with 51 additions and 24 deletions

View File

@ -5,16 +5,16 @@
int main() {
std::cout << "=== basic example ===" << std::endl;
// create an empty lua state
sol::state lua;
sol::state lua;
// by default, libraries are not opened
// you can open libraries by using open_libraries
// the libraries reside in the sol::lib enum class
lua.open_libraries(sol::lib::base);
// by default, libraries are not opened
// you can open libraries by using open_libraries
// the libraries reside in the sol::lib enum class
lua.open_libraries(sol::lib::base);
// you can open all libraries by passing no arguments
//lua.open_libraries();
// call lua code directly
// call lua code directly
lua.script("print('hello world')");
// call lua code, and check to make sure it has loaded and run properly:
@ -27,14 +27,33 @@ int main() {
// You can just pass it through to let the call-site handle it
return result;
};
auto result = lua.script("print('hello hello again, world') \n return 24", simple_handler);
if (result.valid()) {
std::cout << "the code worked, and a double-hello statement should appear above this one!" << std::endl;
int value = result;
assert(value == 24);
// the above lambda is identical to sol::simple_on_error, but it's
// shown here to show you can write whatever you like
//
{
auto result = lua.script("print('hello hello again, world') \n return 24", simple_handler);
if (result.valid()) {
std::cout << "the third script worked, and a double-hello statement should appear above this one!" << std::endl;
int value = result;
assert(value == 24);
}
else {
std::cout << "the third script failed, check the result type for more information!" << std::endl;
}
}
else {
std::cout << "the code failed, check the result type for more information!" << std::endl;
{
auto result = lua.script("does.not.exist", simple_handler);
if (result.valid()) {
std::cout << "the fourth script worked, which it wasn't supposed to! Panic!" << std::endl;
int value = result;
assert(value == 24);
}
else {
sol::error err = result;
std::cout << "the fourth script failed, which was intentional!\t\nError: " << err.what() << std::endl;
}
}
std::cout << std::endl;

View File

@ -20,8 +20,8 @@
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// This file was generated with a script.
// Generated 2017-03-31 21:37:49.518087 UTC
// This header was generated with sol v2.16.0 (revision 1e367ab)
// Generated 2017-04-01 02:39:08.628086 UTC
// This header was generated with sol v2.16.0 (revision 91aff61)
// https://github.com/ThePhD/sol2
#ifndef SOL_SINGLE_INCLUDE_HPP
@ -921,7 +921,7 @@ extern "C" {
/* LuaJIT does not have the updated error codes for thread status/function returns */
#ifndef LUA_ERRGCMM
#define LUA_ERRGCMM (LUA_ERRERR + 1)
#define LUA_ERRGCMM (LUA_ERRERR + 2)
#endif // LUA_ERRGCMM
/* LuaJIT does not support continuation contexts / return error codes? */
@ -7204,7 +7204,7 @@ namespace sol {
if (count < 1)
return;
int top = lua_gettop(L);
if (index == -1 || top == index) {
if (index == -count || top == index) {
// Slice them right off the top
lua_pop(L, static_cast<int>(count));
return;
@ -12731,7 +12731,11 @@ namespace sol {
return kb;
}
inline protected_function_result default_on_error( lua_State* L, const protected_function_result& pfr ) {
inline protected_function_result simple_on_error(lua_State*, sol::protected_function_result result) {
return result;
}
inline protected_function_result default_on_error( lua_State* L, protected_function_result pfr ) {
type t = type_of(L, pfr.stack_index());
std::string err = to_string(pfr.status()) + " error";
if (t == type::string) {
@ -12953,7 +12957,7 @@ namespace sol {
protected_function_result script(const std::string& code, Fx&& on_error) {
protected_function_result pfr = do_string(code);
if (!pfr.valid()) {
return on_error(L, pfr);
return on_error(L, std::move(pfr));
}
return pfr;
}
@ -12962,7 +12966,7 @@ namespace sol {
protected_function_result script_file(const std::string& filename, Fx&& on_error) {
protected_function_result pfr = do_file(filename);
if (!pfr.valid()) {
return on_error(L, pfr);
return on_error(L, std::move(pfr));
}
return pfr;
}

View File

@ -109,7 +109,7 @@ namespace sol {
if (count < 1)
return;
int top = lua_gettop(L);
if (index == -1 || top == index) {
if (index == -count || top == index) {
// Slice them right off the top
lua_pop(L, static_cast<int>(count));
return;

View File

@ -52,7 +52,11 @@ namespace sol {
return kb;
}
inline protected_function_result default_on_error( lua_State* L, const protected_function_result& pfr ) {
inline protected_function_result simple_on_error(lua_State*, sol::protected_function_result result) {
return result;
}
inline protected_function_result default_on_error( lua_State* L, protected_function_result pfr ) {
type t = type_of(L, pfr.stack_index());
std::string err = to_string(pfr.status()) + " error";
if (t == type::string) {
@ -274,7 +278,7 @@ namespace sol {
protected_function_result script(const std::string& code, Fx&& on_error) {
protected_function_result pfr = do_string(code);
if (!pfr.valid()) {
return on_error(L, pfr);
return on_error(L, std::move(pfr));
}
return pfr;
}
@ -283,7 +287,7 @@ namespace sol {
protected_function_result script_file(const std::string& filename, Fx&& on_error) {
protected_function_result pfr = do_file(filename);
if (!pfr.valid()) {
return on_error(L, pfr);
return on_error(L, std::move(pfr));
}
return pfr;
}