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