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

GSFFIInvocation: Use objc_msg_lookup in -invokeWithTarget: to avoid skipping hidden classes #473

Merged
merged 4 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 12 additions & 14 deletions Source/GSFFIInvocation.m
Original file line number Diff line number Diff line change
Expand Up @@ -435,21 +435,19 @@ - (void) invokeWithTarget: (id)anObject
}
else
{
GSMethod method;
method = GSGetMethod((GSObjCIsInstance(_target)
? (Class)object_getClass(_target)
: (Class)_target),
_selector,
GSObjCIsInstance(_target),
YES);
imp = method_getImplementation(method);
/*
* If fast lookup failed, we may be forwarding or something ...
/* The KVO implementation for libobjc2 (located in gnustep-base Source/NSKVO*)
* uses the non-portable `object_addMethod_np` API from libobjc2.
* `object_addMethod_np` creates or reuses a hidden subclass and adds the swizzled
* method to the hidden class.
*
* When retrieving the object's class with `object_getClass`, hidden classes are skipped
* and the original class is returned.
* This is also the case with `class_getInstanceMethod`, where the
* original class or (non-swizzled) method is returned instead.
*
* The proper way to retrieve an IMP is with `objc_msg_lookup`.
*/
if (imp == 0)
{
imp = objc_msg_lookup(_target, _selector);
}
imp = objc_msg_lookup(_target, _selector);
}

[self setTarget: old_target];
Expand Down
165 changes: 165 additions & 0 deletions Tests/base/NSKVOSupport/proxy.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
#import <GNUstepBase/GNUstep.h>

#import <Foundation/NSProxy.h>
#import <Foundation/NSInvocation.h>
#import <Foundation/NSObject.h>
#import <Foundation/NSString.h>
#import <Foundation/NSDictionary.h>
#import <Foundation/NSSet.h>
#import <Foundation/NSAutoreleasePool.h>
#import <Foundation/NSKeyValueObserving.h>

#import "Testing.h"

/**
* The new KVO implementation for libobjc2/clang, located in Source/NSKVO*, reuses
* or installs a hidden class and subsequently adds the swizzled method to the
* hidden class. Make sure that the invocation mechanism calls the swizzled method.
*/

@interface Observee : NSObject
{
NSString *_name;
NSString *_derivedName;
}

- (NSString *) name;
- (void) setName: (NSString *)name;

- (NSString *) derivedName;
- (void) setDerivedName: (NSString *)name;

@end

@implementation Observee

- (NSString *) name
{
return [[_name retain] autorelease];
}
- (void) setName: (NSString *)name
{
ASSIGN(_name, name);
}

- (NSString *)derivedName
{
return [NSString stringWithFormat:@"Derived %@", self.name];
}
- (void) setDerivedName: (NSString *)name
{
ASSIGN(_derivedName, name);
}

+ (NSSet *)keyPathsForValuesAffectingDerivedName
{
return [NSSet setWithObject:@"name"];
}

- (void) dealloc {
RELEASE(_name);
RELEASE(_derivedName);
[super dealloc];
}

@end

@interface TProxy : NSProxy
{
id _proxiedObject;
}
@end

@implementation TProxy

- (instancetype)initWithProxiedObject:(id)proxiedObject
{
ASSIGN(_proxiedObject, proxiedObject);
return self;
}

- (void)forwardInvocation:(NSInvocation *)invocation
{
[invocation invokeWithTarget:_proxiedObject];
}

- (NSMethodSignature *)methodSignatureForSelector:(SEL)sel
{
return [_proxiedObject methodSignatureForSelector:sel];
}

- (void) dealloc
{
RELEASE(_proxiedObject);
[super dealloc];
}

@end

@interface Observer: NSObject
{
int count;
}

- (void)runTest;

@end

@implementation Observer

- (void)runTest
{
Observee *obj = [[Observee alloc] init];
TProxy *proxy = [[TProxy alloc] initWithProxiedObject:obj];

[(Observee *)proxy addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionNew context:NULL];
[(Observee *)proxy addObserver:self forKeyPath:@"derivedName" options:NSKeyValueObservingOptionNew context:NULL];

[((Observee *)proxy) setName: @"MOO"];
PASS(count == 2, "Got two change notifications");

[obj setName: @"BAH"];
PASS(count == 4, "Got two change notifications");

[(Observee *)proxy removeObserver:self forKeyPath:@"name" context:NULL];
[(Observee *)proxy removeObserver:self forKeyPath:@"derivedName" context:NULL];

[proxy release];
[obj release];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
count += 1;
switch (count) {
case 1:
PASS_EQUAL(keyPath, @"derivedName", "change notification for dependent key 'derivedName' is emitted first");
break;
case 2:
PASS_EQUAL(keyPath, @"name", "'name' change notification for proxy is second");
break;
case 3:
PASS_EQUAL(keyPath, @"derivedName", "'derivedName' change notification for object is third");
break;
case 4:
PASS_EQUAL(keyPath, @"name", "'name' change notification for object is fourth");
break;
default:
PASS(0, "unexpected -[Observer observeValueForKeyPath:ofObject:change:context:] callback");
}
}

@end

int
main(int argc, char *argv[])
{
NSAutoreleasePool *arp = [NSAutoreleasePool new];
Observer *obs = [Observer new];

[obs runTest];
[obs release];

DESTROY(arp);
return 0;
}
Loading