debug.get_const_eval_pos
Returns the source position of the constant that the compiler evaluates, or null when the code runs at runtime
Return value
A table with these keys, or null when the code runs at runtime:
func- the name of the function that the expression is insrc- its source fileline- the line of the call that runs the code; for code that an operator runs, such as a metamethod, the first line of the whole expression
Notes
The compiler runs a pure function to evaluate a constant, and to fold a call with constant arguments in any expression. A stack trace taken in that run has no frame for the code that uses the constant. This function gives that position, so a diagnostic in a pure function, such as a report of a missing key, can still name the place. Use the position for diagnostics only: the result of a pure function must not depend on it.
Each call in a constant gives its own line, and all code that a call runs gets the position of that call: when a pure function calls another one, both get the same position. But a call with constant arguments in the body of a pure function folds when that body compiles, so it gets its own line in the body.
When the evaluation compiles more code, for example with compilestring, the constants of that code give their own positions, and the outer position comes back when that compile ends.
Example
from "debug" import get_const_eval_pos
const function [pure] title(key) {
let pos = get_const_eval_pos()
if (pos != null)
println($"{key} is used in {pos.func} at line {pos.line}")
return key.toupper()
}
const HEADER = title("header")
function caption() {
return const title("caption")
}
println(HEADER)
println(caption())
let key = "dynamic" // not a constant, so this call of title runs at runtime
println(title(key))header is used in __main__ at line 10
caption is used in caption at line 13
HEADER
CAPTION
DYNAMICSee also
getstackinfos | Returns call stack information for the given stack level |
| debug | module index |