From 272cfdb89bd88d3276d43c26c040e923a6a5dfa0 Mon Sep 17 00:00:00 2001 From: Alex Orlenko Date: Fri, 14 Mar 2025 23:10:14 +0000 Subject: [PATCH] Change Lua library name constants from `&str` to `*const c_char`. It makes easier to pass them to Lua API functions. --- mlua-sys/src/lua51/lualib.rs | 24 ++++++++++++------------ mlua-sys/src/lua52/lualib.rs | 20 ++++++++++---------- mlua-sys/src/lua53/lauxlib.rs | 4 ++-- mlua-sys/src/lua53/lualib.rs | 22 +++++++++++----------- mlua-sys/src/lua54/lauxlib.rs | 4 ++-- mlua-sys/src/lua54/lualib.rs | 20 ++++++++++---------- mlua-sys/src/luau/lualib.rs | 22 +++++++++++----------- src/state/raw.rs | 27 +++++---------------------- 8 files changed, 63 insertions(+), 80 deletions(-) diff --git a/mlua-sys/src/lua51/lualib.rs b/mlua-sys/src/lua51/lualib.rs index 9ef0b21..de994ce 100644 --- a/mlua-sys/src/lua51/lualib.rs +++ b/mlua-sys/src/lua51/lualib.rs @@ -1,24 +1,24 @@ //! Contains definitions from `lualib.h`. -use std::os::raw::c_int; +use std::os::raw::{c_char, c_int}; use super::lua::lua_State; -pub const LUA_COLIBNAME: &str = "coroutine"; -pub const LUA_TABLIBNAME: &str = "table"; -pub const LUA_IOLIBNAME: &str = "io"; -pub const LUA_OSLIBNAME: &str = "os"; -pub const LUA_STRLIBNAME: &str = "string"; -pub const LUA_MATHLIBNAME: &str = "math"; -pub const LUA_DBLIBNAME: &str = "debug"; -pub const LUA_LOADLIBNAME: &str = "package"; +pub const LUA_COLIBNAME: *const c_char = cstr!("coroutine"); +pub const LUA_TABLIBNAME: *const c_char = cstr!("table"); +pub const LUA_IOLIBNAME: *const c_char = cstr!("io"); +pub const LUA_OSLIBNAME: *const c_char = cstr!("os"); +pub const LUA_STRLIBNAME: *const c_char = cstr!("string"); +pub const LUA_MATHLIBNAME: *const c_char = cstr!("math"); +pub const LUA_DBLIBNAME: *const c_char = cstr!("debug"); +pub const LUA_LOADLIBNAME: *const c_char = cstr!("package"); #[cfg(feature = "luajit")] -pub const LUA_BITLIBNAME: &str = "bit"; +pub const LUA_BITLIBNAME: *const c_char = cstr!("bit"); #[cfg(feature = "luajit")] -pub const LUA_JITLIBNAME: &str = "jit"; +pub const LUA_JITLIBNAME: *const c_char = cstr!("jit"); #[cfg(feature = "luajit")] -pub const LUA_FFILIBNAME: &str = "ffi"; +pub const LUA_FFILIBNAME: *const c_char = cstr!("ffi"); #[cfg_attr(all(windows, raw_dylib), link(name = "lua51", kind = "raw-dylib"))] extern "C-unwind" { diff --git a/mlua-sys/src/lua52/lualib.rs b/mlua-sys/src/lua52/lualib.rs index daf1e2a..a9ed21f 100644 --- a/mlua-sys/src/lua52/lualib.rs +++ b/mlua-sys/src/lua52/lualib.rs @@ -1,18 +1,18 @@ //! Contains definitions from `lualib.h`. -use std::os::raw::c_int; +use std::os::raw::{c_char, c_int}; use super::lua::lua_State; -pub const LUA_COLIBNAME: &str = "coroutine"; -pub const LUA_TABLIBNAME: &str = "table"; -pub const LUA_IOLIBNAME: &str = "io"; -pub const LUA_OSLIBNAME: &str = "os"; -pub const LUA_STRLIBNAME: &str = "string"; -pub const LUA_BITLIBNAME: &str = "bit32"; -pub const LUA_MATHLIBNAME: &str = "math"; -pub const LUA_DBLIBNAME: &str = "debug"; -pub const LUA_LOADLIBNAME: &str = "package"; +pub const LUA_COLIBNAME: *const c_char = cstr!("coroutine"); +pub const LUA_TABLIBNAME: *const c_char = cstr!("table"); +pub const LUA_IOLIBNAME: *const c_char = cstr!("io"); +pub const LUA_OSLIBNAME: *const c_char = cstr!("os"); +pub const LUA_STRLIBNAME: *const c_char = cstr!("string"); +pub const LUA_BITLIBNAME: *const c_char = cstr!("bit32"); +pub const LUA_MATHLIBNAME: *const c_char = cstr!("math"); +pub const LUA_DBLIBNAME: *const c_char = cstr!("debug"); +pub const LUA_LOADLIBNAME: *const c_char = cstr!("package"); #[cfg_attr(all(windows, raw_dylib), link(name = "lua52", kind = "raw-dylib"))] extern "C-unwind" { diff --git a/mlua-sys/src/lua53/lauxlib.rs b/mlua-sys/src/lua53/lauxlib.rs index 7c851ac..53c45bd 100644 --- a/mlua-sys/src/lua53/lauxlib.rs +++ b/mlua-sys/src/lua53/lauxlib.rs @@ -9,10 +9,10 @@ use super::lua::{self, lua_CFunction, lua_Integer, lua_Number, lua_State}; pub const LUA_ERRFILE: c_int = lua::LUA_ERRERR + 1; // Key, in the registry, for table of loaded modules -pub const LUA_LOADED_TABLE: &str = "_LOADED"; +pub const LUA_LOADED_TABLE: *const c_char = cstr!("_LOADED"); // Key, in the registry, for table of preloaded loaders -pub const LUA_PRELOAD_TABLE: &str = "_PRELOAD"; +pub const LUA_PRELOAD_TABLE: *const c_char = cstr!("_PRELOAD"); #[repr(C)] pub struct luaL_Reg { diff --git a/mlua-sys/src/lua53/lualib.rs b/mlua-sys/src/lua53/lualib.rs index 5d7509e..0b556d4 100644 --- a/mlua-sys/src/lua53/lualib.rs +++ b/mlua-sys/src/lua53/lualib.rs @@ -1,19 +1,19 @@ //! Contains definitions from `lualib.h`. -use std::os::raw::c_int; +use std::os::raw::{c_char, c_int}; use super::lua::lua_State; -pub const LUA_COLIBNAME: &str = "coroutine"; -pub const LUA_TABLIBNAME: &str = "table"; -pub const LUA_IOLIBNAME: &str = "io"; -pub const LUA_OSLIBNAME: &str = "os"; -pub const LUA_STRLIBNAME: &str = "string"; -pub const LUA_UTF8LIBNAME: &str = "utf8"; -pub const LUA_BITLIBNAME: &str = "bit32"; -pub const LUA_MATHLIBNAME: &str = "math"; -pub const LUA_DBLIBNAME: &str = "debug"; -pub const LUA_LOADLIBNAME: &str = "package"; +pub const LUA_COLIBNAME: *const c_char = cstr!("coroutine"); +pub const LUA_TABLIBNAME: *const c_char = cstr!("table"); +pub const LUA_IOLIBNAME: *const c_char = cstr!("io"); +pub const LUA_OSLIBNAME: *const c_char = cstr!("os"); +pub const LUA_STRLIBNAME: *const c_char = cstr!("string"); +pub const LUA_UTF8LIBNAME: *const c_char = cstr!("utf8"); +pub const LUA_BITLIBNAME: *const c_char = cstr!("bit32"); +pub const LUA_MATHLIBNAME: *const c_char = cstr!("math"); +pub const LUA_DBLIBNAME: *const c_char = cstr!("debug"); +pub const LUA_LOADLIBNAME: *const c_char = cstr!("package"); #[cfg_attr(all(windows, raw_dylib), link(name = "lua53", kind = "raw-dylib"))] extern "C-unwind" { diff --git a/mlua-sys/src/lua54/lauxlib.rs b/mlua-sys/src/lua54/lauxlib.rs index 78b0881..8a1fd12 100644 --- a/mlua-sys/src/lua54/lauxlib.rs +++ b/mlua-sys/src/lua54/lauxlib.rs @@ -9,10 +9,10 @@ use super::lua::{self, lua_CFunction, lua_Integer, lua_Number, lua_State}; pub const LUA_ERRFILE: c_int = lua::LUA_ERRERR + 1; // Key, in the registry, for table of loaded modules -pub const LUA_LOADED_TABLE: &str = "_LOADED"; +pub const LUA_LOADED_TABLE: *const c_char = cstr!("_LOADED"); // Key, in the registry, for table of preloaded loaders -pub const LUA_PRELOAD_TABLE: &str = "_PRELOAD"; +pub const LUA_PRELOAD_TABLE: *const c_char = cstr!("_PRELOAD"); #[repr(C)] pub struct luaL_Reg { diff --git a/mlua-sys/src/lua54/lualib.rs b/mlua-sys/src/lua54/lualib.rs index c104375..dec577b 100644 --- a/mlua-sys/src/lua54/lualib.rs +++ b/mlua-sys/src/lua54/lualib.rs @@ -1,18 +1,18 @@ //! Contains definitions from `lualib.h`. -use std::os::raw::c_int; +use std::os::raw::{c_char, c_int}; use super::lua::lua_State; -pub const LUA_COLIBNAME: &str = "coroutine"; -pub const LUA_TABLIBNAME: &str = "table"; -pub const LUA_IOLIBNAME: &str = "io"; -pub const LUA_OSLIBNAME: &str = "os"; -pub const LUA_STRLIBNAME: &str = "string"; -pub const LUA_UTF8LIBNAME: &str = "utf8"; -pub const LUA_MATHLIBNAME: &str = "math"; -pub const LUA_DBLIBNAME: &str = "debug"; -pub const LUA_LOADLIBNAME: &str = "package"; +pub const LUA_COLIBNAME: *const c_char = cstr!("coroutine"); +pub const LUA_TABLIBNAME: *const c_char = cstr!("table"); +pub const LUA_IOLIBNAME: *const c_char = cstr!("io"); +pub const LUA_OSLIBNAME: *const c_char = cstr!("os"); +pub const LUA_STRLIBNAME: *const c_char = cstr!("string"); +pub const LUA_UTF8LIBNAME: *const c_char = cstr!("utf8"); +pub const LUA_MATHLIBNAME: *const c_char = cstr!("math"); +pub const LUA_DBLIBNAME: *const c_char = cstr!("debug"); +pub const LUA_LOADLIBNAME: *const c_char = cstr!("package"); #[cfg_attr(all(windows, raw_dylib), link(name = "lua54", kind = "raw-dylib"))] extern "C-unwind" { diff --git a/mlua-sys/src/luau/lualib.rs b/mlua-sys/src/luau/lualib.rs index 0469bfd..834f09e 100644 --- a/mlua-sys/src/luau/lualib.rs +++ b/mlua-sys/src/luau/lualib.rs @@ -1,19 +1,19 @@ //! Contains definitions from `lualib.h`. -use std::os::raw::c_int; +use std::os::raw::{c_char, c_int}; use super::lua::lua_State; -pub const LUA_COLIBNAME: &str = "coroutine"; -pub const LUA_TABLIBNAME: &str = "table"; -pub const LUA_OSLIBNAME: &str = "os"; -pub const LUA_STRLIBNAME: &str = "string"; -pub const LUA_BITLIBNAME: &str = "bit32"; -pub const LUA_BUFFERLIBNAME: &str = "buffer"; -pub const LUA_UTF8LIBNAME: &str = "utf8"; -pub const LUA_MATHLIBNAME: &str = "math"; -pub const LUA_DBLIBNAME: &str = "debug"; -pub const LUA_VECLIBNAME: &str = "vector"; +pub const LUA_COLIBNAME: *const c_char = cstr!("coroutine"); +pub const LUA_TABLIBNAME: *const c_char = cstr!("table"); +pub const LUA_OSLIBNAME: *const c_char = cstr!("os"); +pub const LUA_STRLIBNAME: *const c_char = cstr!("string"); +pub const LUA_BITLIBNAME: *const c_char = cstr!("bit32"); +pub const LUA_BUFFERLIBNAME: *const c_char = cstr!("buffer"); +pub const LUA_UTF8LIBNAME: *const c_char = cstr!("utf8"); +pub const LUA_MATHLIBNAME: *const c_char = cstr!("math"); +pub const LUA_DBLIBNAME: *const c_char = cstr!("debug"); +pub const LUA_VECLIBNAME: *const c_char = cstr!("vector"); extern "C-unwind" { pub fn luaopen_base(L: *mut lua_State) -> c_int; diff --git a/src/state/raw.rs b/src/state/raw.rs index 3f4fa00..5b162e5 100644 --- a/src/state/raw.rs +++ b/src/state/raw.rs @@ -1,6 +1,6 @@ use std::any::TypeId; use std::cell::{Cell, UnsafeCell}; -use std::ffi::{CStr, CString}; +use std::ffi::CStr; use std::mem; use std::os::raw::{c_char, c_int, c_void}; use std::panic::resume_unwind; @@ -1285,16 +1285,14 @@ impl RawLua { // Uses 3 stack spaces unsafe fn load_std_libs(state: *mut ffi::lua_State, libs: StdLib) -> Result<()> { - #[inline(always)] - pub unsafe fn requiref( + unsafe fn requiref( state: *mut ffi::lua_State, - modname: &str, + modname: *const c_char, openf: ffi::lua_CFunction, glb: c_int, ) -> Result<()> { - let modname = mlua_expect!(CString::new(modname), "modname contains nil byte"); - protect_lua!(state, 0, 1, |state| { - ffi::luaL_requiref(state, modname.as_ptr() as *const c_char, openf, glb) + protect_lua!(state, 0, 0, |state| { + ffi::luaL_requiref(state, modname, openf, glb) }) } @@ -1325,36 +1323,30 @@ unsafe fn load_std_libs(state: *mut ffi::lua_State, libs: StdLib) -> Result<()> { if libs.contains(StdLib::COROUTINE) { requiref(state, ffi::LUA_COLIBNAME, ffi::luaopen_coroutine, 1)?; - ffi::lua_pop(state, 1); } } if libs.contains(StdLib::TABLE) { requiref(state, ffi::LUA_TABLIBNAME, ffi::luaopen_table, 1)?; - ffi::lua_pop(state, 1); } #[cfg(not(feature = "luau"))] if libs.contains(StdLib::IO) { requiref(state, ffi::LUA_IOLIBNAME, ffi::luaopen_io, 1)?; - ffi::lua_pop(state, 1); } if libs.contains(StdLib::OS) { requiref(state, ffi::LUA_OSLIBNAME, ffi::luaopen_os, 1)?; - ffi::lua_pop(state, 1); } if libs.contains(StdLib::STRING) { requiref(state, ffi::LUA_STRLIBNAME, ffi::luaopen_string, 1)?; - ffi::lua_pop(state, 1); } #[cfg(any(feature = "lua54", feature = "lua53", feature = "luau"))] { if libs.contains(StdLib::UTF8) { requiref(state, ffi::LUA_UTF8LIBNAME, ffi::luaopen_utf8, 1)?; - ffi::lua_pop(state, 1); } } @@ -1362,7 +1354,6 @@ unsafe fn load_std_libs(state: *mut ffi::lua_State, libs: StdLib) -> Result<()> { if libs.contains(StdLib::BIT) { requiref(state, ffi::LUA_BITLIBNAME, ffi::luaopen_bit32, 1)?; - ffi::lua_pop(state, 1); } } @@ -1370,36 +1361,30 @@ unsafe fn load_std_libs(state: *mut ffi::lua_State, libs: StdLib) -> Result<()> { if libs.contains(StdLib::BIT) { requiref(state, ffi::LUA_BITLIBNAME, ffi::luaopen_bit, 1)?; - ffi::lua_pop(state, 1); } } #[cfg(feature = "luau")] if libs.contains(StdLib::BUFFER) { requiref(state, ffi::LUA_BUFFERLIBNAME, ffi::luaopen_buffer, 1)?; - ffi::lua_pop(state, 1); } #[cfg(feature = "luau")] if libs.contains(StdLib::VECTOR) { requiref(state, ffi::LUA_VECLIBNAME, ffi::luaopen_vector, 1)?; - ffi::lua_pop(state, 1); } if libs.contains(StdLib::MATH) { requiref(state, ffi::LUA_MATHLIBNAME, ffi::luaopen_math, 1)?; - ffi::lua_pop(state, 1); } if libs.contains(StdLib::DEBUG) { requiref(state, ffi::LUA_DBLIBNAME, ffi::luaopen_debug, 1)?; - ffi::lua_pop(state, 1); } #[cfg(not(feature = "luau"))] if libs.contains(StdLib::PACKAGE) { requiref(state, ffi::LUA_LOADLIBNAME, ffi::luaopen_package, 1)?; - ffi::lua_pop(state, 1); } #[cfg(feature = "luau")] if libs.contains(StdLib::PACKAGE) { @@ -1410,13 +1395,11 @@ unsafe fn load_std_libs(state: *mut ffi::lua_State, libs: StdLib) -> Result<()> #[cfg(feature = "luajit")] if libs.contains(StdLib::JIT) { requiref(state, ffi::LUA_JITLIBNAME, ffi::luaopen_jit, 1)?; - ffi::lua_pop(state, 1); } #[cfg(feature = "luajit")] if libs.contains(StdLib::FFI) { requiref(state, ffi::LUA_FFILIBNAME, ffi::luaopen_ffi, 1)?; - ffi::lua_pop(state, 1); } Ok(())