You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
3.8 KiB

  1. //
  2. // FLEXMethodCallingViewController.m
  3. // Flipboard
  4. //
  5. // Created by Ryan Olson on 5/23/14.
  6. // Copyright (c) 2014 Flipboard. All rights reserved.
  7. //
  8. #import "FLEXMethodCallingViewController.h"
  9. #import "FLEXRuntimeUtility.h"
  10. #import "FLEXFieldEditorView.h"
  11. #import "FLEXObjectExplorerFactory.h"
  12. #import "FLEXObjectExplorerViewController.h"
  13. #import "FLEXArgumentInputView.h"
  14. #import "FLEXArgumentInputViewFactory.h"
  15. #import "FLEXUtility.h"
  16. @interface FLEXMethodCallingViewController ()
  17. @property (nonatomic) Method method;
  18. @property (nonatomic) FLEXTypeEncoding *returnType;
  19. @end
  20. @implementation FLEXMethodCallingViewController
  21. - (id)initWithTarget:(id)target method:(Method)method
  22. {
  23. self = [super initWithTarget:target];
  24. if (self) {
  25. self.method = method;
  26. self.returnType = [FLEXRuntimeUtility returnTypeForMethod:method];
  27. self.title = [self isClassMethod] ? @"Class Method" : @"Method";
  28. }
  29. return self;
  30. }
  31. - (void)viewDidLoad
  32. {
  33. [super viewDidLoad];
  34. NSString *returnType = @((const char *)self.returnType);
  35. NSString *methodDescription = [FLEXRuntimeUtility prettyNameForMethod:self.method isClassMethod:[self isClassMethod]];
  36. NSString *format = @"Signature:\n%@\n\nReturn Type:\n%@";
  37. NSString *info = [NSString stringWithFormat:format, methodDescription, returnType];
  38. self.fieldEditorView.fieldDescription = info;
  39. NSArray<NSString *> *methodComponents = [FLEXRuntimeUtility prettyArgumentComponentsForMethod:self.method];
  40. NSMutableArray<FLEXArgumentInputView *> *argumentInputViews = [NSMutableArray array];
  41. unsigned int argumentIndex = kFLEXNumberOfImplicitArgs;
  42. for (NSString *methodComponent in methodComponents) {
  43. char *argumentTypeEncoding = method_copyArgumentType(self.method, argumentIndex);
  44. FLEXArgumentInputView *inputView = [FLEXArgumentInputViewFactory argumentInputViewForTypeEncoding:argumentTypeEncoding];
  45. free(argumentTypeEncoding);
  46. inputView.backgroundColor = self.view.backgroundColor;
  47. inputView.title = methodComponent;
  48. [argumentInputViews addObject:inputView];
  49. argumentIndex++;
  50. }
  51. self.fieldEditorView.argumentInputViews = argumentInputViews;
  52. }
  53. - (void)dealloc
  54. {
  55. free(self.returnType);
  56. self.returnType = NULL;
  57. }
  58. - (BOOL)isClassMethod
  59. {
  60. return self.target && self.target == [self.target class];
  61. }
  62. - (NSString *)titleForActionButton
  63. {
  64. return @"Call";
  65. }
  66. - (void)actionButtonPressed:(id)sender
  67. {
  68. [super actionButtonPressed:sender];
  69. NSMutableArray *arguments = [NSMutableArray array];
  70. for (FLEXArgumentInputView *inputView in self.fieldEditorView.argumentInputViews) {
  71. id argumentValue = inputView.inputValue;
  72. if (!argumentValue) {
  73. // Use NSNulls as placeholders in the array. They will be interpreted as nil arguments.
  74. argumentValue = [NSNull null];
  75. }
  76. [arguments addObject:argumentValue];
  77. }
  78. NSError *error = nil;
  79. id returnedObject = [FLEXRuntimeUtility performSelector:method_getName(self.method) onObject:self.target withArguments:arguments error:&error];
  80. if (error) {
  81. [FLEXAlert showAlert:@"Method Call Failed" message:[error localizedDescription] from:self];
  82. } else if (returnedObject) {
  83. // For non-nil (or void) return types, push an explorer view controller to display the returned object
  84. returnedObject = [FLEXRuntimeUtility potentiallyUnwrapBoxedPointer:returnedObject type:self.returnType];
  85. FLEXObjectExplorerViewController *explorerViewController = [FLEXObjectExplorerFactory explorerViewControllerForObject:returnedObject];
  86. [self.navigationController pushViewController:explorerViewController animated:YES];
  87. } else {
  88. [self exploreObjectOrPopViewController:returnedObject];
  89. }
  90. }
  91. @end