diff --git a/src/logic/scripting/lua/LuaState.cpp b/src/logic/scripting/lua/LuaState.cpp index d3bd3997..174972b8 100644 --- a/src/logic/scripting/lua/LuaState.cpp +++ b/src/logic/scripting/lua/LuaState.cpp @@ -102,21 +102,21 @@ void lua::LuaState::createFuncs() { openlib("file", filelib, 0); openlib("gui", guilib, 0); - addfunc("print", l_print); + addfunc("print", lua_wrap_errors); - addfunc("block_index", l_block_index); - addfunc("block_name", l_block_name); - addfunc("blocks_count", l_blocks_count); - addfunc("is_solid_at", l_is_solid_at); - addfunc("is_replaceable_at", l_is_replaceable_at); - addfunc("set_block", l_set_block); - addfunc("get_block", l_get_block); - addfunc("get_block_X", l_get_block_x); - addfunc("get_block_Y", l_get_block_y); - addfunc("get_block_Z", l_get_block_z); - addfunc("get_block_states", l_get_block_states); - addfunc("get_block_user_bits", l_get_block_user_bits); - addfunc("set_block_user_bits", l_set_block_user_bits); + addfunc("block_index", lua_wrap_errors); + addfunc("block_name", lua_wrap_errors); + addfunc("blocks_count", lua_wrap_errors); + addfunc("is_solid_at", lua_wrap_errors); + addfunc("is_replaceable_at", lua_wrap_errors); + addfunc("set_block", lua_wrap_errors); + addfunc("get_block", lua_wrap_errors); + addfunc("get_block_X", lua_wrap_errors); + addfunc("get_block_Y", lua_wrap_errors); + addfunc("get_block_Z", lua_wrap_errors); + addfunc("get_block_states", lua_wrap_errors); + addfunc("get_block_user_bits", lua_wrap_errors); + addfunc("set_block_user_bits", lua_wrap_errors); } void lua::LuaState::loadbuffer(int env, const std::string& src, const std::string& file) { diff --git a/src/logic/scripting/lua/api_lua.h b/src/logic/scripting/lua/api_lua.h index d6a59bfb..e9dc0a1d 100644 --- a/src/logic/scripting/lua/api_lua.h +++ b/src/logic/scripting/lua/api_lua.h @@ -1,8 +1,26 @@ #ifndef LOGIC_SCRIPTING_API_LUA_H_ #define LOGIC_SCRIPTING_API_LUA_H_ +#include #include +template int lua_wrap_errors(lua_State *L) { + int result = 0; + try { + result = func(L); + } + // transform exception with description into lua_error + catch (std::exception &e) { + luaL_error(L, e.what()); + } + // Rethrow any other exception (lua error for example) + catch (...) { + throw; + } + + return result; +} + /* == file library == */ extern int l_file_resolve(lua_State* L); extern int l_file_read(lua_State* L); @@ -14,14 +32,14 @@ extern int l_file_length(lua_State* L); extern int l_file_mkdir(lua_State* L); static const luaL_Reg filelib [] = { - {"resolve", l_file_resolve}, - {"read", l_file_read}, - {"write", l_file_write}, - {"exists", l_file_exists}, - {"isfile", l_file_isfile}, - {"isdir", l_file_isdir}, - {"length", l_file_length}, - {"mkdir", l_file_mkdir}, + {"resolve", lua_wrap_errors}, + {"read", lua_wrap_errors}, + {"write", lua_wrap_errors}, + {"exists", lua_wrap_errors}, + {"isfile", lua_wrap_errors}, + {"isdir", lua_wrap_errors}, + {"length", lua_wrap_errors}, + {"mkdir", lua_wrap_errors}, {NULL, NULL} }; @@ -30,8 +48,8 @@ extern int l_time_uptime(lua_State* L); extern int l_time_delta(lua_State* L); static const luaL_Reg timelib [] = { - {"uptime", l_time_uptime}, - {"delta", l_time_delta}, + {"uptime", lua_wrap_errors}, + {"delta", lua_wrap_errors}, {NULL, NULL} }; @@ -39,7 +57,7 @@ static const luaL_Reg timelib [] = { extern int l_pack_get_folder(lua_State* L); static const luaL_Reg packlib [] = { - {"get_folder", l_pack_get_folder}, + {"get_folder", lua_wrap_errors}, {NULL, NULL} }; @@ -50,10 +68,10 @@ extern int l_world_set_day_time(lua_State* L); extern int l_world_get_seed(lua_State* L); static const luaL_Reg worldlib [] = { - {"get_total_time", l_world_get_total_time}, - {"get_day_time", l_world_get_day_time}, - {"set_day_time", l_world_set_day_time}, - {"get_seed", l_world_get_seed}, + {"get_total_time", lua_wrap_errors}, + {"get_day_time", lua_wrap_errors}, + {"set_day_time", lua_wrap_errors}, + {"get_seed", lua_wrap_errors}, {NULL, NULL} }; @@ -65,11 +83,11 @@ extern int l_player_set_pos(lua_State* L); extern int l_player_get_inv(lua_State* L); static const luaL_Reg playerlib [] = { - {"get_pos", l_player_get_pos}, - {"set_pos", l_player_set_pos}, - {"get_rot", l_player_get_rot}, - {"set_rot", l_player_set_rot}, - {"get_inventory", l_player_get_inv}, + {"get_pos", lua_wrap_errors}, + {"set_pos", lua_wrap_errors}, + {"get_rot", lua_wrap_errors}, + {"set_rot", lua_wrap_errors}, + {"get_inventory", lua_wrap_errors}, {NULL, NULL} }; @@ -81,11 +99,11 @@ extern int l_inventory_add(lua_State* L); extern int l_inventory_get_block(lua_State* L); static const luaL_Reg inventorylib [] = { - {"get", l_inventory_get}, - {"set", l_inventory_set}, - {"size", l_inventory_size}, - {"add", l_inventory_add}, - {"get_block", l_inventory_get_block}, + {"get", lua_wrap_errors}, + {"set", lua_wrap_errors}, + {"size", lua_wrap_errors}, + {"add", lua_wrap_errors}, + {"get_block", lua_wrap_errors}, {NULL, NULL} }; @@ -96,10 +114,10 @@ extern int l_item_stack_size(lua_State* L); extern int l_item_defs_count(lua_State* L); static const luaL_Reg itemlib [] = { - {"index", l_item_index}, - {"name", l_item_name}, - {"stack_size", l_item_stack_size}, - {"defs_count", l_item_defs_count}, + {"index", lua_wrap_errors}, + {"name", lua_wrap_errors}, + {"stack_size", lua_wrap_errors}, + {"defs_count", lua_wrap_errors}, {NULL, NULL} };