Merge pull request #148 from InfiniteCoder01/LuaErrorHandling
C++ Exceptions from lua handling
This commit is contained in:
commit
526979fa0b
@ -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<l_print>);
|
||||
|
||||
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<l_block_index>);
|
||||
addfunc("block_name", lua_wrap_errors<l_block_name>);
|
||||
addfunc("blocks_count", lua_wrap_errors<l_blocks_count>);
|
||||
addfunc("is_solid_at", lua_wrap_errors<l_is_solid_at>);
|
||||
addfunc("is_replaceable_at", lua_wrap_errors<l_is_replaceable_at>);
|
||||
addfunc("set_block", lua_wrap_errors<l_set_block>);
|
||||
addfunc("get_block", lua_wrap_errors<l_get_block>);
|
||||
addfunc("get_block_X", lua_wrap_errors<l_get_block_x>);
|
||||
addfunc("get_block_Y", lua_wrap_errors<l_get_block_y>);
|
||||
addfunc("get_block_Z", lua_wrap_errors<l_get_block_z>);
|
||||
addfunc("get_block_states", lua_wrap_errors<l_get_block_states>);
|
||||
addfunc("get_block_user_bits", lua_wrap_errors<l_get_block_user_bits>);
|
||||
addfunc("set_block_user_bits", lua_wrap_errors<l_set_block_user_bits>);
|
||||
}
|
||||
|
||||
void lua::LuaState::loadbuffer(int env, const std::string& src, const std::string& file) {
|
||||
|
||||
@ -1,8 +1,26 @@
|
||||
#ifndef LOGIC_SCRIPTING_API_LUA_H_
|
||||
#define LOGIC_SCRIPTING_API_LUA_H_
|
||||
|
||||
#include <exception>
|
||||
#include <lua.hpp>
|
||||
|
||||
template <lua_CFunction func> 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<l_file_resolve>},
|
||||
{"read", lua_wrap_errors<l_file_read>},
|
||||
{"write", lua_wrap_errors<l_file_write>},
|
||||
{"exists", lua_wrap_errors<l_file_exists>},
|
||||
{"isfile", lua_wrap_errors<l_file_isfile>},
|
||||
{"isdir", lua_wrap_errors<l_file_isdir>},
|
||||
{"length", lua_wrap_errors<l_file_length>},
|
||||
{"mkdir", lua_wrap_errors<l_file_mkdir>},
|
||||
{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<l_time_uptime>},
|
||||
{"delta", lua_wrap_errors<l_time_delta>},
|
||||
{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<l_pack_get_folder>},
|
||||
{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<l_world_get_total_time>},
|
||||
{"get_day_time", lua_wrap_errors<l_world_get_day_time>},
|
||||
{"set_day_time", lua_wrap_errors<l_world_set_day_time>},
|
||||
{"get_seed", lua_wrap_errors<l_world_get_seed>},
|
||||
{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<l_player_get_pos>},
|
||||
{"set_pos", lua_wrap_errors<l_player_set_pos>},
|
||||
{"get_rot", lua_wrap_errors<l_player_get_rot>},
|
||||
{"set_rot", lua_wrap_errors<l_player_set_rot>},
|
||||
{"get_inventory", lua_wrap_errors<l_player_get_inv>},
|
||||
{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<l_inventory_get>},
|
||||
{"set", lua_wrap_errors<l_inventory_set>},
|
||||
{"size", lua_wrap_errors<l_inventory_size>},
|
||||
{"add", lua_wrap_errors<l_inventory_add>},
|
||||
{"get_block", lua_wrap_errors<l_inventory_get_block>},
|
||||
{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<l_item_index>},
|
||||
{"name", lua_wrap_errors<l_item_name>},
|
||||
{"stack_size", lua_wrap_errors<l_item_stack_size>},
|
||||
{"defs_count", lua_wrap_errors<l_item_defs_count>},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user