Skip to content

Commit

Permalink
temp change to use objc_weak calls. use squash before submitting
Browse files Browse the repository at this point in the history
  • Loading branch information
dmaclach committed May 2, 2020
1 parent 7b631d1 commit 27c0810
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 12 deletions.
9 changes: 5 additions & 4 deletions Source/OCMock/NSInvocation+OCMAdditions.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#import "NSInvocation+OCMAdditions.h"
#import "OCMFunctionsPrivate.h"
#import "NSMethodSignature+OCMAdditions.h"
#import "NSObject+OCMAdditions.h"


#if (TARGET_OS_OSX && (!defined(__MAC_10_10) || __MAC_OS_X_VERSION_MIN_REQUIRED < __MAC_10_10)) || \
(TARGET_OS_IPHONE && (!defined(__IPHONE_8_0) || __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_8_0))
Expand All @@ -30,6 +30,7 @@ static BOOL OCMObjectIsClass(id object) {
#define object_isClass OCMObjectIsClass
#endif


@implementation NSInvocation(OCMAdditions)

+ (NSInvocation *)invocationForBlock:(id)block withArguments:(NSArray *)arguments
Expand Down Expand Up @@ -65,7 +66,7 @@ - (void)retainObjectArgumentsExcludingObject:(id)objectToExclude
NSMutableArray *retainedArguments = [[NSMutableArray alloc] init];

id target = [self target];
if((target != nil) && (target != objectToExclude) && !object_isClass(target) && ![target _isDeallocating])
if((target != nil) && (target != objectToExclude) && !object_isClass(target) && !OCMIsDeallocating(target))
{
// Bad things will happen if the target is a block since it's not being
// copied. There isn't a very good way to tell if an invocation's target
Expand All @@ -83,7 +84,7 @@ - (void)retainObjectArgumentsExcludingObject:(id)objectToExclude
{
id argument;
[self getArgument:&argument atIndex:index];
if((argument != nil) && (argument != objectToExclude) && ![argument _isDeallocating])
if((argument != nil) && (argument != objectToExclude) && !OCMIsDeallocating(argument))
{
if(OCMIsBlockType(argumentType))
{
Expand All @@ -105,7 +106,7 @@ - (void)retainObjectArgumentsExcludingObject:(id)objectToExclude
{
id returnValue;
[self getReturnValue:&returnValue];
if((returnValue != nil) && (returnValue != objectToExclude) && ![returnValue _isDeallocating])
if((returnValue != nil) && (returnValue != objectToExclude) && !OCMIsDeallocating(returnValue))
{
if(OCMIsBlockType(returnType))
{
Expand Down
6 changes: 0 additions & 6 deletions Source/OCMock/NSObject+OCMAdditions.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,3 @@
+ (void)enumerateMethodsInClass:(Class)aClass usingBlock:(void (^)(Class cls, SEL sel))aBlock;

@end


@interface NSObject (Internals)
// Return YES if an object is in the process of being deallocated.
- (BOOL)_isDeallocating;
@end
21 changes: 21 additions & 0 deletions Source/OCMock/OCMFunctions.m
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ - (void)recordFailureWithDescription:(NSString *)description inFile:(NSString *)
- (void)failWithException:(NSException *)exception;
@end

// From objc/runtime/objc-internal.h
// Only available on macOS 10.11/iOS 9.
extern id objc_initWeakOrNil(id *location, id newObj) __attribute__((weak_import));
extern void objc_destroyWeak(id _Nullable * _Nonnull location) __attribute__((weak_import));

#pragma mark Functions related to ObjC type system

Expand Down Expand Up @@ -406,3 +410,20 @@ void OCMReportFailure(OCMLocation *loc, NSString *description)
}

}

BOOL OCMIsDeallocating(id anObject)
{
if (!objc_initWeakOrNil)
{
// Pre iOS9/macOS10.11 we just assume all is well since we can't check.
return NO;
}
id deallocatingObject = nil;
BOOL isDeallocating = objc_initWeakOrNil(&deallocatingObject, anObject) == nil;
if (deallocatingObject)
{
objc_destroyWeak(&deallocatingObject);
}
return isDeallocating;
}

2 changes: 2 additions & 0 deletions Source/OCMock/OCMFunctionsPrivate.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,5 @@ void OCMSetAssociatedMockForObject(OCClassMockObject *mock, id anObject);
OCPartialMockObject *OCMGetAssociatedMockForObject(id anObject);

void OCMReportFailure(OCMLocation *loc, NSString *description);

BOOL OCMIsDeallocating(id anObject);
4 changes: 2 additions & 2 deletions Source/OCMock/OCMStubRecorder.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#import "OCMRealObjectForwarder.h"
#import "OCMFunctions.h"
#import "OCMInvocationStub.h"
#import "NSObject+OCMAdditions.h"
#import "OCMFunctionsPrivate.h"


@implementation OCMStubRecorder
Expand Down Expand Up @@ -53,7 +53,7 @@ - (OCMInvocationStub *)stub
- (id)andReturn:(id)anObject
{
id action;
if(anObject == mockObject || [anObject _isDeallocating])
if(anObject == mockObject || OCMIsDeallocating(anObject))
{
action = [[[OCMNonRetainingObjectReturnValueProvider alloc] initWithValue:anObject] autorelease];
}
Expand Down
1 change: 1 addition & 0 deletions Source/OCMockTests/OCMockObjectPartialMocksTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -608,4 +608,5 @@ - (void)testMethodSwizzlingWorksForVoidReturns
XCTAssertNoThrow([foo method1], @"Should have worked.");
}


@end

0 comments on commit 27c0810

Please sign in to comment.