]> git.lizzy.rs Git - minetest.git/blob - src/script/common/c_internal.h
Fix race condition in registration leading to duplicate create_auth calls
[minetest.git] / src / script / common / c_internal.h
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
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 /******************************************************************************/
26
27 #pragma once
28
29 extern "C" {
30 #include <lua.h>
31 #include <lauxlib.h>
32 }
33
34 #include "config.h"
35 #include "common/c_types.h"
36
37
38 /*
39         Define our custom indices into the Lua registry table.
40
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.
43
44         Lua 5.1 / LuaJIT do not use any numeric indices (only string indices),
45         so we can use numeric indices freely.
46 */
47 #ifdef LUA_RIDX_LAST
48 #define CUSTOM_RIDX_BASE ((LUA_RIDX_LAST)+1)
49 #else
50 #define CUSTOM_RIDX_BASE 1
51 #endif
52
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_BACKTRACE           (CUSTOM_RIDX_BASE + 3)
57 #define CUSTOM_RIDX_HTTP_API_LUA        (CUSTOM_RIDX_BASE + 4)
58 #define CUSTOM_RIDX_VECTOR_METATABLE    (CUSTOM_RIDX_BASE + 5)
59
60
61 // Determine if CUSTOM_RIDX_SCRIPTAPI will hold a light or full userdata
62 #if defined(__aarch64__) && USE_LUAJIT
63 /* LuaJIT has a 47-bit limit for lightuserdata on this platform and we cannot
64  * assume that the ScriptApi class was allocated at a fitting address. */
65 #define INDIRECT_SCRIPTAPI_RIDX 1
66 #else
67 #define INDIRECT_SCRIPTAPI_RIDX 0
68 #endif
69
70 // Pushes the error handler onto the stack and returns its index
71 #define PUSH_ERROR_HANDLER(L) \
72         (lua_rawgeti((L), LUA_REGISTRYINDEX, CUSTOM_RIDX_BACKTRACE), lua_gettop((L)))
73
74 #define PCALL_RESL(L, RES) {                            \
75         int result_ = (RES);                                \
76         if (result_ != 0) {                                 \
77                 script_error((L), result_, NULL, __FUNCTION__); \
78         }                                                   \
79 }
80
81 // What script_run_callbacks does with the return values of callbacks.
82 // Regardless of the mode, if only one callback is defined,
83 // its return value is the total return value.
84 // Modes only affect the case where 0 or >= 2 callbacks are defined.
85 enum RunCallbacksMode
86 {
87         // Returns the return value of the first callback
88         // Returns nil if list of callbacks is empty
89         RUN_CALLBACKS_MODE_FIRST,
90         // Returns the return value of the last callback
91         // Returns nil if list of callbacks is empty
92         RUN_CALLBACKS_MODE_LAST,
93         // If any callback returns a false value, the first such is returned
94         // Otherwise, the first callback's return value (trueish) is returned
95         // Returns true if list of callbacks is empty
96         RUN_CALLBACKS_MODE_AND,
97         // Like above, but stops calling callbacks (short circuit)
98         // after seeing the first false value
99         RUN_CALLBACKS_MODE_AND_SC,
100         // If any callback returns a true value, the first such is returned
101         // Otherwise, the first callback's return value (falseish) is returned
102         // Returns false if list of callbacks is empty
103         RUN_CALLBACKS_MODE_OR,
104         // Like above, but stops calling callbacks (short circuit)
105         // after seeing the first true value
106         RUN_CALLBACKS_MODE_OR_SC,
107         // Note: "a true value" and "a false value" refer to values that
108         // are converted by readParam<bool> to true or false, respectively.
109 };
110
111 // Gets a backtrace of the current execution point
112 std::string script_get_backtrace(lua_State *L);
113 // Wrapper for CFunction calls that converts C++ exceptions to Lua errors
114 int script_exception_wrapper(lua_State *L, lua_CFunction f);
115 // Takes an error from lua_pcall and throws it as a LuaError
116 void script_error(lua_State *L, int pcall_result, const char *mod, const char *fxn);
117
118 bool script_log_unique(lua_State *L, std::string message, std::ostream &log_to,
119         int stack_depth = 1);
120
121 enum DeprecatedHandlingMode {
122         Ignore,
123         Log,
124         Error
125 };
126
127 /**
128  * Reads `deprecated_lua_api_handling` in settings, returns cached value.
129  *
130  * @return DeprecatedHandlingMode
131  */
132 DeprecatedHandlingMode get_deprecated_handling_mode();
133
134 /**
135  * Handles a deprecation warning based on user settings
136  *
137  * @param L Lua State
138  * @param message The deprecation method
139  * @param stack_depth How far on the stack to the first user function (ie: not builtin or core)
140  */
141 void log_deprecated(lua_State *L, std::string message, int stack_depth = 1);