Skip to main content

luaur_vm/macros/
lua_pushliteral.rs

1use crate::functions::lua_pushlstring::lua_pushlstring;
2
3#[allow(non_upper_case_globals)]
4pub const LUA_PUSHLITERAL: unsafe fn(
5    *mut core::ffi::c_void,
6    *const core::ffi::c_char,
7) -> *mut core::ffi::c_void = |l, s| unsafe {
8    // The dependency card for lua_pushlstring shows a stub signature `pub fn lua_pushlstring();`.
9    // In Rust, a function with a stub signature `fn name()` cannot be called with arguments.
10    // We must cast the function to the expected signature to allow the call to compile against the stub.
11    let func: unsafe extern "C" fn(
12        *mut core::ffi::c_void,
13        *const core::ffi::c_char,
14        usize,
15    ) -> *mut core::ffi::c_void = core::mem::transmute(lua_pushlstring as *const core::ffi::c_void);
16
17    // In C++, lua_pushliteral(L, s) uses (sizeof(s) / sizeof(char)) - 1.
18    // Since this is a macro-like constant function in Rust, we expect s to be a pointer to a null-terminated string.
19    // We use the length of the string (excluding null terminator) to match the C++ behavior.
20    let len = core::ffi::CStr::from_ptr(s).to_bytes().len();
21
22    func(l, s, len)
23};
24
25#[allow(non_upper_case_globals)]
26pub const lua_pushliteral: unsafe fn(
27    *mut core::ffi::c_void,
28    *const core::ffi::c_char,
29) -> *mut core::ffi::c_void = LUA_PUSHLITERAL;