3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
20 #include "common/c_internal.h"
26 std::string script_get_backtrace(lua_State *L)
28 lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_BACKTRACE);
30 std::string result = luaL_checkstring(L, -1);
35 int script_exception_wrapper(lua_State *L, lua_CFunction f)
38 return f(L); // Call wrapped function and return result.
39 } catch (const char *s) { // Catch and convert exceptions.
41 } catch (std::exception &e) {
42 lua_pushstring(L, e.what());
44 return lua_error(L); // Rethrow as a Lua error.
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.
57 void script_error(lua_State *L, int pcall_result, const char *mod, const char *fxn)
59 if (pcall_result == 0)
63 switch (pcall_result) {
71 err_type = "Double fault";
83 const char *err_descr = lua_tostring(L, -1);
85 err_descr = "<no description>";
88 porting::mt_snprintf(buf, sizeof(buf), "%s error from mod '%s' in callback %s(): ",
91 std::string err_msg(buf);
94 if (pcall_result == LUA_ERRMEM) {
95 err_msg += "\nCurrent Lua memory usage: "
96 + itos(lua_gc(L, LUA_GCCOUNT, 0) >> 10) + " MB";
99 throw LuaError(err_msg);
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)
111 FATAL_ERROR_IF(lua_gettop(L) < nargs + 1, "Not enough arguments");
113 // Insert error handler
114 PUSH_ERROR_HANDLER(L);
115 int error_handler = lua_gettop(L) - nargs - 1;
116 lua_insert(L, error_handler);
118 // Insert run_callbacks between error handler and table
119 lua_getglobal(L, "core");
120 lua_getfield(L, -1, "run_callbacks");
122 lua_insert(L, error_handler + 1);
124 // Insert mode after table
125 lua_pushnumber(L, (int) mode);
126 lua_insert(L, error_handler + 3);
128 // Stack now looks like this:
129 // ... <error handler> <run_callbacks> <table> <mode> <arg#1> <arg#2> ... <arg#n>
131 int result = lua_pcall(L, nargs + 2, 1, error_handler);
133 script_error(L, result, NULL, fxn);
135 lua_remove(L, error_handler);
138 static void script_log(lua_State *L, const std::string &message,
139 std::ostream &log_to, bool do_error, int stack_depth)
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 << ")";
148 log_to << "(at ?:?)";
153 script_error(L, LUA_ERRRUN, NULL, NULL);
155 infostream << script_get_backtrace(L) << std::endl;
158 void log_deprecated(lua_State *L, const std::string &message, int stack_depth)
160 static thread_local bool configured = false;
161 static thread_local bool do_log = false;
162 static thread_local bool do_error = false;
164 // Only read settings on first call
166 std::string value = g_settings->get("deprecated_lua_api_handling");
167 if (value == "log") {
169 } else if (value == "error") {
177 script_log(L, message, warningstream, do_error, stack_depth);