Skip to content

Commit

Permalink
Proof of concept, displaying upvalues in functions
Browse files Browse the repository at this point in the history
  • Loading branch information
sewbacca committed Oct 4, 2022
1 parent d51ee29 commit a638c2f
Show file tree
Hide file tree
Showing 7 changed files with 918 additions and 131 deletions.
5 changes: 4 additions & 1 deletion debugger/debugger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -751,8 +751,11 @@ export namespace Debugger {
if (s) {
if (type(r) === "table") {
Send.props(r as AnyTable, kind, tonumber(first), tonumber(count));
} else if (type(r) === "function") {
// eslint-disable-next-line @typescript-eslint/ban-types
Send.functionUpvalues(r as Function);
} else {
Send.error(`Expression "${mappedExpression}" is not a table`);
Send.error(`Expression "${mappedExpression}" is not a table nor a function`);
}
} else {
Send.error(r as string);
Expand Down
33 changes: 32 additions & 1 deletion debugger/send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
//SOFTWARE.

import {luaError, luaLenMetamethodSupported, luaRawLen} from "./luafuncs";
import {luaAssert, luaError, luaLenMetamethodSupported, luaRawLen} from "./luafuncs";
import {Format} from "./format";
import {Vars} from "./debugger";
import {Thread, mainThread, mainThreadName} from "./thread";
Expand Down Expand Up @@ -197,6 +197,37 @@ export namespace Send {
send(dbgProperties);
}

function getUpvalues(info: debug.FunctionInfo): { [name: string]: unknown } {
const ups: { [name: string]: unknown } = { };

if (!info.nups || !info.func) {
return ups;
}

for (const index of $range(1, info.nups)) {
const [name, val] = debug.getupvalue(info.func, index);
ups[luaAssert(name)] = val;
}

return ups;
}

// eslint-disable-next-line @typescript-eslint/ban-types
export function functionUpvalues(f: Function): void {
const dbgProperties: LuaDebug.Properties = {
tag: "$luaDebug",
type: "properties",
properties: Format.makeExplicitArray()
};
const upvalues = getUpvalues(debug.getinfo(f, "fu") as debug.FunctionInfo);
for (const [key, val] of pairs(upvalues)) {
const name = getPrintableValue(key);
const dbgVar = buildVariable(name, val);
table.insert(dbgProperties.properties, dbgVar);
}
send(dbgProperties);
}

export function breakpoints(breaks: Breakpoint[]): void {
const breakpointList: LuaDebug.Breakpoint[] = [];
for (const breakpoint of breaks) {
Expand Down
2 changes: 1 addition & 1 deletion extension/luaDebugSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,7 @@ export class LuaDebugSession extends LoggingDebugSession {
return {success: true, value: "nil", variablesReference: 0};
} else if (msg.results.length === 1) {
const result = msg.results[0];
const variablesReference = result.type === "table" ? this.variableHandles.create(expression) : 0;
const variablesReference = (result.type === "table" || result.type === "function") ? this.variableHandles.create(expression) : 0;
return {success: true, value: this.getValueString(result), variablesReference};
} else {
const variablesReference = this.variableHandles.create(`@({${expression}})`);
Expand Down
Loading

0 comments on commit a638c2f

Please sign in to comment.