Luanti 5.18.0-dev
Loading...
Searching...
No Matches
c_internal.h
Go to the documentation of this file.
1// Luanti
2// SPDX-License-Identifier: LGPL-2.1-or-later
3// Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5/******************************************************************************/
6/******************************************************************************/
7/* WARNING!!!! do NOT add this header in any include file or any code file */
8/* not being a modapi file!!!!!!!! */
9/******************************************************************************/
10/******************************************************************************/
11
12#pragma once
13
14#include <string>
15#include <string_view>
16
17extern "C" {
18#include <lua.h>
19#include <lauxlib.h>
20}
21
22#include "config.h"
23
24/*
25 Define our custom indices into the Lua registry table.
26
27 Lua 5.2 and above define the LUA_RIDX_LAST macro. Only numbers above that
28 may be used for custom indices, anything else is reserved.
29
30 Lua 5.1 / LuaJIT only hardcode the 0 numerical index (FREELIST_REF)
31 but will use free numerical indices at runtime (luaL_ref)
32*/
33enum {
34#ifdef LUA_RIDX_LAST
35 CUSTOM_RIDX_FIRST = LUA_RIDX_LAST + 1,
36#else
38#endif
39
47
48 // The following functions are implemented in Lua because LuaJIT can
49 // trace them and optimize tables/string better than from the C API.
57
59};
60
61
62// Determine if CUSTOM_RIDX_SCRIPTAPI will hold a light or full userdata
63#if defined(__aarch64__) && USE_LUAJIT
64/* LuaJIT has a 47-bit limit for lightuserdata on this platform and we cannot
65 * assume that the ScriptApi class was allocated at a fitting address. */
66#define INDIRECT_SCRIPTAPI_RIDX 1
67#else
68#define INDIRECT_SCRIPTAPI_RIDX 0
69#endif
70
71// Pushes the error handler onto the stack and returns its index
72#define PUSH_ERROR_HANDLER(L) \
73 (lua_rawgeti((L), LUA_REGISTRYINDEX, CUSTOM_RIDX_ERROR_HANDLER), lua_gettop((L)))
74
75#define PCALL_RESL(L, RES) { \
76 int result_ = (RES); \
77 if (result_ != 0) { \
78 script_error((L), result_, NULL, __FUNCTION__); \
79 } \
80}
81
82// What script_run_callbacks does with the return values of callbacks.
83// Regardless of the mode, if only one callback is defined,
84// its return value is the total return value.
85// Modes only affect the case where 0 or >= 2 callbacks are defined.
87{
88 // Returns the return value of the first callback
89 // Returns nil if list of callbacks is empty
91 // Returns the return value of the last callback
92 // Returns nil if list of callbacks is empty
94 // If any callback returns a false value, the first such is returned
95 // Otherwise, the first callback's return value (trueish) is returned
96 // Returns true if list of callbacks is empty
98 // Like above, but stops calling callbacks (short circuit)
99 // after seeing the first false value
101 // If any callback returns a true value, the first such is returned
102 // Otherwise, the first callback's return value (falseish) is returned
103 // Returns false if list of callbacks is empty
105 // Like above, but stops calling callbacks (short circuit)
106 // after seeing the first true value
108 // Note: "a true value" and "a false value" refer to values that
109 // are converted by readParam<bool> to true or false, respectively.
110};
111
112// Gets a backtrace of the current execution point
113std::string script_get_backtrace(lua_State *L);
114// Wrapper for CFunction calls that converts C++ exceptions to Lua errors
115int script_exception_wrapper(lua_State *L, lua_CFunction f);
116// Acts as the error handler for lua_pcall
117int script_error_handler(lua_State *L);
118// Takes an error from lua_pcall and throws it as a LuaError
119void script_error(lua_State *L, int pcall_result, const char *mod, const char *fxn);
120
121bool script_log_unique(lua_State *L, std::string_view message, std::ostream &log_to,
122 int stack_depth = 1);
123
129
136
146void log_deprecated(lua_State *L, std::string_view message,
147 int stack_depth = 1, bool once = false);
154std::string dump_function_to_string(lua_State *L, int idx);
DeprecatedHandlingMode
Definition c_internal.h:124
@ Log
Definition c_internal.h:126
@ Ignore
Definition c_internal.h:125
@ Error
Definition c_internal.h:127
std::string script_get_backtrace(lua_State *L)
Definition c_internal.cpp:14
DeprecatedHandlingMode get_deprecated_handling_mode()
Reads deprecated_lua_api_handling in settings, returns cached value.
Definition c_internal.cpp:149
std::string dump_function_to_string(lua_State *L, int idx)
Dumps a function at index to a string.
Definition c_internal.cpp:195
RunCallbacksMode
Definition c_internal.h:87
@ RUN_CALLBACKS_MODE_OR
Definition c_internal.h:104
@ RUN_CALLBACKS_MODE_LAST
Definition c_internal.h:93
@ RUN_CALLBACKS_MODE_AND_SC
Definition c_internal.h:100
@ RUN_CALLBACKS_MODE_FIRST
Definition c_internal.h:90
@ RUN_CALLBACKS_MODE_OR_SC
Definition c_internal.h:107
@ RUN_CALLBACKS_MODE_AND
Definition c_internal.h:97
void log_deprecated(lua_State *L, std::string_view message, int stack_depth=1, bool once=false)
Handles a deprecation warning based on user settings.
Definition c_internal.cpp:168
int script_exception_wrapper(lua_State *L, lua_CFunction f)
Definition c_internal.cpp:24
void script_error(lua_State *L, int pcall_result, const char *mod, const char *fxn)
Definition c_internal.cpp:66
int script_error_handler(lua_State *L)
Definition c_internal.cpp:37
bool script_log_unique(lua_State *L, std::string_view message, std::ostream &log_to, int stack_depth=1)
Definition c_internal.cpp:131
@ CUSTOM_RIDX_ERROR_HANDLER
Definition c_internal.h:44
@ CUSTOM_RIDX_LAST
Definition c_internal.h:58
@ CUSTOM_RIDX_HTTP_API_LUA
Definition c_internal.h:45
@ CUSTOM_RIDX_PUSH_NODE
Definition c_internal.h:55
@ CUSTOM_RIDX_READ_VECTOR
Definition c_internal.h:50
@ CUSTOM_RIDX_FIRST
Definition c_internal.h:37
@ CUSTOM_RIDX_READ_NODE
Definition c_internal.h:54
@ CUSTOM_RIDX_METATABLE_MAP
Definition c_internal.h:46
@ CUSTOM_RIDX_PUSH_VECTOR
Definition c_internal.h:51
@ CUSTOM_RIDX_PUSH_VECTOR2
Definition c_internal.h:53
@ CUSTOM_RIDX_CURRENT_MOD_NAME
Definition c_internal.h:43
@ CUSTOM_RIDX_READ_VECTOR2
Definition c_internal.h:52
@ CUSTOM_RIDX_PUSH_MOVERESULT1
Definition c_internal.h:56
@ CUSTOM_RIDX_SCRIPTAPI
Definition c_internal.h:40
@ CUSTOM_RIDX_GLOBALS_BACKUP
Definition c_internal.h:42
#define idx(x, y)
Definition noise.cpp:495