diff --git a/mlua-sys/src/lua51/lualib.rs b/mlua-sys/src/lua51/lualib.rs index de994ce..9ef0b21 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_char, c_int}; +use std::os::raw::c_int; use super::lua::lua_State; -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"); +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"; #[cfg(feature = "luajit")] -pub const LUA_BITLIBNAME: *const c_char = cstr!("bit"); +pub const LUA_BITLIBNAME: &str = "bit"; #[cfg(feature = "luajit")] -pub const LUA_JITLIBNAME: *const c_char = cstr!("jit"); +pub const LUA_JITLIBNAME: &str = "jit"; #[cfg(feature = "luajit")] -pub const LUA_FFILIBNAME: *const c_char = cstr!("ffi"); +pub const LUA_FFILIBNAME: &str = "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 a9ed21f..daf1e2a 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_char, c_int}; +use std::os::raw::c_int; use super::lua::lua_State; -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"); +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"; #[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 53c45bd..7c851ac 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: *const c_char = cstr!("_LOADED"); +pub const LUA_LOADED_TABLE: &str = "_LOADED"; // Key, in the registry, for table of preloaded loaders -pub const LUA_PRELOAD_TABLE: *const c_char = cstr!("_PRELOAD"); +pub const LUA_PRELOAD_TABLE: &str = "_PRELOAD"; #[repr(C)] pub struct luaL_Reg { diff --git a/mlua-sys/src/lua53/lualib.rs b/mlua-sys/src/lua53/lualib.rs index 0b556d4..5d7509e 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_char, c_int}; +use std::os::raw::c_int; use super::lua::lua_State; -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"); +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"; #[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 8a1fd12..78b0881 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: *const c_char = cstr!("_LOADED"); +pub const LUA_LOADED_TABLE: &str = "_LOADED"; // Key, in the registry, for table of preloaded loaders -pub const LUA_PRELOAD_TABLE: *const c_char = cstr!("_PRELOAD"); +pub const LUA_PRELOAD_TABLE: &str = "_PRELOAD"; #[repr(C)] pub struct luaL_Reg { diff --git a/mlua-sys/src/lua54/lualib.rs b/mlua-sys/src/lua54/lualib.rs index dec577b..c104375 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_char, c_int}; +use std::os::raw::c_int; use super::lua::lua_State; -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"); +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"; #[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 834f09e..0469bfd 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_char, c_int}; +use std::os::raw::c_int; use super::lua::lua_State; -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"); +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"; 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 e49156b..c40cd1a 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; +use std::ffi::{CStr, CString}; use std::mem; use std::os::raw::{c_char, c_int, c_void}; use std::panic::resume_unwind; @@ -1285,14 +1285,16 @@ impl RawLua { // Uses 3 stack spaces unsafe fn load_std_libs(state: *mut ffi::lua_State, libs: StdLib) -> Result<()> { - unsafe fn requiref( + #[inline(always)] + pub unsafe fn requiref( state: *mut ffi::lua_State, - modname: *const c_char, + modname: &str, openf: ffi::lua_CFunction, glb: c_int, ) -> Result<()> { - protect_lua!(state, 0, 0, |state| { - ffi::luaL_requiref(state, modname, openf, glb) + 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) }) } @@ -1323,30 +1325,36 @@ 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); } } @@ -1354,6 +1362,7 @@ 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); } } @@ -1361,30 +1370,36 @@ 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) { @@ -1395,11 +1410,13 @@ 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(())