@@ -61,6 +61,7 @@ struct ExtraData {
61
61
registered_userdata_mt : HashSet < isize > ,
62
62
registry_unref_list : Arc < Mutex < Option < Vec < c_int > > > > ,
63
63
64
+ libs : StdLib ,
64
65
mem_info : * mut MemoryInfo ,
65
66
66
67
ref_thread : * mut ffi:: lua_State ,
@@ -172,7 +173,9 @@ impl Lua {
172
173
173
174
let mut lua = unsafe { Self :: unsafe_new_with ( libs) } ;
174
175
175
- mlua_expect ! ( lua. disable_c_modules( ) , "Error during disabling C modules" ) ;
176
+ if libs. contains ( StdLib :: PACKAGE ) {
177
+ mlua_expect ! ( lua. disable_c_modules( ) , "Error during disabling C modules" ) ;
178
+ }
176
179
lua. safe = true ;
177
180
178
181
Ok ( lua)
@@ -262,7 +265,7 @@ impl Lua {
262
265
lua. ephemeral = false ;
263
266
#[ cfg( any( feature = "lua54" , feature = "lua53" , feature = "lua52" ) ) ]
264
267
{
265
- lua. extra . lock ( ) . unwrap ( ) . mem_info = mem_info;
268
+ mlua_expect ! ( lua. extra. lock( ) , "extra is poisoned" ) . mem_info = mem_info;
266
269
}
267
270
268
271
mlua_expect ! (
@@ -271,6 +274,7 @@ impl Lua {
271
274
} ) ,
272
275
"Error during loading standard libraries"
273
276
) ;
277
+ mlua_expect ! ( lua. extra. lock( ) , "extra is poisoned" ) . libs |= libs;
274
278
275
279
lua
276
280
}
@@ -321,6 +325,7 @@ impl Lua {
321
325
registered_userdata_mt : HashSet :: new ( ) ,
322
326
registry_unref_list : Arc :: new ( Mutex :: new ( Some ( Vec :: new ( ) ) ) ) ,
323
327
ref_thread,
328
+ libs : StdLib :: NONE ,
324
329
mem_info : ptr:: null_mut ( ) ,
325
330
// We need 1 extra stack space to move values in and out of the ref stack.
326
331
ref_stack_size : ffi:: LUA_MINSTACK - 1 ,
@@ -381,11 +386,20 @@ impl Lua {
381
386
}
382
387
383
388
let state = self . main_state . unwrap_or ( self . state ) ;
384
- unsafe {
389
+ let res = unsafe {
385
390
protect_lua_closure ( state, 0 , 0 , |state| {
386
391
load_from_std_lib ( state, libs) ;
387
392
} )
393
+ } ;
394
+
395
+ // If `package` library loaded into a safe lua state then disable C modules
396
+ let curr_libs = mlua_expect ! ( self . extra. lock( ) , "extra is poisoned" ) . libs ;
397
+ if self . safe && ( curr_libs ^ ( curr_libs | libs) ) . contains ( StdLib :: PACKAGE ) {
398
+ mlua_expect ! ( self . disable_c_modules( ) , "Error during disabling C modules" ) ;
388
399
}
400
+ mlua_expect ! ( self . extra. lock( ) , "extra is poisoned" ) . libs |= libs;
401
+
402
+ res
389
403
}
390
404
391
405
/// Consumes and leaks `Lua` object, returning a static reference `&'static Lua`.
@@ -1655,7 +1669,12 @@ impl Lua {
1655
1669
' lua : ' callback ,
1656
1670
{
1657
1671
#[ cfg( any( feature = "lua54" , feature = "lua53" , feature = "lua52" ) ) ]
1658
- self . load_from_std_lib ( StdLib :: COROUTINE ) ?;
1672
+ {
1673
+ let libs = mlua_expect ! ( self . extra. lock( ) , "extra is poisoned" ) . libs ;
1674
+ if !libs. contains ( StdLib :: COROUTINE ) {
1675
+ self . load_from_std_lib ( StdLib :: COROUTINE ) ?;
1676
+ }
1677
+ }
1659
1678
1660
1679
unsafe extern "C" fn call_callback ( state : * mut ffi:: lua_State ) -> c_int {
1661
1680
callback_error ( state, |nargs| {
0 commit comments