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 /******************************************************************************/
21 /******************************************************************************/
22 /* WARNING!!!! do NOT add this header in any include file or any code file */
23 /* not being a modapi file!!!!!!!! */
24 /******************************************************************************/
25 /******************************************************************************/
35 #include "common/c_types.h"
39 Define our custom indices into the Lua registry table.
41 Lua 5.2 and above define the LUA_RIDX_LAST macro. Only numbers above that
42 may be used for custom indices, anything else is reserved.
44 Lua 5.1 / LuaJIT do not use any numeric indices (only string indices),
45 so we can use numeric indices freely.
48 #define CUSTOM_RIDX_BASE ((LUA_RIDX_LAST)+1)
50 #define CUSTOM_RIDX_BASE 1
53 #define CUSTOM_RIDX_SCRIPTAPI (CUSTOM_RIDX_BASE)
54 #define CUSTOM_RIDX_GLOBALS_BACKUP (CUSTOM_RIDX_BASE + 1)
55 #define CUSTOM_RIDX_CURRENT_MOD_NAME (CUSTOM_RIDX_BASE + 2)
56 #define CUSTOM_RIDX_ERROR_HANDLER (CUSTOM_RIDX_BASE + 3)
58 // Pushes the error handler onto the stack and returns its index
59 #define PUSH_ERROR_HANDLER(L) \
60 (lua_rawgeti((L), LUA_REGISTRYINDEX, CUSTOM_RIDX_ERROR_HANDLER), lua_gettop((L)))
62 #define PCALL_RESL(L, RES) do { \
63 int result_ = (RES); \
65 script_error((L), result_, NULL, __FUNCTION__); \
69 #define script_run_callbacks(L, nargs, mode) \
70 script_run_callbacks_f((L), (nargs), (mode), __FUNCTION__)
72 // What script_run_callbacks does with the return values of callbacks.
73 // Regardless of the mode, if only one callback is defined,
74 // its return value is the total return value.
75 // Modes only affect the case where 0 or >= 2 callbacks are defined.
78 // Returns the return value of the first callback
79 // Returns nil if list of callbacks is empty
80 RUN_CALLBACKS_MODE_FIRST,
81 // Returns the return value of the last callback
82 // Returns nil if list of callbacks is empty
83 RUN_CALLBACKS_MODE_LAST,
84 // If any callback returns a false value, the first such is returned
85 // Otherwise, the first callback's return value (trueish) is returned
86 // Returns true if list of callbacks is empty
87 RUN_CALLBACKS_MODE_AND,
88 // Like above, but stops calling callbacks (short circuit)
89 // after seeing the first false value
90 RUN_CALLBACKS_MODE_AND_SC,
91 // If any callback returns a true value, the first such is returned
92 // Otherwise, the first callback's return value (falseish) is returned
93 // Returns false if list of callbacks is empty
94 RUN_CALLBACKS_MODE_OR,
95 // Like above, but stops calling callbacks (short circuit)
96 // after seeing the first true value
97 RUN_CALLBACKS_MODE_OR_SC,
98 // Note: "a true value" and "a false value" refer to values that
99 // are converted by lua_toboolean to true or false, respectively.
102 std::string script_get_backtrace(lua_State *L);
103 int script_error_handler(lua_State *L);
104 int script_exception_wrapper(lua_State *L, lua_CFunction f);
105 void script_error(lua_State *L, int pcall_result, const char *mod, const char *fxn);
106 void script_run_callbacks_f(lua_State *L, int nargs,
107 RunCallbacksMode mode, const char *fxn);
108 void log_deprecated(lua_State *L, const std::string &message);
110 #endif /* C_INTERNAL_H_ */