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