Skip to content

Commit

Permalink
Merge pull request #2 from sewbacca/master
Browse files Browse the repository at this point in the history
Allowing inspection of functions
  • Loading branch information
Ismoh authored Oct 16, 2023
2 parents b5cb6c3 + ca5162a commit 597e5fb
Show file tree
Hide file tree
Showing 7 changed files with 933 additions and 131 deletions.
17 changes: 16 additions & 1 deletion debugger/debugger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -773,8 +773,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 Expand Up @@ -1190,6 +1193,16 @@ export namespace Debugger {
}
}
}

debug.setmetatable(() => { }, {
__index(key: string) {
const info = debug.getinfo(this, "fu");
if (!info) return undefined;
const val = getUpvalues(info).vars[key];
if (!val) return undefined;
return val.val;
}
});
};

export function clearHook(): void {
Expand All @@ -1211,6 +1224,8 @@ export namespace Debugger {
debug.sethook(thread);
}
}

debug.setmetatable(() => { }, undefined);
}

const breakInCoroutinesEnv: LuaDebug.BreakInCoroutinesEnv = "LOCAL_LUA_DEBUGGER_BREAK_IN_COROUTINES";
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
9 changes: 6 additions & 3 deletions extension/luaDebugSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,10 @@ 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 All @@ -754,7 +757,7 @@ export class LuaDebugSession extends LoggingDebugSession {
? `[error: ${this.filterErrorMessage(variable.error)}]`
: `(${variable.value ?? ""})`;
ref = variable.type === "table" ? this.variableHandles.create("@({...})") : 0;
} else if (variable.type === "table") {
} else if (variable.type === "table" || variable.type === "function") {
valueStr = this.getValueString(variable);
ref = this.variableHandles.create(refName);
} else {
Expand All @@ -764,7 +767,7 @@ export class LuaDebugSession extends LoggingDebugSession {
const indexedVariables = typeof variable.length !== "undefined" && variable.length > 0
? variable.length + 1
: variable.length;
if (variable.type === "table") {
if (variable.type === "table" || variable.type === "function") {
return new Variable(name, valueStr, ref, indexedVariables, 1);
} else {
return new Variable(name, valueStr, ref, indexedVariables);
Expand Down
Loading

0 comments on commit 597e5fb

Please sign in to comment.