Source

pyobjc / pyobjc-core / Modules / objc / objc-runtime-compat.m

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 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
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 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
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
/*
 * Objective-C runtime 2.0 compatibility for MacOS X 10.4 and earlier.
 *
 * This code works by poking into the ObjC runtime, which means loads of 
 * warnings on 10.5+ ;-)
 */

#define PYOBJC_COMPAT_IMPL
#include "pyobjc.h"




#if (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5) &&!defined(__OBJC2__)

BOOL PyObjC_class_isSubclassOf(Class child, Class parent)
{
	if (parent == nil) return YES;

	while (child != nil) {
		if (child == parent) {
			return YES;
		}
		child = PyObjC_class_getSuperclass(child);
	}
	return NO;
}

#import <mach-o/dyld.h>
#import <mach-o/getsect.h>
#import <mach-o/loader.h>

typedef struct _ProtocolTemplate { @defs(Protocol) } ProtocolTemplate;



struct objc10_object {
	Class	isa;
};

struct PyObjC_ivar {
	char* name;
	size_t size;
	uint8_t alignment;
	char* types;
};


static Protocol** compat_objc_copyProtocolList(unsigned int* outCount)
{
	Protocol** protocols = NULL;
	*outCount = 0;

	uint32_t image_count, image_index;
	image_count = _dyld_image_count();
	for (image_index = 0; image_index < image_count; image_index++) {
		uint32_t size = 0;
		const struct mach_header *mh = _dyld_get_image_header(image_index);
		intptr_t slide = _dyld_get_image_vmaddr_slide(image_index);
		ProtocolTemplate *protos = (ProtocolTemplate*)(
			((char *)getsectdatafromheader(mh, SEG_OBJC, "__protocol", &size)) +
			slide);
		uint32_t nprotos = size / sizeof(ProtocolTemplate);
		uint32_t i;

		if (nprotos == 0) continue;

		if (protocols == NULL) {
			protocols = malloc(sizeof(Protocol*) * nprotos);
			if (protocols == NULL) {
				return NULL;
			}
		} else {
			Protocol** tmp = realloc(protocols,
				sizeof(Protocol*) * (*outCount+nprotos));
			if (tmp == NULL) {
				free(protocols);
				return NULL;
			}
			protocols = tmp;
		}

		for (i = 0; i < nprotos; i++) {
			protocols[(*outCount)++] = (Protocol*)&protos[i];
		}
	}
	return protocols;
}

static Protocol*  compat_objc_getProtocol(const char* name)
{
	uint32_t image_count, image_index;
	image_count = _dyld_image_count();
	for (image_index = 0; image_index < image_count; image_index++) {
		uint32_t size = 0;
		const struct mach_header *mh = _dyld_get_image_header(image_index);
		intptr_t slide = _dyld_get_image_vmaddr_slide(image_index);
		ProtocolTemplate *protos = (ProtocolTemplate*)(
			((char *)getsectdatafromheader(mh, SEG_OBJC, "__protocol", &size)) +
			slide);
		uint32_t nprotos = size / sizeof(ProtocolTemplate);
		uint32_t i;

		if (nprotos == 0) continue;

		for (i = 0; i < nprotos; i++) {
			Protocol* p = (Protocol*)&protos[i];
			if (strcmp([p name], name) == 0) {
				return p;
			}
		}
	}
	return nil;
}


static void
compat_objc_registerClassPair(Class cls)
{
	struct objc_method_list* list;

	/* Add the list in the official way, just in case class_addMethods
	 * does something special.
	 */
	list = cls->methodLists[0];
	cls->methodLists[0] = (struct objc_method_list*)-1;
	class_addMethods(cls, list);

	list = cls->isa->methodLists[0];
	cls->isa->methodLists[0] = (struct objc_method_list*)-1;
	class_addMethods(cls->isa, list);

	objc_addClass(cls);
}

static Class
compat_objc_allocateClassPair(Class super_class, const char* name, size_t extra)
{
	struct objc_class* result;
	Class root_class;

	if (objc_getClass(name)) {
		return Nil;
	}

	root_class = super_class;
	while (root_class->super_class) {
		root_class = root_class->super_class;
	}
	
	result = malloc(sizeof(struct objc_class) * 2 + extra);
	if (result == NULL) {
		return Nil;
	}
	memset(result, 0, sizeof(struct objc_class) * 2 + extra);

	result->super_class = super_class;
	result->isa = result + 1;

	result[0].methodLists = NULL;
	result[1].methodLists = NULL;

	result[0].info = CLS_CLASS;
	result[1].info = CLS_META;

	result[0].name = strdup(name);
	if (result[0].name == NULL) goto error_cleanup;

	result[1].name = result[0].name;
	result[0].methodLists = malloc(sizeof(struct objc_method_list*));
	if (result[0].methodLists == NULL) goto error_cleanup;
	memset(result[0].methodLists, 0, sizeof(struct objc_method_list*));

	result[1].methodLists = malloc(sizeof(struct objc_method_list*));
	if (result[1].methodLists == NULL) goto error_cleanup;
	memset(result[1].methodLists, 0, sizeof(struct objc_method_list*));

	/*
	 * This is MacOS X specific, and an undocumented feature (long live
	 * Open Source!).
	 *
	 * The code in the objc runtime assumes that the method lists are
	 * terminated by '-1', and will happily overwite existing data if
	 * they aren't.
	 *
	 * Ronald filed a bugreport for this: Radar #3317376
	 */
	result[0].methodLists[0] = (struct objc_method_list*)-1;
	result[1].methodLists[0] = (struct objc_method_list*)-1;

	result[0].methodLists[0] = malloc(sizeof(struct objc_method_list));
	if (result[0].methodLists[0] == NULL) goto error_cleanup;
	result[0].methodLists[0]->method_count = 0;
	result[0].methodLists[0]->obsolete = NULL;

	result[1].methodLists[0] = malloc(sizeof(struct objc_method_list));
	if (result[1].methodLists[0] == NULL) goto error_cleanup;
	result[1].methodLists[0]->method_count = 0;
	result[1].methodLists[0]->obsolete = NULL;


	result[0].super_class = super_class;
	result[1].super_class = ((struct objc10_object*)super_class)->isa;
	result[1].isa = ((struct objc10_object*)root_class)->isa;

	result[0].instance_size = super_class->instance_size;
	result[1].instance_size = result[1].super_class->instance_size;

	/* Initialize to NULL, otherwise poseAs: won't work */
	result[0].ivars = result[1].ivars = NULL;

	result[0].protocols = result[1].protocols = NULL;

	return result;


error_cleanup:
	if (result) {
		if (result[0].methodLists) {
			if (result[0].methodLists[0] != 0 &&
					result[0].methodLists[0] != 0)  {
				free(result[0].methodLists[0]);
			}
			free(result[0].methodLists);
		}
		if (result[1].methodLists) {
			if (result[1].methodLists[1] != 0 &&
					result[1].methodLists[0] != 0)  {
				free(result[1].methodLists[0]);
			}
			free(result[1].methodLists);
		}

		if (result[0].ivars) {
			free(result[0].ivars);
		}
		if (result[1].ivars) {
			free(result[1].ivars);
		}

		if (result[0].name) {
			free((char*)result[0].name);
		}
		free(result);
	}
	return Nil;

}

static void
compat_objc_disposeClassPair(Class cls)
{
	struct objc_class* val = cls;
	int i;

	for (i = 0; i < 2; i++) {
		if (val[i].methodLists) {
			struct objc_method_list** cur;

			cur = val[i].methodLists;
			if (*cur != (struct objc_method_list*)-1) {
				free(*cur);
				*cur = NULL;
			}
			free(val[i].methodLists);
			val[i].methodLists = NULL;
		}
	}

	free((char*)val[0].name);
	val[0].name = val[1].name = NULL;
	free(val);
}


static size_t 
compat_methodlist_magic(Class cls)
{
	void* iterator = NULL;
	struct objc_method_list* mlist;
	size_t res = 0, cnt = 0;

        if (cls == NULL) return -1;

	while ( (mlist = class_nextMethodList(cls, &iterator )) != NULL ) {
		res += mlist->method_count;
		cnt++;
	}

	return (cnt << 16) | (res & 0xFFFF);
}

#ifndef NO_OBJC2_RUNTIME
static size_t 
objc20_methodlist_magic(Class cls)
{
	Method* methods;
	unsigned int count;
	unsigned int i;
	size_t result;

	methods = class_copyMethodList(cls, &count);
	result = 0;
	for (i = 0; i < count; i++) {
		result = (1000003*result) ^ ((size_t)(
				method_getImplementation(methods[i])) >> 3);
	}
	result = result | (count << 16);
	free(methods);
	return result;
}
#endif

static Class 
compat_object_setClass(id obj, Class cls)
{
	Class prev;
	if (obj == nil) {
		return Nil;
	}
	prev = ((struct objc10_object*)obj)->isa;
	((struct objc10_object*)obj)->isa = cls;
	return prev;
}



static Class 
compat_object_getClass(id obj)
{
	return ((struct objc10_object*)obj)->isa;
}

static const char*
compat_object_getClassName(id obj)
{
	return ((struct objc10_object*)obj)->isa->name;
}



static Method* 
compat_class_copyMethodList(Class cls, unsigned int* outCount)
{
	struct objc_method_list* mlist;
	void* iterator; 
	unsigned int count;
	Method* result;
	Method* tmp;
	
	iterator = NULL;
	count = 0;

	mlist = class_nextMethodList(cls, &iterator);
	result = NULL;
	while (mlist != NULL) {
		int i;

		tmp = realloc(result, (count + mlist->method_count) * sizeof(Method));
		if (tmp == NULL) {
			free(result);
			return NULL;
		} else {
			result = tmp;
		}

		for (i = 0; i < mlist->method_count; i++) {
			result[count] = mlist->method_list + i;
			if (result[count] == NULL) continue;
			count++;
		}
		mlist = class_nextMethodList(cls, &iterator);
	}

	if (outCount != NULL) {
		*outCount = count;
	}
	return result;
}

static Ivar*
compat_class_copyIvarList(Class cls, unsigned int* outCount)
{
	Ivar* list;
	int   i;
	struct objc_ivar_list* ivars = cls->ivars;

	if (ivars) {
		list = malloc(sizeof(Ivar) * ivars->ivar_count);
		if (list == NULL) {
			return NULL;
		}
	

		for (i = 0; i < ivars->ivar_count; i++) {
			list[i] = ivars->ivar_list + i;
		}
		*outCount = ivars->ivar_count;
		return list;

	} else {
		list = malloc(1);
		if (list == NULL) {
			return NULL;
		}

		*outCount = 0;
		return list;
	}

}

static Protocol**
compat_class_copyProtocolList(Class cls, unsigned int* outCount)
{
	Protocol** list;
	unsigned int count = 0;
	struct objc_protocol_list *protocol_list;

	protocol_list = cls->protocols;
	list = malloc(0);

	while (protocol_list != NULL) {
		list = realloc(list, (count + protocol_list->count)*sizeof(Protocol*));
		if (list == NULL) {
			/* Whoops, memory leak */
			*outCount = 0;
			return NULL;
		}
		memcpy(list + count, protocol_list->list, protocol_list->count*sizeof(Protocol*));
		count += protocol_list->count;
		protocol_list = protocol_list->next;
	}

	*outCount = count;
	return list;
}


static const char* 
compat_class_getName(Class cls)
{
	return cls->name;
}

static Class 
compat_class_getSuperclass(Class cls)
{
	return cls->super_class;
}

static BOOL 
compat_class_addMethod(Class cls, SEL name, IMP imp, const char* types)
{
	struct objc_method_list* methodsToAdd;
	struct objc_method* objcMethod;

	methodsToAdd = malloc(sizeof(struct objc_method_list) +
			2*sizeof(struct objc_method));
	methodsToAdd->method_count = 1;
	methodsToAdd->obsolete = NULL;

	objcMethod = methodsToAdd->method_list;
	objcMethod->method_name = name;
	objcMethod->method_imp = imp;
	objcMethod->method_types = (char*)types;

	class_addMethods(cls, methodsToAdd);
	return YES; /* Have to return something ... */
}

static BOOL 
compat_preclass_addMethod(Class cls, SEL name, IMP imp, const char* types)
{
	/* Resize in-place, this is only valid during class construction. */
	struct objc_method_list* new_list;
	struct objc_method* objcMethod;

	new_list = realloc(cls->methodLists[0],
			sizeof(struct objc_method_list) +
			(sizeof(struct objc_method
				)*(cls->methodLists[0]->method_count+1)));
	if (new_list == NULL) {
		return NO;
	}
	cls->methodLists[0] = new_list;
	objcMethod = new_list->method_list + (new_list->method_count)++;

	objcMethod->method_name = name;
	objcMethod->method_imp = imp;
	objcMethod->method_types = strdup(types);
	if (objcMethod == NULL) {
		new_list->method_count--;
		return NO;
	}

	return YES;
}

static BOOL
compat_preclass_addIvar(
		Class cls, 
		const char* name, 
		size_t size, 
		uint8_t align, const char* types)
{
	/* Update the class structure, only valid during class construction */
	struct objc_ivar_list* new_ivars;
	struct objc_ivar* ivar;

	if (cls->ivars) {
		new_ivars = realloc(cls->ivars, 
			sizeof(struct objc_ivar_list) + 
			((cls->ivars->ivar_count+1) * sizeof(struct objc_ivar)));
	} else {
		new_ivars = malloc( 
				sizeof(struct objc_ivar_list) + 
				sizeof(struct objc_ivar));
		new_ivars->ivar_count = 0;
	}
	if (new_ivars == NULL) {
		return NO;
	}
	cls->ivars = new_ivars;
	ivar = new_ivars->ivar_list + new_ivars->ivar_count;
	ivar->ivar_name = strdup(name);
	if (ivar->ivar_name == NULL) {
		return NO;
	}

	ivar->ivar_type = strdup(types);
	if (ivar->ivar_type == NULL) {
		free(ivar->ivar_name);
		return NO;
	}

	ivar->ivar_offset = cls->instance_size;
	if (ivar->ivar_offset % align) {
		ivar->ivar_offset += align - (ivar->ivar_offset % align);
	}

	new_ivars->ivar_count ++;
	cls->instance_size = ivar->ivar_offset + size;
	return YES;
}

static BOOL
compat_preclass_addProtocol(Class cls, Protocol* protocol)
{
	struct objc_protocol_list* protocols;

	if (cls->protocols) {
		protocols = realloc(cls->protocols,
			sizeof(struct objc_protocol_list) +
			  (sizeof(Protocol*)*(cls->protocols->count+1)));
	} else {
		protocols = malloc(sizeof(struct objc_protocol_list)
				+ sizeof(Protocol*));
		protocols->count = 0;
		protocols->next = NULL;
	}
	if (protocols == NULL) {
		return NO;
	}
	cls->protocols = protocols;
	protocols->list[protocols->count] = protocol;
	protocols->count++;
	return YES;
}
	
static SEL 
compat_method_getName(Method m) 
{
	return m->method_name;
}

static IMP 
compat_method_getImplementation(Method m)
{
	return m->method_imp;
}

static IMP 
compat_method_setImplementation(Method m, IMP imp)
{
	IMP result =  m->method_imp;
	m->method_imp = imp;
	return result;
}

static const char *
compat_method_getTypeEncoding(Method m)
{
	return m->method_types;
}

static BOOL 
compat_sel_isEqual(SEL lhs, SEL rhs)
{
	return lhs == rhs;
}

static const char*
compat_ivar_getName(Ivar var)
{
	return var->ivar_name;
}

static const char*
compat_ivar_getTypeEncoding(Ivar var)
{
	return var->ivar_type;
}

static ptrdiff_t
compat_ivar_getOffset(Ivar var)
{
	return var->ivar_offset;
}


#ifndef NO_OBJC2_RUNTIME
static BOOL 
objc20_class_addMethodList(Class cls,
		struct PyObjC_method* list, unsigned int count)
{
	unsigned int i;
	BOOL r;
	Method m;

	for (i = 0; i < count; i++) {
		r = class_addMethod(cls, 
			list[i].name, list[i].imp, list[i].type);
		if (!r) {
			m = class_getInstanceMethod(cls, list[i].name);
			if (m != NULL) {
				method_setImplementation(m, list[i].imp);
			} else {
				return NO;
			}
		}
	}
	return YES;
}
#endif

static BOOL
compat_class_isMetaClass(Class cls)
{
	return CLS_GETINFO(cls, CLS_META) == CLS_META;
}

static BOOL 
compat_class_addMethodList(Class cls,
		struct PyObjC_method* list, unsigned int count)
{
	unsigned int i;
	struct objc_method_list* method_list;

	method_list = malloc(
			sizeof(struct objc_method_list) +
			((count+1) * sizeof(struct objc_method)));
	if (method_list == NULL) {
		return NO;
	}
	memset(method_list, 0, 
			sizeof(struct objc_method_list) +
			((count+1) * sizeof(struct objc_method)));

	method_list->method_count = 0;
	method_list->obsolete = 0;

	for (i = 0; i < count; i++) {
		method_list->method_list[i].method_name = list[i].name;
		method_list->method_list[i].method_types = (char*)list[i].type;
		method_list->method_list[i].method_imp = list[i].imp;
	}
	method_list->method_count = count;
	class_addMethods(cls, method_list);
	return YES;
}

static BOOL 
compat_protocol_conformsToProtocol(Protocol *proto, Protocol *other)
{
	return [proto conformsTo:other];
}

static const char *
compat_protocol_getName(Protocol *p)
{
	return [p name];
}

static struct objc_method_description *
compat_protocol_copyMethodDescriptionList(Protocol *p, BOOL isRequiredMethod, BOOL isInstanceMethod, unsigned int *outCount)
{
	if (!isRequiredMethod) {
		*outCount = 0;
		return NULL;
	}

	struct objc_method_description_list* list;

	if (isInstanceMethod) {
		list = ((ProtocolTemplate*)p)->instance_methods;
	} else {
		list = ((ProtocolTemplate*)p)->class_methods;
	}

	if (list == NULL || list->count == 0) {
		*outCount = 0;
		return NULL;
	}

	*outCount = list->count;
	struct objc_method_description* result;

	result = malloc(sizeof(struct objc_method_description) * list->count);
	if (result == NULL) {
		return NULL;
	}

	int i;
	*outCount = 0;
	for (i = 0; i < list->count; i++) {
		if (list->list[i].name == NULL) continue;
		result[*outCount].name = list->list[i].name;
		result[*outCount].types = list->list[i].types;
		(*outCount)++;
	}
	return result;
}

static Protocol **
compat_protocol_copyProtocolList(Protocol *proto, unsigned int *outCount)
{
	struct objc_protocol_list* list = 
		((ProtocolTemplate*)proto)->protocol_list;

	*outCount = 0;
	
	struct objc_protocol_list* cur;
	for (cur = list; cur != NULL; cur = cur->next) {
		*outCount += cur->count;
	}

	Protocol** result = (Protocol**)malloc(sizeof(Protocol*) * *outCount);
	if (result == NULL) {
		return NULL;
	}

	unsigned curIdx = 0;
	for (cur = list; cur != NULL; cur = cur->next) {
		int i;
		for (i = 0; i < cur->count; i++) {
			if (cur->list[i] != NULL) {
				result[curIdx++] = cur->list[i];
			} else {
				*outCount--;
			}
		}
	}
	return result;
}

static struct objc_method_description 
compat_protocol_getMethodDescription(Protocol *p, SEL aSel, BOOL isRequiredMethod, BOOL isInstanceMethod)
{
static struct objc_method_description empty_description =  { NULL, NULL };
	if (!isRequiredMethod) {
		return empty_description;
	}

	struct objc_method_description* result;

	if (isInstanceMethod) {
		result = [p descriptionForInstanceMethod: aSel];
	} else {
		result = [p descriptionForClassMethod: aSel];
	}

	if (result == NULL) {
		return empty_description;
	} else {
		return *result;
	}
}

static id 
compat_object_getIvar(id obj, Ivar ivar)
{
	return *(id*)(((char*)obj) + ivar->ivar_offset);
}
static void 
compat_object_setIvar(id obj, Ivar ivar, id value)
{
	*(id*)(((char*)obj) + ivar->ivar_offset) = value;
}

/* Dispatch table */
BOOL (*PyObjC_protocol_conformsToProtocol)(Protocol *proto, Protocol *other) = NULL;
const char *(*PyObjC_protocol_getName)(Protocol *p) = NULL;
struct objc_method_description *(*PyObjC_protocol_copyMethodDescriptionList)(Protocol *p, BOOL isRequiredMethod, BOOL isInstanceMethod, unsigned int *outCount) = NULL;
Protocol **(*PyObjC_protocol_copyProtocolList)(Protocol *proto, unsigned int *outCount) = NULL;
struct objc_method_description (*PyObjC_protocol_getMethodDescription)(Protocol *p, SEL aSel, BOOL isRequiredMethod, BOOL isInstanceMethod) = NULL;

id (*PyObjC_object_getIvar)(id obj, Ivar ivar) = NULL;
void (*PyObjC_object_setIvar)(id obj, Ivar ivar, id value) = NULL;



Class (*PyObjC_objc_allocateClassPair)(Class, const char*, size_t) = NULL;
void (*PyObjC_objc_registerClassPair)(Class) = NULL;
void (*PyObjC_objc_disposeClassPair)(Class) = NULL;
Protocol** (*PyObjC_objc_copyProtocolList)(unsigned int*) = NULL;
Protocol*  (*PyObjC_objc_getProtocol)(const char* name) = NULL;

BOOL (*PyObjC_preclass_addMethod)(Class, SEL, IMP, const char*) = NULL;
BOOL (*PyObjC_preclass_addIvar)(Class cls, 
	const char *name, size_t size, uint8_t alignment,
	const char *types) = NULL;
BOOL (*PyObjC_preclass_addProtocol)(Class cls, Protocol *protocol) = NULL;


Class   (*PyObjC_object_getClass)(id obj) = NULL;
Class (*PyObjC_object_setClass)(id obj, Class cls) = NULL;
const char* (*PyObjC_object_getClassName)(id obj) = NULL;

Method* (*PyObjC_class_copyMethodList)(Class, unsigned int*) = NULL;
const char*  (*PyObjC_class_getName)(Class) = NULL;
Class (*PyObjC_class_getSuperclass)(Class) = NULL;
BOOL (*PyObjC_class_addMethod)(Class, SEL, IMP, const char*) = NULL;
BOOL (*PyObjC_class_addMethodList)(Class,
		struct PyObjC_method*, unsigned int) = NULL;
Ivar* (*PyObjC_class_copyIvarList)(Class, unsigned int*) = NULL;
Protocol** (*PyObjC_class_copyProtocolList)(Class, unsigned int*) = NULL;
BOOL (*PyObjC_class_isMetaClass)(Class) = NULL;

SEL (*PyObjC_method_getName)(Method m) = NULL;
IMP (*PyObjC_method_getImplementation)(Method m) = NULL;
IMP (*PyObjC_method_setImplementation)(Method m, IMP imp) = NULL;
const char *(*PyObjC_method_getTypeEncoding)(Method m) = NULL;

BOOL (*PyObjC_sel_isEqual)(SEL, SEL) = NULL;

size_t (*PyObjC_methodlist_magic)(Class cls);

const char*  (*PyObjC_ivar_getName)(Ivar) = NULL;
const char*  (*PyObjC_ivar_getTypeEncoding)(Ivar) = NULL;
ptrdiff_t    (*PyObjC_ivar_getOffset)(Ivar) = NULL;




#else


BOOL PyObjC_class_isSubclassOf(Class child, Class parent)
{
	if (parent == nil) return YES;

	while (child != nil) {
		if (child == parent) {
			return YES;
		}
		child = class_getSuperclass(child);
	}
	return NO;
}

BOOL PyObjC_class_addMethodList(Class cls,
		struct PyObjC_method* list, unsigned int count)
{
	unsigned int i;
	BOOL r;
	Method m;

	for (i = 0; i < count; i++) {
		/*
		 * XXX: First try class_addMethod, if that fails assume this is
		 * because the method already exists in the class.
		 * Strictly speaking this isn't correct, but this is the best
		 * we can do through the 2.0 API (see 4809039 in RADAR)
		 */
		r = class_addMethod(cls, 
			list[i].name, list[i].imp, list[i].type);
		if (!r) {
			m = class_getInstanceMethod(cls, list[i].name);
			if (m != NULL) {
				method_setImplementation(m, list[i].imp);
			} else {
				return NO;
			}
		}
	}
	return YES;
}

size_t PyObjC_methodlist_magic(Class cls)
{
	/* This is likely to be much slower than compat_methodlist_magic,
	 * but should works on the 64-bit runtime. Hopefully a callback will
	 * be added to the 2.0 runtime that will take away the need for this
	 * function...
	 */
	Method* methods;
	unsigned int count;

	methods = class_copyMethodList(cls, &count);
	free(methods);
	return (size_t)count;
}

#endif

#if defined(__x86_64__)

@implementation Protocol (NSOBjectCompat)
- (id)self
{
	return self;
}
@end

#if PyObjC_BUILD_RELEASE < 1008
@implementation Object (NSOBjectCompat)
- (id)self
{
	return self;
}

-doesNotRecognizeSelector:(SEL)sel
{
	printf("--> %s\n", sel_getName(sel));
	abort();
}
@end
#endif
	

#endif

#if (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_7)
Protocol* (*PyObjC_objc_allocateProtocol)(const char *) = NULL;
void (*PyObjC_objc_registerProtocol)(Protocol*) = NULL;
void (*PyObjC_protocol_addMethodDescription)(Protocol*, SEL, const char*, BOOL, BOOL) = NULL;
void (*PyObjC_protocol_addProtocol)(Protocol*, Protocol*) = NULL;

#ifndef __LP64__
struct Protocol_struct {
	Class _isa;
	char *protocol_name;
	struct objc_protocol_list *protocol_list;
	struct objc_method_description_list *instance_methods, *class_methods;
	struct objc_method_description_list *optional_instance_methods, *optional_class_methods;
	void *instance_properties;
};

static Protocol* compat_objc_allocateProtocol(const char *name)
{
	struct Protocol_struct* result;

	result = (struct Protocol_struct*)NSAllocateObject([Protocol class], 0, NULL);
	if (result == NULL) {
		return NULL;
	}
	result->protocol_name = strdup(name);
	if (result->protocol_name == NULL) {
		/* Leaking object */
		return NULL;
	}
	result->protocol_list = NULL;
	result->instance_methods = NULL;
	result->class_methods = NULL;
	result->optional_instance_methods = NULL;
	result->optional_class_methods = NULL;
	result->instance_properties = NULL;
	return (Protocol*)result;
}

static void compat_objc_registerProtocol(Protocol* proto __attribute__((__unused__)))
{
	/* Don't know how to register a new protocol in classic
	 * runtime. Luckily we don't actually need this.
	 */
}

static void compat_protocol_addMethodDescription(Protocol* proto, SEL sel, const char* types, BOOL required, BOOL instance_method)
{
	struct Protocol_struct* proto_struct = (struct Protocol_struct*)proto;
	struct objc_method_description_list** plist;

	if (!instance_method) {
		if (required) {
			plist = &(proto_struct->class_methods);
		} else {
			plist = &(proto_struct->optional_class_methods);
		}
	} else {
		if (required) {
			plist = &(proto_struct->instance_methods);
		} else {
			plist = &(proto_struct->optional_instance_methods);
		}
	}

	if (*plist == NULL) {
		*plist = malloc(sizeof(struct objc_method_description_list) + (2*sizeof(struct objc_method_description)));
		if (*plist == NULL) {
			/* Cannot report errors */
			abort();
		}
		(*plist)->count = 0;
	} else {
		*plist = realloc(*plist, sizeof(struct objc_method_description_list) + (2+((*plist)->count)*sizeof(struct objc_method_description)));
		if (*plist == NULL) {
			/* Cannot report errors */
			abort();
		}
	}
	(*plist)->list[(*plist)->count].name = sel;
	(*plist)->list[(*plist)->count].types = strdup(types);
	if ((*plist)->list[(*plist)->count].types == NULL) {
		/* Cannot report errors */
		abort();
	}
	(*plist)->count++;

	(*plist)->list[(*plist)->count].name = NULL;
	(*plist)->list[(*plist)->count].types = NULL;
}

static void compat_protocol_addProtocol(Protocol* proto, Protocol* newProto)
{
	struct Protocol_struct* proto_struct = (struct Protocol_struct*)proto;

	if (proto_struct->protocol_list == NULL) {
		proto_struct->protocol_list = malloc(sizeof(struct objc_protocol_list) + 2*sizeof(Protocol*));
		if (proto_struct->protocol_list == NULL) {
			/* Cannot report an error! */
			abort();
		}
		proto_struct->protocol_list->next = NULL;
		proto_struct->protocol_list->count = 0;
	} else {
		proto_struct->protocol_list = realloc(proto_struct->protocol_list,
				sizeof(struct objc_protocol_list) + (2+proto_struct->protocol_list->count)*sizeof(Protocol*));
		if (proto_struct->protocol_list == NULL) {
			/* Cannot report an error! */
			abort();
		}
	}
	proto_struct->protocol_list->list[proto_struct->protocol_list->count] = newProto;
	proto_struct->protocol_list->list[proto_struct->protocol_list->count+1] = NULL;
	proto_struct->protocol_list->count++;
}

#endif
#endif

#if !((MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5) &&!defined(__OBJC2__))
#undef protocol_getMethodDescription
struct objc_method_description 
PyObjC_protocol_getMethodDescription(Protocol *p, SEL aSel, BOOL isRequiredMethod, BOOL isInstanceMethod)
{
	struct objc_method_description  result = protocol_getMethodDescription(p, aSel, isRequiredMethod, isInstanceMethod);
	if (result.name != NULL) {
		return result;
	}

	{
		/* This code should not be necessary.... 
		 * 
		 * for some reason the protocols created by PyObjC don't always work, without this
		 * function test_protocol.py sometimes (but not always!) fails.
		 */

		struct objc_method_description* methods;
		unsigned int count, i;
		methods = protocol_copyMethodDescriptionList(p, isRequiredMethod, isInstanceMethod, &count);
		if (methods == NULL) {
			return result;
		}

		for (i = 0; i < count; i++) {
			if (sel_isEqual(methods[i].name, aSel)) {
				result = methods[i];
				free(methods);
				return result;
			}
		}
		free(methods);
		return result;

	}
}
#endif



void PyObjC_SetupRuntimeCompat(void)
{
#if (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5)  && !defined(__OBJC2__)

#ifdef NO_OBJC2_RUNTIME
	/* 
	 * Don't use ObjC 2.0 runtime (compiling on 10.4 or earlier), always
	 * use the compat implementation.
	 */
	PyObjC_class_addMethodList  = compat_class_addMethodList;
	PyObjC_methodlist_magic     = compat_methodlist_magic;
	PyObjC_objc_disposeClassPair   = compat_objc_disposeClassPair;
	PyObjC_preclass_addMethod   = compat_preclass_addMethod;
	PyObjC_preclass_addIvar     = compat_preclass_addIvar;
	PyObjC_preclass_addProtocol = compat_preclass_addProtocol;

#   define SETUP(funcname) \
		PyObjC_##funcname = compat_##funcname

#else
	if (class_addMethod) {
		PyObjC_class_addMethodList = objc20_class_addMethodList;
		PyObjC_preclass_addMethod  = class_addMethod;
		PyObjC_preclass_addIvar    = class_addIvar;
		PyObjC_preclass_addProtocol= class_addProtocol;
	} else {
		PyObjC_class_addMethodList = compat_class_addMethodList;
		PyObjC_preclass_addMethod  = compat_preclass_addMethod;
		PyObjC_preclass_addIvar    = compat_preclass_addIvar;
		PyObjC_preclass_addProtocol= compat_preclass_addProtocol;
	}


	if (class_copyMethodList) {
		PyObjC_methodlist_magic = objc20_methodlist_magic;
	} else {
		PyObjC_methodlist_magic = compat_methodlist_magic;
	}

#   define SETUP(funcname) \
	if ((funcname) == NULL) { \
		PyObjC_##funcname = compat_##funcname; \
	} else { \
		PyObjC_##funcname = funcname; \
	} 
#endif
	SETUP(protocol_getName);
	SETUP(protocol_conformsToProtocol);
	SETUP(protocol_copyMethodDescriptionList);
	SETUP(protocol_copyProtocolList);
	SETUP(protocol_getMethodDescription);

	SETUP(objc_allocateClassPair);
	SETUP(objc_registerClassPair);
	SETUP(objc_disposeClassPair);
	SETUP(objc_copyProtocolList);
	SETUP(objc_getProtocol);

	SETUP(object_getClass);
	SETUP(object_setClass);
	SETUP(object_getClassName);
	SETUP(object_getIvar);
	SETUP(object_setIvar);

	SETUP(class_getSuperclass);
	SETUP(class_addMethod);
	SETUP(class_copyIvarList);
	SETUP(class_copyProtocolList);
	SETUP(class_copyMethodList);
	SETUP(class_getName);
	SETUP(class_isMetaClass);

	SETUP(method_getName);
	SETUP(method_getTypeEncoding);
	SETUP(method_getImplementation);
	SETUP(method_setImplementation);

	SETUP(sel_isEqual);

	SETUP(ivar_getName);
	SETUP(ivar_getTypeEncoding);
	SETUP(ivar_getOffset);
#endif /* MIN_REQUIRED < 10.5 && !OBJC2 */

#ifdef SETUP 
#undef SETUP
#endif


#if (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_7)
	/* Compat definitions for protocol creation
	 *
	 * - Runtime APIs were introduced in OSX 10.7
	 * - Compat functions can only be implemented for 32-bit runtime
	 */

#if PyObjC_BUILD_RELEASE < 1007
	
#ifndef __LP64__
	PyObjC_objc_allocateProtocol = compat_objc_allocateProtocol;
	PyObjC_objc_registerProtocol = compat_objc_registerProtocol;
	PyObjC_protocol_addMethodDescription = compat_protocol_addMethodDescription;
	PyObjC_protocol_addProtocol = compat_protocol_addProtocol;
#endif


#elif defined(__LP64__)
	PyObjC_objc_allocateProtocol = objc_allocateProtocol;
	PyObjC_objc_registerProtocol = objc_registerProtocol;
	PyObjC_protocol_addMethodDescription = protocol_addMethodDescription;
	PyObjC_protocol_addProtocol = protocol_addProtocol;

#else
#   define SETUP(funcname) \
	if ((funcname) == NULL) { \
		PyObjC_##funcname = compat_##funcname; \
	} else { \
		PyObjC_##funcname = funcname; \
	} 
	SETUP(objc_allocateProtocol);
	SETUP(objc_registerProtocol);
	SETUP(protocol_addMethodDescription);
	SETUP(protocol_addProtocol);
#endif

#endif

#undef SETUP
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.