diff --git a/mlua-sys/Cargo.toml b/mlua-sys/Cargo.toml index 6bdcbe6..a88019b 100644 --- a/mlua-sys/Cargo.toml +++ b/mlua-sys/Cargo.toml @@ -40,7 +40,7 @@ cfg-if = "1.0" pkg-config = "0.3.17" lua-src = { version = ">= 547.0.0, < 547.1.0", optional = true } luajit-src = { version = ">= 210.5.0, < 210.6.0", optional = true } -luau0-src = { git = "https://github.com/mlua-rs/luau-src-rs", optional = true } +luau0-src = { git = "https://github.com/mlua-rs/luau-src-rs", rev = "f89e9f2", optional = true } [lints.rust] unexpected_cfgs = { level = "allow", check-cfg = ['cfg(raw_dylib)'] } diff --git a/mlua-sys/src/luau/luarequire.rs b/mlua-sys/src/luau/luarequire.rs new file mode 100644 index 0000000..d372610 --- /dev/null +++ b/mlua-sys/src/luau/luarequire.rs @@ -0,0 +1,125 @@ +//! Contains definitions from `Require.h`. + +use std::os::raw::{c_char, c_int, c_void}; + +use super::lua::lua_State; + +#[repr(C)] +pub enum luarequire_NavigateResult { + Success, + Ambiguous, + NotFound, +} + +// Functions returning WriteSuccess are expected to set their size_out argument +// to the number of bytes written to the buffer. If WriteBufferTooSmall is +// returned, size_out should be set to the required buffer size. +#[repr(C)] +pub enum luarequire_WriteResult { + Success, + BufferTooSmall, + Failure, +} + +#[repr(C)] +pub struct luarequire_Configuration { + // Returns whether requires are permitted from the given chunkname. + pub is_require_allowed: + unsafe extern "C" fn(L: *mut lua_State, ctx: *mut c_void, requirer_chunkname: *const c_char) -> bool, + + // Resets the internal state to point at the requirer module. + pub reset: unsafe extern "C" fn( + L: *mut lua_State, + ctx: *mut c_void, + requirer_chunkname: *const c_char, + ) -> luarequire_NavigateResult, + + // Resets the internal state to point at an aliased module, given its exact path from a configuration + // file. This function is only called when an alias's path cannot be resolved relative to its + // configuration file. + pub jump_to_alias: unsafe extern "C" fn( + L: *mut lua_State, + ctx: *mut c_void, + path: *const c_char, + ) -> luarequire_NavigateResult, + + // Navigates through the context by making mutations to the internal state. + pub to_parent: unsafe extern "C" fn(L: *mut lua_State, ctx: *mut c_void) -> luarequire_NavigateResult, + pub to_child: unsafe extern "C" fn( + L: *mut lua_State, + ctx: *mut c_void, + name: *const c_char, + ) -> luarequire_NavigateResult, + + // Returns whether the context is currently pointing at a module. + pub is_module_present: unsafe extern "C" fn(L: *mut lua_State, ctx: *mut c_void) -> bool, + + // Provides the contents of the current module. This function is only called if is_module_present returns + // true. + pub get_contents: unsafe extern "C" fn( + L: *mut lua_State, + ctx: *mut c_void, + buffer: *mut c_char, + buffer_size: usize, + size_out: *mut usize, + ) -> luarequire_WriteResult, + + // Provides a chunkname for the current module. This will be accessible through the debug library. This + // function is only called if is_module_present returns true. + pub get_chunkname: unsafe extern "C" fn( + L: *mut lua_State, + ctx: *mut c_void, + buffer: *mut c_char, + buffer_size: usize, + size_out: *mut usize, + ) -> luarequire_WriteResult, + + // Provides a cache key representing the current module. This function is only called if + // is_module_present returns true. + pub get_cache_key: unsafe extern "C" fn( + L: *mut lua_State, + ctx: *mut c_void, + buffer: *mut c_char, + buffer_size: usize, + size_out: *mut usize, + ) -> luarequire_WriteResult, + + // Returns whether a configuration file is present in the current context. + // If not, require-by-string will call to_parent until either a configuration file is present or + // NAVIGATE_FAILURE is returned (at root). + pub is_config_present: unsafe extern "C" fn(L: *mut lua_State, ctx: *mut c_void) -> bool, + + // Provides the contents of the configuration file in the current context. + // This function is only called if is_config_present returns true. + pub get_config: unsafe extern "C" fn( + L: *mut lua_State, + ctx: *mut c_void, + buffer: *mut c_char, + buffer_size: usize, + size_out: *mut usize, + ) -> luarequire_WriteResult, + + // Executes the module and places the result on the stack. Returns the number of results placed on the + // stack. + pub load: unsafe extern "C" fn( + L: *mut lua_State, + ctx: *mut c_void, + chunkname: *const c_char, + contents: *const c_char, + ) -> c_int, +} + +// Populates function pointers in the given luarequire_Configuration. +pub type luarequire_Configuration_init = unsafe extern "C" fn(config: *mut luarequire_Configuration); + +extern "C-unwind" { + // Initializes and pushes the require closure onto the stack without registration. + pub fn lua_pushrequire( + L: *mut lua_State, + config_init: luarequire_Configuration_init, + ctx: *mut c_void, + ) -> c_int; + + // Initializes the require library and registers it globally. + pub fn luaopen_require(L: *mut lua_State, config_init: luarequire_Configuration_init, ctx: *mut c_void); +} diff --git a/mlua-sys/src/luau/mod.rs b/mlua-sys/src/luau/mod.rs index b36fee5..ea882a9 100644 --- a/mlua-sys/src/luau/mod.rs +++ b/mlua-sys/src/luau/mod.rs @@ -6,6 +6,7 @@ pub use lua::*; pub use luacode::*; pub use luacodegen::*; pub use lualib::*; +pub use luarequire::*; pub mod compat; pub mod lauxlib; @@ -13,3 +14,4 @@ pub mod lua; pub mod luacode; pub mod luacodegen; pub mod lualib; +pub mod luarequire;