File tree Expand file tree Collapse file tree 2 files changed +32
-0
lines changed
Expand file tree Collapse file tree 2 files changed +32
-0
lines changed Original file line number Diff line number Diff line change @@ -581,6 +581,24 @@ impl Table {
581581 unsafe { ffi:: lua_getreadonly ( ref_thread, self . 0 . index ) != 0 }
582582 }
583583
584+ /// Controls `safeenv` attribute on the table.
585+ ///
586+ /// This a special flag that activates some performance optimizations for environment tables.
587+ /// In particular, it controls:
588+ /// - Optimization of import resolution (cache values of constant keys).
589+ /// - Fast-path for built-in iteration with pairs/ipairs.
590+ /// - Fast-path for some built-in functions (fastcall).
591+ ///
592+ /// For `safeenv` environments, monkey patching or modifying values may not work as expected.
593+ ///
594+ /// Requires `feature = "luau"`
595+ #[ cfg( any( feature = "luau" , doc) ) ]
596+ #[ cfg_attr( docsrs, doc( cfg( feature = "luau" ) ) ) ]
597+ pub fn set_safeenv ( & self , enabled : bool ) {
598+ let lua = self . 0 . lua . lock ( ) ;
599+ unsafe { ffi:: lua_setsafeenv ( lua. ref_thread ( ) , self . 0 . index , enabled as _ ) } ;
600+ }
601+
584602 /// Converts this table to a generic C pointer.
585603 ///
586604 /// Different tables will give different pointers.
Original file line number Diff line number Diff line change @@ -282,6 +282,20 @@ fn test_sandbox() -> Result<()> {
282282 Ok ( ( ) )
283283}
284284
285+ #[ test]
286+ fn test_sandbox_safeenv ( ) -> Result < ( ) > {
287+ let lua = Lua :: new ( ) ;
288+
289+ lua. sandbox ( true ) ?;
290+ lua. globals ( ) . set ( "state" , lua. create_table ( ) ?) ?;
291+ lua. globals ( ) . set_safeenv ( false ) ;
292+ lua. load ( "state.a = 123" ) . exec ( ) ?;
293+ let a: i32 = lua. load ( "state.a = 321; return state.a" ) . eval ( ) ?;
294+ assert_eq ! ( a, 321 ) ;
295+
296+ Ok ( ( ) )
297+ }
298+
285299#[ test]
286300fn test_sandbox_nolibs ( ) -> Result < ( ) > {
287301 let lua = Lua :: new_with ( StdLib :: NONE , LuaOptions :: default ( ) ) . unwrap ( ) ;
You can’t perform that action at this time.
0 commit comments