Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allowing inspection of functions #72

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 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 Expand Up @@ -1160,6 +1163,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 @@ -1180,6 +1193,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 @@ -692,7 +692,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 @@ -718,7 +721,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 @@ -728,7 +731,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