ObjFW  Check-in [7b5b0c8174]

Overview
Comment:Don't use any other classes for +[inheritMethodsFromClass:].

Using other classes there would make it impossible to use
+[inheritMethodsFromClass:] in the classes being used, which would be
unfortunate as the classes that were used are potential candidates for
collection classes.

Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 7b5b0c817497a5ba2472404333c7242d0140d44ee5f7e77af398d29ebc7895df
User & Date: js on 2011-08-01 19:01:21
Other Links: manifest | tags
Context
2011-08-03
00:30
Make OFArray an abstract class. check-in: a855618045 user: js tags: trunk
2011-08-01
19:01
Don't use any other classes for +[inheritMethodsFromClass:]. check-in: 7b5b0c8174 user: js tags: trunk
2011-07-31
20:03
Correctly update the dtable for the old GNU runtime. check-in: df914ca51d user: js tags: trunk
Changes

Modified src/OFIntrospection.m from [6307d1d02a] to [5d382d3723].

144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
		MethodList_t methodList;

		classMethods = [[OFMutableArray alloc] init];
		instanceMethods = [[OFMutableArray alloc] init];

		for (methodList = class->class_pointer->methods;
		    methodList != NULL; methodList = methodList->method_next) {
			size_t i;

			for (i = 0; i < methodList->method_count; i++)
				[classMethods addObject: [[[OFMethod alloc]
				    _initWithMethod:
				    &methodList->method_list[i]] autorelease]];
		}

		for (methodList = class->methods; methodList != NULL;
		    methodList = methodList->method_next) {
			size_t i;

			for (i = 0; i < methodList->method_count; i++)
				[instanceMethods addObject: [[[OFMethod alloc]
				    _initWithMethod:
				    &methodList->method_list[i]] autorelease]];
		}
#endif







|









|







144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
		MethodList_t methodList;

		classMethods = [[OFMutableArray alloc] init];
		instanceMethods = [[OFMutableArray alloc] init];

		for (methodList = class->class_pointer->methods;
		    methodList != NULL; methodList = methodList->method_next) {
			int i;

			for (i = 0; i < methodList->method_count; i++)
				[classMethods addObject: [[[OFMethod alloc]
				    _initWithMethod:
				    &methodList->method_list[i]] autorelease]];
		}

		for (methodList = class->methods; methodList != NULL;
		    methodList = methodList->method_next) {
			int i;

			for (i = 0; i < methodList->method_count; i++)
				[instanceMethods addObject: [[[OFMethod alloc]
				    _initWithMethod:
				    &methodList->method_list[i]] autorelease]];
		}
#endif

Modified src/OFObject.m from [7f71e00414] to [b74fa282bf].

23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <string.h>

#include <unistd.h>

#include <assert.h>

#import "OFObject.h"
#import "OFArray.h"
#import "OFSet.h"
#import "OFIntrospection.h"
#import "OFAutoreleasePool.h"

#import "OFAllocFailedException.h"
#import "OFEnumerationMutationException.h"
#import "OFInitializationFailedException.h"
#import "OFInvalidArgumentException.h"
#import "OFMemoryNotPartOfObjectException.h"







<
<
<







23
24
25
26
27
28
29



30
31
32
33
34
35
36
#include <string.h>

#include <unistd.h>

#include <assert.h>

#import "OFObject.h"



#import "OFAutoreleasePool.h"

#import "OFAllocFailedException.h"
#import "OFEnumerationMutationException.h"
#import "OFInitializationFailedException.h"
#import "OFInvalidArgumentException.h"
#import "OFMemoryNotPartOfObjectException.h"
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626



627

628
629
630
631
632
633
634
635
636


637
638
639
640
641

642
643
644



645
646
647
648




649
650



































651





652


























653
654
655
656
657
658


659
660
661
662
663

664



665
666
667
668
669
670
671
	@throw [OFNotImplementedException newWithClass: self
					      selector: _cmd];
#endif
}

+ (void)inheritMethodsFromClass: (Class)class
{
	OFAutoreleasePool *pool;
	OFMutableSet *classMethods, *instanceMethods;
	OFIntrospection *introspection;
	OFMethod **cArray;
	size_t i, count;

	if ([self isSubclassOfClass: class])
		return;

	pool = [[OFAutoreleasePool alloc] init];

	classMethods = [OFMutableSet set];
	instanceMethods = [OFMutableSet set];

	introspection = [OFIntrospection introspectionWithClass: self];

	cArray = [[introspection classMethods] cArray];
	count = [[introspection classMethods] count];

	for (i = 0; i < count; i++)
		[classMethods addObject: [cArray[i] name]];

	cArray = [[introspection instanceMethods] cArray];
	count = [[introspection instanceMethods] count];

	for (i = 0; i < count; i++)
		[instanceMethods addObject: [cArray[i] name]];

	introspection = [OFIntrospection introspectionWithClass: class];

	cArray = [[introspection classMethods] cArray];
	count = [[introspection classMethods] count];

	for (i = 0; i < count; i++) {
		SEL selector;
		IMP implementation;




		if ([classMethods containsObject: [cArray[i] name]])

			continue;

		selector = [cArray[i] selector];
		implementation = [class methodForSelector: selector];

		if ([self respondsToSelector: selector])
			[self setImplementation: implementation
				 forClassMethod: selector];
		else


			[self addClassMethod: selector
			    withTypeEncoding: [cArray[i] typeEncoding]
			      implementation: implementation];
	}


	cArray = [[introspection instanceMethods] cArray];
	count = [[introspection instanceMethods] count];




	for (i = 0; i < count; i++) {
		SEL selector;
		IMP implementation;





		if ([instanceMethods containsObject: [cArray[i] name]])
			continue;









































		selector = [cArray[i] selector];


























		implementation = [class instanceMethodForSelector: selector];

		if ([self instancesRespondToSelector: selector])
			[self setImplementation: implementation
			      forInstanceMethod: selector];
		else


			[self addInstanceMethod: selector
			       withTypeEncoding: [cArray[i] typeEncoding]
				 implementation: implementation];
	}


	[pool release];




	[self inheritMethodsFromClass: [class superclass]];
}

- init
{
	return self;







<
|
<
<
<




<
|
<
<
|
<
|
<
<

<
<
|
<
<
|
<
<
<
<
<
<
<
<
|
|
|

>
>
>
|
>
|

<
|

|
|
|
|
>
>
|
|
|
|
|
>
|
<
|
>
>
>
|
|
|

>
>
>
>
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>

>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|

|
|
|
|
>
>
|
|
|
|
|
>
|
>
>
>







580
581
582
583
584
585
586

587



588
589
590
591

592


593

594


595


596


597








598
599
600
601
602
603
604
605
606
607
608

609
610
611
612
613
614
615
616
617
618
619
620
621
622
623

624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
	@throw [OFNotImplementedException newWithClass: self
					      selector: _cmd];
#endif
}

+ (void)inheritMethodsFromClass: (Class)class
{

	Class superclass = [self superclass];




	if ([self isSubclassOfClass: class])
		return;


#if defined(OF_APPLE_RUNTIME) || defined(OF_GNU_RUNTIME)


	Method *methodList;

	unsigned i, count;





	methodList = class_copyMethodList(((OFObject*)class)->isa, &count);


	@try {








		for (i = 0; i < count; i++) {
			SEL selector = method_getName(methodList[i]);
			IMP implementation;

			/*
			 * Don't replace methods implemented in receiving class.
			 */
			if ([self methodForSelector: selector] !=
			    [superclass methodForSelector: selector])
				continue;


			implementation = [class methodForSelector: selector];

			if ([self respondsToSelector: selector])
				[self setImplementation: implementation
					 forClassMethod: selector];
			else {
				const char *typeEncoding =
				    method_getTypeEncoding(methodList[i]);
				[self addClassMethod: selector
				    withTypeEncoding: typeEncoding
				      implementation: implementation];
			}
		}
	} @finally {
		free(methodList);

	}

	methodList = class_copyMethodList(class, &count);
	@try {
		for (i = 0; i < count; i++) {
			SEL selector = method_getName(methodList[i]);
			IMP implementation;

			/*
			 * Don't replace methods implemented in receiving class.
			 */
			if ([self instanceMethodForSelector: selector] !=
			    [superclass instanceMethodForSelector: selector])
				continue;

			implementation =
			    [class instanceMethodForSelector: selector];

			if ([self instancesRespondToSelector: selector])
				     [self setImplementation: implementation
					   forInstanceMethod: selector];
			else {
				const char *typeEncoding =
				    method_getTypeEncoding(methodList[i]);
				[self addInstanceMethod: selector
				       withTypeEncoding: typeEncoding
					 implementation: implementation];
			}
		}
	} @finally {
		free(methodList);
	}
#elif defined(OF_OLD_GNU_RUNTIME)
	MethodList_t methodList;

	for (methodList = class->class_pointer->methods;
	    methodList != NULL; methodList = methodList->method_next) {
		int i;

		for (i = 0; i < methodList->method_count; i++) {
			SEL selector = methodList->method_list[i].method_name;
			IMP implementation;

			/*
			 * Don't replace methods implemented in receiving class.
			 */
			if ([self instanceMethodForSelector: selector] !=
			    [superclass instanceMethodForSelector: selector])
				continue;

			implementation =
			    [class instanceMethodForSelector: selector];

			if ([self instancesRespondToSelector: selector])
				     [self setImplementation: implementation
					      forClassMethod: selector];
			else {
				const char *typeEncoding =
				    methodList->method_list[i].method_types;
				[self addClassMethod: selector
				    withTypeEncoding: typeEncoding
				      implementation: implementation];
			}
		}
	}

	for (methodList = class->methods; methodList != NULL;
	    methodList = methodList->method_next) {
		int i;

		for (i = 0; i < methodList->method_count; i++) {
			SEL selector = methodList->method_list[i].method_name;
			IMP implementation;

			/*
			 * Don't replace methods implemented in receiving class.
			 */
			if ([self instanceMethodForSelector: selector] !=
			    [superclass instanceMethodForSelector: selector])
				continue;

			implementation =
			    [class instanceMethodForSelector: selector];

			if ([self instancesRespondToSelector: selector])
				     [self setImplementation: implementation
					   forInstanceMethod: selector];
			else {
				const char *typeEncoding =
				    methodList->method_list[i].method_types;
				[self addInstanceMethod: selector
				       withTypeEncoding: typeEncoding
					 implementation: implementation];
			}
		}
	}
#else
	@throw [OFNotImplementedException newWithClass: self
					      selector: _cmd];
#endif

	[self inheritMethodsFromClass: [class superclass]];
}

- init
{
	return self;