|
| 1 | +//! Contains definitions from `Require.h`. |
| 2 | +
|
| 3 | +use std::os::raw::{c_char, c_int, c_void}; |
| 4 | + |
| 5 | +use super::lua::lua_State; |
| 6 | + |
| 7 | +#[repr(C)] |
| 8 | +pub enum luarequire_NavigateResult { |
| 9 | + Success, |
| 10 | + Ambiguous, |
| 11 | + NotFound, |
| 12 | +} |
| 13 | + |
| 14 | +// Functions returning WriteSuccess are expected to set their size_out argument |
| 15 | +// to the number of bytes written to the buffer. If WriteBufferTooSmall is |
| 16 | +// returned, size_out should be set to the required buffer size. |
| 17 | +#[repr(C)] |
| 18 | +pub enum luarequire_WriteResult { |
| 19 | + Success, |
| 20 | + BufferTooSmall, |
| 21 | + Failure, |
| 22 | +} |
| 23 | + |
| 24 | +#[repr(C)] |
| 25 | +pub struct luarequire_Configuration { |
| 26 | + // Returns whether requires are permitted from the given chunkname. |
| 27 | + pub is_require_allowed: |
| 28 | + unsafe extern "C" fn(L: *mut lua_State, ctx: *mut c_void, requirer_chunkname: *const c_char) -> bool, |
| 29 | + |
| 30 | + // Resets the internal state to point at the requirer module. |
| 31 | + pub reset: unsafe extern "C" fn( |
| 32 | + L: *mut lua_State, |
| 33 | + ctx: *mut c_void, |
| 34 | + requirer_chunkname: *const c_char, |
| 35 | + ) -> luarequire_NavigateResult, |
| 36 | + |
| 37 | + // Resets the internal state to point at an aliased module, given its exact path from a configuration |
| 38 | + // file. This function is only called when an alias's path cannot be resolved relative to its |
| 39 | + // configuration file. |
| 40 | + pub jump_to_alias: unsafe extern "C" fn( |
| 41 | + L: *mut lua_State, |
| 42 | + ctx: *mut c_void, |
| 43 | + path: *const c_char, |
| 44 | + ) -> luarequire_NavigateResult, |
| 45 | + |
| 46 | + // Navigates through the context by making mutations to the internal state. |
| 47 | + pub to_parent: unsafe extern "C" fn(L: *mut lua_State, ctx: *mut c_void) -> luarequire_NavigateResult, |
| 48 | + pub to_child: unsafe extern "C" fn( |
| 49 | + L: *mut lua_State, |
| 50 | + ctx: *mut c_void, |
| 51 | + name: *const c_char, |
| 52 | + ) -> luarequire_NavigateResult, |
| 53 | + |
| 54 | + // Returns whether the context is currently pointing at a module. |
| 55 | + pub is_module_present: unsafe extern "C" fn(L: *mut lua_State, ctx: *mut c_void) -> bool, |
| 56 | + |
| 57 | + // Provides the contents of the current module. This function is only called if is_module_present returns |
| 58 | + // true. |
| 59 | + pub get_contents: unsafe extern "C" fn( |
| 60 | + L: *mut lua_State, |
| 61 | + ctx: *mut c_void, |
| 62 | + buffer: *mut c_char, |
| 63 | + buffer_size: usize, |
| 64 | + size_out: *mut usize, |
| 65 | + ) -> luarequire_WriteResult, |
| 66 | + |
| 67 | + // Provides a chunkname for the current module. This will be accessible through the debug library. This |
| 68 | + // function is only called if is_module_present returns true. |
| 69 | + pub get_chunkname: unsafe extern "C" fn( |
| 70 | + L: *mut lua_State, |
| 71 | + ctx: *mut c_void, |
| 72 | + buffer: *mut c_char, |
| 73 | + buffer_size: usize, |
| 74 | + size_out: *mut usize, |
| 75 | + ) -> luarequire_WriteResult, |
| 76 | + |
| 77 | + // Provides a cache key representing the current module. This function is only called if |
| 78 | + // is_module_present returns true. |
| 79 | + pub get_cache_key: unsafe extern "C" fn( |
| 80 | + L: *mut lua_State, |
| 81 | + ctx: *mut c_void, |
| 82 | + buffer: *mut c_char, |
| 83 | + buffer_size: usize, |
| 84 | + size_out: *mut usize, |
| 85 | + ) -> luarequire_WriteResult, |
| 86 | + |
| 87 | + // Returns whether a configuration file is present in the current context. |
| 88 | + // If not, require-by-string will call to_parent until either a configuration file is present or |
| 89 | + // NAVIGATE_FAILURE is returned (at root). |
| 90 | + pub is_config_present: unsafe extern "C" fn(L: *mut lua_State, ctx: *mut c_void) -> bool, |
| 91 | + |
| 92 | + // Provides the contents of the configuration file in the current context. |
| 93 | + // This function is only called if is_config_present returns true. |
| 94 | + pub get_config: unsafe extern "C" fn( |
| 95 | + L: *mut lua_State, |
| 96 | + ctx: *mut c_void, |
| 97 | + buffer: *mut c_char, |
| 98 | + buffer_size: usize, |
| 99 | + size_out: *mut usize, |
| 100 | + ) -> luarequire_WriteResult, |
| 101 | + |
| 102 | + // Executes the module and places the result on the stack. Returns the number of results placed on the |
| 103 | + // stack. |
| 104 | + pub load: unsafe extern "C" fn( |
| 105 | + L: *mut lua_State, |
| 106 | + ctx: *mut c_void, |
| 107 | + chunkname: *const c_char, |
| 108 | + contents: *const c_char, |
| 109 | + ) -> c_int, |
| 110 | +} |
| 111 | + |
| 112 | +// Populates function pointers in the given luarequire_Configuration. |
| 113 | +pub type luarequire_Configuration_init = unsafe extern "C" fn(config: *mut luarequire_Configuration); |
| 114 | + |
| 115 | +extern "C-unwind" { |
| 116 | + // Initializes and pushes the require closure onto the stack without registration. |
| 117 | + pub fn lua_pushrequire( |
| 118 | + L: *mut lua_State, |
| 119 | + config_init: luarequire_Configuration_init, |
| 120 | + ctx: *mut c_void, |
| 121 | + ) -> c_int; |
| 122 | + |
| 123 | + // Initializes the require library and registers it globally. |
| 124 | + pub fn luaopen_require(L: *mut lua_State, config_init: luarequire_Configuration_init, ctx: *mut c_void); |
| 125 | +} |
0 commit comments