Skip to content

Commit

Permalink
Support SetVariable request (#32)
Browse files Browse the repository at this point in the history
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]
  • Loading branch information
robertsipka authored Nov 16, 2018
1 parent c82b8f2 commit 4aa3daf
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 13 deletions.
26 changes: 25 additions & 1 deletion src/IotjsDebugger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ class IotjsDebugSession extends DebugSession {
response.body.supportsStepBack = false;
response.body.supportsRestartRequest = true;
response.body.supportsDelayedStackTraceLoading = true;
response.body.supportsSetVariable = true;

this._sourceSendingOptions = <SourceSendingOptions>{
contextReset: false,
Expand Down Expand Up @@ -409,7 +410,7 @@ class IotjsDebugSession extends DebugSession {
response: DebugProtocol.EvaluateResponse, args: DebugProtocol.EvaluateArguments
): Promise<void> {
try {
const result: JerryEvalResult = await this._protocolhandler.evaluate(args.expression);
const result: JerryEvalResult = await this._protocolhandler.evaluate(args.expression, 0);
const value: string = result.subtype === EVAL_RESULT_SUBTYPE.JERRY_DEBUGGER_EVAL_OK
? result.value
: 'Evaluate Error';
Expand Down Expand Up @@ -486,6 +487,7 @@ class IotjsDebugSession extends DebugSession {

for (const variable of scopeVariables) {
variables.push({name: variable.name,
evaluateName: variable.name,
type: variable.type,
value: variable.value,
variablesReference: 0});
Expand All @@ -501,6 +503,28 @@ class IotjsDebugSession extends DebugSession {
}
}

protected async setVariableRequest(response: DebugProtocol.SetVariableResponse,
args: DebugProtocol.SetVariableArguments
): Promise <void> {
try {
const expression = args.name + '=' + args.value;
const scope_index = Number(this._variableHandles.get(args.variablesReference));
const result: JerryEvalResult = await this._protocolhandler.evaluate(expression, scope_index);
const value: string = result.subtype === EVAL_RESULT_SUBTYPE.JERRY_DEBUGGER_EVAL_OK
? result.value
: 'Evaluate Error';

response.body = {
value: value,
variablesReference: 0
};
this.sendResponse(response);
} catch (error) {
this.log(error.message, LOG_LEVEL.ERROR);
this.sendErrorResponse(response, 0, (<Error>error).message);
}
}

protected customRequest(command: string, response: DebugProtocol.Response, args: any): void {
switch (command) {
case 'sendSource': {
Expand Down
2 changes: 1 addition & 1 deletion src/JerryProtocolConstants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
'use strict';

// Expected JerryScript debugger protocol version.
export const JERRY_DEBUGGER_VERSION = 7;
export const JERRY_DEBUGGER_VERSION = 8;

// Packages sent from the server to the client.
export enum SERVER {
Expand Down
16 changes: 13 additions & 3 deletions src/JerryProtocolHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -832,20 +832,30 @@ export class JerryDebugProtocolHandler {
return bps.length ? bps.filter(b => b.func.name !== '') : [];
}

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

this.evalsPending++;

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

if (index < 0 || index > 65535) {
throw new Error('Invalid scope chain index');
}

setUint32(this.byteConfig.littleEndian, array, header_size, index);

let offset = 0;
let request: Promise<any> = null;
while (offset < arrayLength - 1) {
Expand Down
30 changes: 22 additions & 8 deletions src/test/JerryProtocolHandler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -454,11 +454,17 @@ suite('JerryProtocolHandler', () => {
};
(handler as any).maxMessageSize = 16;
(handler as any).debuggerClient = debugClient;
handler.evaluate('foo');
handler.evaluate('foo', 0);
assert(debugClient.send.calledOnce);
const expression_length = [8, 0, 0, 0]; // length of chain index + eval subtype + length of code (4 + 1 + 3)
const scope_chain_index = [0, 0, 0, 0];
// chain index + eval subtype + code
const expression = [...scope_chain_index,
SP.EVAL_SUBTYPE.JERRY_DEBUGGER_EVAL_EVAL.charCodeAt(0),
'f'.charCodeAt(0), 'o'.charCodeAt(0), 'o'.charCodeAt(0)];

assert.deepStrictEqual(debugClient.send.args[0][0], Uint8Array.from([
SP.CLIENT.JERRY_DEBUGGER_EVAL, 4, 0, 0, 0, 0,
'f'.charCodeAt(0), 'o'.charCodeAt(0), 'o'.charCodeAt(0),
SP.CLIENT.JERRY_DEBUGGER_EVAL, ...expression_length, ...expression
]));
});

Expand All @@ -473,17 +479,25 @@ suite('JerryProtocolHandler', () => {
};
(handler as any).maxMessageSize = 6;
(handler as any).debuggerClient = debugClient;
handler.evaluate('foobar');
handler.evaluate('foobar', 0);
const expression_length = [11, 0, 0, 0]; // length of chain index + eval subtype + length of code (4 + 1 + 6)
const scope_chain_index = [0, 0, 0, 0];

// chain index + eval subtype + code
const expression = [...scope_chain_index,
SP.EVAL_SUBTYPE.JERRY_DEBUGGER_EVAL_EVAL.charCodeAt(0),
'f'.charCodeAt(0), 'o'.charCodeAt(0), 'o'.charCodeAt(0),
'b'.charCodeAt(0), 'a'.charCodeAt(0), 'r'.charCodeAt(0)];

assert(debugClient.send.calledThrice);
assert.deepStrictEqual(debugClient.send.args[0][0], Uint8Array.from([
SP.CLIENT.JERRY_DEBUGGER_EVAL, 7, 0, 0, 0, 0,
SP.CLIENT.JERRY_DEBUGGER_EVAL, ...expression_length, expression[0],
]));
assert.deepStrictEqual(debugClient.send.args[1][0], Uint8Array.from([
SP.CLIENT.JERRY_DEBUGGER_EVAL_PART, 'f'.charCodeAt(0), 'o'.charCodeAt(0), 'o'.charCodeAt(0),
'b'.charCodeAt(0), 'a'.charCodeAt(0),
SP.CLIENT.JERRY_DEBUGGER_EVAL_PART, ...expression.slice(1, 6)
]));
assert.deepStrictEqual(debugClient.send.args[2][0], Uint8Array.from([
SP.CLIENT.JERRY_DEBUGGER_EVAL_PART, 'r'.charCodeAt(0),
SP.CLIENT.JERRY_DEBUGGER_EVAL_PART, ...expression.slice(6, 11)
]));
});
});
Expand Down

0 comments on commit 4aa3daf

Please sign in to comment.