11use std:: any:: TypeId ;
22use std:: cell:: { Cell , UnsafeCell } ;
3- use std:: ffi:: CStr ;
3+ use std:: ffi:: { CStr , CString } ;
44use std:: mem;
55use std:: os:: raw:: { c_char, c_int, c_void} ;
66use std:: panic:: resume_unwind;
@@ -1285,14 +1285,16 @@ impl RawLua {
12851285
12861286// Uses 3 stack spaces
12871287unsafe fn load_std_libs ( state : * mut ffi:: lua_State , libs : StdLib ) -> Result < ( ) > {
1288- unsafe fn requiref (
1288+ #[ inline( always) ]
1289+ pub unsafe fn requiref (
12891290 state : * mut ffi:: lua_State ,
1290- modname : * const c_char ,
1291+ modname : & str ,
12911292 openf : ffi:: lua_CFunction ,
12921293 glb : c_int ,
12931294 ) -> Result < ( ) > {
1294- protect_lua ! ( state, 0 , 0 , |state| {
1295- ffi:: luaL_requiref( state, modname, openf, glb)
1295+ let modname = mlua_expect ! ( CString :: new( modname) , "modname contains nil byte" ) ;
1296+ protect_lua ! ( state, 0 , 1 , |state| {
1297+ ffi:: luaL_requiref( state, modname. as_ptr( ) as * const c_char, openf, glb)
12961298 } )
12971299 }
12981300
@@ -1323,68 +1325,81 @@ unsafe fn load_std_libs(state: *mut ffi::lua_State, libs: StdLib) -> Result<()>
13231325 {
13241326 if libs. contains ( StdLib :: COROUTINE ) {
13251327 requiref ( state, ffi:: LUA_COLIBNAME , ffi:: luaopen_coroutine, 1 ) ?;
1328+ ffi:: lua_pop ( state, 1 ) ;
13261329 }
13271330 }
13281331
13291332 if libs. contains ( StdLib :: TABLE ) {
13301333 requiref ( state, ffi:: LUA_TABLIBNAME , ffi:: luaopen_table, 1 ) ?;
1334+ ffi:: lua_pop ( state, 1 ) ;
13311335 }
13321336
13331337 #[ cfg( not( feature = "luau" ) ) ]
13341338 if libs. contains ( StdLib :: IO ) {
13351339 requiref ( state, ffi:: LUA_IOLIBNAME , ffi:: luaopen_io, 1 ) ?;
1340+ ffi:: lua_pop ( state, 1 ) ;
13361341 }
13371342
13381343 if libs. contains ( StdLib :: OS ) {
13391344 requiref ( state, ffi:: LUA_OSLIBNAME , ffi:: luaopen_os, 1 ) ?;
1345+ ffi:: lua_pop ( state, 1 ) ;
13401346 }
13411347
13421348 if libs. contains ( StdLib :: STRING ) {
13431349 requiref ( state, ffi:: LUA_STRLIBNAME , ffi:: luaopen_string, 1 ) ?;
1350+ ffi:: lua_pop ( state, 1 ) ;
13441351 }
13451352
13461353 #[ cfg( any( feature = "lua54" , feature = "lua53" , feature = "luau" ) ) ]
13471354 {
13481355 if libs. contains ( StdLib :: UTF8 ) {
13491356 requiref ( state, ffi:: LUA_UTF8LIBNAME , ffi:: luaopen_utf8, 1 ) ?;
1357+ ffi:: lua_pop ( state, 1 ) ;
13501358 }
13511359 }
13521360
13531361 #[ cfg( any( feature = "lua52" , feature = "luau" ) ) ]
13541362 {
13551363 if libs. contains ( StdLib :: BIT ) {
13561364 requiref ( state, ffi:: LUA_BITLIBNAME , ffi:: luaopen_bit32, 1 ) ?;
1365+ ffi:: lua_pop ( state, 1 ) ;
13571366 }
13581367 }
13591368
13601369 #[ cfg( feature = "luajit" ) ]
13611370 {
13621371 if libs. contains ( StdLib :: BIT ) {
13631372 requiref ( state, ffi:: LUA_BITLIBNAME , ffi:: luaopen_bit, 1 ) ?;
1373+ ffi:: lua_pop ( state, 1 ) ;
13641374 }
13651375 }
13661376
13671377 #[ cfg( feature = "luau" ) ]
13681378 if libs. contains ( StdLib :: BUFFER ) {
13691379 requiref ( state, ffi:: LUA_BUFFERLIBNAME , ffi:: luaopen_buffer, 1 ) ?;
1380+ ffi:: lua_pop ( state, 1 ) ;
13701381 }
13711382
13721383 #[ cfg( feature = "luau" ) ]
13731384 if libs. contains ( StdLib :: VECTOR ) {
13741385 requiref ( state, ffi:: LUA_VECLIBNAME , ffi:: luaopen_vector, 1 ) ?;
1386+ ffi:: lua_pop ( state, 1 ) ;
13751387 }
13761388
13771389 if libs. contains ( StdLib :: MATH ) {
13781390 requiref ( state, ffi:: LUA_MATHLIBNAME , ffi:: luaopen_math, 1 ) ?;
1391+ ffi:: lua_pop ( state, 1 ) ;
13791392 }
13801393
13811394 if libs. contains ( StdLib :: DEBUG ) {
13821395 requiref ( state, ffi:: LUA_DBLIBNAME , ffi:: luaopen_debug, 1 ) ?;
1396+ ffi:: lua_pop ( state, 1 ) ;
13831397 }
13841398
13851399 #[ cfg( not( feature = "luau" ) ) ]
13861400 if libs. contains ( StdLib :: PACKAGE ) {
13871401 requiref ( state, ffi:: LUA_LOADLIBNAME , ffi:: luaopen_package, 1 ) ?;
1402+ ffi:: lua_pop ( state, 1 ) ;
13881403 }
13891404 #[ cfg( feature = "luau" ) ]
13901405 if libs. contains ( StdLib :: PACKAGE ) {
@@ -1395,11 +1410,13 @@ unsafe fn load_std_libs(state: *mut ffi::lua_State, libs: StdLib) -> Result<()>
13951410 #[ cfg( feature = "luajit" ) ]
13961411 if libs. contains ( StdLib :: JIT ) {
13971412 requiref ( state, ffi:: LUA_JITLIBNAME , ffi:: luaopen_jit, 1 ) ?;
1413+ ffi:: lua_pop ( state, 1 ) ;
13981414 }
13991415
14001416 #[ cfg( feature = "luajit" ) ]
14011417 if libs. contains ( StdLib :: FFI ) {
14021418 requiref ( state, ffi:: LUA_FFILIBNAME , ffi:: luaopen_ffi, 1 ) ?;
1419+ ffi:: lua_pop ( state, 1 ) ;
14031420 }
14041421
14051422 Ok ( ( ) )
0 commit comments