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:
30 unsafe extern "C" fn(L: *mut lua_State, ctx: *mut c_void, requirer_chunkname: *const c_char) -> bool,
31
32 // Resets the internal state to point at the requirer module.
33 pub reset: unsafe extern "C" fn(
34 L: *mut lua_State,
35 ctx: *mut c_void,
36 requirer_chunkname: *const c_char,
37 ) -> luarequire_NavigateResult,
38
39 // Resets the internal state to point at an aliased module, given its exact path from a configuration
40 // file. This function is only called when an alias's path cannot be resolved relative to its
41 // configuration file.
42 pub jump_to_alias: unsafe extern "C" fn(
43 L: *mut lua_State,
44 ctx: *mut c_void,
45 path: *const c_char,
46 ) -> luarequire_NavigateResult,
47
48 // Navigates through the context by making mutations to the internal state.
49 pub to_parent: unsafe extern "C" fn(L: *mut lua_State, ctx: *mut c_void) -> luarequire_NavigateResult,
50 pub to_child: unsafe extern "C" fn(
51 L: *mut lua_State,
52 ctx: *mut c_void,
53 name: *const c_char,
54 ) -> luarequire_NavigateResult,
55
56 // Returns whether the context is currently pointing at a module.
57 pub is_module_present: unsafe extern "C" fn(L: *mut lua_State, ctx: *mut c_void) -> bool,
58
59 // Provides the contents of the current module. This function is only called if is_module_present returns
60 // true.
61 pub get_contents: unsafe extern "C" fn(
62 L: *mut lua_State,
63 ctx: *mut c_void,
64 buffer: *mut c_char,
65 buffer_size: usize,
66 size_out: *mut usize,
67 ) -> luarequire_WriteResult,
68
69 // Provides a chunkname for the current module. This will be accessible through the debug library. This
70 // function is only called if is_module_present returns true.
71 pub get_chunkname: unsafe extern "C" fn(
72 L: *mut lua_State,
73 ctx: *mut c_void,
74 buffer: *mut c_char,
75 buffer_size: usize,
76 size_out: *mut usize,
77 ) -> luarequire_WriteResult,
78
79 // Provides a cache key representing the current module. This function is only called if
80 // is_module_present returns true.
81 pub get_cache_key: unsafe extern "C" fn(
82 L: *mut lua_State,
83 ctx: *mut c_void,
84 buffer: *mut c_char,
85 buffer_size: usize,
86 size_out: *mut usize,
87 ) -> luarequire_WriteResult,
88
89 // Returns whether a configuration file is present in the current context.
90 // If not, require-by-string will call to_parent until either a configuration file is present or
91 // NAVIGATE_FAILURE is returned (at root).
92 pub is_config_present: unsafe extern "C" fn(L: *mut lua_State, ctx: *mut c_void) -> bool,
93
94 // Provides the contents of the configuration file in the current context.
95 // This function is only called if is_config_present returns true.
96 pub get_config: unsafe extern "C" 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 // Executes the module and places the result on the stack. Returns the number of results placed on the
105 // stack.
106 // Returning -1 directs the requiring thread to yield. In this case, this thread should be resumed with
107 // the module result pushed onto its stack.
108 pub load: unsafe extern "C-unwind" fn(
109 L: *mut lua_State,
110 ctx: *mut c_void,
111 path: *const c_char,
112 chunkname: *const c_char,
113 contents: *const c_char,
114 ) -> c_int,
115}
116
117// Populates function pointers in the given luarequire_Configuration.
118pub type luarequire_Configuration_init = unsafe extern "C" fn(config: *mut luarequire_Configuration);
119
120extern "C-unwind" {
121 // Initializes and pushes the require closure onto the stack without registration.
122 pub fn luarequire_pushrequire(
123 L: *mut lua_State,
124 config_init: luarequire_Configuration_init,
125 ctx: *mut c_void,
126 ) -> c_int;
127
128 // Initializes the require library and registers it globally.
129 pub fn luaopen_require(L: *mut lua_State, config_init: luarequire_Configuration_init, ctx: *mut c_void);
130
131 // Initializes and pushes a "proxyrequire" closure onto the stack.
132 //
133 // The closure takes two parameters: the string path to resolve and the chunkname of an existing
134 // module.
135 pub fn luarequire_pushproxyrequire(
136 L: *mut lua_State,
137 config_init: luarequire_Configuration_init,
138 ctx: *mut c_void,
139 ) -> c_int;
140
141 // Registers an aliased require path to a result.
142 //
143 // After registration, the given result will always be immediately returned when the given path is
144 // required.
145 // Expects the path and table to be passed as arguments on the stack.
146 pub fn luarequire_registermodule(L: *mut lua_State) -> c_int;
147
148 // Clears the entry associated with the given cache key from the require cache.
149 // Expects the cache key to be passed as an argument on the stack.
150 pub fn luarequire_clearcacheentry(L: *mut lua_State) -> c_int;
151
152 // Clears all entries from the require cache.
153 pub fn luarequire_clearcache(L: *mut lua_State) -> c_int;
154}