Skip to main content

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 an initial alias override opportunity prior to searching for configuration files.
62    // If NAVIGATE_SUCCESS is returned, the internal state must be updated to point at the
63    // aliased location.
64    // Can be left undefined.
65    pub to_alias_override: Option<
66        unsafe extern "C-unwind" fn(
67            L: *mut lua_State,
68            ctx: *mut c_void,
69            alias_unprefixed: *const c_char,
70        ) -> luarequire_NavigateResult,
71    >,
72
73    // Provides a final override opportunity if an alias cannot be found in configuration files. If
74    // NAVIGATE_SUCCESS is returned, this must update the internal state to point at the aliased module.
75    // Can be left undefined.
76    pub to_alias_fallback: Option<
77        unsafe extern "C-unwind" fn(
78            L: *mut lua_State,
79            ctx: *mut c_void,
80            alias_unprefixed: *const c_char,
81        ) -> luarequire_NavigateResult,
82    >,
83
84    // Navigates through the context by making mutations to the internal state.
85    pub to_parent:
86        unsafe extern "C-unwind" fn(L: *mut lua_State, ctx: *mut c_void) -> luarequire_NavigateResult,
87    pub to_child: unsafe extern "C-unwind" fn(
88        L: *mut lua_State,
89        ctx: *mut c_void,
90        name: *const c_char,
91    ) -> luarequire_NavigateResult,
92
93    // Returns whether the context is currently pointing at a module.
94    pub is_module_present: unsafe extern "C-unwind" fn(L: *mut lua_State, ctx: *mut c_void) -> bool,
95
96    // Provides a chunkname for the current module. This will be accessible through the debug library. This
97    // function is only called if is_module_present returns true.
98    pub get_chunkname: unsafe extern "C-unwind" fn(
99        L: *mut lua_State,
100        ctx: *mut c_void,
101        buffer: *mut c_char,
102        buffer_size: usize,
103        size_out: *mut usize,
104    ) -> luarequire_WriteResult,
105
106    // Provides a loadname that identifies the current module and is passed to load. This function
107    // is only called if is_module_present returns true.
108    pub get_loadname: unsafe extern "C-unwind" fn(
109        L: *mut lua_State,
110        ctx: *mut c_void,
111        buffer: *mut c_char,
112        buffer_size: usize,
113        size_out: *mut usize,
114    ) -> luarequire_WriteResult,
115
116    // Provides a cache key representing the current module. This function is only called if
117    // is_module_present returns true.
118    pub get_cache_key: unsafe extern "C-unwind" fn(
119        L: *mut lua_State,
120        ctx: *mut c_void,
121        buffer: *mut c_char,
122        buffer_size: usize,
123        size_out: *mut usize,
124    ) -> luarequire_WriteResult,
125
126    // Returns whether a configuration file is present in the current context, and if so, its syntax.
127    // If not present, require-by-string will call to_parent until either a configuration file is present or
128    // NAVIGATE_FAILURE is returned (at root).
129    pub get_config_status:
130        unsafe extern "C-unwind" fn(L: *mut lua_State, ctx: *mut c_void) -> luarequire_ConfigStatus,
131
132    // Parses the configuration file in the current context for the given alias and returns its
133    // value or WRITE_FAILURE if not found. This function is only called if get_config_status
134    // returns true. If this function pointer is set, get_config must not be set. Opting in to this
135    // function pointer disables parsing configuration files internally and can be used for finer
136    // control over the configuration file parsing process.
137    pub get_alias: Option<
138        unsafe extern "C-unwind" fn(
139            L: *mut lua_State,
140            ctx: *mut c_void,
141            alias: *const c_char,
142            buffer: *mut c_char,
143            buffer_size: usize,
144            size_out: *mut usize,
145        ) -> luarequire_WriteResult,
146    >,
147
148    // Provides the contents of the configuration file in the current context.
149    // This function is only called if get_config_status does not return CONFIG_ABSENT. If this function
150    // pointer is set, get_alias must not be set. Opting in to this function pointer enables parsing
151    // configuration files internally.
152    pub get_config: Option<
153        unsafe extern "C-unwind" fn(
154            L: *mut lua_State,
155            ctx: *mut c_void,
156            buffer: *mut c_char,
157            buffer_size: usize,
158            size_out: *mut usize,
159        ) -> luarequire_WriteResult,
160    >,
161
162    // Returns the maximum number of milliseconds to allow for executing a given Luau-syntax configuration
163    // file. This function is only called if get_config_status returns CONFIG_PRESENT_LUAU and can be left
164    // undefined if support for Luau-syntax configuration files is not needed. A default value of 2000ms is
165    // used. Negative values are treated as infinite.
166    pub get_luau_config_timeout:
167        Option<unsafe extern "C-unwind" fn(L: *mut lua_State, ctx: *mut c_void) -> c_int>,
168
169    // Executes the module and places the result on the stack. Returns the number of results placed on the
170    // stack.
171    // Returning -1 directs the requiring thread to yield. In this case, this thread should be resumed with
172    // the module result pushed onto its stack.
173    pub load: unsafe extern "C-unwind" fn(
174        L: *mut lua_State,
175        ctx: *mut c_void,
176        path: *const c_char,
177        chunkname: *const c_char,
178        loadname: *const c_char,
179    ) -> c_int,
180}
181
182// Populates function pointers in the given luarequire_Configuration.
183pub type luarequire_Configuration_init = unsafe extern "C-unwind" fn(config: *mut luarequire_Configuration);
184
185unsafe extern "C-unwind" {
186    // Initializes and pushes the require closure onto the stack without registration.
187    pub fn luarequire_pushrequire(
188        L: *mut lua_State,
189        config_init: luarequire_Configuration_init,
190        ctx: *mut c_void,
191    ) -> c_int;
192
193    // Initializes the require library and registers it globally.
194    pub fn luaopen_require(L: *mut lua_State, config_init: luarequire_Configuration_init, ctx: *mut c_void);
195
196    // Initializes and pushes a "proxyrequire" closure onto the stack.
197    //
198    // The closure takes two parameters: the string path to resolve and the chunkname of an existing
199    // module.
200    pub fn luarequire_pushproxyrequire(
201        L: *mut lua_State,
202        config_init: luarequire_Configuration_init,
203        ctx: *mut c_void,
204    ) -> c_int;
205
206    // Registers an aliased require path to a result.
207    //
208    // After registration, the given result will always be immediately returned when the given path is
209    // required.
210    // Expects the path and table to be passed as arguments on the stack.
211    pub fn luarequire_registermodule(L: *mut lua_State) -> c_int;
212
213    // Clears the entry associated with the given cache key from the require cache.
214    // Expects the cache key to be passed as an argument on the stack.
215    pub fn luarequire_clearcacheentry(L: *mut lua_State) -> c_int;
216
217    // Clears all entries from the require cache.
218    pub fn luarequire_clearcache(L: *mut lua_State) -> c_int;
219}