|
| 1 | +// Based on https://www.lua.org/manual/5.0/manual.html#5.8 |
| 2 | + |
| 3 | +/** @noSelfInFile */ |
| 4 | + |
| 5 | +/** |
| 6 | + * This library provides the functionality of the debug interface (§4.9) to Lua |
| 7 | + * programs. You should exert care when using this library. Several of its |
| 8 | + * functions violate basic assumptions about Lua code (e.g., that variables |
| 9 | + * local to a function cannot be accessed from outside; that userdata metatables |
| 10 | + * cannot be changed by Lua code; that Lua programs do not crash) and therefore |
| 11 | + * can compromise otherwise secure code. Moreover, some functions in this |
| 12 | + * library may be slow. |
| 13 | + * |
| 14 | + * All functions in this library are provided inside the debug table. All |
| 15 | + * functions that operate over a thread have an optional first argument which is |
| 16 | + * the thread to operate over. The default is always the current thread. |
| 17 | + */ |
| 18 | +declare namespace debug { |
| 19 | + /** |
| 20 | + * Enters an interactive mode with the user, running each string that the user |
| 21 | + * enters. Using simple commands and other debug facilities, the user can |
| 22 | + * inspect global and local variables, change their values, evaluate |
| 23 | + * expressions, and so on. A line containing only the word cont finishes this |
| 24 | + * function, so that the caller continues its execution. |
| 25 | + * |
| 26 | + * Note that commands for debug.debug are not lexically nested within any |
| 27 | + * function and so have no direct access to local variables. |
| 28 | + */ |
| 29 | + function debug(): void; |
| 30 | + |
| 31 | + interface FunctionInfo<T extends Function = Function> { |
| 32 | + /** |
| 33 | + * The function itself. |
| 34 | + */ |
| 35 | + func: T; |
| 36 | + |
| 37 | + /** |
| 38 | + * A reasonable name for the function. |
| 39 | + */ |
| 40 | + name?: string; |
| 41 | + /** |
| 42 | + * What the `name` field means. The empty string means that Lua did not find |
| 43 | + * a name for the function. |
| 44 | + */ |
| 45 | + namewhat: 'global' | 'local' | 'method' | 'field' | ''; |
| 46 | + |
| 47 | + source: string; |
| 48 | + /** |
| 49 | + * A short version of source (up to 60 characters), useful for error |
| 50 | + * messages. |
| 51 | + */ |
| 52 | + short_src: string; |
| 53 | + linedefined: number; |
| 54 | + lastlinedefined: number; |
| 55 | + /** |
| 56 | + * What this function is. |
| 57 | + */ |
| 58 | + what: 'Lua' | 'C' | 'main'; |
| 59 | + |
| 60 | + currentline: number; |
| 61 | + |
| 62 | + /** |
| 63 | + * Number of upvalues of that function. |
| 64 | + */ |
| 65 | + nups: number; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Returns the current hook settings of the thread, as three values: the |
| 70 | + * current hook function, the current hook mask, and the current hook count |
| 71 | + * (as set by the debug.sethook function). |
| 72 | + */ |
| 73 | + function gethook(): LuaMultiReturn<[undefined, 0] | [Function, number, string?]>; |
| 74 | + |
| 75 | + /** |
| 76 | + * Returns a table with information about a function. You can give the |
| 77 | + * function directly or you can give a number as the value of f, which means |
| 78 | + * the function running at level f of the call stack of the given thread: |
| 79 | + * level 0 is the current function (getinfo itself); level 1 is the function |
| 80 | + * that called getinfo (except for tail calls, which do not count on the |
| 81 | + * stack); and so on. If f is a number larger than the number of active |
| 82 | + * functions, then getinfo returns nil. |
| 83 | + * |
| 84 | + * The returned table can contain all the fields returned by lua_getinfo, with |
| 85 | + * the string what describing which fields to fill in. The default for what is |
| 86 | + * to get all information available, except the table of valid lines. If |
| 87 | + * present, the option 'f' adds a field named func with the function itself. |
| 88 | + * If present, the option 'L' adds a field named activelines with the table of |
| 89 | + * valid lines. |
| 90 | + * |
| 91 | + * For instance, the expression debug.getinfo(1,"n").name returns a name for |
| 92 | + * the current function, if a reasonable name can be found, and the expression |
| 93 | + * debug.getinfo(print) returns a table with all available information about |
| 94 | + * the print function. |
| 95 | + */ |
| 96 | + function getinfo<T extends Function>(f: T): FunctionInfo<T>; |
| 97 | + function getinfo<T extends Function>(f: T, what: string): Partial<FunctionInfo<T>>; |
| 98 | + function getinfo(f: number): FunctionInfo | undefined; |
| 99 | + function getinfo(f: number, what: string): Partial<FunctionInfo> | undefined; |
| 100 | + |
| 101 | + /** |
| 102 | + * This function returns the name and the value of the local variable with |
| 103 | + * index local of the function at level level of the stack. (The first |
| 104 | + * parameter or local variable has index 1, and so on, until the last active |
| 105 | + * local variable.) The function returns nil if there is no local variable |
| 106 | + * with the given index, and raises an error when called with a level out of |
| 107 | + * range. (You can call debug.getinfo to check whether the level is valid.) |
| 108 | + */ |
| 109 | + function getlocal(level: number, local: number): LuaMultiReturn<[string, any]>; |
| 110 | + |
| 111 | + /** |
| 112 | + * This function returns the name and the value of the upvalue with index up |
| 113 | + * of the function f. The function returns nil if there is no upvalue with the |
| 114 | + * given index. |
| 115 | + * |
| 116 | + * Variable names starting with '(' (open parenthesis) represent variables |
| 117 | + * with no known names (variables from chunks saved without debug |
| 118 | + * information). |
| 119 | + */ |
| 120 | + function getupvalue(f: Function, up: number): LuaMultiReturn<[string, any] | []>; |
| 121 | + |
| 122 | + /** |
| 123 | + * Sets the given function as a hook. The string mask and the number count |
| 124 | + * describe when the hook will be called. The string mask may have any |
| 125 | + * combination of the following characters, with the given meaning: |
| 126 | + * |
| 127 | + * * 'c': the hook is called every time Lua calls a function; |
| 128 | + * * 'r': the hook is called every time Lua returns from a function; |
| 129 | + * * 'l': the hook is called every time Lua enters a new line of code. |
| 130 | + * |
| 131 | + * Moreover, with a count different from zero, the hook is called also after |
| 132 | + * every count instructions. |
| 133 | + * |
| 134 | + * When called without arguments, debug.sethook turns off the hook. |
| 135 | + * |
| 136 | + * When the hook is called, its first parameter is a string describing the |
| 137 | + * event that has triggered its call: "call" (or "tail call"), "return", |
| 138 | + * "line", and "count". For line events, the hook also gets the new line |
| 139 | + * number as its second parameter. Inside a hook, you can call getinfo with |
| 140 | + * level 2 to get more information about the running function (level 0 is the |
| 141 | + * getinfo function, and level 1 is the hook function). |
| 142 | + */ |
| 143 | + function sethook(): void; |
| 144 | + function sethook( |
| 145 | + hook: (event: 'call' | 'return' | 'line' | 'count', line?: number) => any, |
| 146 | + mask: string, |
| 147 | + count?: number |
| 148 | + ): void; |
| 149 | + |
| 150 | + /** |
| 151 | + * This function assigns the value value to the local variable with index |
| 152 | + * local of the function at level level of the stack. The function returns nil |
| 153 | + * if there is no local variable with the given index, and raises an error |
| 154 | + * when called with a level out of range. (You can call getinfo to check |
| 155 | + * whether the level is valid.) Otherwise, it returns the name of the local |
| 156 | + * variable. |
| 157 | + * |
| 158 | + * See debug.getlocal for more information about variable indices and names. |
| 159 | + */ |
| 160 | + function setlocal(level: number, local: number, value: any): string | undefined; |
| 161 | + |
| 162 | + /** |
| 163 | + * This function assigns the value value to the upvalue with index up of the |
| 164 | + * function f. The function returns nil if there is no upvalue with the given |
| 165 | + * index. Otherwise, it returns the name of the upvalue. |
| 166 | + */ |
| 167 | + function setupvalue(f: Function, up: number, value: any): string | undefined; |
| 168 | + |
| 169 | + /** |
| 170 | + * Sets the given value as the Lua value associated to the given udata. udata |
| 171 | + * must be a full userdata. |
| 172 | + * |
| 173 | + * Returns udata. |
| 174 | + */ |
| 175 | + function setuservalue(udata: LuaUserdata, value: any): LuaUserdata; |
| 176 | + |
| 177 | + /** |
| 178 | + * Returns a string with a traceback of the call stack. An optional message |
| 179 | + * string is appended at the beginning of the traceback. This function is |
| 180 | + * typically used with xpcall to produce better error messages. |
| 181 | + */ |
| 182 | + function traceback<T>(message: T): T; |
| 183 | +} |
0 commit comments