-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathDebugServices.ts
139 lines (123 loc) · 5.51 KB
/
DebugServices.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Copyright 2024-2025 The MathWorks, Inc.
import { IMVM } from '../mvm/impl/MVM'
import EventEmitter from 'events';
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type MatlabData = any;
export class BreakpointInfo {
filePath: string;
lineNumber: number;
anonymousFunctionIndex: number;
condition?: string;
enabled: boolean;
constructor (filePath: string, lineNumber: number, condition: string | undefined, anonymousFunctionIndex: number) {
this.filePath = filePath;
this.lineNumber = lineNumber;
this.condition = condition === '' ? undefined : condition;
this.anonymousFunctionIndex = 0;
this.enabled = condition === undefined || !(condition === 'false' || /$false && \(.*\)^/.test(condition));
}
equals (other: BreakpointInfo, ignoreCondition: boolean): boolean {
const result = other.filePath === this.filePath && other.lineNumber === this.lineNumber && other.anonymousFunctionIndex === this.anonymousFunctionIndex;
if (!result || ignoreCondition) {
return result;
} else {
return this.condition === other.condition;
}
}
}
export enum GlobalBreakpointType {
ERROR = 'ERROR',
CAUGHT_ERROR = 'CAUGHT_ERROR',
WARNING = 'WARNING',
NAN_INF = 'NAN_INF'
}
export class GlobalBreakpointInfo {
static TypeMap: {[index: string | number]: GlobalBreakpointType} = {
0: GlobalBreakpointType.ERROR,
1: GlobalBreakpointType.CAUGHT_ERROR,
2: GlobalBreakpointType.WARNING,
3: GlobalBreakpointType.NAN_INF,
error: GlobalBreakpointType.ERROR,
'caught error': GlobalBreakpointType.CAUGHT_ERROR,
warning: GlobalBreakpointType.WARNING,
naninf: GlobalBreakpointType.NAN_INF,
[GlobalBreakpointType.ERROR]: GlobalBreakpointType.ERROR,
[GlobalBreakpointType.CAUGHT_ERROR]: GlobalBreakpointType.CAUGHT_ERROR,
[GlobalBreakpointType.WARNING]: GlobalBreakpointType.WARNING,
[GlobalBreakpointType.NAN_INF]: GlobalBreakpointType.NAN_INF
}
type: GlobalBreakpointType;
identifiers: string[];
constructor (type: GlobalBreakpointType, identifiers: string[]) {
this.type = type;
this.identifiers = identifiers;
}
}
enum Events {
DBEnter = 'DBEnter',
DBStop = 'DBStop',
DBExit = 'DBExit',
DBCont = 'DBCont',
DBWorkspaceChanged = 'DBWorkspaceChanged',
BreakpointAdded = 'BreakpointAdded',
BreakpointRemoved = 'BreakpointRemoved',
BreakpointsCleared = 'BreakpointsCleared',
GlobalBreakpointAdded = 'GlobalBreakpointAdded',
GlobalBreakpointRemoved = 'GlobalBreakpointRemoved'
}
export class DebugServices extends EventEmitter {
static Events = Events;
private readonly _mvm: IMVM;
constructor (mvm: IMVM) {
super();
this._mvm = mvm;
this._setupListeners();
}
private _setupListeners (): void {
this._mvm.on('EnterDebuggerEvent', this._handleEnterEvent.bind(this));
this._mvm.on('EnterDebuggerWithWarningEvent', this._handleEnterEvent.bind(this));
this._mvm.on('ExitDebuggerEvent', this._handleExitEvent.bind(this));
this._mvm.on('ContinueExecutionEvent', (data: MatlabData) => {
this.emit(DebugServices.Events.DBCont);
});
this._mvm.on('ChangeCurrentWorkspaceEvent', (data: MatlabData) => {
this.emit(DebugServices.Events.DBWorkspaceChanged);
});
this._mvm.on('AddLineNumberBreakpointEvent', (data: MatlabData) => {
this.emit(DebugServices.Events.BreakpointAdded, new BreakpointInfo(data.Filespec, data.LineNumber, data.Condition, data.whichAnonymousFunctionOnCurrentLine));
});
this._mvm.on('DeleteLineNumberBreakpointEvent', (data: MatlabData) => {
this.emit(DebugServices.Events.BreakpointRemoved, new BreakpointInfo(data.Filespec, data.LineNumber, data.Condition, data.whichAnonymousFunctionOnCurrentLine));
});
this._mvm.on('DeleteAllBreakpointsEvent', () => {
this.emit(DebugServices.Events.BreakpointsCleared);
});
this._mvm.on('AddProgramWideBreakpointEvent', (data: MatlabData) => {
this.emit(DebugServices.Events.GlobalBreakpointAdded, new GlobalBreakpointInfo(GlobalBreakpointInfo.TypeMap[data.programWideTag], data.messageIdentifier === 'all' ? [] : [data.messageIdentifier]));
});
this._mvm.on('DeleteProgramWideBreakpointEvent', (data: MatlabData) => {
this.emit(DebugServices.Events.GlobalBreakpointRemoved, new GlobalBreakpointInfo(GlobalBreakpointInfo.TypeMap[data.programWideTag], data.messageIdentifier === 'all' ? [] : [data.messageIdentifier]));
});
}
private _isSessionLevelEvent (eventData: MatlabData): boolean {
if (this._mvm.getMatlabRelease() === 'R2021b') {
return eventData.DebugNestLevel >= 3;
} else {
return eventData.DebugNestLevel === 2;
}
}
private _handleEnterEvent (data: MatlabData): void {
if (this._isSessionLevelEvent(data)) {
this.emit(DebugServices.Events.DBEnter);
} else {
const filepath = data.Filespec;
const lineNumber = (data.IsAtEndOfFunction as boolean) ? -data.LineNumber : data.LineNumber;
this.emit(DebugServices.Events.DBStop, filepath, lineNumber, data.Stack ?? []);
}
}
private _handleExitEvent (data: MatlabData): void {
if (this._isSessionLevelEvent(data)) {
this.emit(DebugServices.Events.DBExit, data.IsDebuggerActive);
}
}
}