Skip to content

Commit 766e885

Browse files
authoredSep 10, 2021
Clean up/improve some scriptapi error handling code
1 parent 7423c4c commit 766e885

11 files changed

+120
-94
lines changed
 

‎src/client/client.h

+4
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,10 @@ class Client : public con::PeerHandler, public InventoryManager, public IGameDef
325325
m_access_denied = true;
326326
m_access_denied_reason = reason;
327327
}
328+
inline void setFatalError(const LuaError &e)
329+
{
330+
setFatalError(std::string("Lua :") + e.what());
331+
}
328332

329333
// Renaming accessDeniedReason to better name could be good as it's used to
330334
// disconnect client when CSM failed.

‎src/emerge.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,7 @@ MapBlock *EmergeThread::finishGen(v3s16 pos, BlockMakeData *bmdata,
647647
m_server->getScriptIface()->environment_OnGenerated(
648648
minp, maxp, m_mapgen->blockseed);
649649
} catch (LuaError &e) {
650-
m_server->setAsyncFatalError("Lua: finishGen" + std::string(e.what()));
650+
m_server->setAsyncFatalError(e);
651651
}
652652

653653
/*

‎src/script/common/c_internal.cpp

-36
Original file line numberDiff line numberDiff line change
@@ -101,42 +101,6 @@ void script_error(lua_State *L, int pcall_result, const char *mod, const char *f
101101
throw LuaError(err_msg);
102102
}
103103

104-
// Push the list of callbacks (a lua table).
105-
// Then push nargs arguments.
106-
// Then call this function, which
107-
// - runs the callbacks
108-
// - replaces the table and arguments with the return value,
109-
// computed depending on mode
110-
void script_run_callbacks_f(lua_State *L, int nargs,
111-
RunCallbacksMode mode, const char *fxn)
112-
{
113-
FATAL_ERROR_IF(lua_gettop(L) < nargs + 1, "Not enough arguments");
114-
115-
// Insert error handler
116-
PUSH_ERROR_HANDLER(L);
117-
int error_handler = lua_gettop(L) - nargs - 1;
118-
lua_insert(L, error_handler);
119-
120-
// Insert run_callbacks between error handler and table
121-
lua_getglobal(L, "core");
122-
lua_getfield(L, -1, "run_callbacks");
123-
lua_remove(L, -2);
124-
lua_insert(L, error_handler + 1);
125-
126-
// Insert mode after table
127-
lua_pushnumber(L, (int) mode);
128-
lua_insert(L, error_handler + 3);
129-
130-
// Stack now looks like this:
131-
// ... <error handler> <run_callbacks> <table> <mode> <arg#1> <arg#2> ... <arg#n>
132-
133-
int result = lua_pcall(L, nargs + 2, 1, error_handler);
134-
if (result != 0)
135-
script_error(L, result, NULL, fxn);
136-
137-
lua_remove(L, error_handler);
138-
}
139-
140104
static void script_log_add_source(lua_State *L, std::string &message, int stack_depth)
141105
{
142106
lua_Debug ar;

‎src/script/common/c_internal.h

+4-6
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,6 @@ extern "C" {
7575
} \
7676
}
7777

78-
#define script_run_callbacks(L, nargs, mode) \
79-
script_run_callbacks_f((L), (nargs), (mode), __FUNCTION__)
80-
8178
// What script_run_callbacks does with the return values of callbacks.
8279
// Regardless of the mode, if only one callback is defined,
8380
// its return value is the total return value.
@@ -108,16 +105,17 @@ enum RunCallbacksMode
108105
// are converted by readParam<bool> to true or false, respectively.
109106
};
110107

108+
// Gets a backtrace of the current execution point
111109
std::string script_get_backtrace(lua_State *L);
110+
// Wrapper for CFunction calls that converts C++ exceptions to Lua errors
112111
int script_exception_wrapper(lua_State *L, lua_CFunction f);
112+
// Takes an error from lua_pcall and throws it as a LuaError
113113
void script_error(lua_State *L, int pcall_result, const char *mod, const char *fxn);
114-
void script_run_callbacks_f(lua_State *L, int nargs,
115-
RunCallbacksMode mode, const char *fxn);
116114

117115
bool script_log_unique(lua_State *L, std::string message, std::ostream &log_to,
118116
int stack_depth = 1);
119117

120-
enum class DeprecatedHandlingMode {
118+
enum DeprecatedHandlingMode {
121119
Ignore,
122120
Log,
123121
Error

‎src/script/cpp_api/s_base.h

+3
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,11 @@ class ScriptApiBase : protected LuaHelper {
128128
lua_State* getStack()
129129
{ return m_luastack; }
130130

131+
// Checks that stack size is sane
131132
void realityCheck();
133+
// Takes an error from lua_pcall and throws it as a LuaError
132134
void scriptError(int result, const char *fxn);
135+
// Dumps stack contents for debugging
133136
void stackDump(std::ostream &o);
134137

135138
void setGameDef(IGameDef* gamedef) { m_gamedef = gamedef; }

‎src/script/cpp_api/s_client.cpp

+73-15
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@ void ScriptApiClient::on_mods_loaded()
3333
lua_getglobal(L, "core");
3434
lua_getfield(L, -1, "registered_on_mods_loaded");
3535
// Call callbacks
36-
runCallbacks(0, RUN_CALLBACKS_MODE_FIRST);
36+
try {
37+
runCallbacks(0, RUN_CALLBACKS_MODE_FIRST);
38+
} catch (LuaError &e) {
39+
getClient()->setFatalError(e);
40+
}
3741
}
3842

3943
void ScriptApiClient::on_shutdown()
@@ -44,7 +48,11 @@ void ScriptApiClient::on_shutdown()
4448
lua_getglobal(L, "core");
4549
lua_getfield(L, -1, "registered_on_shutdown");
4650
// Call callbacks
47-
runCallbacks(0, RUN_CALLBACKS_MODE_FIRST);
51+
try {
52+
runCallbacks(0, RUN_CALLBACKS_MODE_FIRST);
53+
} catch (LuaError &e) {
54+
getClient()->setFatalError(e);
55+
}
4856
}
4957

5058
bool ScriptApiClient::on_sending_message(const std::string &message)
@@ -56,7 +64,12 @@ bool ScriptApiClient::on_sending_message(const std::string &message)
5664
lua_getfield(L, -1, "registered_on_sending_chat_message");
5765
// Call callbacks
5866
lua_pushstring(L, message.c_str());
59-
runCallbacks(1, RUN_CALLBACKS_MODE_OR_SC);
67+
try {
68+
runCallbacks(1, RUN_CALLBACKS_MODE_OR_SC);
69+
} catch (LuaError &e) {
70+
getClient()->setFatalError(e);
71+
return true;
72+
}
6073
return readParam<bool>(L, -1);
6174
}
6275

@@ -69,7 +82,12 @@ bool ScriptApiClient::on_receiving_message(const std::string &message)
6982
lua_getfield(L, -1, "registered_on_receiving_chat_message");
7083
// Call callbacks
7184
lua_pushstring(L, message.c_str());
72-
runCallbacks(1, RUN_CALLBACKS_MODE_OR_SC);
85+
try {
86+
runCallbacks(1, RUN_CALLBACKS_MODE_OR_SC);
87+
} catch (LuaError &e) {
88+
getClient()->setFatalError(e);
89+
return true;
90+
}
7391
return readParam<bool>(L, -1);
7492
}
7593

@@ -82,7 +100,11 @@ void ScriptApiClient::on_damage_taken(int32_t damage_amount)
82100
lua_getfield(L, -1, "registered_on_damage_taken");
83101
// Call callbacks
84102
lua_pushinteger(L, damage_amount);
85-
runCallbacks(1, RUN_CALLBACKS_MODE_OR_SC);
103+
try {
104+
runCallbacks(1, RUN_CALLBACKS_MODE_OR_SC);
105+
} catch (LuaError &e) {
106+
getClient()->setFatalError(e);
107+
}
86108
}
87109

88110
void ScriptApiClient::on_hp_modification(int32_t newhp)
@@ -94,7 +116,11 @@ void ScriptApiClient::on_hp_modification(int32_t newhp)
94116
lua_getfield(L, -1, "registered_on_hp_modification");
95117
// Call callbacks
96118
lua_pushinteger(L, newhp);
97-
runCallbacks(1, RUN_CALLBACKS_MODE_OR_SC);
119+
try {
120+
runCallbacks(1, RUN_CALLBACKS_MODE_OR_SC);
121+
} catch (LuaError &e) {
122+
getClient()->setFatalError(e);
123+
}
98124
}
99125

100126
void ScriptApiClient::on_death()
@@ -105,7 +131,11 @@ void ScriptApiClient::on_death()
105131
lua_getglobal(L, "core");
106132
lua_getfield(L, -1, "registered_on_death");
107133
// Call callbacks
108-
runCallbacks(0, RUN_CALLBACKS_MODE_FIRST);
134+
try {
135+
runCallbacks(0, RUN_CALLBACKS_MODE_FIRST);
136+
} catch (LuaError &e) {
137+
getClient()->setFatalError(e);
138+
}
109139
}
110140

111141
void ScriptApiClient::environment_step(float dtime)
@@ -120,8 +150,7 @@ void ScriptApiClient::environment_step(float dtime)
120150
try {
121151
runCallbacks(1, RUN_CALLBACKS_MODE_FIRST);
122152
} catch (LuaError &e) {
123-
getClient()->setFatalError(std::string("Client environment_step: ") + e.what() + "\n"
124-
+ script_get_backtrace(L));
153+
getClient()->setFatalError(e);
125154
}
126155
}
127156

@@ -146,7 +175,11 @@ void ScriptApiClient::on_formspec_input(const std::string &formname,
146175
lua_pushlstring(L, value.c_str(), value.size());
147176
lua_settable(L, -3);
148177
}
149-
runCallbacks(2, RUN_CALLBACKS_MODE_OR_SC);
178+
try {
179+
runCallbacks(2, RUN_CALLBACKS_MODE_OR_SC);
180+
} catch (LuaError &e) {
181+
getClient()->setFatalError(e);
182+
}
150183
}
151184

152185
bool ScriptApiClient::on_dignode(v3s16 p, MapNode node)
@@ -164,7 +197,12 @@ bool ScriptApiClient::on_dignode(v3s16 p, MapNode node)
164197
pushnode(L, node, ndef);
165198

166199
// Call functions
167-
runCallbacks(2, RUN_CALLBACKS_MODE_OR);
200+
try {
201+
runCallbacks(2, RUN_CALLBACKS_MODE_OR);
202+
} catch (LuaError &e) {
203+
getClient()->setFatalError(e);
204+
return true;
205+
}
168206
return lua_toboolean(L, -1);
169207
}
170208

@@ -183,7 +221,12 @@ bool ScriptApiClient::on_punchnode(v3s16 p, MapNode node)
183221
pushnode(L, node, ndef);
184222

185223
// Call functions
186-
runCallbacks(2, RUN_CALLBACKS_MODE_OR);
224+
try {
225+
runCallbacks(2, RUN_CALLBACKS_MODE_OR);
226+
} catch (LuaError &e) {
227+
getClient()->setFatalError(e);
228+
return true;
229+
}
187230
return readParam<bool>(L, -1);
188231
}
189232

@@ -200,7 +243,12 @@ bool ScriptApiClient::on_placenode(const PointedThing &pointed, const ItemDefini
200243
push_item_definition(L, item);
201244

202245
// Call functions
203-
runCallbacks(2, RUN_CALLBACKS_MODE_OR);
246+
try {
247+
runCallbacks(2, RUN_CALLBACKS_MODE_OR);
248+
} catch (LuaError &e) {
249+
getClient()->setFatalError(e);
250+
return true;
251+
}
204252
return readParam<bool>(L, -1);
205253
}
206254

@@ -217,7 +265,12 @@ bool ScriptApiClient::on_item_use(const ItemStack &item, const PointedThing &poi
217265
push_pointed_thing(L, pointed, true);
218266

219267
// Call functions
220-
runCallbacks(2, RUN_CALLBACKS_MODE_OR);
268+
try {
269+
runCallbacks(2, RUN_CALLBACKS_MODE_OR);
270+
} catch (LuaError &e) {
271+
getClient()->setFatalError(e);
272+
return true;
273+
}
221274
return readParam<bool>(L, -1);
222275
}
223276

@@ -238,7 +291,12 @@ bool ScriptApiClient::on_inventory_open(Inventory *inventory)
238291
lua_rawset(L, -3);
239292
}
240293

241-
runCallbacks(1, RUN_CALLBACKS_MODE_OR);
294+
try {
295+
runCallbacks(1, RUN_CALLBACKS_MODE_OR);
296+
} catch (LuaError &e) {
297+
getClient()->setFatalError(e);
298+
return true;
299+
}
242300
return readParam<bool>(L, -1);
243301
}
244302

‎src/script/cpp_api/s_env.cpp

+5-18
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,7 @@ void ScriptApiEnv::environment_Step(float dtime)
5353
lua_getfield(L, -1, "registered_globalsteps");
5454
// Call callbacks
5555
lua_pushnumber(L, dtime);
56-
try {
57-
runCallbacks(1, RUN_CALLBACKS_MODE_FIRST);
58-
} catch (LuaError &e) {
59-
getServer()->setAsyncFatalError(
60-
std::string("environment_Step: ") + e.what() + "\n"
61-
+ script_get_backtrace(L));
62-
}
56+
runCallbacks(1, RUN_CALLBACKS_MODE_FIRST);
6357
}
6458

6559
void ScriptApiEnv::player_event(ServerActiveObject *player, const std::string &type)
@@ -76,13 +70,7 @@ void ScriptApiEnv::player_event(ServerActiveObject *player, const std::string &t
7670
// Call callbacks
7771
objectrefGetOrCreate(L, player); // player
7872
lua_pushstring(L,type.c_str()); // event type
79-
try {
80-
runCallbacks(2, RUN_CALLBACKS_MODE_FIRST);
81-
} catch (LuaError &e) {
82-
getServer()->setAsyncFatalError(
83-
std::string("player_event: ") + e.what() + "\n"
84-
+ script_get_backtrace(L) );
85-
}
73+
runCallbacks(2, RUN_CALLBACKS_MODE_FIRST);
8674
}
8775

8876
void ScriptApiEnv::initializeEnvironment(ServerEnvironment *env)
@@ -257,9 +245,8 @@ void ScriptApiEnv::on_emerge_area_completion(
257245
try {
258246
PCALL_RES(lua_pcall(L, 4, 0, error_handler));
259247
} catch (LuaError &e) {
260-
server->setAsyncFatalError(
261-
std::string("on_emerge_area_completion: ") + e.what() + "\n"
262-
+ script_get_backtrace(L));
248+
// Note: don't throw here, we still need to run the cleanup code below
249+
server->setAsyncFatalError(e);
263250
}
264251

265252
lua_pop(L, 1); // Pop error handler
@@ -300,4 +287,4 @@ void ScriptApiEnv::on_liquid_transformed(
300287
}
301288

302289
runCallbacks(2, RUN_CALLBACKS_MODE_FIRST);
303-
}
290+
}

0 commit comments

Comments
 (0)
Please sign in to comment.