Skip to content

Commit 4aa3daf

Browse files
authored
Support SetVariable request (#32)
Set the variable with the given name in the variable container to a new value IoT.js-VSCode-DCO-1.0-Signed-off-by: Robert Sipka [email protected]
1 parent c82b8f2 commit 4aa3daf

File tree

4 files changed

+61
-13
lines changed

4 files changed

+61
-13
lines changed

src/IotjsDebugger.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ class IotjsDebugSession extends DebugSession {
8282
response.body.supportsStepBack = false;
8383
response.body.supportsRestartRequest = true;
8484
response.body.supportsDelayedStackTraceLoading = true;
85+
response.body.supportsSetVariable = true;
8586

8687
this._sourceSendingOptions = <SourceSendingOptions>{
8788
contextReset: false,
@@ -409,7 +410,7 @@ class IotjsDebugSession extends DebugSession {
409410
response: DebugProtocol.EvaluateResponse, args: DebugProtocol.EvaluateArguments
410411
): Promise<void> {
411412
try {
412-
const result: JerryEvalResult = await this._protocolhandler.evaluate(args.expression);
413+
const result: JerryEvalResult = await this._protocolhandler.evaluate(args.expression, 0);
413414
const value: string = result.subtype === EVAL_RESULT_SUBTYPE.JERRY_DEBUGGER_EVAL_OK
414415
? result.value
415416
: 'Evaluate Error';
@@ -486,6 +487,7 @@ class IotjsDebugSession extends DebugSession {
486487

487488
for (const variable of scopeVariables) {
488489
variables.push({name: variable.name,
490+
evaluateName: variable.name,
489491
type: variable.type,
490492
value: variable.value,
491493
variablesReference: 0});
@@ -501,6 +503,28 @@ class IotjsDebugSession extends DebugSession {
501503
}
502504
}
503505

506+
protected async setVariableRequest(response: DebugProtocol.SetVariableResponse,
507+
args: DebugProtocol.SetVariableArguments
508+
): Promise <void> {
509+
try {
510+
const expression = args.name + '=' + args.value;
511+
const scope_index = Number(this._variableHandles.get(args.variablesReference));
512+
const result: JerryEvalResult = await this._protocolhandler.evaluate(expression, scope_index);
513+
const value: string = result.subtype === EVAL_RESULT_SUBTYPE.JERRY_DEBUGGER_EVAL_OK
514+
? result.value
515+
: 'Evaluate Error';
516+
517+
response.body = {
518+
value: value,
519+
variablesReference: 0
520+
};
521+
this.sendResponse(response);
522+
} catch (error) {
523+
this.log(error.message, LOG_LEVEL.ERROR);
524+
this.sendErrorResponse(response, 0, (<Error>error).message);
525+
}
526+
}
527+
504528
protected customRequest(command: string, response: DebugProtocol.Response, args: any): void {
505529
switch (command) {
506530
case 'sendSource': {

src/JerryProtocolConstants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
'use strict';
1818

1919
// Expected JerryScript debugger protocol version.
20-
export const JERRY_DEBUGGER_VERSION = 7;
20+
export const JERRY_DEBUGGER_VERSION = 8;
2121

2222
// Packages sent from the server to the client.
2323
export enum SERVER {

src/JerryProtocolHandler.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -832,20 +832,30 @@ export class JerryDebugProtocolHandler {
832832
return bps.length ? bps.filter(b => b.func.name !== '') : [];
833833
}
834834

835-
public evaluate(expression: string): Promise<any> {
835+
public evaluate(expression: string, index: number): Promise<any> {
836836
if (!this.lastBreakpointHit) {
837837
return Promise.reject(new Error('attempted eval while not at breakpoint'));
838838
}
839839

840840
this.evalsPending++;
841841

842842
// send an _EVAL message prefixed with the byte length, followed by _EVAL_PARTs if necessary
843-
const array = stringToCesu8(SP.EVAL_SUBTYPE.JERRY_DEBUGGER_EVAL_EVAL + expression, 1 + 4, this.byteConfig);
843+
const header_size = 5; // message type + code length
844+
const index_size = 4; // length of scope chain index
845+
const array = stringToCesu8(SP.EVAL_SUBTYPE.JERRY_DEBUGGER_EVAL_EVAL + expression,
846+
header_size + index_size,
847+
this.byteConfig);
844848
const arrayLength = array.byteLength;
845-
const byteLength = arrayLength - 1 - 4;
849+
const byteLength = arrayLength - header_size;
846850
array[0] = SP.CLIENT.JERRY_DEBUGGER_EVAL;
847851
setUint32(this.byteConfig.littleEndian, array, 1, byteLength);
848852

853+
if (index < 0 || index > 65535) {
854+
throw new Error('Invalid scope chain index');
855+
}
856+
857+
setUint32(this.byteConfig.littleEndian, array, header_size, index);
858+
849859
let offset = 0;
850860
let request: Promise<any> = null;
851861
while (offset < arrayLength - 1) {

src/test/JerryProtocolHandler.test.ts

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -454,11 +454,17 @@ suite('JerryProtocolHandler', () => {
454454
};
455455
(handler as any).maxMessageSize = 16;
456456
(handler as any).debuggerClient = debugClient;
457-
handler.evaluate('foo');
457+
handler.evaluate('foo', 0);
458458
assert(debugClient.send.calledOnce);
459+
const expression_length = [8, 0, 0, 0]; // length of chain index + eval subtype + length of code (4 + 1 + 3)
460+
const scope_chain_index = [0, 0, 0, 0];
461+
// chain index + eval subtype + code
462+
const expression = [...scope_chain_index,
463+
SP.EVAL_SUBTYPE.JERRY_DEBUGGER_EVAL_EVAL.charCodeAt(0),
464+
'f'.charCodeAt(0), 'o'.charCodeAt(0), 'o'.charCodeAt(0)];
465+
459466
assert.deepStrictEqual(debugClient.send.args[0][0], Uint8Array.from([
460-
SP.CLIENT.JERRY_DEBUGGER_EVAL, 4, 0, 0, 0, 0,
461-
'f'.charCodeAt(0), 'o'.charCodeAt(0), 'o'.charCodeAt(0),
467+
SP.CLIENT.JERRY_DEBUGGER_EVAL, ...expression_length, ...expression
462468
]));
463469
});
464470

@@ -473,17 +479,25 @@ suite('JerryProtocolHandler', () => {
473479
};
474480
(handler as any).maxMessageSize = 6;
475481
(handler as any).debuggerClient = debugClient;
476-
handler.evaluate('foobar');
482+
handler.evaluate('foobar', 0);
483+
const expression_length = [11, 0, 0, 0]; // length of chain index + eval subtype + length of code (4 + 1 + 6)
484+
const scope_chain_index = [0, 0, 0, 0];
485+
486+
// chain index + eval subtype + code
487+
const expression = [...scope_chain_index,
488+
SP.EVAL_SUBTYPE.JERRY_DEBUGGER_EVAL_EVAL.charCodeAt(0),
489+
'f'.charCodeAt(0), 'o'.charCodeAt(0), 'o'.charCodeAt(0),
490+
'b'.charCodeAt(0), 'a'.charCodeAt(0), 'r'.charCodeAt(0)];
491+
477492
assert(debugClient.send.calledThrice);
478493
assert.deepStrictEqual(debugClient.send.args[0][0], Uint8Array.from([
479-
SP.CLIENT.JERRY_DEBUGGER_EVAL, 7, 0, 0, 0, 0,
494+
SP.CLIENT.JERRY_DEBUGGER_EVAL, ...expression_length, expression[0],
480495
]));
481496
assert.deepStrictEqual(debugClient.send.args[1][0], Uint8Array.from([
482-
SP.CLIENT.JERRY_DEBUGGER_EVAL_PART, 'f'.charCodeAt(0), 'o'.charCodeAt(0), 'o'.charCodeAt(0),
483-
'b'.charCodeAt(0), 'a'.charCodeAt(0),
497+
SP.CLIENT.JERRY_DEBUGGER_EVAL_PART, ...expression.slice(1, 6)
484498
]));
485499
assert.deepStrictEqual(debugClient.send.args[2][0], Uint8Array.from([
486-
SP.CLIENT.JERRY_DEBUGGER_EVAL_PART, 'r'.charCodeAt(0),
500+
SP.CLIENT.JERRY_DEBUGGER_EVAL_PART, ...expression.slice(6, 11)
487501
]));
488502
});
489503
});

0 commit comments

Comments
 (0)