mlua_sys/luau/
luarequire.rs

1//! Contains definitions from `Require.h`.
2
3use std::os::raw::{c_char, c_int, c_void};
4
5use super::lua::lua_State;
6
7pub const LUA_REGISTERED_MODULES_TABLE: *const c_char = cstr!("_REGISTEREDMODULES");
8
9#[repr(C)]
10pub enum luarequire_NavigateResult {
11    Success,
12    Ambiguous,
13    NotFound,
14}
15
16// Functions returning WriteSuccess are expected to set their size_out argument
17// to the number of bytes written to the buffer. If WriteBufferTooSmall is
18// returned, size_out should be set to the required buffer size.
19#[repr(C)]
20pub enum luarequire_WriteResult {
21    Success,
22    BufferTooSmall,
23    Failure,
24}
25
26/// Represents whether a configuration file is present, and if so, its syntax.
27#[repr(C)]
28pub enum luarequire_ConfigStatus {
29    Absent,
30    // Signals the presence of multiple configuration files
31    Ambiguous,
32    PresentJson,
33    PresentLuau,
34}
35
36#[repr(C)]
37pub struct luarequire_Configuration {
38    // Returns whether requires are permitted from the given chunkname.
39    pub is_require_allowed: unsafe extern "C-unwind" fn(
40        L: *mut lua_State,
41        ctx: *mut c_void,
42        requirer_chunkname: *const c_char,
43    ) -> bool,
44
45    // Resets the internal state to point at the requirer module.
46    pub reset: unsafe extern "C-unwind" fn(
47        L: *mut lua_State,
48        ctx: *mut c_void,
49        requirer_chunkname: *const c_char,
50    ) -> luarequire_NavigateResult,
51
52    // Resets the internal state to point at an aliased module, given its exact path from a configuration
53    // file. This function is only called when an alias's path cannot be resolved relative to its
54    // configuration file.
55    pub jump_to_alias: unsafe extern "C-unwind" fn(
56        L: *mut lua_State,
57        ctx: *mut c_void,
58        path: *const c_char,
59    ) -> luarequire_NavigateResult,
60
61    // Provides a final override opportunity if an alias cannot be found in configuration files. If
62    // NAVIGATE_SUCCESS is returned, this must update the internal state to point at the aliased module.
63    // Can be left undefined.
64    pub to_alias_fallback: Option<
65        unsafe extern "C-unwind" fn(
66            L: *mut lua_State,
67            ctx: *mut c_void,
68            alias_unprefixed: *const c_char,
69        ) -> luarequire_NavigateResult,
70    >,
71
72    // Navigates through the context by making mutations to the internal state.
73    pub to_parent:
74        unsafe extern "C-unwind" fn(L: *mut lua_State, ctx: *mut c_void) -> luarequire_NavigateResult,
75    pub to_child: unsafe extern "C-unwind" fn(
76        L: *mut lua_State,
77        ctx: *mut c_void,
78        name: *const c_char,
79    ) -> luarequire_NavigateResult,
80
81    // Returns whether the context is currently pointing at a module.
82    pub is_module_present: unsafe extern "C-unwind" fn(L: *mut lua_State, ctx: *mut c_void) -> bool,
83
84    // Provides a chunkname for the current module. This will be accessible through the debug library. This
85    // function is only called if is_module_present returns true.
86    pub get_chunkname: unsafe extern "C-unwind" fn(
87        L: *mut lua_State,
88        ctx: *mut c_void,
89        buffer: *mut c_char,
90        buffer_size: usize,
91        size_out: *mut usize,
92    ) -> luarequire_WriteResult,
93
94    // Provides a loadname that identifies the current module and is passed to load. This function
95    // is only called if is_module_present returns true.
96    pub get_loadname: unsafe extern "C-unwind" fn(
97        L: *mut lua_State,
98        ctx: *mut c_void,
99        buffer: *mut c_char,
100        buffer_size: usize,
101        size_out: *mut usize,
102    ) -> luarequire_WriteResult,
103
104    // Provides a cache key representing the current module. This function is only called if
105    // is_module_present returns true.
106    pub get_cache_key: unsafe extern "C-unwind" fn(
107        L: *mut lua_State,
108        ctx: *mut c_void,
109        buffer: *mut c_char,
110        buffer_size: usize,
111        size_out: *mut usize,
112    ) -> luarequire_WriteResult,
113
114    // Returns whether a configuration file is present in the current context, and if so, its syntax.
115    // If not present, require-by-string will call to_parent until either a configuration file is present or
116    // NAVIGATE_FAILURE is returned (at root).
117    pub get_config_status:
118        unsafe extern "C-unwind" fn(L: *mut lua_State, ctx: *mut c_void) -> luarequire_ConfigStatus,
119
120    // Parses the configuration file in the current context for the given alias and returns its
121    // value or WRITE_FAILURE if not found. This function is only called if get_config_status
122    // returns true. If this function pointer is set, get_config must not be set. Opting in to this
123    // function pointer disables parsing configuration files internally and can be used for finer
124    // control over the configuration file parsing process.
125    pub get_alias: Option<
126        unsafe extern "C-unwind" fn(
127            L: *mut lua_State,
128            ctx: *mut c_void,
129            alias: *const c_char,
130            buffer: *mut c_char,
131            buffer_size: usize,
132            size_out: *mut usize,
133        ) -> luarequire_WriteResult,
134    >,
135
136    // Provides the contents of the configuration file in the current context.
137    // This function is only called if get_config_status does not return CONFIG_ABSENT. If this function
138    // pointer is set, get_alias must not be set. Opting in to this function pointer enables parsing
139    // configuration files internally.
140    pub get_config: Option<
141        unsafe extern "C-unwind" fn(
142            L: *mut lua_State,
143            ctx: *mut c_void,
144            buffer: *mut c_char,
145            buffer_size: usize,
146            size_out: *mut usize,
147        ) -> luarequire_WriteResult,
148    >,
149
150    // Returns the maximum number of milliseconds to allow for executing a given Luau-syntax configuration
151    // file. This function is only called if get_config_status returns CONFIG_PRESENT_LUAU and can be left
152    // undefined if support for Luau-syntax configuration files is not needed. A default value of 2000ms is
153    // used. Negative values are treated as infinite.
154    pub get_luau_config_timeout:
155        Option<unsafe extern "C-unwind" fn(L: *mut lua_State, ctx: *mut c_void) -> c_int>,
156
157    // Executes the module and places the result on the stack. Returns the number of results placed on the
158    // stack.
159    // Returning -1 directs the requiring thread to yield. In this case, this thread should be resumed with
160    // the module result pushed onto its stack.
161    pub load: unsafe extern "C-unwind" fn(
162        L: *mut lua_State,
163        ctx: *mut c_void,
164        path: *const c_char,
165        chunkname: *const c_char,
166        loadname: *const c_char,
167    ) -> c_int,
168}
169
170// Populates function pointers in the given luarequire_Configuration.
171pub type luarequire_Configuration_init = unsafe extern "C-unwind" fn(config: *mut luarequire_Configuration);
172
173unsafe extern "C-unwind" {
174    // Initializes and pushes the require closure onto the stack without registration.
175    pub fn luarequire_pushrequire(
176        L: *mut lua_State,
177        config_init: luarequire_Configuration_init,
178        ctx: *mut c_void,
179    ) -> c_int;
180
181    // Initializes the require library and registers it globally.
182    pub fn luaopen_require(L: *mut lua_State, config_init: luarequire_Configuration_init, ctx: *mut c_void);
183
184    // Initializes and pushes a "proxyrequire" closure onto the stack.
185    //
186    // The closure takes two parameters: the string path to resolve and the chunkname of an existing
187    // module.
188    pub fn luarequire_pushproxyrequire(
189        L: *mut lua_State,
190        config_init: luarequire_Configuration_init,
191        ctx: *mut c_void,
192    ) -> c_int;
193
194    // Registers an aliased require path to a result.
195    //
196    // After registration, the given result will always be immediately returned when the given path is
197    // required.
198    // Expects the path and table to be passed as arguments on the stack.
199    pub fn luarequire_registermodule(L: *mut lua_State) -> c_int;
200
201    // Clears the entry associated with the given cache key from the require cache.
202    // Expects the cache key to be passed as an argument on the stack.
203    pub fn luarequire_clearcacheentry(L: *mut lua_State) -> c_int;
204
205    // Clears all entries from the require cache.
206    pub fn luarequire_clearcache(L: *mut lua_State) -> c_int;
207}