@@ -1550,6 +1550,39 @@ impl Lua {
15501550 }
15511551 }
15521552
1553+ /// Sets the global environment.
1554+ ///
1555+ /// This will replace the current global environment with the provided `globals` table.
1556+ ///
1557+ /// For Lua 5.2+ the globals table is stored in the registry and shared between all threads.
1558+ /// For Lua 5.1 and Luau the globals table is stored in each thread.
1559+ ///
1560+ /// Please note that any existing Lua functions have cached global environment and will not
1561+ /// see the changes made by this method.
1562+ /// To update the environment for existing Lua functions, use [`Function::set_environment`].
1563+ pub fn set_globals ( & self , globals : Table ) -> Result < ( ) > {
1564+ let lua = self . lock ( ) ;
1565+ let state = lua. state ( ) ;
1566+ unsafe {
1567+ #[ cfg( feature = "luau" ) ]
1568+ if ( * lua. extra . get ( ) ) . sandboxed {
1569+ return Err ( Error :: runtime ( "cannot change globals in a sandboxed Lua state" ) ) ;
1570+ }
1571+
1572+ let _sg = StackGuard :: new ( state) ;
1573+ check_stack ( state, 1 ) ?;
1574+
1575+ lua. push_ref ( & globals. 0 ) ;
1576+
1577+ #[ cfg( any( feature = "lua54" , feature = "lua53" , feature = "lua52" ) ) ]
1578+ ffi:: lua_rawseti ( state, ffi:: LUA_REGISTRYINDEX , ffi:: LUA_RIDX_GLOBALS ) ;
1579+ #[ cfg( any( feature = "lua51" , feature = "luajit" , feature = "luau" ) ) ]
1580+ ffi:: lua_replace ( state, ffi:: LUA_GLOBALSINDEX ) ;
1581+ }
1582+
1583+ Ok ( ( ) )
1584+ }
1585+
15531586 /// Returns a handle to the active `Thread`.
15541587 ///
15551588 /// For calls to `Lua` this will be the main Lua thread, for parameters given to a callback,
0 commit comments