Difference between revisions of "Rawiter"

From GiderosMobile
(wip, need demo)
 
(One intermediate revision by the same user not shown)
Line 6: Line 6:
 
Iterates keys in a table, without invoking any metamethod. ''table'' must be a table, ''key'' may be any value.
 
Iterates keys in a table, without invoking any metamethod. ''table'' must be a table, ''key'' may be any value.
 
<syntaxhighlight lang="lua">
 
<syntaxhighlight lang="lua">
(any) = rawiter(table,key)
+
(key),(value),(nextindex) = rawiter(table,index)
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 +
Lua ''next''() and ''pairs''() calls work on keys, which means that Luau have to lookup the previous key to find the next one on each iteration. Enabling optimizations with the above call enables a fast path for 'for' loops with a 'pairs' call, but nothing is done if you use them outside a 'for' loop.
 +
 +
But Luau 'C' API had a lua_rawiter() call which allowed to keep an internal index during key/value pairs iterations, it is now available to Lua code.
  
 
=== Parameters ===
 
=== Parameters ===
 
'''table''': (table) table to iterate<br/>
 
'''table''': (table) table to iterate<br/>
'''key''': (any) key value in the table<br/>
+
'''index''': (number) index in the table<br/>
  
 
=== Return values ===
 
=== Return values ===
'''Returns''' (any) value from the table<br/>
+
'''Returns''' (any) the table key<br/>
 +
'''Returns''' (any) the table value<br/>
 +
'''Returns''' (number) the table next index<br/>
  
 
{{(global)}}
 
{{(global)}}

Latest revision as of 07:53, 6 February 2025

Available since: Gideros 2025.2
Class: (global)

Description

Iterates keys in a table, without invoking any metamethod. table must be a table, key may be any value.

(key),(value),(nextindex) = rawiter(table,index)

Lua next() and pairs() calls work on keys, which means that Luau have to lookup the previous key to find the next one on each iteration. Enabling optimizations with the above call enables a fast path for 'for' loops with a 'pairs' call, but nothing is done if you use them outside a 'for' loop.

But Luau 'C' API had a lua_rawiter() call which allowed to keep an internal index during key/value pairs iterations, it is now available to Lua code.

Parameters

table: (table) table to iterate
index: (number) index in the table

Return values

Returns (any) the table key
Returns (any) the table value
Returns (number) the table next index