Skip to content

Commit 5347a94

Browse files
committed
Improve handling of "set variable" failure (e.g. on consts)
Signed-off-by: itowlson <ivan.towlson@fermyon.com>
1 parent 969ee29 commit 5347a94

5 files changed

Lines changed: 39 additions & 11 deletions

File tree

debugger/vscode-dap-extension/shared/messages.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,15 @@ export interface VariablesResponse {
155155
type: 'variables',
156156
value: ReadonlyArray<IRuntimeVariable>,
157157
}
158-
export interface VariableSetResponse {
159-
type: 'variableSet',
160-
value: IRuntimeVariable,
161-
}
158+
export type VariableSetResponse = TypedErrorable<'variableSet', IRuntimeVariable>;
159+
// export interface VariableSetResponse {
160+
// type: 'variableSet',
161+
// value: IRuntimeVariable,
162+
// }
163+
// export interface VariableSetErrorResponse {
164+
// type: 'variableSet',
165+
// error: string,
166+
// }
162167
export interface EvaluateResponse {
163168
type: 'evaluate';
164169
value: {
@@ -173,3 +178,12 @@ export interface IRuntimeVariable {
173178
variablesReference: number;
174179
}
175180

181+
export function succeeded<M, V>(message: TypedErrorable<M, V>): message is { type: M, value: V } {
182+
return !failed(message);
183+
}
184+
export function failed<T>(e: Errorable<T>): e is { error: string } {
185+
return (<{ error?: string }>e).error !== undefined;
186+
}
187+
188+
export type TypedErrorable<M, V> = { type: M, value: V } | { type: M, error: string };
189+
export type Errorable<V> = V | { error: string };

debugger/vscode-dap-extension/src/starlingMonkeyDebugger.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
StarlingMonkeyRuntime,
1818
FileAccessor,
1919
} from "./starlingMonkeyRuntime.js";
20+
import { failed } from "../shared/messages.js";
2021

2122
interface ILaunchRequestArguments extends DebugProtocol.LaunchRequestArguments {
2223
program: string;
@@ -316,7 +317,12 @@ export class StarlingMonkeyDebugSession extends LoggingDebugSession {
316317
args.name,
317318
args.value
318319
);
319-
response.body = { value: message.value, type: message.type, variablesReference: message.variablesReference };
320+
if (failed(message)) {
321+
response.success = false;
322+
response.message = message.error;
323+
} else {
324+
response.body = { value: message.value, type: message.type, variablesReference: message.variablesReference };
325+
}
320326
this.sendResponse(response);
321327
}
322328

debugger/vscode-dap-extension/src/starlingMonkeyRuntime.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { Signal } from "./signals.js";
66
import { Terminal, TerminalShellExecution, window } from "vscode";
77
import { SourceLocation, SourceMaps } from "./sourcemaps/sourceMaps.js";
88
import { dirname } from "path";
9-
import { BreakpointSetResponse, BreakpointsForLineResponse, EvaluateResponse, InstanceToRuntimeMessage, IRuntimeBreakpoint, IRuntimeStackFrame, IRuntimeVariable, RuntimeToInstanceMessage, ScopesResponse, StackResponse, VariableSetResponse, VariablesResponse } from '../shared/messages';
9+
import { BreakpointSetResponse, BreakpointsForLineResponse, EvaluateResponse, InstanceToRuntimeMessage, IRuntimeBreakpoint, IRuntimeStackFrame, IRuntimeVariable, RuntimeToInstanceMessage, ScopesResponse, StackResponse, succeeded, VariableSetResponse, VariablesResponse } from '../shared/messages';
1010

1111
export interface FileAccessor {
1212
isWindows: boolean;
@@ -633,7 +633,7 @@ export class StarlingMonkeyRuntime extends EventEmitter<RuntimeEventMap> {
633633
variablesReference: number,
634634
name: string,
635635
value: string
636-
): Promise<IRuntimeVariable> {
636+
): Promise<IRuntimeVariable | { error: string }> {
637637
const jsValue: unknown = JSON.parse(value);
638638
this.sendMessage(
639639
{ type: "setVariable", value: {
@@ -644,7 +644,11 @@ export class StarlingMonkeyRuntime extends EventEmitter<RuntimeEventMap> {
644644
);
645645

646646
const message = await this._variableSet.wait();
647-
return message.value;
647+
if (succeeded(message)) {
648+
return message.value;
649+
} else {
650+
return { error: message.error };
651+
}
648652
}
649653

650654
public async evaluate(expression: string): Promise<{

debugger/vscode-dap-extension/starling-debugger/debugger.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,12 @@ try {
493493
} else {
494494
assert(currentFrame);
495495
let frame = findFrame(currentFrame, variablesReference - 1);
496-
frame.environment.setVariable(name, value);
496+
try {
497+
frame.environment.setVariable(name, value);
498+
} catch (e) {
499+
sendMessage({ type: 'variableSet', error: `${e}` });
500+
return;
501+
}
497502
newValue = { name, ...formatValue(frame.environment.getVariable(name)) };
498503
}
499504

debugger/vscode-dap-extension/tests/spin-ts/src/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
21
function handle(_request: Request): Response {
3-
let thingy = 'world';
2+
const thingy = 'world';
43
let message = `Hello, ${thingy}`;
54
return new Response(message, {
65
status: 200,

0 commit comments

Comments
 (0)