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 return luaL_checkstring(L, -1);
33 int script_exception_wrapper(lua_State *L, lua_CFunction f)
36 return f(L); // Call wrapped function and return result.
37 } catch (const char *s) { // Catch and convert exceptions.
39 } catch (std::exception &e) {
40 lua_pushstring(L, e.what());
42 return lua_error(L); // Rethrow as a Lua error.
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.
55 void script_error(lua_State *L, int pcall_result, const char *mod, const char *fxn)
57 if (pcall_result == 0)
61 switch (pcall_result) {
69 err_type = "Double fault";
81 const char *err_descr = lua_tostring(L, -1);
83 err_descr = "<no description>";
86 porting::mt_snprintf(buf, sizeof(buf), "%s error from mod '%s' in callback %s(): ",
89 std::string err_msg(buf);
92 if (pcall_result == LUA_ERRMEM) {
93 err_msg += "\nCurrent Lua memory usage: "
94 + itos(lua_gc(L, LUA_GCCOUNT, 0) >> 10) + " MB";
97 throw LuaError(err_msg);
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)
109 FATAL_ERROR_IF(lua_gettop(L) < nargs + 1, "Not enough arguments");
111 // Insert error handler
112 PUSH_ERROR_HANDLER(L);
113 int error_handler = lua_gettop(L) - nargs - 1;
114 lua_insert(L, error_handler);
116 // Insert run_callbacks between error handler and table
117 lua_getglobal(L, "core");
118 lua_getfield(L, -1, "run_callbacks");
120 lua_insert(L, error_handler + 1);
122 // Insert mode after table
123 lua_pushnumber(L, (int) mode);
124 lua_insert(L, error_handler + 3);
126 // Stack now looks like this:
127 // ... <error handler> <run_callbacks> <table> <mode> <arg#1> <arg#2> ... <arg#n>
129 int result = lua_pcall(L, nargs + 2, 1, error_handler);
131 script_error(L, result, NULL, fxn);
133 lua_remove(L, error_handler);
136 void log_deprecated(lua_State *L, const std::string &message)
138 static bool configured = false;
139 static bool do_log = false;
140 static bool do_error = false;
142 // Only read settings on first call
144 std::string value = g_settings->get("deprecated_lua_api_handling");
145 if (value == "log") {
147 } else if (value == "error") {
154 warningstream << message;
155 if (L) { // L can be NULL if we get called from scripting_game.cpp
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 << ")";
163 warningstream << std::endl;
167 script_error(L, LUA_ERRRUN, NULL, NULL);
169 infostream << script_get_backtrace(L) << std::endl;