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("file", filelib, 0);
|
||||||
openlib("gui", guilib, 0);
|
openlib("gui", guilib, 0);
|
||||||
|
|
||||||
addfunc("print", l_print);
|
addfunc("print", lua_wrap_errors<l_print>);
|
||||||
|
|
||||||
addfunc("block_index", l_block_index);
|
addfunc("block_index", lua_wrap_errors<l_block_index>);
|
||||||
addfunc("block_name", l_block_name);
|
addfunc("block_name", lua_wrap_errors<l_block_name>);
|
||||||
addfunc("blocks_count", l_blocks_count);
|
addfunc("blocks_count", lua_wrap_errors<l_blocks_count>);
|
||||||
addfunc("is_solid_at", l_is_solid_at);
|
addfunc("is_solid_at", lua_wrap_errors<l_is_solid_at>);
|
||||||
addfunc("is_replaceable_at", l_is_replaceable_at);
|
addfunc("is_replaceable_at", lua_wrap_errors<l_is_replaceable_at>);
|
||||||
addfunc("set_block", l_set_block);
|
addfunc("set_block", lua_wrap_errors<l_set_block>);
|
||||||
addfunc("get_block", l_get_block);
|
addfunc("get_block", lua_wrap_errors<l_get_block>);
|
||||||
addfunc("get_block_X", l_get_block_x);
|
addfunc("get_block_X", lua_wrap_errors<l_get_block_x>);
|
||||||
addfunc("get_block_Y", l_get_block_y);
|
addfunc("get_block_Y", lua_wrap_errors<l_get_block_y>);
|
||||||
addfunc("get_block_Z", l_get_block_z);
|
addfunc("get_block_Z", lua_wrap_errors<l_get_block_z>);
|
||||||
addfunc("get_block_states", l_get_block_states);
|
addfunc("get_block_states", lua_wrap_errors<l_get_block_states>);
|
||||||
addfunc("get_block_user_bits", l_get_block_user_bits);
|
addfunc("get_block_user_bits", lua_wrap_errors<l_get_block_user_bits>);
|
||||||
addfunc("set_block_user_bits", l_set_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) {
|
void lua::LuaState::loadbuffer(int env, const std::string& src, const std::string& file) {
|
||||||
|
|||||||
@ -1,8 +1,26 @@
|
|||||||
#ifndef LOGIC_SCRIPTING_API_LUA_H_
|
#ifndef LOGIC_SCRIPTING_API_LUA_H_
|
||||||
#define LOGIC_SCRIPTING_API_LUA_H_
|
#define LOGIC_SCRIPTING_API_LUA_H_
|
||||||
|
|
||||||
|
#include <exception>
|
||||||
#include <lua.hpp>
|
#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 == */
|
/* == file library == */
|
||||||
extern int l_file_resolve(lua_State* L);
|
extern int l_file_resolve(lua_State* L);
|
||||||
extern int l_file_read(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);
|
extern int l_file_mkdir(lua_State* L);
|
||||||
|
|
||||||
static const luaL_Reg filelib [] = {
|
static const luaL_Reg filelib [] = {
|
||||||
{"resolve", l_file_resolve},
|
{"resolve", lua_wrap_errors<l_file_resolve>},
|
||||||
{"read", l_file_read},
|
{"read", lua_wrap_errors<l_file_read>},
|
||||||
{"write", l_file_write},
|
{"write", lua_wrap_errors<l_file_write>},
|
||||||
{"exists", l_file_exists},
|
{"exists", lua_wrap_errors<l_file_exists>},
|
||||||
{"isfile", l_file_isfile},
|
{"isfile", lua_wrap_errors<l_file_isfile>},
|
||||||
{"isdir", l_file_isdir},
|
{"isdir", lua_wrap_errors<l_file_isdir>},
|
||||||
{"length", l_file_length},
|
{"length", lua_wrap_errors<l_file_length>},
|
||||||
{"mkdir", l_file_mkdir},
|
{"mkdir", lua_wrap_errors<l_file_mkdir>},
|
||||||
{NULL, NULL}
|
{NULL, NULL}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -30,8 +48,8 @@ extern int l_time_uptime(lua_State* L);
|
|||||||
extern int l_time_delta(lua_State* L);
|
extern int l_time_delta(lua_State* L);
|
||||||
|
|
||||||
static const luaL_Reg timelib [] = {
|
static const luaL_Reg timelib [] = {
|
||||||
{"uptime", l_time_uptime},
|
{"uptime", lua_wrap_errors<l_time_uptime>},
|
||||||
{"delta", l_time_delta},
|
{"delta", lua_wrap_errors<l_time_delta>},
|
||||||
{NULL, NULL}
|
{NULL, NULL}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -39,7 +57,7 @@ static const luaL_Reg timelib [] = {
|
|||||||
extern int l_pack_get_folder(lua_State* L);
|
extern int l_pack_get_folder(lua_State* L);
|
||||||
|
|
||||||
static const luaL_Reg packlib [] = {
|
static const luaL_Reg packlib [] = {
|
||||||
{"get_folder", l_pack_get_folder},
|
{"get_folder", lua_wrap_errors<l_pack_get_folder>},
|
||||||
{NULL, NULL}
|
{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);
|
extern int l_world_get_seed(lua_State* L);
|
||||||
|
|
||||||
static const luaL_Reg worldlib [] = {
|
static const luaL_Reg worldlib [] = {
|
||||||
{"get_total_time", l_world_get_total_time},
|
{"get_total_time", lua_wrap_errors<l_world_get_total_time>},
|
||||||
{"get_day_time", l_world_get_day_time},
|
{"get_day_time", lua_wrap_errors<l_world_get_day_time>},
|
||||||
{"set_day_time", l_world_set_day_time},
|
{"set_day_time", lua_wrap_errors<l_world_set_day_time>},
|
||||||
{"get_seed", l_world_get_seed},
|
{"get_seed", lua_wrap_errors<l_world_get_seed>},
|
||||||
{NULL, NULL}
|
{NULL, NULL}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -65,11 +83,11 @@ extern int l_player_set_pos(lua_State* L);
|
|||||||
extern int l_player_get_inv(lua_State* L);
|
extern int l_player_get_inv(lua_State* L);
|
||||||
|
|
||||||
static const luaL_Reg playerlib [] = {
|
static const luaL_Reg playerlib [] = {
|
||||||
{"get_pos", l_player_get_pos},
|
{"get_pos", lua_wrap_errors<l_player_get_pos>},
|
||||||
{"set_pos", l_player_set_pos},
|
{"set_pos", lua_wrap_errors<l_player_set_pos>},
|
||||||
{"get_rot", l_player_get_rot},
|
{"get_rot", lua_wrap_errors<l_player_get_rot>},
|
||||||
{"set_rot", l_player_set_rot},
|
{"set_rot", lua_wrap_errors<l_player_set_rot>},
|
||||||
{"get_inventory", l_player_get_inv},
|
{"get_inventory", lua_wrap_errors<l_player_get_inv>},
|
||||||
{NULL, NULL}
|
{NULL, NULL}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -81,11 +99,11 @@ extern int l_inventory_add(lua_State* L);
|
|||||||
extern int l_inventory_get_block(lua_State* L);
|
extern int l_inventory_get_block(lua_State* L);
|
||||||
|
|
||||||
static const luaL_Reg inventorylib [] = {
|
static const luaL_Reg inventorylib [] = {
|
||||||
{"get", l_inventory_get},
|
{"get", lua_wrap_errors<l_inventory_get>},
|
||||||
{"set", l_inventory_set},
|
{"set", lua_wrap_errors<l_inventory_set>},
|
||||||
{"size", l_inventory_size},
|
{"size", lua_wrap_errors<l_inventory_size>},
|
||||||
{"add", l_inventory_add},
|
{"add", lua_wrap_errors<l_inventory_add>},
|
||||||
{"get_block", l_inventory_get_block},
|
{"get_block", lua_wrap_errors<l_inventory_get_block>},
|
||||||
{NULL, NULL}
|
{NULL, NULL}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -96,10 +114,10 @@ extern int l_item_stack_size(lua_State* L);
|
|||||||
extern int l_item_defs_count(lua_State* L);
|
extern int l_item_defs_count(lua_State* L);
|
||||||
|
|
||||||
static const luaL_Reg itemlib [] = {
|
static const luaL_Reg itemlib [] = {
|
||||||
{"index", l_item_index},
|
{"index", lua_wrap_errors<l_item_index>},
|
||||||
{"name", l_item_name},
|
{"name", lua_wrap_errors<l_item_name>},
|
||||||
{"stack_size", l_item_stack_size},
|
{"stack_size", lua_wrap_errors<l_item_stack_size>},
|
||||||
{"defs_count", l_item_defs_count},
|
{"defs_count", lua_wrap_errors<l_item_defs_count>},
|
||||||
{NULL, NULL}
|
{NULL, NULL}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user