Difference between revisions of "Setsafeenv"

From GiderosMobile
(Created page with "__NOTOC__ '''Available since:''' Gideros 2011.6<br/> '''Class:''' (global)<br/> === Description === Sets a safe environment to be used by the given function. <syntaxhighl...")
 
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
__NOTOC__
 
__NOTOC__
'''Available since:''' Gideros 2011.6<br/>
+
'''Available since:''' Gideros 2025.2<br/>
 
'''Class:''' [[(global)]]<br/>
 
'''Class:''' [[(global)]]<br/>
  
Line 6: Line 6:
 
Sets a safe environment to be used by the given function.
 
Sets a safe environment to be used by the given function.
 
<syntaxhighlight lang="lua">
 
<syntaxhighlight lang="lua">
(varies) = setsafeenv(f,table)
+
(varies) = setsafeenv(flags)
 
</syntaxhighlight>
 
</syntaxhighlight>
  
''f'' can be a Lua function or a number that specifies the function at that stack level: Level 1 is the function calling ''setsafeenv''. ''setsafeenv'' returns the given function.
+
Gideros 2025.2 introduces a new global function ''setsafeenv(flags)'' which tells Luau the global environment is "safe" and so enables optimizations. ''flags'' parameter indicates which optimizations to enable, and you'll probably want them all by using -1 value.
 +
 
 +
'''To enable optimizations, ''setsafeenv'' assumes that you don’t redefine global functions (math, pairs, ipairs, ...), even your own!'''
 +
 
 +
''flags'' can be a Lua function or a number that specifies the function at that stack level: Level 1 is the function calling ''setsafeenv''. ''setsafeenv'' returns the given function.
  
 
  As a special case, when f is 0 ''setsafeenv'' changes the environment of the running thread. In this case, ''setsafeenv'' returns no values.
 
  As a special case, when f is 0 ''setsafeenv'' changes the environment of the running thread. In this case, ''setsafeenv'' returns no values.
  
 
=== Parameters ===
 
=== Parameters ===
'''f''': (varies) function or number(call stack level)<br/>
+
'''flags''': (varies) function or number(call stack level)<br/>
'''table''': (table) environment table to set<br/>
 
  
 
=== Return values ===
 
=== Return values ===
 
'''Returns''' (varies) returns provided function or nil<br/>
 
'''Returns''' (varies) returns provided function or nil<br/>
  
=== See also ===
+
=== Example ===
'''[[setfenv]]'''
+
<syntaxhighlight lang="lua">
 +
setsafeenv(-1)
 +
</syntaxhighlight>
 +
 
 +
=== Reference ===
 +
'''https://luau.org/library#global-functions'''<br/>
 +
'''https://github.com/gideros/gideros/blob/master/winrt_xaml/giderosgame/giderosgame.Shared/lua.h#L209'''
  
 
{{(global)}}
 
{{(global)}}

Latest revision as of 12:08, 21 July 2025

Available since: Gideros 2025.2
Class: (global)

Description

Sets a safe environment to be used by the given function.

(varies) = setsafeenv(flags)

Gideros 2025.2 introduces a new global function setsafeenv(flags) which tells Luau the global environment is "safe" and so enables optimizations. flags parameter indicates which optimizations to enable, and you'll probably want them all by using -1 value.

To enable optimizations, setsafeenv assumes that you don’t redefine global functions (math, pairs, ipairs, ...), even your own!

flags can be a Lua function or a number that specifies the function at that stack level: Level 1 is the function calling setsafeenv. setsafeenv returns the given function.

As a special case, when f is 0 setsafeenv changes the environment of the running thread. In this case, setsafeenv returns no values.

Parameters

flags: (varies) function or number(call stack level)

Return values

Returns (varies) returns provided function or nil

Example

setsafeenv(-1)

Reference

https://luau.org/library#global-functions
https://github.com/gideros/gideros/blob/master/winrt_xaml/giderosgame/giderosgame.Shared/lua.h#L209