Skip to content

Commit 18f1a10

Browse files
committed
Use async/await in variablesRequest
1 parent 6713545 commit 18f1a10

File tree

1 file changed

+54
-49
lines changed

1 file changed

+54
-49
lines changed

src/mibase.ts

Lines changed: 54 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { DebugSession, InitializedEvent, TerminatedEvent, StoppedEvent, OutputEvent, Thread, StackFrame, Scope, Source, Handles } from 'vscode-debugadapter';
22
import { DebugProtocol } from 'vscode-debugprotocol';
3-
import { Breakpoint, IBackend } from './backend/backend';
3+
import { Breakpoint, IBackend, Variable } from './backend/backend';
44
import { MINode } from './backend/mi_parse';
55
import { expandValue, isExpandable } from './backend/gdb_expansion';
66
import { MI2 } from './backend/mi2/mi2';
@@ -261,7 +261,7 @@ export class MI2DebugSession extends DebugSession {
261261
this.sendResponse(response);
262262
}
263263

264-
protected variablesRequest(response: DebugProtocol.VariablesResponse, args: DebugProtocol.VariablesArguments): void {
264+
protected async variablesRequest(response: DebugProtocol.VariablesResponse, args: DebugProtocol.VariablesArguments): Promise<void> {
265265
const variables: DebugProtocol.Variable[] = [];
266266
const id = this.variableHandles.get(args.variablesReference);
267267

@@ -274,10 +274,12 @@ export class MI2DebugSession extends DebugSession {
274274

275275
if (typeof id == "string") {
276276
if (id.startsWith("@frame:")) {
277-
this.miDebugger.getStackVariables(this.threadID, parseInt(id.substr("@frame:".length))).then(stack => {
278-
stack.forEach(variable => {
277+
let stack: Variable[];
278+
try {
279+
stack = await this.miDebugger.getStackVariables(this.threadID, parseInt(id.substr("@frame:".length)));
280+
for (const variable of stack) {
279281
if (variable.valueStr !== undefined) {
280-
let expanded = expandValue(createVariable, "{" + variable.name + "=" + variable.valueStr + ")", "", variable.raw);
282+
let expanded = expandValue(createVariable, `{${variable.name}=${variable.valueStr})`, "", variable.raw);
281283
if (expanded) {
282284
if (typeof expanded[0] == "string")
283285
expanded = [
@@ -296,18 +298,21 @@ export class MI2DebugSession extends DebugSession {
296298
value: "<unknown>",
297299
variablesReference: createVariable(variable.name)
298300
});
299-
});
301+
}
300302
response.body = {
301303
variables: variables
302304
};
303305
this.sendResponse(response);
304-
}, err => {
305-
this.sendErrorResponse(response, 1, "Could not expand variable: " + err);
306-
});
306+
}
307+
catch (err) {
308+
this.sendErrorResponse(response, 1, `Could not expand variable: ${err}`);
309+
}
307310
}
308311
else {
309312
// Variable members
310-
this.miDebugger.evalExpression(JSON.stringify(id)).then(variable => {
313+
let variable;
314+
try {
315+
variable = await this.miDebugger.evalExpression(JSON.stringify(id));
311316
try {
312317
let expanded = expandValue(createVariable, variable.result("value"), id, variable);
313318
if (!expanded) {
@@ -329,11 +334,12 @@ export class MI2DebugSession extends DebugSession {
329334
}
330335
}
331336
catch (e) {
332-
this.sendErrorResponse(response, 2, `Could not expand variable: ` + e);
337+
this.sendErrorResponse(response, 2, `Could not expand variable: ${e}`);
333338
}
334-
}, err => {
335-
this.sendErrorResponse(response, 1, `Could not expand variable`);
336-
});
339+
}
340+
catch (err) {
341+
this.sendErrorResponse(response, 1, `Could not expand variable: ${err}`);
342+
}
337343
}
338344
}
339345
else if (typeof id == "object") {
@@ -349,55 +355,54 @@ export class MI2DebugSession extends DebugSession {
349355
};
350356
this.sendResponse(response);
351357
};
352-
let addOne = () => {
353-
this.miDebugger.evalExpression(JSON.stringify(varReq.name + "+" + arrIndex + ")")).then(variable => {
354-
try {
355-
let expanded = expandValue(createVariable, variable.result("value"), varReq.name, variable);
356-
if (!expanded) {
357-
this.sendErrorResponse(response, 15, `Could not expand variable`);
358-
}
359-
else {
360-
if (typeof expanded == "string") {
361-
if (expanded == "<nullptr>") {
362-
if (argsPart)
363-
argsPart = false;
364-
else
365-
return submit();
366-
}
367-
else if (expanded[0] != '"') {
368-
strArr.push({
369-
name: "[err]",
370-
value: expanded,
371-
variablesReference: 0
372-
});
358+
let addOne = async () => {
359+
const variable = await this.miDebugger.evalExpression(JSON.stringify(`${varReq.name}+${arrIndex})`));
360+
try {
361+
let expanded = expandValue(createVariable, variable.result("value"), varReq.name, variable);
362+
if (!expanded) {
363+
this.sendErrorResponse(response, 15, `Could not expand variable`);
364+
}
365+
else {
366+
if (typeof expanded == "string") {
367+
if (expanded == "<nullptr>") {
368+
if (argsPart)
369+
argsPart = false;
370+
else
373371
return submit();
374-
}
375-
strArr.push({
376-
name: "[" + (arrIndex++) + "]",
377-
value: expanded,
378-
variablesReference: 0
379-
});
380-
addOne();
381372
}
382-
else {
373+
else if (expanded[0] != '"') {
383374
strArr.push({
384375
name: "[err]",
385376
value: expanded,
386377
variablesReference: 0
387378
});
388-
submit();
379+
return submit();
389380
}
381+
strArr.push({
382+
name: `[${(arrIndex++)}]`,
383+
value: expanded,
384+
variablesReference: 0
385+
});
386+
addOne();
387+
}
388+
else {
389+
strArr.push({
390+
name: "[err]",
391+
value: expanded,
392+
variablesReference: 0
393+
});
394+
submit();
390395
}
391396
}
392-
catch (e) {
393-
this.sendErrorResponse(response, 14, `Could not expand variable: ` + e);
394-
}
395-
});
397+
}
398+
catch (e) {
399+
this.sendErrorResponse(response, 14, `Could not expand variable: ${e}`);
400+
}
396401
};
397402
addOne();
398403
}
399404
else
400-
this.sendErrorResponse(response, 13, `Unimplemented variable request options: ` + JSON.stringify(varReq.options));
405+
this.sendErrorResponse(response, 13, `Unimplemented variable request options: ${JSON.stringify(varReq.options)}`);
401406
}
402407
else {
403408
response.body = {

0 commit comments

Comments
 (0)