| title | debug.isvalidlevel |
|---|
Checks whether a number is a valid stack level for the thread that calls it.
debug.isvalidlevel(level: number): booleanNew to stack levels? Learn more here.
It tracks debug.getinfo exactly: a level this returns true for is one debug.getinfo can read, and a level it returns false for is one debug.getinfo errors on.
| Parameter | Type | Description |
|---|---|---|
level |
number |
Which stack frame to test. |
boolean - true for a level the calling thread currently has on its stack, false for one it does not.
isvalidlevelvalidlevel
Measure how deep the current call stack is by counting levels until debug.isvalidlevel runs off the top.
local isvalidlevel = debug.isvalidlevel(8)local function stackDepth()
local level = 0
while debug.isvalidlevel(level) do
level += 1
end
return level
end
local function inner()
print(`inner: {stackDepth()} levels`)
end
print(`top level: {stackDepth()} levels`)
inner()
-- inner() prints the higher number: it runs one call deeper, so
-- isvalidlevel stays true for more levels before it runs off the top