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#[repr(C)]
27pub struct luarequire_Configuration {
28 // Returns whether requires are permitted from the given chunkname.
29 pub is_require_allowed: unsafe extern "C-unwind" fn(
30 L: *mut lua_State,
31 ctx: *mut c_void,
32 requirer_chunkname: *const c_char,
33 ) -> bool,
34
35 // Resets the internal state to point at the requirer module.
36 pub reset: unsafe extern "C-unwind" fn(
37 L: *mut lua_State,
38 ctx: *mut c_void,
39 requirer_chunkname: *const c_char,
40 ) -> luarequire_NavigateResult,
41
42 // Resets the internal state to point at an aliased module, given its exact path from a configuration
43 // file. This function is only called when an alias's path cannot be resolved relative to its
44 // configuration file.
45 pub jump_to_alias: unsafe extern "C-unwind" fn(
46 L: *mut lua_State,
47 ctx: *mut c_void,
48 path: *const c_char,
49 ) -> luarequire_NavigateResult,
50
51 // Navigates through the context by making mutations to the internal state.
52 pub to_parent:
53 unsafe extern "C-unwind" fn(L: *mut lua_State, ctx: *mut c_void) -> luarequire_NavigateResult,
54 pub to_child: unsafe extern "C-unwind" fn(
55 L: *mut lua_State,
56 ctx: *mut c_void,
57 name: *const c_char,
58 ) -> luarequire_NavigateResult,
59
60 // Returns whether the context is currently pointing at a module.
61 pub is_module_present: unsafe extern "C-unwind" fn(L: *mut lua_State, ctx: *mut c_void) -> bool,
62
63 // Provides a chunkname for the current module. This will be accessible through the debug library. This
64 // function is only called if is_module_present returns true.
65 pub get_chunkname: unsafe extern "C-unwind" fn(
66 L: *mut lua_State,
67 ctx: *mut c_void,
68 buffer: *mut c_char,
69 buffer_size: usize,
70 size_out: *mut usize,
71 ) -> luarequire_WriteResult,
72
73 // Provides a loadname that identifies the current module and is passed to load. This function
74 // is only called if is_module_present returns true.
75 pub get_loadname: unsafe extern "C-unwind" fn(
76 L: *mut lua_State,
77 ctx: *mut c_void,
78 buffer: *mut c_char,
79 buffer_size: usize,
80 size_out: *mut usize,
81 ) -> luarequire_WriteResult,
82
83 // Provides a cache key representing the current module. This function is only called if
84 // is_module_present returns true.
85 pub get_cache_key: unsafe extern "C-unwind" fn(
86 L: *mut lua_State,
87 ctx: *mut c_void,
88 buffer: *mut c_char,
89 buffer_size: usize,
90 size_out: *mut usize,
91 ) -> luarequire_WriteResult,
92
93 // Returns whether a configuration file is present in the current context.
94 // If not, require-by-string will call to_parent until either a configuration file is present or
95 // NAVIGATE_FAILURE is returned (at root).
96 pub is_config_present: unsafe extern "C-unwind" fn(L: *mut lua_State, ctx: *mut c_void) -> bool,
97
98 // Parses the configuration file in the current context for the given alias and returns its
99 // value or WRITE_FAILURE if not found. This function is only called if is_config_present
100 // returns true. If this function pointer is set, get_config must not be set. Opting in to this
101 // function pointer disables parsing configuration files internally and can be used for finer
102 // control over the configuration file parsing process.
103 pub get_alias: Option<
104 unsafe extern "C-unwind" fn(
105 L: *mut lua_State,
106 ctx: *mut c_void,
107 alias: *const c_char,
108 buffer: *mut c_char,
109 buffer_size: usize,
110 size_out: *mut usize,
111 ) -> luarequire_WriteResult,
112 >,
113
114 // Provides the contents of the configuration file in the current context. This function is only called
115 // if is_config_present returns true. If this function pointer is set, get_alias must not be set. Opting
116 // in to this function pointer enables parsing configuration files internally.
117 pub get_config: Option<
118 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
127 // Executes the module and places the result on the stack. Returns the number of results placed on the
128 // stack.
129 // Returning -1 directs the requiring thread to yield. In this case, this thread should be resumed with
130 // the module result pushed onto its stack.
131 pub load: unsafe extern "C-unwind" fn(
132 L: *mut lua_State,
133 ctx: *mut c_void,
134 path: *const c_char,
135 chunkname: *const c_char,
136 loadname: *const c_char,
137 ) -> c_int,
138}
139
140// Populates function pointers in the given luarequire_Configuration.
141pub type luarequire_Configuration_init = unsafe extern "C-unwind" fn(config: *mut luarequire_Configuration);
142
143unsafe extern "C-unwind" {
144 // Initializes and pushes the require closure onto the stack without registration.
145 pub fn luarequire_pushrequire(
146 L: *mut lua_State,
147 config_init: luarequire_Configuration_init,
148 ctx: *mut c_void,
149 ) -> c_int;
150
151 // Initializes the require library and registers it globally.
152 pub fn luaopen_require(L: *mut lua_State, config_init: luarequire_Configuration_init, ctx: *mut c_void);
153
154 // Initializes and pushes a "proxyrequire" closure onto the stack.
155 //
156 // The closure takes two parameters: the string path to resolve and the chunkname of an existing
157 // module.
158 pub fn luarequire_pushproxyrequire(
159 L: *mut lua_State,
160 config_init: luarequire_Configuration_init,
161 ctx: *mut c_void,
162 ) -> c_int;
163
164 // Registers an aliased require path to a result.
165 //
166 // After registration, the given result will always be immediately returned when the given path is
167 // required.
168 // Expects the path and table to be passed as arguments on the stack.
169 pub fn luarequire_registermodule(L: *mut lua_State) -> c_int;
170
171 // Clears the entry associated with the given cache key from the require cache.
172 // Expects the cache key to be passed as an argument on the stack.
173 pub fn luarequire_clearcacheentry(L: *mut lua_State) -> c_int;
174
175 // Clears all entries from the require cache.
176 pub fn luarequire_clearcache(L: *mut lua_State) -> c_int;
177}