Skip to content

Latest commit

 

History

History
63 lines (44 loc) · 1.4 KB

File metadata and controls

63 lines (44 loc) · 1.4 KB
title debug.isvalidlevel

Checks whether a number is a valid stack level for the thread that calls it.

debug.isvalidlevel(level: number): boolean

New 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.

Parameters

Parameter Type Description
level number Which stack frame to test.

Returns

boolean - true for a level the calling thread currently has on its stack, false for one it does not.

Aliases

  • isvalidlevel
  • validlevel

Example

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
Useful for iterating up the stack without causing stack level out-of-range errors.