Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

avoid AspectPositionAfter | AspectPositionInstead or `AspectPositio… #86

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 5 additions & 4 deletions Aspects.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
#import <Foundation/Foundation.h>

typedef NS_OPTIONS(NSUInteger, AspectOptions) {
AspectPositionAfter = 0, /// Called after the original implementation (default)
AspectPositionInstead = 1, /// Will replace the original implementation.
AspectPositionBefore = 2, /// Called before the original implementation.
AspectPositionAfter = 1 << 0, /// Called after the original implementation (default)
AspectPositionInstead = 1 << 1, /// Will replace the original implementation.
AspectPositionBefore = 1 << 2, /// Called before the original implementation.

AspectOptionAutomaticRemoval = 1 << 3 /// Will remove the hook after the first execution.
AspectOptionAutomaticRemoval = 1 << 3 /// Will remove the hook after the first execution.
};

/// Opaque Aspect Token that allows to deregister the hook.
Expand Down Expand Up @@ -71,6 +71,7 @@ typedef NS_OPTIONS(NSUInteger, AspectOptions) {
typedef NS_ENUM(NSUInteger, AspectErrorCode) {
AspectErrorSelectorBlacklisted, /// Selectors like release, retain, autorelease are blacklisted.
AspectErrorDoesNotRespondToSelector, /// Selector could not be found.
AspectErrorOptionsValue, /// Options value should less than 0xc(AspectPositionBefore | AspectOptionAutomaticRemoval).
AspectErrorSelectorDeallocPosition, /// When hooking dealloc, only AspectPositionBefore is allowed.
AspectErrorSelectorAlreadyHookedInClassHierarchy, /// Statically hooking the same method in subclasses is not allowed.
AspectErrorFailedToAllocateClassPair, /// The runtime failed creating a class pair.
Expand Down
28 changes: 21 additions & 7 deletions Aspects.m
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ @interface NSInvocation (Aspects)
- (NSArray *)aspects_arguments;
@end

#define AspectPositionFilter 0x07
#define AspectPositionFilter 0xc

#define AspectError(errorCode, errorDescription) do { \
AspectLogError(@"Aspects: %@", errorDescription); \
Expand Down Expand Up @@ -465,7 +465,7 @@ static void aspect_undoSwizzleClassInPlace(Class klass) {
#define aspect_invoke(aspects, info) \
for (AspectIdentifier *aspect in aspects) {\
[aspect invokeWithInfo:info];\
if (aspect.options & AspectOptionAutomaticRemoval) { \
if (aspect.options >= AspectOptionAutomaticRemoval) { \
aspectsToRemove = [aspectsToRemove?:@[] arrayByAddingObject:aspect]; \
} \
}
Expand Down Expand Up @@ -581,7 +581,13 @@ static BOOL aspect_isSelectorAllowedAndTrack(NSObject *self, SEL selector, Aspec
}

// Additional checks.
AspectOptions position = options&AspectPositionFilter;
if (options > AspectPositionFilter) {
NSString *errorDesc = @"AspectOptions max value is AspectPositionBefore | AspectOptionAutomaticRemoval";
AspectError(AspectErrorOptionsValue, errorDesc);
return NO;
}

AspectOptions position = options % AspectOptionAutomaticRemoval;
if ([selectorName isEqualToString:@"dealloc"] && position != AspectPositionBefore) {
NSString *errorDesc = @"AspectPositionBefore is the only valid position when hooking dealloc.";
AspectError(AspectErrorSelectorDeallocPosition, errorDesc);
Expand Down Expand Up @@ -887,11 +893,19 @@ - (BOOL)hasAspects {

- (void)addAspect:(AspectIdentifier *)aspect withOptions:(AspectOptions)options {
NSParameterAssert(aspect);
NSUInteger position = options&AspectPositionFilter;
NSUInteger position = options % AspectOptionAutomaticRemoval;

switch (position) {
case AspectPositionBefore: self.beforeAspects = [(self.beforeAspects ?:@[]) arrayByAddingObject:aspect]; break;
case AspectPositionInstead: self.insteadAspects = [(self.insteadAspects?:@[]) arrayByAddingObject:aspect]; break;
case AspectPositionAfter: self.afterAspects = [(self.afterAspects ?:@[]) arrayByAddingObject:aspect]; break;
case AspectPositionBefore:
self.beforeAspects = [(self.beforeAspects ?:@[]) arrayByAddingObject:aspect];
break;
case AspectPositionInstead:
self.insteadAspects = [(self.insteadAspects?:@[]) arrayByAddingObject:aspect];
break;
case AspectPositionAfter:
case 0:// AspectOptionAutomaticRemoval
self.afterAspects = [(self.afterAspects ?:@[]) arrayByAddingObject:aspect];
break;
}
}

Expand Down