Skip to content

Commit edbf1e9

Browse files
committed
Change Lua library name constants from &str to *const c_char.
It makes easier to pass them to Lua API functions.
1 parent 69011a8 commit edbf1e9

File tree

8 files changed

+63
-80
lines changed

8 files changed

+63
-80
lines changed

mlua-sys/src/lua51/lualib.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
//! Contains definitions from `lualib.h`.
22
3-
use std::os::raw::c_int;
3+
use std::os::raw::{c_char, c_int};
44

55
use super::lua::lua_State;
66

7-
pub const LUA_COLIBNAME: &str = "coroutine";
8-
pub const LUA_TABLIBNAME: &str = "table";
9-
pub const LUA_IOLIBNAME: &str = "io";
10-
pub const LUA_OSLIBNAME: &str = "os";
11-
pub const LUA_STRLIBNAME: &str = "string";
12-
pub const LUA_MATHLIBNAME: &str = "math";
13-
pub const LUA_DBLIBNAME: &str = "debug";
14-
pub const LUA_LOADLIBNAME: &str = "package";
7+
pub const LUA_COLIBNAME: *const c_char = cstr!("coroutine");
8+
pub const LUA_TABLIBNAME: *const c_char = cstr!("table");
9+
pub const LUA_IOLIBNAME: *const c_char = cstr!("io");
10+
pub const LUA_OSLIBNAME: *const c_char = cstr!("os");
11+
pub const LUA_STRLIBNAME: *const c_char = cstr!("string");
12+
pub const LUA_MATHLIBNAME: *const c_char = cstr!("math");
13+
pub const LUA_DBLIBNAME: *const c_char = cstr!("debug");
14+
pub const LUA_LOADLIBNAME: *const c_char = cstr!("package");
1515

1616
#[cfg(feature = "luajit")]
17-
pub const LUA_BITLIBNAME: &str = "bit";
17+
pub const LUA_BITLIBNAME: *const c_char = cstr!("bit");
1818
#[cfg(feature = "luajit")]
19-
pub const LUA_JITLIBNAME: &str = "jit";
19+
pub const LUA_JITLIBNAME: *const c_char = cstr!("jit");
2020
#[cfg(feature = "luajit")]
21-
pub const LUA_FFILIBNAME: &str = "ffi";
21+
pub const LUA_FFILIBNAME: *const c_char = cstr!("ffi");
2222

2323
#[cfg_attr(all(windows, raw_dylib), link(name = "lua51", kind = "raw-dylib"))]
2424
extern "C-unwind" {

mlua-sys/src/lua52/lualib.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
//! Contains definitions from `lualib.h`.
22
3-
use std::os::raw::c_int;
3+
use std::os::raw::{c_char, c_int};
44

55
use super::lua::lua_State;
66

7-
pub const LUA_COLIBNAME: &str = "coroutine";
8-
pub const LUA_TABLIBNAME: &str = "table";
9-
pub const LUA_IOLIBNAME: &str = "io";
10-
pub const LUA_OSLIBNAME: &str = "os";
11-
pub const LUA_STRLIBNAME: &str = "string";
12-
pub const LUA_BITLIBNAME: &str = "bit32";
13-
pub const LUA_MATHLIBNAME: &str = "math";
14-
pub const LUA_DBLIBNAME: &str = "debug";
15-
pub const LUA_LOADLIBNAME: &str = "package";
7+
pub const LUA_COLIBNAME: *const c_char = cstr!("coroutine");
8+
pub const LUA_TABLIBNAME: *const c_char = cstr!("table");
9+
pub const LUA_IOLIBNAME: *const c_char = cstr!("io");
10+
pub const LUA_OSLIBNAME: *const c_char = cstr!("os");
11+
pub const LUA_STRLIBNAME: *const c_char = cstr!("string");
12+
pub const LUA_BITLIBNAME: *const c_char = cstr!("bit32");
13+
pub const LUA_MATHLIBNAME: *const c_char = cstr!("math");
14+
pub const LUA_DBLIBNAME: *const c_char = cstr!("debug");
15+
pub const LUA_LOADLIBNAME: *const c_char = cstr!("package");
1616

1717
#[cfg_attr(all(windows, raw_dylib), link(name = "lua52", kind = "raw-dylib"))]
1818
extern "C-unwind" {

mlua-sys/src/lua53/lauxlib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ use super::lua::{self, lua_CFunction, lua_Integer, lua_Number, lua_State};
99
pub const LUA_ERRFILE: c_int = lua::LUA_ERRERR + 1;
1010

1111
// Key, in the registry, for table of loaded modules
12-
pub const LUA_LOADED_TABLE: &str = "_LOADED";
12+
pub const LUA_LOADED_TABLE: *const c_char = cstr!("_LOADED");
1313

1414
// Key, in the registry, for table of preloaded loaders
15-
pub const LUA_PRELOAD_TABLE: &str = "_PRELOAD";
15+
pub const LUA_PRELOAD_TABLE: *const c_char = cstr!("_PRELOAD");
1616

1717
#[repr(C)]
1818
pub struct luaL_Reg {

mlua-sys/src/lua53/lualib.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
//! Contains definitions from `lualib.h`.
22
3-
use std::os::raw::c_int;
3+
use std::os::raw::{c_char, c_int};
44

55
use super::lua::lua_State;
66

7-
pub const LUA_COLIBNAME: &str = "coroutine";
8-
pub const LUA_TABLIBNAME: &str = "table";
9-
pub const LUA_IOLIBNAME: &str = "io";
10-
pub const LUA_OSLIBNAME: &str = "os";
11-
pub const LUA_STRLIBNAME: &str = "string";
12-
pub const LUA_UTF8LIBNAME: &str = "utf8";
13-
pub const LUA_BITLIBNAME: &str = "bit32";
14-
pub const LUA_MATHLIBNAME: &str = "math";
15-
pub const LUA_DBLIBNAME: &str = "debug";
16-
pub const LUA_LOADLIBNAME: &str = "package";
7+
pub const LUA_COLIBNAME: *const c_char = cstr!("coroutine");
8+
pub const LUA_TABLIBNAME: *const c_char = cstr!("table");
9+
pub const LUA_IOLIBNAME: *const c_char = cstr!("io");
10+
pub const LUA_OSLIBNAME: *const c_char = cstr!("os");
11+
pub const LUA_STRLIBNAME: *const c_char = cstr!("string");
12+
pub const LUA_UTF8LIBNAME: *const c_char = cstr!("utf8");
13+
pub const LUA_BITLIBNAME: *const c_char = cstr!("bit32");
14+
pub const LUA_MATHLIBNAME: *const c_char = cstr!("math");
15+
pub const LUA_DBLIBNAME: *const c_char = cstr!("debug");
16+
pub const LUA_LOADLIBNAME: *const c_char = cstr!("package");
1717

1818
#[cfg_attr(all(windows, raw_dylib), link(name = "lua53", kind = "raw-dylib"))]
1919
extern "C-unwind" {

mlua-sys/src/lua54/lauxlib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ use super::lua::{self, lua_CFunction, lua_Integer, lua_Number, lua_State};
99
pub const LUA_ERRFILE: c_int = lua::LUA_ERRERR + 1;
1010

1111
// Key, in the registry, for table of loaded modules
12-
pub const LUA_LOADED_TABLE: &str = "_LOADED";
12+
pub const LUA_LOADED_TABLE: *const c_char = cstr!("_LOADED");
1313

1414
// Key, in the registry, for table of preloaded loaders
15-
pub const LUA_PRELOAD_TABLE: &str = "_PRELOAD";
15+
pub const LUA_PRELOAD_TABLE: *const c_char = cstr!("_PRELOAD");
1616

1717
#[repr(C)]
1818
pub struct luaL_Reg {

mlua-sys/src/lua54/lualib.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
//! Contains definitions from `lualib.h`.
22
3-
use std::os::raw::c_int;
3+
use std::os::raw::{c_char, c_int};
44

55
use super::lua::lua_State;
66

7-
pub const LUA_COLIBNAME: &str = "coroutine";
8-
pub const LUA_TABLIBNAME: &str = "table";
9-
pub const LUA_IOLIBNAME: &str = "io";
10-
pub const LUA_OSLIBNAME: &str = "os";
11-
pub const LUA_STRLIBNAME: &str = "string";
12-
pub const LUA_UTF8LIBNAME: &str = "utf8";
13-
pub const LUA_MATHLIBNAME: &str = "math";
14-
pub const LUA_DBLIBNAME: &str = "debug";
15-
pub const LUA_LOADLIBNAME: &str = "package";
7+
pub const LUA_COLIBNAME: *const c_char = cstr!("coroutine");
8+
pub const LUA_TABLIBNAME: *const c_char = cstr!("table");
9+
pub const LUA_IOLIBNAME: *const c_char = cstr!("io");
10+
pub const LUA_OSLIBNAME: *const c_char = cstr!("os");
11+
pub const LUA_STRLIBNAME: *const c_char = cstr!("string");
12+
pub const LUA_UTF8LIBNAME: *const c_char = cstr!("utf8");
13+
pub const LUA_MATHLIBNAME: *const c_char = cstr!("math");
14+
pub const LUA_DBLIBNAME: *const c_char = cstr!("debug");
15+
pub const LUA_LOADLIBNAME: *const c_char = cstr!("package");
1616

1717
#[cfg_attr(all(windows, raw_dylib), link(name = "lua54", kind = "raw-dylib"))]
1818
extern "C-unwind" {

mlua-sys/src/luau/lualib.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
//! Contains definitions from `lualib.h`.
22
3-
use std::os::raw::c_int;
3+
use std::os::raw::{c_char, c_int};
44

55
use super::lua::lua_State;
66

7-
pub const LUA_COLIBNAME: &str = "coroutine";
8-
pub const LUA_TABLIBNAME: &str = "table";
9-
pub const LUA_OSLIBNAME: &str = "os";
10-
pub const LUA_STRLIBNAME: &str = "string";
11-
pub const LUA_BITLIBNAME: &str = "bit32";
12-
pub const LUA_BUFFERLIBNAME: &str = "buffer";
13-
pub const LUA_UTF8LIBNAME: &str = "utf8";
14-
pub const LUA_MATHLIBNAME: &str = "math";
15-
pub const LUA_DBLIBNAME: &str = "debug";
16-
pub const LUA_VECLIBNAME: &str = "vector";
7+
pub const LUA_COLIBNAME: *const c_char = cstr!("coroutine");
8+
pub const LUA_TABLIBNAME: *const c_char = cstr!("table");
9+
pub const LUA_OSLIBNAME: *const c_char = cstr!("os");
10+
pub const LUA_STRLIBNAME: *const c_char = cstr!("string");
11+
pub const LUA_BITLIBNAME: *const c_char = cstr!("bit32");
12+
pub const LUA_BUFFERLIBNAME: *const c_char = cstr!("buffer");
13+
pub const LUA_UTF8LIBNAME: *const c_char = cstr!("utf8");
14+
pub const LUA_MATHLIBNAME: *const c_char = cstr!("math");
15+
pub const LUA_DBLIBNAME: *const c_char = cstr!("debug");
16+
pub const LUA_VECLIBNAME: *const c_char = cstr!("vector");
1717

1818
extern "C-unwind" {
1919
pub fn luaopen_base(L: *mut lua_State) -> c_int;

src/state/raw.rs

+5-22
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::any::TypeId;
22
use std::cell::{Cell, UnsafeCell};
3-
use std::ffi::{CStr, CString};
3+
use std::ffi::CStr;
44
use std::mem;
55
use std::os::raw::{c_char, c_int, c_void};
66
use std::panic::resume_unwind;
@@ -1351,16 +1351,14 @@ impl RawLua {
13511351

13521352
// Uses 3 stack spaces
13531353
unsafe fn load_std_libs(state: *mut ffi::lua_State, libs: StdLib) -> Result<()> {
1354-
#[inline(always)]
1355-
pub unsafe fn requiref(
1354+
unsafe fn requiref(
13561355
state: *mut ffi::lua_State,
1357-
modname: &str,
1356+
modname: *const c_char,
13581357
openf: ffi::lua_CFunction,
13591358
glb: c_int,
13601359
) -> Result<()> {
1361-
let modname = mlua_expect!(CString::new(modname), "modname contains nil byte");
1362-
protect_lua!(state, 0, 1, |state| {
1363-
ffi::luaL_requiref(state, modname.as_ptr() as *const c_char, openf, glb)
1360+
protect_lua!(state, 0, 0, |state| {
1361+
ffi::luaL_requiref(state, modname, openf, glb)
13641362
})
13651363
}
13661364

@@ -1391,81 +1389,68 @@ unsafe fn load_std_libs(state: *mut ffi::lua_State, libs: StdLib) -> Result<()>
13911389
{
13921390
if libs.contains(StdLib::COROUTINE) {
13931391
requiref(state, ffi::LUA_COLIBNAME, ffi::luaopen_coroutine, 1)?;
1394-
ffi::lua_pop(state, 1);
13951392
}
13961393
}
13971394

13981395
if libs.contains(StdLib::TABLE) {
13991396
requiref(state, ffi::LUA_TABLIBNAME, ffi::luaopen_table, 1)?;
1400-
ffi::lua_pop(state, 1);
14011397
}
14021398

14031399
#[cfg(not(feature = "luau"))]
14041400
if libs.contains(StdLib::IO) {
14051401
requiref(state, ffi::LUA_IOLIBNAME, ffi::luaopen_io, 1)?;
1406-
ffi::lua_pop(state, 1);
14071402
}
14081403

14091404
if libs.contains(StdLib::OS) {
14101405
requiref(state, ffi::LUA_OSLIBNAME, ffi::luaopen_os, 1)?;
1411-
ffi::lua_pop(state, 1);
14121406
}
14131407

14141408
if libs.contains(StdLib::STRING) {
14151409
requiref(state, ffi::LUA_STRLIBNAME, ffi::luaopen_string, 1)?;
1416-
ffi::lua_pop(state, 1);
14171410
}
14181411

14191412
#[cfg(any(feature = "lua54", feature = "lua53", feature = "luau"))]
14201413
{
14211414
if libs.contains(StdLib::UTF8) {
14221415
requiref(state, ffi::LUA_UTF8LIBNAME, ffi::luaopen_utf8, 1)?;
1423-
ffi::lua_pop(state, 1);
14241416
}
14251417
}
14261418

14271419
#[cfg(any(feature = "lua52", feature = "luau"))]
14281420
{
14291421
if libs.contains(StdLib::BIT) {
14301422
requiref(state, ffi::LUA_BITLIBNAME, ffi::luaopen_bit32, 1)?;
1431-
ffi::lua_pop(state, 1);
14321423
}
14331424
}
14341425

14351426
#[cfg(feature = "luajit")]
14361427
{
14371428
if libs.contains(StdLib::BIT) {
14381429
requiref(state, ffi::LUA_BITLIBNAME, ffi::luaopen_bit, 1)?;
1439-
ffi::lua_pop(state, 1);
14401430
}
14411431
}
14421432

14431433
#[cfg(feature = "luau")]
14441434
if libs.contains(StdLib::BUFFER) {
14451435
requiref(state, ffi::LUA_BUFFERLIBNAME, ffi::luaopen_buffer, 1)?;
1446-
ffi::lua_pop(state, 1);
14471436
}
14481437

14491438
#[cfg(feature = "luau")]
14501439
if libs.contains(StdLib::VECTOR) {
14511440
requiref(state, ffi::LUA_VECLIBNAME, ffi::luaopen_vector, 1)?;
1452-
ffi::lua_pop(state, 1);
14531441
}
14541442

14551443
if libs.contains(StdLib::MATH) {
14561444
requiref(state, ffi::LUA_MATHLIBNAME, ffi::luaopen_math, 1)?;
1457-
ffi::lua_pop(state, 1);
14581445
}
14591446

14601447
if libs.contains(StdLib::DEBUG) {
14611448
requiref(state, ffi::LUA_DBLIBNAME, ffi::luaopen_debug, 1)?;
1462-
ffi::lua_pop(state, 1);
14631449
}
14641450

14651451
#[cfg(not(feature = "luau"))]
14661452
if libs.contains(StdLib::PACKAGE) {
14671453
requiref(state, ffi::LUA_LOADLIBNAME, ffi::luaopen_package, 1)?;
1468-
ffi::lua_pop(state, 1);
14691454
}
14701455
#[cfg(feature = "luau")]
14711456
if libs.contains(StdLib::PACKAGE) {
@@ -1476,13 +1461,11 @@ unsafe fn load_std_libs(state: *mut ffi::lua_State, libs: StdLib) -> Result<()>
14761461
#[cfg(feature = "luajit")]
14771462
if libs.contains(StdLib::JIT) {
14781463
requiref(state, ffi::LUA_JITLIBNAME, ffi::luaopen_jit, 1)?;
1479-
ffi::lua_pop(state, 1);
14801464
}
14811465

14821466
#[cfg(feature = "luajit")]
14831467
if libs.contains(StdLib::FFI) {
14841468
requiref(state, ffi::LUA_FFILIBNAME, ffi::luaopen_ffi, 1)?;
1485-
ffi::lua_pop(state, 1);
14861469
}
14871470

14881471
Ok(())

0 commit comments

Comments
 (0)