This repository was archived by the owner on Sep 12, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +43
-1
lines changed Expand file tree Collapse file tree 2 files changed +43
-1
lines changed Original file line number Diff line number Diff line change @@ -65,9 +65,11 @@ bitflags! {
6565 pub struct InitFlags : u32 {
6666 const PCALL_WRAPPERS = 0x1 ;
6767 const LOAD_WRAPPERS = 0x2 ;
68+ const REMOVE_LOADLIB = 0x4 ;
6869
6970 const DEFAULT = InitFlags :: PCALL_WRAPPERS . bits |
70- InitFlags :: LOAD_WRAPPERS . bits;
71+ InitFlags :: LOAD_WRAPPERS . bits |
72+ InitFlags :: REMOVE_LOADLIB . bits;
7173 const NONE = 0 ;
7274 }
7375}
@@ -707,6 +709,21 @@ unsafe fn create_lua(lua_mod_to_load: StdLib, init_flags: InitFlags) -> Lua {
707709 assert_eq!( result, 0 ) ;
708710 }
709711
712+ if init_flags. contains( InitFlags :: REMOVE_LOADLIB ) {
713+ ffi:: lua_getglobal( state, cstr!( "package" ) ) ;
714+ let t = ffi:: lua_type( state, -1 ) ;
715+ if t == ffi:: LUA_TTABLE {
716+ // Package is loaded
717+ ffi:: lua_pushnil( state) ;
718+ ffi:: lua_setfield( state, -2 , cstr!( "loadlib" ) ) ;
719+ } else {
720+ // Assume it's not present otherwise.
721+ assert_eq!( t, ffi:: LUA_TNIL ) ;
722+ }
723+ // Pop the package (or nil) off the stack.
724+ ffi:: lua_pop( state, 1 ) ;
725+ }
726+
710727 // Create ref stack thread and place it in the registry to prevent it from being garbage
711728 // collected.
712729
Original file line number Diff line number Diff line change @@ -865,6 +865,31 @@ fn test_no_loadstring_wrappers() {
865865 } ;
866866}
867867
868+ #[ test]
869+ fn test_default_loadlib ( ) {
870+ Lua :: new ( ) . context ( |lua| {
871+ let globals = lua. globals ( ) ;
872+ let package = globals. get :: < _ , Table > ( "package" ) . unwrap ( ) ;
873+ let loadlib = package. get :: < _ , Function > ( "loadlib" ) ;
874+ assert ! ( loadlib. is_err( ) ) ;
875+ } ) ;
876+ }
877+
878+ #[ test]
879+ fn test_no_remove_loadlib ( ) {
880+ unsafe {
881+ Lua :: unsafe_new_with_flags (
882+ StdLib :: ALL_NO_DEBUG ,
883+ InitFlags :: DEFAULT - InitFlags :: REMOVE_LOADLIB ,
884+ )
885+ . context ( |lua| {
886+ let globals = lua. globals ( ) ;
887+ let package = globals. get :: < _ , Table > ( "package" ) . unwrap ( ) ;
888+ let _loadlib = package. get :: < _ , Function > ( "loadlib" ) . unwrap ( ) ;
889+ } ) ;
890+ }
891+ }
892+
868893#[ test]
869894fn test_result_conversions ( ) {
870895 Lua :: new ( ) . context ( |lua| {
You can’t perform that action at this time.
0 commit comments