]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/common/c_internal.cpp
Remove unused functions reported by cppcheck (#10463)
[dragonfireclient.git] / src / script / common / c_internal.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "common/c_internal.h"
21 #include "debug.h"
22 #include "log.h"
23 #include "porting.h"
24 #include "settings.h"
25
26 std::string script_get_backtrace(lua_State *L)
27 {
28         lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_BACKTRACE);
29         lua_call(L, 0, 1);
30         std::string result = luaL_checkstring(L, -1);
31         lua_pop(L, 1);
32         return result;
33 }
34
35 int script_exception_wrapper(lua_State *L, lua_CFunction f)
36 {
37         try {
38                 return f(L);  // Call wrapped function and return result.
39         } catch (const char *s) {  // Catch and convert exceptions.
40                 lua_pushstring(L, s);
41         } catch (std::exception &e) {
42                 lua_pushstring(L, e.what());
43         }
44         return lua_error(L);  // Rethrow as a Lua error.
45 }
46
47 /*
48  * Note that we can't get tracebacks for LUA_ERRMEM or LUA_ERRERR (without
49  * hacking Lua internals).  For LUA_ERRMEM, this is because memory errors will
50  * not execute the error handler, and by the time lua_pcall returns the
51  * execution stack will have already been unwound.  For LUA_ERRERR, there was
52  * another error while trying to generate a backtrace from a LUA_ERRRUN.  It is
53  * presumed there is an error with the internal Lua state and thus not possible
54  * to gather a coherent backtrace.  Realistically, the best we can do here is
55  * print which C function performed the failing pcall.
56  */
57 void script_error(lua_State *L, int pcall_result, const char *mod, const char *fxn)
58 {
59         if (pcall_result == 0)
60                 return;
61
62         const char *err_type;
63         switch (pcall_result) {
64         case LUA_ERRRUN:
65                 err_type = "Runtime";
66                 break;
67         case LUA_ERRMEM:
68                 err_type = "OOM";
69                 break;
70         case LUA_ERRERR:
71                 err_type = "Double fault";
72                 break;
73         default:
74                 err_type = "Unknown";
75         }
76
77         if (!mod)
78                 mod = "??";
79
80         if (!fxn)
81                 fxn = "??";
82
83         const char *err_descr = lua_tostring(L, -1);
84         if (!err_descr)
85                 err_descr = "<no description>";
86
87         char buf[256];
88         porting::mt_snprintf(buf, sizeof(buf), "%s error from mod '%s' in callback %s(): ",
89                 err_type, mod, fxn);
90
91         std::string err_msg(buf);
92         err_msg += err_descr;
93
94         if (pcall_result == LUA_ERRMEM) {
95                 err_msg += "\nCurrent Lua memory usage: "
96                         + itos(lua_gc(L, LUA_GCCOUNT, 0) >> 10) + " MB";
97         }
98
99         throw LuaError(err_msg);
100 }
101
102 // Push the list of callbacks (a lua table).
103 // Then push nargs arguments.
104 // Then call this function, which
105 // - runs the callbacks
106 // - replaces the table and arguments with the return value,
107 //     computed depending on mode
108 void script_run_callbacks_f(lua_State *L, int nargs,
109         RunCallbacksMode mode, const char *fxn)
110 {
111         FATAL_ERROR_IF(lua_gettop(L) < nargs + 1, "Not enough arguments");
112
113         // Insert error handler
114         PUSH_ERROR_HANDLER(L);
115         int error_handler = lua_gettop(L) - nargs - 1;
116         lua_insert(L, error_handler);
117
118         // Insert run_callbacks between error handler and table
119         lua_getglobal(L, "core");
120         lua_getfield(L, -1, "run_callbacks");
121         lua_remove(L, -2);
122         lua_insert(L, error_handler + 1);
123
124         // Insert mode after table
125         lua_pushnumber(L, (int) mode);
126         lua_insert(L, error_handler + 3);
127
128         // Stack now looks like this:
129         // ... <error handler> <run_callbacks> <table> <mode> <arg#1> <arg#2> ... <arg#n>
130
131         int result = lua_pcall(L, nargs + 2, 1, error_handler);
132         if (result != 0)
133                 script_error(L, result, NULL, fxn);
134
135         lua_remove(L, error_handler);
136 }
137
138 static void script_log(lua_State *L, const std::string &message,
139         std::ostream &log_to, bool do_error, int stack_depth)
140 {
141         lua_Debug ar;
142
143         log_to << message << " ";
144         if (lua_getstack(L, stack_depth, &ar)) {
145                 FATAL_ERROR_IF(!lua_getinfo(L, "Sl", &ar), "lua_getinfo() failed");
146                 log_to << "(at " << ar.short_src << ":" << ar.currentline << ")";
147         } else {
148                 log_to << "(at ?:?)";
149         }
150         log_to << std::endl;
151
152         if (do_error)
153                 script_error(L, LUA_ERRRUN, NULL, NULL);
154         else
155                 infostream << script_get_backtrace(L) << std::endl;
156 }
157
158 void log_deprecated(lua_State *L, const std::string &message, int stack_depth)
159 {
160         static thread_local bool configured = false;
161         static thread_local bool do_log = false;
162         static thread_local bool do_error = false;
163
164         // Only read settings on first call
165         if (!configured) {
166                 std::string value = g_settings->get("deprecated_lua_api_handling");
167                 if (value == "log") {
168                         do_log = true;
169                 } else if (value == "error") {
170                         do_log = true;
171                         do_error = true;
172                 }
173                 configured = true;
174         }
175
176         if (do_log)
177                 script_log(L, message, warningstream, do_error, stack_depth);
178 }