17
17
'use strict' ;
18
18
19
19
import {
20
- LoggingDebugSession , DebugSession , Logger , logger , InitializedEvent , OutputEvent , Thread , Source ,
21
- StoppedEvent , ContinuedEvent , StackFrame , TerminatedEvent , Breakpoint as AdapterBreakpoint
20
+ DebugSession , InitializedEvent , OutputEvent , Thread , Source ,
21
+ StoppedEvent , ContinuedEvent , StackFrame , TerminatedEvent , Breakpoint as AdapterBreakpoint , Event , ErrorDestination
22
22
} from 'vscode-debugadapter' ;
23
23
import { DebugProtocol } from 'vscode-debugprotocol' ;
24
24
import * as Fs from 'fs' ;
25
25
import * as Path from 'path' ;
26
- import { IAttachRequestArguments } from './IotjsDebuggerInterfaces' ;
26
+ import { IAttachRequestArguments , SourceSendingOptions } from './IotjsDebuggerInterfaces' ;
27
27
import { JerryDebuggerClient , JerryDebuggerOptions } from './JerryDebuggerClient' ;
28
28
import {
29
29
JerryDebugProtocolDelegate , JerryDebugProtocolHandler , JerryMessageScriptParsed , JerryEvalResult ,
30
30
JerryMessageExceptionHit
31
31
} from './JerryProtocolHandler' ;
32
- import { EVAL_RESULT_SUBTYPE } from './JerryProtocolConstants' ;
32
+ import { EVAL_RESULT_SUBTYPE , CLIENT as CLIENT_PACKAGE } from './JerryProtocolConstants' ;
33
33
34
- class IotjsDebugSession extends LoggingDebugSession {
34
+ enum SOURCE_SENDING_STATES {
35
+ NOP = 0 ,
36
+ WAITING = 1 ,
37
+ IN_PROGRESS = 2 ,
38
+ LAST_SENT = 3
39
+ }
40
+
41
+ class IotjsDebugSession extends DebugSession {
35
42
36
43
// We don't support multiple threads, so we can use a hardcoded ID for the default thread
37
44
private static THREAD_ID = 1 ;
@@ -40,15 +47,14 @@ class IotjsDebugSession extends LoggingDebugSession {
40
47
private _debugLog : boolean = false ;
41
48
private _debuggerClient : JerryDebuggerClient ;
42
49
private _protocolhandler : JerryDebugProtocolHandler ;
50
+ private _sourceSendingOptions : SourceSendingOptions ;
43
51
44
52
public constructor ( ) {
45
- super ( 'iotjs-debug.txt' ) ;
53
+ super ( ) ;
46
54
47
55
// The debugger uses zero-based lines and columns.
48
56
this . setDebuggerLinesStartAt1 ( false ) ;
49
57
this . setDebuggerColumnsStartAt1 ( false ) ;
50
-
51
- logger . setup ( Logger . LogLevel . Verbose , /*logToFile=*/ false ) ;
52
58
}
53
59
54
60
protected threadsRequest ( response : DebugProtocol . ThreadsResponse ) : void {
@@ -79,6 +85,11 @@ class IotjsDebugSession extends LoggingDebugSession {
79
85
response . body . supportsStepBack = false ;
80
86
response . body . supportsRestartRequest = true ;
81
87
88
+ this . _sourceSendingOptions = < SourceSendingOptions > {
89
+ contextReset : false ,
90
+ state : SOURCE_SENDING_STATES . NOP
91
+ } ;
92
+
82
93
this . sendResponse ( response ) ;
83
94
}
84
95
@@ -140,23 +151,14 @@ class IotjsDebugSession extends LoggingDebugSession {
140
151
141
152
const onWaitForSource = async ( ) => {
142
153
this . log ( 'onWaitForSource' ) ;
143
-
144
- if ( args . program !== '' && args . program !== '\0' ) {
145
- if ( Fs . existsSync ( `${ args . localRoot } /${ args . program } ` ) ) {
146
- const content = Fs . readFileSync ( `${ args . localRoot } /${ args . program } ` , {
147
- encoding : 'utf8' ,
148
- flag : 'r'
149
- } ) ;
150
- await this . _protocolhandler . sendClientSource ( args . program , content )
151
- . then ( ( ) => this . log ( 'Source has been sended to the engine.' ) )
152
- . catch ( error => {
153
- this . sendErrorResponse ( response , 0 , error ) ;
154
- } ) ;
155
- } else {
156
- this . sendErrorResponse ( response , 0 , 'You must provide a valid path to source' ) ;
154
+ if ( this . _sourceSendingOptions . state === SOURCE_SENDING_STATES . NOP ) {
155
+ this . _sourceSendingOptions . state = SOURCE_SENDING_STATES . WAITING ;
156
+ this . sendEvent ( new Event ( 'waitForSource' ) ) ;
157
+ } else if ( this . _sourceSendingOptions . state === SOURCE_SENDING_STATES . LAST_SENT ) {
158
+ if ( ! this . _sourceSendingOptions . contextReset ) {
159
+ this . _sourceSendingOptions . state = SOURCE_SENDING_STATES . NOP ;
160
+ this . _protocolhandler . sendClientSourceControl ( CLIENT_PACKAGE . JERRY_DEBUGGER_NO_MORE_SOURCES ) ;
157
161
}
158
- } else {
159
- this . sendErrorResponse ( response , 0 , 'You must provide a source' ) ;
160
162
}
161
163
} ;
162
164
@@ -180,10 +182,15 @@ class IotjsDebugSession extends LoggingDebugSession {
180
182
this . _protocolhandler . debuggerClient = this . _debuggerClient ;
181
183
182
184
this . _debuggerClient . connect ( )
183
- . then ( ( ) => this . log ( `Connected to: ${ args . address } :${ args . port } ` ) )
184
- . catch ( error => this . log ( error ) ) ;
185
+ . then ( ( ) => {
186
+ this . log ( `Connected to: ${ args . address } :${ args . port } ` ) ;
187
+ this . sendResponse ( response ) ;
188
+ } )
189
+ . catch ( error => {
190
+ this . log ( error ) ;
191
+ this . sendErrorResponse ( response , 0 , error . message ) ;
192
+ } ) ;
185
193
186
- this . sendResponse ( response ) ;
187
194
this . sendEvent ( new InitializedEvent ( ) ) ;
188
195
}
189
196
@@ -349,6 +356,30 @@ class IotjsDebugSession extends LoggingDebugSession {
349
356
. catch ( error => this . sendErrorResponse ( response , 0 , error ) ) ;
350
357
}
351
358
359
+ protected customRequest ( command : string , response : DebugProtocol . Response , args : any ) : void {
360
+ this . log ( 'customRequest' ) ;
361
+
362
+ switch ( command ) {
363
+ case 'sendSource' : {
364
+ this . _sourceSendingOptions . state = SOURCE_SENDING_STATES . IN_PROGRESS ;
365
+ this . _protocolhandler . sendClientSource ( args . program . name , args . program . source )
366
+ . then ( ( ) => {
367
+ this . log ( 'Source has been sent to the engine.' ) ;
368
+ this . _sourceSendingOptions . state = SOURCE_SENDING_STATES . LAST_SENT ;
369
+ this . sendResponse ( response ) ;
370
+ } )
371
+ . catch ( error => {
372
+ this . log ( error ) ;
373
+ this . _sourceSendingOptions . state = SOURCE_SENDING_STATES . NOP ;
374
+ this . sendErrorResponse ( response , 0 , error , null , ErrorDestination . User ) ;
375
+ } ) ;
376
+ return ;
377
+ }
378
+ default :
379
+ super . customRequest ( command , response , args ) ;
380
+ }
381
+ }
382
+
352
383
private handleSource ( data : JerryMessageScriptParsed ) : void {
353
384
const path = `${ this . _args . localRoot } /${ this . pathToBasename ( data . name ) } ` ;
354
385
const src = this . _protocolhandler . getSource ( data . id ) ;
0 commit comments