3 * Copyright (c) 2015, Blue Static <https://www.bluestatic.org>
5 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU
6 * General Public License as published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
10 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along with this program; if not,
14 * write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
17 #import "DebuggerModel.h"
19 #import "StackFrame.h"
21 @implementation DebuggerModel
{
22 NSMutableArray
* _stack
;
25 - (instancetype
)init
{
26 if (self = [super init
]) {
27 _stack
= [NSMutableArray
new];
38 - (NSUInteger
)stackDepth
{
39 return self.stack.count
;
42 - (void)onNewConnection
{
44 [_stack removeAllObjects
];
47 - (void)updateStack
:(NSArray
<StackFrame
*>*)newStack
{
48 // Iterate, in reverse order from the bottom to the top, both stacks to find
49 // the point of divergence.
50 NSEnumerator
* itNewStack
= [newStack reverseObjectEnumerator
];
51 NSEnumerator
* itOldStack
= [self.stack reverseObjectEnumerator
];
54 StackFrame
* frameOld
= [itOldStack nextObject
];
55 NSUInteger oldStackOffset
= self.stack.count
;
56 while (frameNew
= [itNewStack nextObject
]) {
57 if ([frameNew isEqual
:frameOld
]) {
59 frameOld
= [itOldStack nextObject
];
65 [self willChangeValueForKey
:@
"stack"];
67 // Remove any frames from the top of the stack that are not shared with the
69 [_stack removeObjectsInRange
:NSMakeRange(0, oldStackOffset
)];
71 // Continue inserting objects to update the stack with the new frames.
73 [_stack insertObject
:frameNew atIndex
:0];
74 frameNew
= [itNewStack nextObject
];
77 // Renumber the stack.
78 for (NSUInteger i
= 0; i
< self.stack.count
; ++i
)
79 self.stack
[i
].index
= i
;
81 [self didChangeValueForKey
:@
"stack"];