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)
29 lua_getglobal(L, "debug");
30 if(lua_istable(L, -1)){
31 lua_getfield(L, -1, "traceback");
32 if(lua_isfunction(L, -1)) {
34 if(lua_isstring(L, -1)){
35 s = lua_tostring(L, -1);
44 int script_error_handler(lua_State *L) {
45 lua_getglobal(L, "debug");
46 if (!lua_istable(L, -1)) {
50 lua_getfield(L, -1, "traceback");
51 if (!lua_isfunction(L, -1)) {
56 lua_pushinteger(L, 2);
61 int script_exception_wrapper(lua_State *L, lua_CFunction f)
64 return f(L); // Call wrapped function and return result.
65 } catch (const char *s) { // Catch and convert exceptions.
67 } catch (std::exception& e) {
68 lua_pushstring(L, e.what());
70 lua_pushliteral(L, "caught (...)");
72 return lua_error(L); // Rethrow as a Lua error.
75 void script_error(lua_State *L)
77 const char *s = lua_tostring(L, -1);
78 std::string str(s ? s : "");
82 // Push the list of callbacks (a lua table).
83 // Then push nargs arguments.
84 // Then call this function, which
85 // - runs the callbacks
86 // - replaces the table and arguments with the return value,
87 // computed depending on mode
88 void script_run_callbacks(lua_State *L, int nargs, RunCallbacksMode mode)
90 assert(lua_gettop(L) >= nargs + 1);
92 // Insert error handler
93 lua_pushcfunction(L, script_error_handler);
94 int errorhandler = lua_gettop(L) - nargs - 1;
95 lua_insert(L, errorhandler);
97 // Insert run_callbacks between error handler and table
98 lua_getglobal(L, "core");
99 lua_getfield(L, -1, "run_callbacks");
101 lua_insert(L, errorhandler + 1);
103 // Insert mode after table
104 lua_pushnumber(L, (int) mode);
105 lua_insert(L, errorhandler + 3);
107 // Stack now looks like this:
108 // ... <error handler> <run_callbacks> <table> <mode> <arg#1> <arg#2> ... <arg#n>
110 if (lua_pcall(L, nargs + 2, 1, errorhandler)) {
114 lua_remove(L, -2); // Remove error handler
117 void log_deprecated(lua_State *L, std::string message)
119 static bool configured = false;
120 static bool dolog = false;
121 static bool doerror = false;
123 // performance optimization to not have to read and compare setting for every logline
125 std::string value = g_settings->get("deprecated_lua_api_handling");
126 if (value == "log") {
129 if (value == "error") {
139 /* As of april 2014 assert is not optimized to nop in release builds
140 * therefore this is correct. */
141 assert("Can't do a scripterror for this deprecated message, so exit completely!");
146 /* abusing actionstream because of lack of file-only-logged loglevel */
147 actionstream << message << std::endl;
149 actionstream << script_get_backtrace(L) << std::endl;