]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/common/c_internal.cpp
Add option to give every object a nametag
[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 "settings.h"
24
25 std::string script_get_backtrace(lua_State *L)
26 {
27         std::string s;
28         lua_getglobal(L, "debug");
29         if(lua_istable(L, -1)){
30                 lua_getfield(L, -1, "traceback");
31                 if(lua_isfunction(L, -1)) {
32                         lua_call(L, 0, 1);
33                         if(lua_isstring(L, -1)){
34                                 s = lua_tostring(L, -1);
35                         }
36                 }
37                 lua_pop(L, 1);
38         }
39         lua_pop(L, 1);
40         return s;
41 }
42
43 int script_error_handler(lua_State *L) {
44         lua_getglobal(L, "debug");
45         if (!lua_istable(L, -1)) {
46                 lua_pop(L, 1);
47                 return 1;
48         }
49         lua_getfield(L, -1, "traceback");
50         if (!lua_isfunction(L, -1)) {
51                 lua_pop(L, 2);
52                 return 1;
53         }
54         lua_pushvalue(L, 1);
55         lua_pushinteger(L, 2);
56         lua_call(L, 2, 1);
57         return 1;
58 }
59
60 int script_exception_wrapper(lua_State *L, lua_CFunction f)
61 {
62         try {
63                 return f(L);  // Call wrapped function and return result.
64         } catch (const char *s) {  // Catch and convert exceptions.
65                 lua_pushstring(L, s);
66         } catch (std::exception &e) {
67                 lua_pushstring(L, e.what());
68         }
69         return lua_error(L);  // Rethrow as a Lua error.
70 }
71
72 /*
73  * Note that we can't get tracebacks for LUA_ERRMEM or LUA_ERRERR (without
74  * hacking Lua internals).  For LUA_ERRMEM, this is because memory errors will
75  * not execute the the error handler, and by the time lua_pcall returns the
76  * execution stack will have already been unwound.  For LUA_ERRERR, there was
77  * another error while trying to generate a backtrace from a LUA_ERRRUN.  It is
78  * presumed there is an error with the internal Lua state and thus not possible
79  * to gather a coherent backtrace.  Realistically, the best we can do here is
80  * print which C function performed the failing pcall.
81  */
82 void script_error(lua_State *L, int pcall_result, const char *mod, const char *fxn)
83 {
84         if (pcall_result == 0)
85                 return;
86
87         const char *err_type;
88         switch (pcall_result) {
89         case LUA_ERRRUN:
90                 err_type = "Runtime";
91                 break;
92         case LUA_ERRMEM:
93                 err_type = "OOM";
94                 break;
95         case LUA_ERRERR:
96                 err_type = "Double fault";
97                 break;
98         default:
99                 err_type = "Unknown";
100         }
101
102         if (!mod)
103                 mod = "??";
104
105         if (!fxn)
106                 fxn = "??";
107
108         const char *err_descr = lua_tostring(L, -1);
109         if (!err_descr)
110                 err_descr = "<no description>";
111
112         char buf[256];
113         snprintf(buf, sizeof(buf), "%s error from mod '%s' in callback %s(): ",
114                 err_type, mod, fxn);
115
116         std::string err_msg(buf);
117         err_msg += err_descr;
118
119         if (pcall_result == LUA_ERRMEM) {
120                 err_msg += "\nCurrent Lua memory usage: "
121                         + itos(lua_gc(L, LUA_GCCOUNT, 0) >> 10) + " MB";
122         }
123
124         throw LuaError(err_msg);
125 }
126
127 // Push the list of callbacks (a lua table).
128 // Then push nargs arguments.
129 // Then call this function, which
130 // - runs the callbacks
131 // - replaces the table and arguments with the return value,
132 //     computed depending on mode
133 void script_run_callbacks_f(lua_State *L, int nargs,
134         RunCallbacksMode mode, const char *fxn)
135 {
136         FATAL_ERROR_IF(lua_gettop(L) < nargs + 1, "Not enough arguments");
137
138         // Insert error handler
139         PUSH_ERROR_HANDLER(L);
140         int error_handler = lua_gettop(L) - nargs - 1;
141         lua_insert(L, error_handler);
142
143         // Insert run_callbacks between error handler and table
144         lua_getglobal(L, "core");
145         lua_getfield(L, -1, "run_callbacks");
146         lua_remove(L, -2);
147         lua_insert(L, error_handler + 1);
148
149         // Insert mode after table
150         lua_pushnumber(L, (int) mode);
151         lua_insert(L, error_handler + 3);
152
153         // Stack now looks like this:
154         // ... <error handler> <run_callbacks> <table> <mode> <arg#1> <arg#2> ... <arg#n>
155
156         int result = lua_pcall(L, nargs + 2, 1, error_handler);
157         if (result != 0)
158                 script_error(L, result, NULL, fxn);
159
160         lua_remove(L, error_handler);
161 }
162
163 void log_deprecated(lua_State *L, const std::string &message)
164 {
165         static bool configured = false;
166         static bool do_log     = false;
167         static bool do_error   = false;
168
169         // Only read settings on first call
170         if (!configured) {
171                 std::string value = g_settings->get("deprecated_lua_api_handling");
172                 if (value == "log") {
173                         do_log = true;
174                 } else if (value == "error") {
175                         do_log   = true;
176                         do_error = true;
177                 }
178         }
179
180         if (do_log) {
181                 warningstream << message << std::endl;
182                 // L can be NULL if we get called by log_deprecated(const std::string &msg)
183                 // from scripting_game.cpp.
184                 if (L) {
185                         if (do_error)
186                                 script_error(L, LUA_ERRRUN, NULL, NULL);
187                         else
188                                 infostream << script_get_backtrace(L) << std::endl;
189                 }
190         }
191 }
192