Rewrite parts of DebuggerBackEnd to use the new block-based ProtocolClient.
authorRobert Sesek <rsesek@bluestatic.org>
Sat, 10 Oct 2015 16:16:58 +0000 (12:16 -0400)
committerRobert Sesek <rsesek@bluestatic.org>
Sat, 10 Oct 2015 17:17:16 +0000 (13:17 -0400)
This removes the need for callbackContext_.

Source/DebuggerBackEnd.h
Source/DebuggerBackEnd.m

index 55963a313afb79b2ea3043925ba29e9bd557f7c4..7c2839cce8eaf8353c10f5e7b0a63efc57f5c61d 100644 (file)
   // returns a response to the debugger, we will dispatch the response XML to
   // the selector, based on transaction_id.
   NSMutableDictionary* callTable_;
-
-  // This stores additional context information for the callback selector.
-  // This dictionary is keyed by the same transaction IDs in |callTable_|, but
-  // also stores some other object that can be accessed in the callback.
-  NSMutableDictionary* callbackContext_;
 }
 
 @property (readonly, copy) NSString* status;
index 942eeb7ef6f5cfad7b30db8f55bcd7ec5d07044e..a06066deaa96bdb3aa977db834298abfc27587a9 100644 (file)
@@ -31,9 +31,6 @@
 - (void)debuggerStep:(NSXMLDocument*)response;
 - (void)rebuildStack:(NSXMLDocument*)response;
 - (void)getStackFrame:(NSXMLDocument*)response;
-- (void)setSource:(NSXMLDocument*)response;
-- (void)contextsReceived:(NSXMLDocument*)response;
-- (void)variablesReceived:(NSXMLDocument*)response;
 - (void)propertiesReceived:(NSXMLDocument*)response;
 - (void)evalScriptReceived:(NSXMLDocument*)response;
 
@@ -56,7 +53,6 @@
   if (self = [super init])
   {
     stackFrames_ = [[NSMutableDictionary alloc] init];
-    callbackContext_ = [NSMutableDictionary new];
     callTable_ = [NSMutableDictionary new];
 
     [[BreakpointManager sharedManager] setConnection:self];
@@ -79,7 +75,6 @@
   [client_ release];
   [stackFrames_ release];
   [callTable_ release];
-  [callbackContext_ release];
   [super dealloc];
 }
 
 /**
  * Tells the debugger to continue running the script. Returns the current stack frame.
  */
-- (void)run
-{
-  NSNumber* tx = [client_ sendCommandWithFormat:@"run"];
-  [self recordCallback:@selector(debuggerStep:) forTransaction:tx];
+- (void)run {
+  [client_ sendCommandWithFormat:@"run" handler:^(NSXMLDocument* message) {
+    [self debuggerStep:message];
+  }];
 }
 
 /**
 /**
  * Tells the debugger to step out of the current context
  */
-- (void)stepOut
-{
-  NSNumber* tx = [client_ sendCommandWithFormat:@"step_out"];
-  [self recordCallback:@selector(debuggerStep:) forTransaction:tx];
+- (void)stepOut {
+  [client_ sendCommandWithFormat:@"step_out" handler:^(NSXMLDocument* message) {
+    [self debuggerStep:message];
+  }];
 }
 
 /**
  * Tells the debugger to step over the current function
  */
-- (void)stepOver
-{
-  NSNumber* tx = [client_ sendCommandWithFormat:@"step_over"];
-  [self recordCallback:@selector(debuggerStep:) forTransaction:tx];
+- (void)stepOver {
+  [client_ sendCommandWithFormat:@"step_over" handler:^(NSXMLDocument* message) {
+    [self debuggerStep:message];
+  }];
 }
 
 /**
   if (frame.loaded)
     return;
 
-  NSNumber* routingNumber = [NSNumber numberWithInt:frame.routingID];
-  NSNumber* transaction = nil;
-
   // Get the source code of the file. Escape % in URL chars.
   if ([frame.filename length]) {
-    transaction = [client_ sendCommandWithFormat:@"source -f %@", frame.filename];
-    [self recordCallback:@selector(setSource:) forTransaction:transaction];
-    [callbackContext_ setObject:routingNumber forKey:transaction];
+    ProtocolClientMessageHandler handler = ^(NSXMLDocument* message) {
+      int receivedTransaction = [client_ transactionIDFromResponse:message];
+      if (receivedTransaction < stackFirstTransactionID_)
+        return;
+
+      frame.source = [[message rootElement] base64DecodedValue];
+      if ([self.delegate respondsToSelector:@selector(sourceUpdated:)])
+        [self.delegate sourceUpdated:frame];
+    };
+    [client_ sendCommandWithFormat:@"source -f %@" handler:handler, frame.filename];
   }
-  
+
   // Get the names of all the contexts.
-  transaction = [client_ sendCommandWithFormat:@"context_names -d %d", frame.index];
-  [self recordCallback:@selector(contextsReceived:) forTransaction:transaction];
-  [callbackContext_ setObject:routingNumber forKey:transaction];
-  
+  ProtocolClientMessageHandler handler = ^(NSXMLDocument* message) {
+    [self loadContexts:message forFrame:frame];
+  };
+  [client_ sendCommandWithFormat:@"context_names -d %d" handler:handler, frame.index];
+
   // This frame will be fully loaded.
   frame.loaded = YES;
 }
     return;
   
   NSString* file = [ProtocolClient escapedFilePathURI:[bp transformedPath]];
-  NSNumber* tx = [client_ sendCommandWithFormat:@"breakpoint_set -t line -f %@ -n %i", file, [bp line]];
-  [self recordCallback:@selector(breakpointReceived:) forTransaction:tx];
-  [callbackContext_ setObject:bp forKey:tx];
+  ProtocolClientMessageHandler handler = ^(NSXMLDocument* message) {
+    [bp setDebuggerId:[[[[message rootElement] attributeForName:@"id"] stringValue] intValue]];
+  };
+  [client_ sendCommandWithFormat:@"breakpoint_set -t line -f %@ -n %i" handler:handler, file, [bp line]];
 }
 
 /**
     [delegate newStackFrame:frame];
 }
 
-/**
- * Callback for setting the source of a file while rebuilding a specific stack
- * frame.
- */
-- (void)setSource:(NSXMLDocument*)response
-{
-  NSNumber* transaction = [NSNumber numberWithInt:[client_ transactionIDFromResponse:response]];
-  if ([transaction intValue] < stackFirstTransactionID_)
-    return;
-  NSNumber* routingNumber = [callbackContext_ objectForKey:transaction];
-  if (!routingNumber)
-    return;
-  
-  [callbackContext_ removeObjectForKey:transaction];
-  StackFrame* frame = [stackFrames_ objectForKey:routingNumber];
-  if (!frame)
-    return;
-  
-  frame.source = [[response rootElement] base64DecodedValue];
-  
-  if ([delegate respondsToSelector:@selector(sourceUpdated:)])
-    [delegate sourceUpdated:frame];
-}
-
 /**
  * Enumerates all the contexts of a given stack frame. We then in turn get the
  * contents of each one of these contexts.
  */
-- (void)contextsReceived:(NSXMLDocument*)response
-{
-  // Get the stack frame's routing ID and use it again.
-  NSNumber* receivedTransaction = [NSNumber numberWithInt:[client_ transactionIDFromResponse:response]];
-  if ([receivedTransaction intValue] < stackFirstTransactionID_)
+- (void)loadContexts:(NSXMLDocument*)response forFrame:(StackFrame*)frame {
+  int receivedTransaction = [client_ transactionIDFromResponse:response];
+  if (receivedTransaction < stackFirstTransactionID_)
     return;
-  NSNumber* routingID = [callbackContext_ objectForKey:receivedTransaction];
-  if (!routingID)
-    return;
-  
-  // Get the stack frame by the |routingID|.
-  StackFrame* frame = [stackFrames_ objectForKey:routingID];
-  
+
   NSXMLElement* contextNames = [response rootElement];
-  for (NSXMLElement* context in [contextNames children])
-  {
+  for (NSXMLElement* context in [contextNames children]) {
     NSInteger cid = [[[context attributeForName:@"id"] stringValue] intValue];
     
     // Fetch each context's variables.
-    NSNumber* tx = [client_ sendCommandWithFormat:@"context_get -d %d -c %d", frame.index, cid];
-    [self recordCallback:@selector(variablesReceived:) forTransaction:tx];
-    [callbackContext_ setObject:routingID forKey:tx];
+    ProtocolClientMessageHandler handler = ^(NSXMLDocument* message) {
+      NSMutableArray* variables = [NSMutableArray array];
+
+      // Merge the frame's existing variables.
+      if (frame.variables)
+        [variables addObjectsFromArray:frame.variables];
+
+      // Add these new variables.
+      NSArray* addVariables = [[message rootElement] children];
+      if (addVariables) {
+        for (NSXMLElement* elm in addVariables) {
+          VariableNode* node = [[VariableNode alloc] initWithXMLNode:elm];
+          [variables addObject:[node autorelease]];
+        }
+      }
+
+      frame.variables = variables;
+    };
+    [client_ sendCommandWithFormat:@"context_get -d %d -c %d" handler:handler, frame.index, cid];
   }
 }
 
-/**
- * Receives the variables from the context and attaches them to the stack frame.
- */
-- (void)variablesReceived:(NSXMLDocument*)response
-{
-  // Get the stack frame's routing ID and use it again.
-  NSInteger transaction = [client_ transactionIDFromResponse:response];
-  if (transaction < stackFirstTransactionID_)
-    return;
-  NSNumber* receivedTransaction = [NSNumber numberWithInt:transaction];
-  NSNumber* routingID = [callbackContext_ objectForKey:receivedTransaction];
-  if (!routingID)
-    return;
-  
-  // Get the stack frame by the |routingID|.
-  StackFrame* frame = [stackFrames_ objectForKey:routingID];
-
-  NSMutableArray* variables = [NSMutableArray array];
-
-  // Merge the frame's existing variables.
-  if (frame.variables)
-    [variables addObjectsFromArray:frame.variables];
-
-  // Add these new variables.
-  NSArray* addVariables = [[response rootElement] children];
-  if (addVariables) {
-    for (NSXMLElement* elm in addVariables) {
-      VariableNode* node = [[VariableNode alloc] initWithXMLNode:elm];
-      [variables addObject:[node autorelease]];
-    }
-  }
-
-  frame.variables = variables;
-}
-
 /**
  * Callback from a |-getProperty:| request.
  */
   [delegate scriptWasEvaluatedWithResult:value];
 }
 
-/**
- * Callback for setting a breakpoint.
- */
-- (void)breakpointReceived:(NSXMLDocument*)response
-{
-  NSNumber* transaction = [NSNumber numberWithInt:[client_ transactionIDFromResponse:response]];
-  Breakpoint* bp = [callbackContext_ objectForKey:transaction];
-  if (!bp)
-    return;
-  
-  [callbackContext_ removeObjectForKey:callbackContext_];
-  [bp setDebuggerId:[[[[response rootElement] attributeForName:@"id"] stringValue] intValue]];
-}
-
 // Private /////////////////////////////////////////////////////////////////////
 
 - (void)recordCallback:(SEL)callback forTransaction:(NSNumber*)txn