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 (LuaError& e) {
68 lua_pushstring(L, e.what());
70 return lua_error(L); // Rethrow as a Lua error.
73 void script_error(lua_State *L)
75 const char *s = lua_tostring(L, -1);
76 std::string str(s ? s : "");
80 // Push the list of callbacks (a lua table).
81 // Then push nargs arguments.
82 // Then call this function, which
83 // - runs the callbacks
84 // - replaces the table and arguments with the return value,
85 // computed depending on mode
86 void script_run_callbacks(lua_State *L, int nargs, RunCallbacksMode mode)
88 assert(lua_gettop(L) >= nargs + 1);
90 // Insert error handler
91 lua_pushcfunction(L, script_error_handler);
92 int errorhandler = lua_gettop(L) - nargs - 1;
93 lua_insert(L, errorhandler);
95 // Insert run_callbacks between error handler and table
96 lua_getglobal(L, "core");
97 lua_getfield(L, -1, "run_callbacks");
99 lua_insert(L, errorhandler + 1);
101 // Insert mode after table
102 lua_pushnumber(L, (int) mode);
103 lua_insert(L, errorhandler + 3);
105 // Stack now looks like this:
106 // ... <error handler> <run_callbacks> <table> <mode> <arg#1> <arg#2> ... <arg#n>
108 if (lua_pcall(L, nargs + 2, 1, errorhandler)) {
112 lua_remove(L, -2); // Remove error handler
115 void log_deprecated(lua_State *L, std::string message)
117 static bool configured = false;
118 static bool dolog = false;
119 static bool doerror = false;
121 // performance optimization to not have to read and compare setting for every logline
123 std::string value = g_settings->get("deprecated_lua_api_handling");
124 if (value == "log") {
127 if (value == "error") {
137 /* As of april 2014 assert is not optimized to nop in release builds
138 * therefore this is correct. */
139 assert("Can't do a scripterror for this deprecated message, so exit completely!");
144 /* abusing actionstream because of lack of file-only-logged loglevel */
145 actionstream << message << std::endl;
147 actionstream << script_get_backtrace(L) << std::endl;