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"
25 std::string script_get_backtrace(lua_State *L)
28 lua_getglobal(L, "debug");
29 if(lua_istable(L, -1)){
30 lua_getfield(L, -1, "traceback");
31 if(lua_isfunction(L, -1)) {
33 if(lua_isstring(L, -1)){
34 s = lua_tostring(L, -1);
43 int script_error_handler(lua_State *L) {
44 lua_getglobal(L, "debug");
45 if (!lua_istable(L, -1)) {
49 lua_getfield(L, -1, "traceback");
50 if (!lua_isfunction(L, -1)) {
55 lua_pushinteger(L, 2);
60 int script_exception_wrapper(lua_State *L, lua_CFunction f)
63 return f(L); // Call wrapped function and return result.
64 } catch (const char *s) { // Catch and convert exceptions.
66 } catch (std::exception &e) {
67 lua_pushstring(L, e.what());
69 return lua_error(L); // Rethrow as a Lua error.
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.
82 void script_error(lua_State *L, int pcall_result, const char *mod, const char *fxn)
84 if (pcall_result == 0)
88 switch (pcall_result) {
96 err_type = "Double fault";
108 const char *err_descr = lua_tostring(L, -1);
110 err_descr = "<no description>";
113 snprintf(buf, sizeof(buf), "%s error from mod '%s' in callback %s(): ",
116 std::string err_msg(buf);
117 err_msg += err_descr;
119 if (pcall_result == LUA_ERRMEM) {
120 err_msg += "\nCurrent Lua memory usage: "
121 + itos(lua_gc(L, LUA_GCCOUNT, 0) >> 10) + " MB";
124 throw LuaError(err_msg);
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)
136 FATAL_ERROR_IF(lua_gettop(L) < nargs + 1, "Not enough arguments");
138 // Insert error handler
139 PUSH_ERROR_HANDLER(L);
140 int error_handler = lua_gettop(L) - nargs - 1;
141 lua_insert(L, error_handler);
143 // Insert run_callbacks between error handler and table
144 lua_getglobal(L, "core");
145 lua_getfield(L, -1, "run_callbacks");
147 lua_insert(L, error_handler + 1);
149 // Insert mode after table
150 lua_pushnumber(L, (int) mode);
151 lua_insert(L, error_handler + 3);
153 // Stack now looks like this:
154 // ... <error handler> <run_callbacks> <table> <mode> <arg#1> <arg#2> ... <arg#n>
156 int result = lua_pcall(L, nargs + 2, 1, error_handler);
158 script_error(L, result, NULL, fxn);
160 lua_remove(L, error_handler);
163 void log_deprecated(lua_State *L, const std::string &message)
165 static bool configured = false;
166 static bool dolog = false;
167 static bool doerror = false;
169 // performance optimization to not have to read and compare setting for every logline
171 std::string value = g_settings->get("deprecated_lua_api_handling");
172 if (value == "log") {
174 } else if (value == "error") {
182 script_error(L, LUA_ERRRUN, NULL, NULL);
184 FATAL_ERROR("Can't do a scripterror for this deprecated message, "
185 "so exit completely!");
190 /* abusing actionstream because of lack of file-only-logged loglevel */
191 actionstream << message << std::endl;
193 actionstream << script_get_backtrace(L) << std::endl;