This feature request may not be a massively popular one, mainly due to the specific nature of what the feature is. Essentially, as far as I know, there is no way to easily get the highest stack level currently in use for functions such as
The current implementation does not allow for negative numbers, but I would love if the team working on Luau would make an alteration to that, so that the use of
-1 as an argument
getfenv(-1) would return the highest stack level function environment.
Currently, the only way I believe this can be achieved is through a for loop, testing each call to getfenv until an error arises. My implementation is below.
--// int getHighestStackLevel() -- this function loops through function environments until the highest stack level has been reached function module.getHighestStackLevel() local stackLevel = 1 -- Loop through stack levels until we reach the highest one while true do local success = (pcall(getfenv, stackLevel + 3)) -- +3 to test next stack level, plus the pcall function env, plus the getHighestStackLevel function env if success then stackLevel += 1 else break end end return stackLevel end
There probably aren’t a massive number of use cases for this on Roblox, especially as the getfenv and setfenv functions are not widely used either. I only ran into this because I was creating a custom logging system to improve the process of creating logs in the output. I found that in order to accurately describe the script in which the log came from, I needed to know the highest stack level as some calls would be nested in other functions.
This is all fine, but it would be great if we could get this information in a simpler, less rustic way.
Any and all feedback is greatly appreciated, I love to hear what you guys think!