1/* 2 * XXX: TestClass3 is contains some test-code that is not replicated in 3 * testbndl2.m, the rest should be removed after checking that 4 * testbndl2.m really does contain all tests in this file!. 5 * 6 * This file implements a (number of) class(es) that are used to test 7 * method calling with PyObjC (both python -> ObjC and back) 8 * 9 * NOTES 10 * - The implementation must be synchronized with test_methods.py, see that 11 * file for more details. 12 * - When adding new methods to OC_TestClass1 *always* add invoke- and call- 13 * variants to OC_TestClass2. 14 */ 15 16#include <Python.h> 17#include <pyobjc-api.h> 18#include <pyobjc-compat.h> 19 20#import <Foundation/Foundation.h> 21 22#ifndef GNU_RUNTIME 23#include <objc/objc-runtime.h> 24#endif 25 26#include <limits.h> 27#include <float.h> 28 29#if !defined(LLONG_MAX) && defined(LONG_LONG_MAX) 30 31# define LLONG_MAX LONG_LONG_MAX 32# define LLONG_MIN LONG_LONG_MIN 33# define ULLONG_MAX ULONG_LONG_MAX 34 35#endif 36 37struct dummy 38{ 39 int f1; 40 short f2; 41}; 42 43struct dummy2 44{ 45 int array[4]; 46}; 47 48 49struct s1 { 50 int i; 51 double d; 52}; 53 54 55struct complexStruct 56{ 57 /* 0 */ char ch; 58 /* 1 */ unsigned char uch; 59 /* 4 */ int i1; 60 /* 8 */ unsigned int u; 61 /* 12 */ short s; 62 /* 20 */ double d; 63 /* 28 */ struct s1 sub1[2]; 64 /* 60 */ int i2; 65 /* 62 */ unsigned short us; 66 /* 64 */ char* str; 67}; 68 69 70 71@interface OC_TestClass1 : NSObject 72{ 73} 74 75/* Reset the test counter */ 76+ (void)clsReset; 77- (void)reset; 78 79 80/* Test return values, return various values */ 81+ (long long)longlongClsFunc; 82+ (unsigned long long)ulonglongClsFunc; 83+ (long)longClsFunc; 84+ (unsigned long)ulongClsFunc; 85+ (int)intClsFunc; 86+ (unsigned int)uintClsFunc; 87+ (short)shortClsFunc; 88+ (unsigned short)ushortClsFunc; 89+ (char)charClsFunc; 90+ (unsigned char)ucharClsFunc; 91+ (float)floatClsFunc; 92+ (double)doubleClsFunc; 93+ (char*)charpClsFunc; 94+ (id)idClsFunc; 95 96- (long long)longlongFunc; 97- (unsigned long long)ulonglongFunc; 98- (long)longFunc; 99- (unsigned long)ulongFunc; 100- (int)intFunc; 101- (unsigned int)uintFunc; 102- (short)shortFunc; 103- (unsigned short)ushortFunc; 104- (char)charFunc; 105- (unsigned char)ucharFunc; 106- (float)floatFunc; 107- (double)doubleFunc; 108- (char*)charpFunc; 109- (id)idFunc; 110- (NSPoint)nspointFunc; 111 112/* returns of complex values */ 113- (struct dummy)dummyFunc; 114- (struct dummy2)dummy2Func; 115/* TODO: Nested structs, unions, strings */ 116 117/* argument passing for simple types */ 118- (long long)longlongArg:(long long)arg; 119- (unsigned long long)ulonglongArg:(unsigned long long)arg; 120- (long)longArg:(long)arg; 121- (unsigned long)ulongArg:(unsigned long)arg; 122- (int)intArg:(int)arg; 123- (unsigned int)uintArg:(unsigned int)arg; 124- (short)shortArg:(short)arg; 125- (unsigned short)ushortArg:(unsigned short)arg; 126- (char)charArg:(char)arg; 127- (unsigned char)ucharArg:(unsigned char)arg; 128- (float)floatArg:(float)arg; 129- (double)doubleArg:(double)arg; 130- (char*)charpArg:(char*)arg; 131- (id)idArg:(id)arg; 132 133/* argument passing for complex values */ 134- (struct dummy)dummyArg:(struct dummy)arg; 135- (struct dummy2)dummy2Arg:(struct dummy2)arg; 136/* TODO: Nested structs, unions, strings */ 137 138/* in, out and in-out arguments */ 139- (char)passInChar:(char*)arg; 140- (void)passOutChar:(char*)arg; 141- (void)passInOutChar:(char*)arg; 142- (short)passInShort:(short*)arg; 143- (void)passOutShort:(short*)arg; 144- (void)passInOutShort:(short*)arg; 145- (int)passInInt:(int*)arg; 146- (void)passOutInt:(int*)arg; 147- (void)passInOutInt:(int*)arg; 148- (long)passInLong:(long*)arg; 149- (void)passOutLong:(long*)arg; 150- (void)passInOutLong:(long*)arg; 151- (long long)passInLongLong:(long long*)arg; 152- (void)passOutLongLong:(long long*)arg; 153- (void)passInOutLongLong:(long long*)arg; 154- (unsigned char)passInUChar:(unsigned char*)arg; 155- (void)passOutUChar:(unsigned char*)arg; 156- (void)passInOutUChar:(unsigned char*)arg; 157- (unsigned short)passInUShort:(unsigned short*)arg; 158- (void)passOutUShort:(unsigned short*)arg; 159- (void)passInOutUShort:(unsigned short*)arg; 160- (unsigned int)passInUInt:(unsigned int*)arg; 161- (void)passOutUInt:(unsigned int*)arg; 162- (void)passInOutUInt:(unsigned int*)arg; 163- (unsigned long)passInULong:(unsigned long*)arg; 164- (void)passOutULong:(unsigned long*)arg; 165- (void)passInOutULong:(unsigned long*)arg; 166- (unsigned long long)passInULongLong:(unsigned long long*)arg; 167- (void)passOutULongLong:(unsigned long long*)arg; 168- (void)passInOutULongLong:(unsigned long long*)arg; 169- (float)passInFloat:(float*)arg; 170- (void)passOutFloat:(float*)arg; 171- (void)passInOutFloat:(float*)arg; 172- (double)passInDouble:(double*)arg; 173- (void)passOutDouble:(double*)arg; 174- (void)passInOutDouble:(double*)arg; 175- (char*)passInCharp:(char**)arg; 176- (void)passOutCharp:(char**)arg; 177- (void)passInOutCharp:(char**)arg; 178- (id)passInID:(id*)arg; 179- (void)passOutID:(id*)arg; 180- (void)passInOutID:(id*)arg; 181 182@end 183 184@implementation OC_TestClass1 185 186#define ARRAYSIZE(a) (sizeof(a)/sizeof(a[0])) 187static size_t g_idx = 0; 188static unsigned long long g_ulonglongs[] = { 189 0, 42, (1LL << 63) 190}; 191static unsigned long g_ulongs[] = { 192 0, 42, (1 << 30) 193}; 194static long long g_longlongs[] = { 195 -(1LL << 60), -42, 0, 42, (1LL << 60) 196}; 197static long g_longs[] = { 198 -(1 << 30), -42, 0, 42, (1 << 30) 199}; 200static int g_ints[] = { 201 -(1 << 30), -42, 0, 42, (1 << 30) 202}; 203static unsigned int g_uints[] = { 204 0, 42, 1 << 30 205}; 206static short g_shorts[] = { 207 -(1 << 14), -42, 0, 42, (1 << 14) 208}; 209static unsigned short g_ushorts[] = { 210 0, 42, 1 << 14 211}; 212static char g_chars[] = { 213 -128, 0, 127 214}; 215static unsigned char g_uchars[] = { 216 0, 128, 255 217}; 218static float g_floats[] = { 219 0.128, 1.0, 42.0, 1e10 220}; 221static double g_doubles[] = { 222 0.128, 1.0, 42.0, 1e10 223}; 224 225static char* g_charps[] = { 226 "hello", 227 "world", 228 "foobar" 229}; 230 231+ (void)clsReset 232{ 233 g_idx = 0; 234} 235 236+ (long long)longlongClsFunc 237{ 238 if (g_idx > ARRAYSIZE(g_longlongs)) g_idx = 0; 239 return g_longlongs[g_idx++]; 240} 241 242+ (unsigned long long)ulonglongClsFunc 243{ 244 if (g_idx > ARRAYSIZE(g_ulonglongs)) g_idx = 0; 245 return g_ulonglongs[g_idx++]; 246} 247 248+ (long)longClsFunc 249{ 250 if (g_idx > ARRAYSIZE(g_longs)) g_idx = 0; 251 return g_longs[g_idx++]; 252} 253 254+ (unsigned long)ulongClsFunc 255{ 256 if (g_idx > ARRAYSIZE(g_ulongs)) g_idx = 0; 257 return g_ulongs[g_idx++]; 258} 259 260+ (int)intClsFunc 261{ 262 if (g_idx > ARRAYSIZE(g_ints)) g_idx = 0; 263 return g_ints[g_idx++]; 264} 265 266+ (unsigned int)uintClsFunc 267{ 268 if (g_idx > ARRAYSIZE(g_uints)) g_idx = 0; 269 return g_uints[g_idx++]; 270} 271 272+ (short)shortClsFunc 273{ 274 if (g_idx > ARRAYSIZE(g_shorts)) g_idx = 0; 275 return g_shorts[g_idx++]; 276} 277 278+ (unsigned short)ushortClsFunc 279{ 280 if (g_idx > ARRAYSIZE(g_ushorts)) g_idx = 0; 281 return g_ushorts[g_idx++]; 282} 283 284+ (char)charClsFunc 285{ 286 if (g_idx > ARRAYSIZE(g_chars)) g_idx = 0; 287 return g_chars[g_idx++]; 288} 289 290+ (unsigned char)ucharClsFunc 291{ 292 if (g_idx > ARRAYSIZE(g_uchars)) g_idx = 0; 293 return g_uchars[g_idx++]; 294} 295 296+ (float)floatClsFunc 297{ 298 if (g_idx > ARRAYSIZE(g_floats)) g_idx = 0; 299 return g_floats[g_idx++]; 300} 301 302+ (double)doubleClsFunc 303{ 304 if (g_idx > ARRAYSIZE(g_doubles)) g_idx = 0; 305 return g_doubles[g_idx++]; 306} 307 308+ (char*)charpClsFunc 309{ 310 if (g_idx > ARRAYSIZE(g_charps)) g_idx = 0; 311 return g_charps[g_idx++]; 312} 313 314+ (id)idClsFunc 315{ 316 if (g_idx > 3) g_idx = 0; 317 318 switch (g_idx ++) { 319 case 0: return [NSArray array]; 320 case 1: return [NSHost hostWithAddress:@"127.0.0.1"]; 321 case 2: return [NSMutableDictionary dictionary]; 322 case 3: return NULL; 323 } 324 return NULL; 325} 326 327- (void)reset 328{ 329 g_idx = 0; 330} 331 332- (long long)longlongFunc 333{ 334 if (g_idx > ARRAYSIZE(g_longlongs)) g_idx = 0; 335 return g_longlongs[g_idx++]; 336} 337 338- (unsigned long long)ulonglongFunc 339{ 340 if (g_idx > ARRAYSIZE(g_ulonglongs)) g_idx = 0; 341 return g_ulonglongs[g_idx++]; 342} 343 344- (long)longFunc 345{ 346 if (g_idx > ARRAYSIZE(g_longs)) g_idx = 0; 347 return g_longs[g_idx++]; 348} 349 350- (unsigned long)ulongFunc 351{ 352 if (g_idx > ARRAYSIZE(g_ulongs)) g_idx = 0; 353 return g_ulongs[g_idx++]; 354} 355 356- (int)intFunc 357{ 358 if (g_idx > ARRAYSIZE(g_ints)) g_idx = 0; 359 return g_ints[g_idx++]; 360} 361 362- (unsigned int)uintFunc 363{ 364 if (g_idx > ARRAYSIZE(g_uints)) g_idx = 0; 365 return g_uints[g_idx++]; 366} 367 368- (short)shortFunc 369{ 370 if (g_idx > ARRAYSIZE(g_shorts)) g_idx = 0; 371 return g_shorts[g_idx++]; 372} 373 374- (unsigned short)ushortFunc 375{ 376 if (g_idx > ARRAYSIZE(g_ushorts)) g_idx = 0; 377 return g_ushorts[g_idx++]; 378} 379 380- (char)charFunc 381{ 382 if (g_idx > ARRAYSIZE(g_chars)) g_idx = 0; 383 return g_chars[g_idx++]; 384} 385 386- (unsigned char)ucharFunc 387{ 388 if (g_idx > ARRAYSIZE(g_uchars)) g_idx = 0; 389 return g_uchars[g_idx++]; 390} 391 392- (float)floatFunc 393{ 394 if (g_idx > ARRAYSIZE(g_floats)) g_idx = 0; 395 return g_floats[g_idx++]; 396} 397 398- (double)doubleFunc 399{ 400 if (g_idx > ARRAYSIZE(g_doubles)) g_idx = 0; 401 return g_doubles[g_idx++]; 402} 403 404- (char*)charpFunc 405{ 406 if (g_idx > ARRAYSIZE(g_charps)) g_idx = 0; 407 return g_charps[g_idx++]; 408} 409 410- (id)idFunc 411{ 412 if (g_idx > 3) g_idx = 0; 413 414 switch (g_idx ++) { 415 case 0: return [NSArray array]; 416 case 1: return [NSHost hostWithAddress:@"127.0.0.1"]; 417 case 2: return [NSMutableDictionary dictionary]; 418 case 3: return NULL; 419 } 420 return NULL; 421} 422 423- (NSPoint)nspointFunc 424{ 425 NSPoint p = { 1.0, 2.0 }; 426 return p; 427} 428 429- (struct dummy)dummyFunc 430{ 431 struct dummy res; 432 433 res.f1 = -1; 434 res.f2 = +1; 435 436 return res; 437} 438 439- (struct dummy2)dummy2Func 440{ 441 struct dummy2 res; 442 443 res.array[0] = 1; 444 res.array[1] = 2; 445 res.array[2] = 3; 446 res.array[3] = 4; 447 448 return res; 449} 450 451/* 452 * 453 * Simple, single, pass by value arguments. We return the argument after 454 * modifying it. This should help to uncover cases where the bridge returns 455 * the first argument instead of the real return value. 456 */ 457 458- (long long)longlongArg:(long long)arg 459{ 460 return arg / 2; 461} 462 463- (unsigned long long)ulonglongArg:(unsigned long long)arg 464{ 465 return arg / 2; 466} 467 468- (long)longArg:(long)arg 469{ 470 return arg / 2; 471} 472 473- (unsigned long)ulongArg:(unsigned long)arg 474{ 475 return arg / 2; 476} 477 478- (int)intArg:(int)arg 479{ 480 return arg / 2; 481} 482 483- (unsigned int)uintArg:(unsigned int)arg 484{ 485 return arg / 2; 486} 487 488- (short)shortArg:(short)arg 489{ 490 return arg / 2; 491} 492 493- (unsigned short)ushortArg:(unsigned short)arg 494{ 495 return arg / 2; 496} 497 498- (char)charArg:(char)arg 499{ 500 return arg / 2; 501} 502 503- (unsigned char)ucharArg:(unsigned char)arg 504{ 505 return arg / 2; 506} 507 508- (float)floatArg:(float)arg 509{ 510 return arg / 2; 511} 512 513- (double)doubleArg:(double)arg 514{ 515 return arg / 2; 516} 517 518- (char*)charpArg:(char*)arg 519{ 520static char buf[1024]; 521 int len = strlen(arg); 522 int i; 523 524 for (i = 0; i < len; i++) { 525 buf[len - i - 1] = arg[i]; 526 } 527 buf[len] = 0; 528 529 return buf; 530} 531 532- (id)idArg:(id)arg 533{ 534 id temp; 535 536 if (arg == NULL) { 537 temp = [NSNull null]; 538 } else { 539 temp = arg; 540 } 541 return [NSArray arrayWithObject:temp]; 542} 543 544- (struct dummy)dummyArg:(struct dummy)arg 545{ 546 struct dummy result; 547 548 result.f1 = arg.f1 * 2; 549 result.f2 = arg.f2 * 2; 550 551 return result; 552} 553 554- (struct dummy2) dummy2Arg:(struct dummy2)arg 555{ 556 struct dummy2 result = { {-1, -1, -1, -1} }; 557 558 result.array[0] = arg.array[3] * 2; 559 result.array[1] = arg.array[2] * 2; 560 result.array[2] = arg.array[1] * 2; 561 result.array[3] = arg.array[0] * 2; 562 563 return result; 564} 565 566- (char)passInChar:(char*)arg 567{ 568 return *arg + 9; 569} 570 571- (void)passOutChar:(char*)arg 572{ 573 if (g_idx > ARRAYSIZE(g_ints)) g_idx = 0; 574 *arg = g_chars[g_idx++]; 575} 576 577- (void)passInOutChar:(char*)arg 578{ 579 *arg += 42; 580} 581 582- (unsigned char)passInUChar:(unsigned char*)arg 583{ 584 return *arg + 9; 585} 586 587- (void)passOutUChar:(unsigned char*)arg 588{ 589 if (g_idx > ARRAYSIZE(g_uchars)) g_idx = 0; 590 *arg = g_uchars[g_idx++]; 591} 592 593- (void)passInOutUChar:(unsigned char*)arg 594{ 595 *arg += 42; 596} 597 598- (short)passInShort:(short*)arg 599{ 600 return *arg + 9; 601} 602 603- (void)passOutShort:(short*)arg 604{ 605 if (g_idx > ARRAYSIZE(g_shorts)) g_idx = 0; 606 *arg = g_shorts[g_idx++]; 607} 608 609- (void)passInOutShort:(short*)arg 610{ 611 *arg += 42; 612} 613 614- (unsigned short)passInUShort:(unsigned short*)arg 615{ 616 return *arg + 9; 617} 618 619- (void)passOutUShort:(unsigned short*)arg 620{ 621 if (g_idx > ARRAYSIZE(g_ushorts)) g_idx = 0; 622 *arg = g_ushorts[g_idx++]; 623} 624 625- (void)passInOutUShort:(unsigned short*)arg 626{ 627 *arg += 42; 628} 629 630- (int)passInInt:(int*)arg 631{ 632 return *arg + 9; 633} 634 635- (void)passOutInt:(int*)arg 636{ 637 if (g_idx > ARRAYSIZE(g_ints)) g_idx = 0; 638 *arg = g_ints[g_idx++]; 639} 640 641- (void)passInOutInt:(int*)arg 642{ 643 *arg += 42; 644} 645 646- (unsigned int)passInUInt:(unsigned int*)arg 647{ 648 return *arg + 9; 649} 650 651- (void)passOutUInt:(unsigned int*)arg 652{ 653 if (g_idx > ARRAYSIZE(g_uints)) g_idx = 0; 654 *arg = g_uints[g_idx++]; 655} 656 657- (void)passInOutUInt:(unsigned int*)arg 658{ 659 *arg += 42; 660} 661 662- (long)passInLong:(long*)arg 663{ 664 return *arg + 9; 665} 666 667- (void)passOutLong:(long*)arg 668{ 669 if (g_idx > ARRAYSIZE(g_longs)) g_idx = 0; 670 *arg = g_longs[g_idx++]; 671} 672 673- (void)passInOutLong:(long*)arg 674{ 675 *arg += 42; 676} 677 678- (unsigned long)passInULong:(unsigned long*)arg 679{ 680 return *arg + 9; 681} 682 683- (void)passOutULong:(unsigned long*)arg 684{ 685 if (g_idx > ARRAYSIZE(g_ulongs)) g_idx = 0; 686 *arg = g_ulongs[g_idx++]; 687} 688 689- (void)passInOutULong:(unsigned long*)arg 690{ 691 *arg += 42; 692} 693 694- (long long)passInLongLong:(long long*)arg 695{ 696 return *arg + 9; 697} 698 699- (void)passOutLongLong:(long long*)arg 700{ 701 if (g_idx > ARRAYSIZE(g_longlongs)) g_idx = 0; 702 *arg = g_longlongs[g_idx++]; 703} 704 705- (void)passInOutLongLong:(long long*)arg 706{ 707 *arg += 42; 708} 709 710- (unsigned long long)passInULongLong:(unsigned long long*)arg 711{ 712 return *arg + 9; 713} 714 715- (void)passOutULongLong:(unsigned long long*)arg 716{ 717 if (g_idx > ARRAYSIZE(g_ulonglongs)) g_idx = 0; 718 *arg = g_ulonglongs[g_idx++]; 719} 720 721- (void)passInOutULongLong:(unsigned long long*)arg 722{ 723 *arg += 42; 724} 725 726- (float)passInFloat:(float*)arg 727{ 728 return *arg * 9; 729} 730 731- (void)passOutFloat:(float*)arg 732{ 733 if (g_idx > ARRAYSIZE(g_floats)) g_idx = 0; 734 *arg = g_floats[g_idx++]; 735} 736 737- (void)passInOutFloat:(float*)arg 738{ 739 *arg *= 42; 740} 741 742- (double)passInDouble:(double*)arg 743{ 744 return *arg * 9; 745} 746 747- (void)passOutDouble:(double*)arg 748{ 749 if (g_idx > ARRAYSIZE(g_doubles)) g_idx = 0; 750 *arg = g_doubles[g_idx++]; 751} 752 753- (void)passInOutDouble:(double*)arg 754{ 755 *arg *= 42; 756} 757 758- (char*)passInCharp:(char**)arg 759{ 760 /* Yes this is leaking, but we're only testing method calling */ 761 int len = strlen(*arg); 762 char* res = malloc(len * 2 + 1); 763 char* p; 764 char* q; 765 766 for (p = *arg, q = res; *p; p++) { 767 *q ++ = *p; 768 *q ++ = *p; 769 } 770 *q = 0; 771 return res; 772} 773 774- (void)passOutCharp:(char**)arg 775{ 776 if (g_idx > ARRAYSIZE(g_charps)) g_idx = 0; 777 *arg = g_charps[g_idx++]; 778} 779 780- (void)passInOutCharp:(char**)arg 781{ 782 /* Yes this is leaking, but we're only testing method calling */ 783 int len = strlen(*arg); 784 char* res = malloc(len * 2 + 1); 785 char* p; 786 char* q; 787 788 for (p = *arg, q = res; *p; p++) { 789 *q ++ = *p; 790 *q ++ = *p; 791 } 792 *q = 0; 793 *arg = res; 794} 795 796- (id)passInID:(id*)arg 797{ 798 id temp; 799 800 if (*arg == NULL) { 801 temp = [NSNull null]; 802 } else { 803 temp = *arg; 804 } 805 return [NSArray arrayWithObject:temp]; 806} 807 808- (void)passOutID:(id*)arg 809{ 810 if (g_idx > 3) g_idx = 0; 811 812 switch (g_idx ++) { 813 case 0: *arg = [NSArray array]; break; 814 case 1: *arg = [NSHost hostWithAddress:@"127.0.0.1"]; break; 815 case 2: *arg = [NSMutableDictionary dictionary]; break; 816 case 3: *arg = NULL; break; 817 } 818} 819 820- (void)passInOutID:(id*)arg 821{ 822 id temp; 823 824 if (*arg == NULL) { 825 temp = [NSNull null]; 826 } else { 827 temp = *arg; 828 } 829 *arg = [NSArray arrayWithObject:temp]; 830} 831 832@end 833 834 835 836 837/* 838 * Testing of calling into python 839 * 840 * This is *very* incomplete at the moment, we need 'call' and 'invoke' 841 * versions for all methods in OC_TestClass1 (both class and instance methods) 842 */ 843 844@interface OC_TestClass2: NSObject 845{ 846} 847 848+(Class)classOfObject:(NSObject*)arg; 849 850/* "plain" calls */ 851-(char)callInstanceCharFuncOf:(OC_TestClass1*)arg; 852-(unsigned char)callInstanceUnsignedCharFuncOf:(OC_TestClass1*)arg; 853 854-(short)callInstanceShortFuncOf:(OC_TestClass1*)arg; 855-(unsigned short)callInstanceUnsignedShortFuncOf:(OC_TestClass1*)arg; 856 857-(int)callInstanceIntFuncOf:(OC_TestClass1*)arg; 858-(unsigned int)callInstanceUnsignedIntFuncOf:(OC_TestClass1*)arg; 859 860-(long)callInstanceLongFuncOf:(OC_TestClass1*)arg; 861-(unsigned long)callInstanceUnsignedLongFuncOf:(OC_TestClass1*)arg; 862 863-(long long)callInstanceLongLongFuncOf:(OC_TestClass1*)arg; 864-(unsigned long long)callInstanceUnsignedLongLongFuncOf:(OC_TestClass1*)arg; 865 866-(float)callInstanceFloatFuncOf:(OC_TestClass1*)arg; 867-(double)callInstanceDoubleFuncOf:(OC_TestClass1*)arg; 868 869-(id)callInstanceIdFuncOf:(OC_TestClass1*)arg; 870-(struct dummy)callInstanceDummyFuncOf:(OC_TestClass1*)arg; 871-(struct dummy2)callInstanceDummy2FuncOf:(OC_TestClass1*)arg; 872-(NSPoint)callInstanceNSPointFuncOf:(OC_TestClass1*)arg; 873 874- (long long)callInstanceLongLongArg:(long long)arg on:(OC_TestClass1*)obj; 875- (unsigned long long)callInstanceUnsignedLongLongArg:(unsigned long long)arg on:(OC_TestClass1*)obj; 876- (long)callInstanceLongArg:(long)arg on:(OC_TestClass1*)obj; 877- (unsigned long)callInstanceUnsignedLongArg:(unsigned long)arg on:(OC_TestClass1*)obj; 878- (int)callInstanceIntArg:(int)arg on:(OC_TestClass1*)obj; 879- (unsigned int)callInstanceUnsignedIntArg:(unsigned int)arg on:(OC_TestClass1*)obj; 880- (short)callInstanceShortArg:(short)arg on:(OC_TestClass1*)obj; 881- (unsigned short)callInstanceUnsignedShortArg:(unsigned short)arg on:(OC_TestClass1*)obj; 882- (char)callInstanceCharArg:(char)arg on:(OC_TestClass1*)obj; 883- (unsigned char)callInstanceUnsignedCharArg:(unsigned char)arg on:(OC_TestClass1*)obj; 884- (float)callInstanceFloatArg:(float)arg on:(OC_TestClass1*)obj; 885- (double)callInstanceDoubleArg:(double)arg on:(OC_TestClass1*)obj; 886- (char*)callInstanceCharpArg:(char*)arg on:(OC_TestClass1*)obj; 887- (id)callInstanceIdArg:(id)arg on:(OC_TestClass1*)obj; 888 889/* "NSInvocation" calls */ 890-(char)invokeInstanceCharFuncOf:(OC_TestClass1*)arg; 891-(unsigned char)invokeInstanceUnsignedCharFuncOf:(OC_TestClass1*)arg; 892 893-(short)invokeInstanceShortFuncOf:(OC_TestClass1*)arg; 894-(unsigned short)invokeInstanceUnsignedShortFuncOf:(OC_TestClass1*)arg; 895 896-(int)invokeInstanceIntFuncOf:(OC_TestClass1*)arg; 897-(unsigned int)invokeInstanceUnsignedIntFuncOf:(OC_TestClass1*)arg; 898 899-(long)invokeInstanceLongFuncOf:(OC_TestClass1*)arg; 900-(unsigned long)invokeInstanceUnsignedLongFuncOf:(OC_TestClass1*)arg; 901 902-(long long)invokeInstanceLongLongFuncOf:(OC_TestClass1*)arg; 903-(unsigned long long)invokeInstanceUnsignedLongLongFuncOf:(OC_TestClass1*)arg; 904 905-(float)invokeInstanceFloatFuncOf:(OC_TestClass1*)arg; 906-(double)invokeInstanceDoubleFuncOf:(OC_TestClass1*)arg; 907 908-(id)invokeInstanceIdFuncOf:(OC_TestClass1*)arg; 909-(struct dummy)invokeInstanceDummyFuncOf:(OC_TestClass1*)arg; 910-(struct dummy2)invokeInstanceDummy2FuncOf:(OC_TestClass1*)arg; 911-(NSPoint)invokeInstanceNSPointFuncOf:(OC_TestClass1*)arg; 912 913- (long long)invokeInstanceLongLongArg:(long long)arg on:(OC_TestClass1*)obj; 914- (unsigned long long)invokeInstanceUnsignedLongLongArg:(unsigned long long)arg on:(OC_TestClass1*)obj; 915- (long)invokeInstanceLongArg:(long)arg on:(OC_TestClass1*)obj; 916- (unsigned long)invokeInstanceUnsignedLongArg:(unsigned long)arg on:(OC_TestClass1*)obj; 917- (int)invokeInstanceIntArg:(int)arg on:(OC_TestClass1*)obj; 918- (unsigned int)invokeInstanceUnsignedIntArg:(unsigned int)arg on:(OC_TestClass1*)obj; 919- (short)invokeInstanceShortArg:(short)arg on:(OC_TestClass1*)obj; 920- (unsigned short)invokeInstanceUnsignedShortArg:(unsigned short)arg on:(OC_TestClass1*)obj; 921- (char)invokeInstanceCharArg:(char)arg on:(OC_TestClass1*)obj; 922- (unsigned char)invokeInstanceUnsignedCharArg:(unsigned char)arg on:(OC_TestClass1*)obj; 923- (float)invokeInstanceFloatArg:(float)arg on:(OC_TestClass1*)obj; 924- (double)invokeInstanceDoubleArg:(double)arg on:(OC_TestClass1*)obj; 925- (char*)invokeInstanceCharpArg:(char*)arg on:(OC_TestClass1*)obj; 926- (id)invokeInstanceIdArg:(id)arg on:(OC_TestClass1*)obj; 927 928@end 929 930 931#define SETUP_INVOCATION(inv, target, selector) \ 932 inv = [NSInvocation invocationWithMethodSignature: \ 933 [target methodSignatureForSelector:selector]]; \ 934 [inv setTarget:target]; \ 935 [inv setSelector:selector]; 936 937@implementation OC_TestClass2 938 939+(Class)classOfObject:(NSObject*)arg 940{ 941 return [arg class]; 942} 943 944-(char)callInstanceCharFuncOf:(OC_TestClass1*)arg 945{ 946 return [arg charFunc]; 947} 948 949-(unsigned char)callInstanceUnsignedCharFuncOf:(OC_TestClass1*)arg 950{ 951 return [arg ucharFunc]; 952} 953 954-(short)callInstanceShortFuncOf:(OC_TestClass1*)arg 955{ 956 return [arg shortFunc]; 957} 958 959-(unsigned short)callInstanceUnsignedShortFuncOf:(OC_TestClass1*)arg 960{ 961 return [arg ushortFunc]; 962} 963 964-(int)callInstanceIntFuncOf:(OC_TestClass1*)arg 965{ 966 return [arg intFunc]; 967} 968 969-(unsigned int)callInstanceUnsignedIntFuncOf:(OC_TestClass1*)arg 970{ 971 return [arg uintFunc]; 972} 973 974-(long)callInstanceLongFuncOf:(OC_TestClass1*)arg 975{ 976 return [arg longFunc]; 977} 978 979-(unsigned long)callInstanceUnsignedLongFuncOf:(OC_TestClass1*)arg 980{ 981 return [arg ulongFunc]; 982} 983 984-(char)invokeInstanceCharFuncOf:(OC_TestClass1*)arg 985{ 986 char res; 987 NSInvocation* inv; 988 989 SETUP_INVOCATION(inv, arg, @selector(charFunc)) 990 991 [arg forwardInvocation:inv]; 992 [inv getReturnValue:&res]; 993 return res; 994} 995 996-(unsigned char)invokeInstanceUnsignedCharFuncOf:(OC_TestClass1*)arg 997{ 998 unsigned char res; 999 NSInvocation* inv; 1000 1001 SETUP_INVOCATION(inv, arg, @selector(ucharFunc)) 1002 1003 [arg forwardInvocation:inv]; 1004 [inv getReturnValue:&res]; 1005 return res; 1006} 1007 1008-(short)invokeInstanceShortFuncOf:(OC_TestClass1*)arg 1009{ 1010 short res; 1011 NSInvocation* inv; 1012 1013 SETUP_INVOCATION(inv, arg, @selector(shortFunc)) 1014 1015 [arg forwardInvocation:inv]; 1016 [inv getReturnValue:&res]; 1017 return res; 1018} 1019 1020-(unsigned short)invokeInstanceUnsignedShortFuncOf:(OC_TestClass1*)arg 1021{ 1022 unsigned short res; 1023 NSInvocation* inv; 1024 1025 SETUP_INVOCATION(inv, arg, @selector(ushortFunc)) 1026 1027 [arg forwardInvocation:inv]; 1028 [inv getReturnValue:&res]; 1029 return res; 1030} 1031 1032-(int)invokeInstanceIntFuncOf:(OC_TestClass1*)arg 1033{ 1034 int res; 1035 NSInvocation* inv; 1036 1037 SETUP_INVOCATION(inv, arg, @selector(intFunc)) 1038 1039 [arg forwardInvocation:inv]; 1040 [inv getReturnValue:&res]; 1041 return res; 1042} 1043 1044-(unsigned int)invokeInstanceUnsignedIntFuncOf:(OC_TestClass1*)arg 1045{ 1046 unsigned int res; 1047 NSInvocation* inv; 1048 1049 SETUP_INVOCATION(inv, arg, @selector(uintFunc)) 1050 1051 [arg forwardInvocation:inv]; 1052 [inv getReturnValue:&res]; 1053 return res; 1054} 1055 1056-(long)invokeInstanceLongFuncOf:(OC_TestClass1*)arg 1057{ 1058 long res; 1059 NSInvocation* inv; 1060 1061 SETUP_INVOCATION(inv, arg, @selector(longFunc)) 1062 1063 [arg forwardInvocation:inv]; 1064 [inv getReturnValue:&res]; 1065 return res; 1066} 1067 1068-(unsigned long)invokeInstanceUnsignedLongFuncOf:(OC_TestClass1*)arg 1069{ 1070 unsigned long res; 1071 NSInvocation* inv; 1072 1073 SETUP_INVOCATION(inv, arg, @selector(ulongFunc)) 1074 1075 [arg forwardInvocation:inv]; 1076 [inv getReturnValue:&res]; 1077 return res; 1078} 1079 1080-(long long)callInstanceLongLongFuncOf:(OC_TestClass1*)arg 1081{ 1082 return [arg longlongFunc]; 1083} 1084 1085-(unsigned long long)callInstanceUnsignedLongLongFuncOf:(OC_TestClass1*)arg 1086{ 1087 return [arg ulonglongFunc]; 1088} 1089 1090-(float)callInstanceFloatFuncOf:(OC_TestClass1*)arg 1091{ 1092 return [arg floatFunc]; 1093} 1094 1095 1096-(double)callInstanceDoubleFuncOf:(OC_TestClass1*)arg 1097{ 1098 return [arg doubleFunc]; 1099} 1100 1101-(id)callInstanceIdFuncOf:(OC_TestClass1*)arg 1102{ 1103 return [arg idFunc]; 1104} 1105 1106-(struct dummy)callInstanceDummyFuncOf:(OC_TestClass1*)arg 1107{ 1108 return [arg dummyFunc]; 1109} 1110 1111-(struct dummy2)callInstanceDummy2FuncOf:(OC_TestClass1*)arg 1112{ 1113 struct dummy2 tmpval; 1114 1115 tmpval = [arg dummy2Func]; 1116 return tmpval; 1117} 1118 1119-(NSPoint)callInstanceNSPointFuncOf:(OC_TestClass1*)arg 1120{ 1121 return [arg nspointFunc]; 1122} 1123 1124- (long long)callInstanceLongLongArg:(long long)arg on:(OC_TestClass1*)obj 1125{ 1126 return [obj longlongArg:arg]; 1127} 1128 1129- (unsigned long long)callInstanceUnsignedLongLongArg:(unsigned long long)arg on:(OC_TestClass1*)obj 1130{ 1131 return [obj ulonglongArg:arg]; 1132} 1133 1134- (long)callInstanceLongArg:(long)arg on:(OC_TestClass1*)obj 1135{ 1136 return [obj longArg:arg]; 1137} 1138 1139- (unsigned long)callInstanceUnsignedLongArg:(unsigned long)arg on:(OC_TestClass1*)obj 1140{ 1141 return [obj ulongArg:arg]; 1142} 1143 1144- (int)callInstanceIntArg:(int)arg on:(OC_TestClass1*)obj 1145{ 1146 return [obj intArg:arg]; 1147} 1148 1149- (unsigned int)callInstanceUnsignedIntArg:(unsigned int)arg on:(OC_TestClass1*)obj 1150{ 1151 return [obj uintArg:arg]; 1152} 1153 1154- (short)callInstanceShortArg:(short)arg on:(OC_TestClass1*)obj 1155{ 1156 return [obj shortArg:arg]; 1157} 1158 1159- (unsigned short)callInstanceUnsignedShortArg:(unsigned short)arg on:(OC_TestClass1*)obj 1160{ 1161 return [obj ushortArg:arg]; 1162} 1163 1164- (char)callInstanceCharArg:(char)arg on:(OC_TestClass1*)obj 1165{ 1166 return [obj charArg:arg]; 1167} 1168 1169- (unsigned char)callInstanceUnsignedCharArg:(unsigned char)arg on:(OC_TestClass1*)obj 1170{ 1171 return [obj ucharArg:arg]; 1172} 1173 1174- (float)callInstanceFloatArg:(float)arg on:(OC_TestClass1*)obj 1175{ 1176 return [obj floatArg:arg]; 1177} 1178 1179- (double)callInstanceDoubleArg:(double)arg on:(OC_TestClass1*)obj 1180{ 1181 return [obj doubleArg:arg]; 1182} 1183 1184- (char*)callInstanceCharpArg:(char*)arg on:(OC_TestClass1*)obj 1185{ 1186 return [obj charpArg:arg]; 1187} 1188 1189- (id)callInstanceIdArg:(id)arg on:(OC_TestClass1*)obj 1190{ 1191 return [obj idArg:arg]; 1192} 1193 1194 1195 1196-(long long)invokeInstanceLongLongFuncOf:(OC_TestClass1*)arg 1197{ 1198 long long res; 1199 NSInvocation* inv; 1200 1201 SETUP_INVOCATION(inv, arg, @selector(longlongFunc)) 1202 1203 [arg forwardInvocation:inv]; 1204 [inv getReturnValue:&res]; 1205 return res; 1206} 1207 1208-(unsigned long long)invokeInstanceUnsignedLongLongFuncOf:(OC_TestClass1*)arg 1209{ 1210 unsigned long long res; 1211 NSInvocation* inv; 1212 1213 SETUP_INVOCATION(inv, arg, @selector(ulonglongFunc)) 1214 1215 [arg forwardInvocation:inv]; 1216 [inv getReturnValue:&res]; 1217 return res; 1218} 1219 1220-(float)invokeInstanceFloatFuncOf:(OC_TestClass1*)arg 1221{ 1222 float res; 1223 NSInvocation* inv; 1224 1225 SETUP_INVOCATION(inv, arg, @selector(floatFunc)) 1226 1227 [arg forwardInvocation:inv]; 1228 [inv getReturnValue:&res]; 1229 return res; 1230} 1231 1232-(double)invokeInstanceDoubleFuncOf:(OC_TestClass1*)arg 1233{ 1234 double res; 1235 NSInvocation* inv; 1236 1237 SETUP_INVOCATION(inv, arg, @selector(doubleFunc)) 1238 1239 [arg forwardInvocation:inv]; 1240 [inv getReturnValue:&res]; 1241 return res; 1242} 1243 1244-(id)invokeInstanceIdFuncOf:(OC_TestClass1*)arg 1245{ 1246 id res; 1247 NSInvocation* inv; 1248 1249 SETUP_INVOCATION(inv, arg, @selector(idFunc)) 1250 1251 [arg forwardInvocation:inv]; 1252 [inv getReturnValue:&res]; 1253 return res; 1254} 1255 1256-(struct dummy)invokeInstanceDummyFuncOf:(OC_TestClass1*)arg 1257{ 1258 struct dummy res; 1259 NSInvocation* inv; 1260 1261 SETUP_INVOCATION(inv, arg, @selector(dummyFunc)) 1262 1263 [arg forwardInvocation:inv]; 1264 [inv getReturnValue:&res]; 1265 return res; 1266} 1267 1268-(struct dummy2)invokeInstanceDummy2FuncOf:(OC_TestClass1*)arg 1269{ 1270 struct dummy2 res; 1271 NSInvocation* inv; 1272 1273 SETUP_INVOCATION(inv, arg, @selector(dummy2Func)) 1274 1275 [arg forwardInvocation:inv]; 1276 [inv getReturnValue:&res]; 1277 return res; 1278} 1279 1280-(NSPoint)invokeInstanceNSPointFuncOf:(OC_TestClass1*)arg 1281{ 1282 NSPoint res; 1283 NSInvocation* inv; 1284 1285 SETUP_INVOCATION(inv, arg, @selector(nspointFunc)) 1286 1287 [arg forwardInvocation:inv]; 1288 [inv getReturnValue:&res]; 1289 return res; 1290} 1291 1292- (long long)invokeInstanceLongLongArg:(long long)arg on:(OC_TestClass1*)obj 1293{ 1294 long long res; 1295 NSInvocation* inv; 1296 1297 SETUP_INVOCATION(inv, obj, @selector(longlongArg:)) 1298 [inv setArgument:&arg atIndex:2]; // First real argument 1299 1300 [obj forwardInvocation:inv]; 1301 [inv getReturnValue:&res]; 1302 return res; 1303} 1304 1305- (unsigned long long)invokeInstanceUnsignedLongLongArg:(unsigned long long)arg on:(OC_TestClass1*)obj 1306{ 1307 unsigned long long res; 1308 NSInvocation* inv; 1309 1310 SETUP_INVOCATION(inv, obj, @selector(ulonglongArg:)) 1311 [inv setArgument:&arg atIndex:2]; // First real argument 1312 1313 [obj forwardInvocation:inv]; 1314 [inv getReturnValue:&res]; 1315 return res; 1316} 1317 1318- (long)invokeInstanceLongArg:(long)arg on:(OC_TestClass1*)obj 1319{ 1320 long res; 1321 NSInvocation* inv; 1322 1323 SETUP_INVOCATION(inv, obj, @selector(longArg:)) 1324 [inv setArgument:&arg atIndex:2]; // First real argument 1325 1326 [obj forwardInvocation:inv]; 1327 [inv getReturnValue:&res]; 1328 return res; 1329} 1330 1331- (unsigned long)invokeInstanceUnsignedLongArg:(unsigned long)arg on:(OC_TestClass1*)obj 1332{ 1333 unsigned long res; 1334 NSInvocation* inv; 1335 1336 SETUP_INVOCATION(inv, obj, @selector(ulonglongArg:)) 1337 [inv setArgument:&arg atIndex:2]; // First real argument 1338 1339 [obj forwardInvocation:inv]; 1340 [inv getReturnValue:&res]; 1341 return res; 1342} 1343 1344- (int)invokeInstanceIntArg:(int)arg on:(OC_TestClass1*)obj 1345{ 1346 int res; 1347 NSInvocation* inv; 1348 1349 SETUP_INVOCATION(inv, obj, @selector(intArg:)) 1350 [inv setArgument:&arg atIndex:2]; // First real argument 1351 1352 [obj forwardInvocation:inv]; 1353 [inv getReturnValue:&res]; 1354 return res; 1355} 1356 1357- (unsigned int)invokeInstanceUnsignedIntArg:(unsigned int)arg on:(OC_TestClass1*)obj 1358{ 1359 unsigned int res; 1360 NSInvocation* inv; 1361 1362 SETUP_INVOCATION(inv, obj, @selector(uintArg:)) 1363 [inv setArgument:&arg atIndex:2]; // First real argument 1364 1365 [obj forwardInvocation:inv]; 1366 [inv getReturnValue:&res]; 1367 return res; 1368} 1369 1370- (short)invokeInstanceShortArg:(short)arg on:(OC_TestClass1*)obj 1371{ 1372 short res; 1373 NSInvocation* inv; 1374 1375 SETUP_INVOCATION(inv, obj, @selector(shortArg:)) 1376 [inv setArgument:&arg atIndex:2]; // First real argument 1377 1378 [obj forwardInvocation:inv]; 1379 [inv getReturnValue:&res]; 1380 return res; 1381} 1382 1383- (unsigned short)invokeInstanceUnsignedShortArg:(unsigned short)arg on:(OC_TestClass1*)obj 1384{ 1385 unsigned short res; 1386 NSInvocation* inv; 1387 1388 SETUP_INVOCATION(inv, obj, @selector(ushortArg:)) 1389 [inv setArgument:&arg atIndex:2]; // First real argument 1390 1391 [obj forwardInvocation:inv]; 1392 [inv getReturnValue:&res]; 1393 return res; 1394} 1395 1396- (char)invokeInstanceCharArg:(char)arg on:(OC_TestClass1*)obj 1397{ 1398 char res; 1399 NSInvocation* inv; 1400 1401 SETUP_INVOCATION(inv, obj, @selector(charArg:)) 1402 [inv setArgument:&arg atIndex:2]; // First real argument 1403 1404 [obj forwardInvocation:inv]; 1405 [inv getReturnValue:&res]; 1406 return res; 1407} 1408 1409- (unsigned char)invokeInstanceUnsignedCharArg:(unsigned char)arg on:(OC_TestClass1*)obj 1410{ 1411 unsigned char res; 1412 NSInvocation* inv; 1413 1414 SETUP_INVOCATION(inv, obj, @selector(ucharArg:)) 1415 [inv setArgument:&arg atIndex:2]; // First real argument 1416 1417 [obj forwardInvocation:inv]; 1418 [inv getReturnValue:&res]; 1419 return res; 1420} 1421 1422- (float)invokeInstanceFloatArg:(float)arg on:(OC_TestClass1*)obj 1423{ 1424 float res; 1425 NSInvocation* inv; 1426 1427 SETUP_INVOCATION(inv, obj, @selector(floatArg:)) 1428 [inv setArgument:&arg atIndex:2]; // First real argument 1429 1430 [obj forwardInvocation:inv]; 1431 [inv getReturnValue:&res]; 1432 return res; 1433} 1434 1435- (double)invokeInstanceDoubleArg:(double)arg on:(OC_TestClass1*)obj 1436{ 1437 double res; 1438 NSInvocation* inv; 1439 1440 SETUP_INVOCATION(inv, obj, @selector(doubleArg:)) 1441 [inv setArgument:&arg atIndex:2]; // First real argument 1442 1443 [obj forwardInvocation:inv]; 1444 [inv getReturnValue:&res]; 1445 return res; 1446} 1447 1448- (char*)invokeInstanceCharpArg:(char*)arg on:(OC_TestClass1*)obj 1449{ 1450 char* res; 1451 NSInvocation* inv; 1452 1453 SETUP_INVOCATION(inv, obj, @selector(charpArg:)) 1454 [inv setArgument:&arg atIndex:2]; // First real argument 1455 1456 [obj forwardInvocation:inv]; 1457 [inv getReturnValue:&res]; 1458 return res; 1459} 1460 1461- (id)invokeInstanceIdArg:(id)arg on:(OC_TestClass1*)obj 1462{ 1463 id res; 1464 NSInvocation* inv; 1465 1466 SETUP_INVOCATION(inv, obj, @selector(idArg:)) 1467 [inv setArgument:&arg atIndex:2]; // First real argument 1468 1469 [obj forwardInvocation:inv]; 1470 [inv getReturnValue:&res]; 1471 return res; 1472} 1473 1474 1475@end 1476 1477 1478/*============================================================================*/ 1479 1480@interface PyObjC_TestClass3 : NSObject 1481{ 1482} 1483+(NSHost*)createAHostWithAddress:(NSString*)address; 1484+(id)copyValue:(id<NSCopying>)source; 1485+(NSData*)getBytes:(NSData*)data; 1486+(id)keyValue:(int)idx forObject: value key: id; 1487+(void)setKeyValue:(int)idx forObject: object key: key value: value; 1488@end 1489 1490@implementation PyObjC_TestClass3 1491 1492+(NSHost*)createAHostWithAddress:(NSString*)address 1493{ 1494 return [NSHost hostWithAddress:address]; 1495} 1496 1497+(NSData*)getBytes:(NSData*)data 1498{ 1499 const void* bytes = [data bytes]; 1500 1501 if (bytes == NULL) { 1502 return nil; 1503 } else { 1504 return [NSData dataWithBytes:bytes length:[data length]]; 1505 } 1506} 1507 1508+(id)copyValue:(NSObject<NSCopying>*)source 1509{ 1510 id theCopy; 1511 id pool; 1512 1513 /* Copy the source, bracketed by the creation and 1514 * destruction of an autorelease pool. This should 1515 * cause a core-dump if the copy is not a 'new' 1516 * object. 1517 */ 1518 pool = [[NSAutoreleasePool alloc] init]; 1519 theCopy = [source copy]; 1520 [pool release]; 1521 pool = nil; 1522 1523 return theCopy; 1524} 1525 1526+(id)keyValue:(int)idx forObject: object key: key 1527{ 1528 switch (idx) { 1529 case 0: return [object valueForKey: key]; 1530 case 1: return [object valueForKeyPath: key]; 1531 case 2: return [object storedValueForKey: key]; 1532 case 3: return [object valuesForKeys: key]; 1533 } 1534 return nil; 1535} 1536 1537+(void)setKeyValue:(int)idx forObject: object key: key value: value 1538{ 1539 switch (idx) { 1540 case 0: [object takeValue: value forKey: key]; break; 1541 case 1: [object takeValue: value forKeyPath: key]; break; 1542 case 2: [object takeStoredValue: value forKey: key]; break; 1543 case 3: [object takeValuesFromDictionary: value]; break; 1544#if defined (MAC_OS_X_VERSION_10_3) && (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3) 1545 1546 case 4: [object setValue: value forKey: key]; break; 1547 case 5: [object setValue: value forKeyPath: key]; break; 1548 case 6: [object setValuesForKeysWithDictionary: value]; break; 1549#endif 1550 } 1551} 1552 1553+(NSObject*)makeObservedOfClass:(Class)theClass observer:(NSObject*)obj keyPath:(NSString*)path 1554{ 1555#if defined (MAC_OS_X_VERSION_10_3) && (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3) 1556 NSObject* o = [[[theClass alloc] init] autorelease]; 1557 [o addObserver:obj 1558 forKeyPath:path 1559 options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld 1560 context:0]; 1561 return o; 1562#else 1563 /* Use arguments */ 1564 int i; 1565 i = (int)&theClass; i = (int)&obj; i = (int)&path; 1566 return nil; 1567#endif 1568} 1569@end 1570 1571@interface PyObjC_TestClass4 : NSObject 1572{ 1573 id returnObject; 1574} 1575- (void)encodeWithCoder:(NSCoder*)coder; 1576- (void)runThread:(id)object; 1577- (id)returnObject; 1578 1579+ (int)fetchInt:(NSCoder*)coder; 1580+ (double)fetchDouble:(NSCoder*)coder; 1581+ (NSData*)fetchData:(NSCoder*)coder; 1582+ (NSArray*)fetchArray:(NSCoder*)coder; 1583@end 1584 1585@interface NSObject (IKnowWhatImDoing) 1586- (id)call; 1587@end 1588 1589@implementation PyObjC_TestClass4 1590-(int)_privateMethodWithArg:(float)arg 1591{ 1592 return (int)arg; 1593} 1594 1595- (void)runThread:(id)object 1596{ 1597 NSObject* pool = [[NSAutoreleasePool alloc] init]; 1598 returnObject = [object call]; 1599 [returnObject retain]; 1600 [pool release]; 1601} 1602 1603- (id)returnObject 1604{ 1605 return returnObject; 1606} 1607 1608- (void)encodeWithCoder:(NSCoder*)coder 1609{ 1610 double d = 1.5; 1611 int iArray[] = { 3,4,5,6}; 1612 [coder encodeValueOfObjCType:@encode(double) at:&d]; 1613 [coder encodeArrayOfObjCType:@encode(int) count:4 at:iArray]; 1614 [coder encodeBytes:"hello world" length:11]; 1615} 1616 1617+ (int)fetchInt:(NSCoder*)coder 1618{ 1619 int i; 1620 [coder decodeValueOfObjCType:@encode(int) at:&i]; 1621 return i; 1622} 1623 1624+ (double)fetchDouble:(NSCoder*)coder 1625{ 1626 double i; 1627 [coder decodeValueOfObjCType:@encode(double) at:&i]; 1628 return i; 1629} 1630 1631+ (NSData*)fetchData:(NSCoder*)coder 1632{ 1633 void* data; 1634 NSUInteger length; 1635 1636 data = [coder decodeBytesWithReturnedLength:&length]; 1637 return [NSData dataWithBytes:data length:length]; 1638} 1639 1640+ (NSArray*)fetchArray:(NSCoder*)coder 1641{ 1642 int data[10]; 1643 1644 [coder decodeArrayOfObjCType:@encode(int) count:10 at:data]; 1645 return [NSArray arrayWithObjects: 1646 [NSNumber numberWithInt:data[0]], 1647 [NSNumber numberWithInt:data[1]], 1648 [NSNumber numberWithInt:data[2]], 1649 [NSNumber numberWithInt:data[3]], 1650 [NSNumber numberWithInt:data[4]], 1651 [NSNumber numberWithInt:data[5]], 1652 [NSNumber numberWithInt:data[6]], 1653 [NSNumber numberWithInt:data[7]], 1654 [NSNumber numberWithInt:data[8]], 1655 [NSNumber numberWithInt:data[9]], 1656 nil]; 1657} 1658 1659+ (NSString*) fetchObjectDescription: (NSObject*) value 1660{ 1661 return [value description]; 1662} 1663 1664@end 1665 1666 1667@interface PyObjCTest_KVBaseClass : NSObject 1668{ 1669 NSString *directString; 1670 NSNumber *directNumber; 1671 NSString *indirectString; 1672 NSNumber *indirectNumber; 1673} 1674@end 1675 1676@implementation PyObjCTest_KVBaseClass 1677- (instancetype)init 1678{ 1679 self = [super init]; 1680 if (!self) return nil; 1681 1682 directString = [@"Direct String" retain]; 1683 directNumber = [[NSNumber numberWithInt: 42] retain]; 1684 indirectString = [@"Indirect String" retain]; 1685 indirectNumber = [[NSNumber numberWithInt: 84] retain]; 1686 1687 return self; 1688} 1689 1690- (NSString *) indirectString { return indirectString; } 1691- (void) setIndirectString: (NSString *) aString 1692{ 1693 [aString retain]; 1694 [indirectString release]; 1695 indirectString = aString; 1696} 1697 1698- (NSNumber *) indirectNumber { return indirectNumber; } 1699- (void) setIndirectNumber: (NSNumber *) aNumber 1700{ 1701 [aNumber retain]; 1702 [indirectNumber release]; 1703 indirectNumber = aNumber; 1704} 1705@end 1706 1707@interface PyObjCTest_KVPathClass : NSObject 1708{ 1709 PyObjCTest_KVBaseClass *directHead; 1710 PyObjCTest_KVBaseClass *indirectHead; 1711} 1712@end 1713 1714@implementation PyObjCTest_KVPathClass 1715- (instancetype)init 1716{ 1717 self = [super init]; 1718 if (!self) return nil; 1719 1720 directHead = [[PyObjCTest_KVBaseClass alloc] init]; 1721 indirectHead = [[PyObjCTest_KVBaseClass alloc] init]; 1722 1723 return self; 1724} 1725 1726- (PyObjCTest_KVBaseClass *) indirectHead { return indirectHead; } 1727- (void) setInidrectHead: (PyObjCTest_KVBaseClass *) aHead 1728{ 1729 [aHead retain]; 1730 [indirectHead release]; 1731 indirectHead = aHead; 1732} 1733@end 1734 1735#if defined (MAC_OS_X_VERSION_10_3) && (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3) 1736 1737@interface PyObjCTest_KeyValueObserver : NSObject 1738{ 1739 id observed; 1740 NSString* key; 1741 id value; 1742} 1743-(instancetype)initWithInstanceOfClass:(Class)cls withKey:(NSString*)key; 1744-(id)getValue; 1745-(void)dealloc; 1746-(void)observeValueForKeyPath:(NSString*)keyPath ofObject:(id)object change:(NSDictionary*)change context:(void*)context; 1747@end 1748 1749@implementation PyObjCTest_KeyValueObserver 1750 1751-(instancetype)initWithInstanceOfClass:(Class)cls withKey:(NSString*)aKey 1752{ 1753 self = [super init]; 1754 if (self == nil) return nil; 1755 value = nil; 1756 observed = nil; 1757 1758 observed = [[cls alloc] init]; 1759 if (observed == nil) { 1760 [self release]; 1761 return nil; 1762 } 1763 1764 key = aKey; 1765 [aKey retain]; 1766 1767 [observed addObserver:self 1768 forKeyPath:key 1769 options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld 1770 context:0]; 1771 value = [[observed valueForKey:key] retain]; 1772 1773 // Not actually used... 1774 //[observed setValue:@"Hello there" forKey:key]; 1775 return self; 1776} 1777 1778-(void)dealloc 1779{ 1780 [observed removeObserver:self forKeyPath:key]; 1781 [key release]; 1782 [observed release]; 1783 [value release]; 1784 [super dealloc]; 1785} 1786 1787-(id)getValue 1788{ 1789 return value; 1790} 1791 1792-(void)observeValueForKeyPath:(NSString*)keyPath ofObject:(id)object change:(NSDictionary*)change context:(void*)context 1793{ 1794 id newValue = [change objectForKey:NSKeyValueChangeNewKey]; 1795 [newValue retain]; 1796 [value release]; 1797 value = newValue; 1798 1799 1800 // use all arguments to avoid warnings... 1801 (void)&keyPath; 1802 (void)&context; 1803 (void)&object; 1804} 1805 1806 1807@end 1808 1809#endif /* ! MacOS X 10.3 */ 1810 1811 1812static PyObject* pyobjcpy(PyObject* self __attribute__((__unused__)), PyObject* args) 1813{ 1814 char* signature; 1815 PyObject* o; 1816 char* buf; 1817 int r; 1818 1819 if (!PyArg_ParseTuple(args, 1820#if PY_VERSION_HEX < 0x03000000 1821 "sO", 1822#else 1823 "yO", 1824#endif 1825 &signature, &o)) { 1826 1827 return NULL; 1828 } 1829 1830 buf = PyMem_Malloc(PyObjCRT_SizeOfType(signature)); 1831 if (buf == NULL) { 1832 PyErr_NoMemory(); 1833 return NULL; 1834 } 1835 1836 r = PyObjC_PythonToObjC(signature, o, buf); 1837 if (r < 0) { 1838 PyMem_Free(buf); 1839 return NULL; 1840 } 1841 1842 o = PyObjC_ObjCToPython(signature, buf); 1843 PyMem_Free(buf); 1844 return o; 1845 1846} 1847 1848static PyObject* carrayMaker(PyObject* self __attribute__((__unused__)), PyObject* args) 1849{ 1850 char* signature; 1851 PyObject* o1; 1852 PyObject* o2; 1853 char* buf; 1854 int r; 1855 Py_ssize_t buflen; 1856 PyObject* res; 1857 PyObject* v = NULL; 1858 1859 if (!PyArg_ParseTuple(args, 1860#if PY_VERSION_HEX < 0x03000000 1861 "sOO", 1862#else 1863 "yOO", 1864#endif 1865 &signature, &o1, &o2)) { 1866 return NULL; 1867 } 1868 1869 if (o2 == Py_None) { 1870 buflen = -1; 1871 } else { 1872 r = PyObjC_PythonToObjC(@encode(Py_ssize_t), o2, &buflen); 1873 if (r == -1) { 1874 return NULL; 1875 } 1876 } 1877 1878 r = PyObjC_PythonToCArray(NO, NO, signature, o1, (void**)&buf, &buflen, &v); 1879 Py_XDECREF(v); 1880 if (r == -1) { 1881 return NULL; 1882 } 1883 1884 res = PyObjC_CArrayToPython(signature, buf, buflen); 1885 PyObjC_FreeCArray(r, buf); 1886 1887 return res; 1888} 1889 1890 1891 1892static PyMethodDef mod_methods[] = { 1893 { 1894 "pyObjCPy", 1895 (PyCFunction)pyobjcpy, 1896 METH_VARARGS, 1897 1898 "pyObjCPy(signature, object) -> object\n" 1899 "\n" 1900 "convert object to ObjC and back." 1901 }, 1902 1903 { 1904 "carrayMaker", 1905 (PyCFunction)carrayMaker, 1906 METH_VARARGS, 1907 1908 "carrayMaker(signature, seq, count) -> str\n" 1909 "\n" 1910 "Convert a sequence of 'count' 'signature' objects to\n" 1911 "a C buffer, and rebuild a python tuple from it.\n" 1912 "count can be None." 1913 }, 1914 1915 1916 { 0, 0, 0, 0 } 1917}; 1918 1919 1920/* Python glue */ 1921#if PY_VERSION_HEX >= 0x03000000 1922 1923static struct PyModuleDef mod_module = { 1924 PyModuleDef_HEAD_INIT, 1925 "testbndl", 1926 NULL, 1927 0, 1928 mod_methods, 1929 NULL, 1930 NULL, 1931 NULL, 1932 NULL 1933}; 1934 1935#define INITERROR() return NULL 1936#define INITDONE() return m 1937 1938PyObject* PyInit_testbndl(void); 1939 1940PyObject* 1941PyInit_testbndl(void) 1942 1943#else 1944 1945#define INITERROR() return 1946#define INITDONE() return 1947 1948void inittestbndl(void); 1949 1950void 1951inittestbndl(void) 1952#endif 1953{ 1954 PyObject* m; 1955 1956#if PY_VERSION_HEX >= 0x03000000 1957 m = PyModule_Create(&mod_module); 1958#else 1959 m = Py_InitModule4("testbndl", mod_methods, 1960 NULL, NULL, PYTHON_API_VERSION); 1961#endif 1962 if (!m) { 1963 INITERROR(); 1964 } 1965 1966 if (PyObjC_ImportAPI(m) < 0) { 1967 INITERROR(); 1968 } 1969 1970 if (PyModule_AddObject(m, "OC_TestClass1", 1971 PyObjCClass_New([OC_TestClass1 class])) < 0) { 1972 INITERROR(); 1973 } 1974 if (PyModule_AddObject(m, "OC_TestClass2", 1975 PyObjCClass_New([OC_TestClass2 class])) < 0) { 1976 INITERROR(); 1977 } 1978 if (PyModule_AddObject(m, "PyObjC_TestClass3", 1979 PyObjCClass_New([PyObjC_TestClass3 class])) < 0) { 1980 INITERROR(); 1981 } 1982 if (PyModule_AddObject(m, "PyObjC_TestClass4", 1983 PyObjCClass_New([PyObjC_TestClass4 class])) < 0) { 1984 INITERROR(); 1985 } 1986 if (PyModule_AddObject(m, "PyObjCTest_KVBaseClass", 1987 PyObjCClass_New([PyObjCTest_KVBaseClass class])) < 0) { 1988 INITERROR(); 1989 } 1990 if (PyModule_AddObject(m, "PyObjCTest_KVPathClass", 1991 PyObjCClass_New([PyObjCTest_KVPathClass class])) < 0) { 1992 INITERROR(); 1993 } 1994 1995#if defined (MAC_OS_X_VERSION_10_3) && (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3) 1996 if (PyModule_AddObject(m, "PyObjCTest_KeyValueObserver", 1997 PyObjCClass_New([PyObjCTest_KeyValueObserver class])) < 0) { 1998 INITERROR(); 1999 } 2000#else 2001 if (PyModule_AddObject(m, "PyObjCTest_KeyValueObserver", Py_None) < 0) { 2002 INITERROR(); 2003 } 2004 2005#endif /* ! MacOS X 10.3 */ 2006 2007 if (PyModule_AddObject(m, "DO_VALUEFORKEY", PyInt_FromLong(0)) < 0) { 2008 INITERROR(); 2009 } 2010 if (PyModule_AddObject(m, "DO_VALUEFORKEYPATH", PyInt_FromLong(1)) < 0) { 2011 INITERROR(); 2012 } 2013 if (PyModule_AddObject(m, "DO_STOREDVALUEFORKEY", PyInt_FromLong(2)) < 0) { 2014 INITERROR(); 2015 } 2016 if (PyModule_AddObject(m, "DO_VALUESFORKEYS", PyInt_FromLong(3)) < 0) { 2017 INITERROR(); 2018 } 2019 2020 if (PyModule_AddObject(m, "DO_TAKEVALUE_FORKEY", PyInt_FromLong(0)) < 0) { 2021 INITERROR(); 2022 } 2023 if (PyModule_AddObject(m, "DO_TAKEVALUE_FORKEYPATH", PyInt_FromLong(1)) < 0) { 2024 INITERROR(); 2025 } 2026 if (PyModule_AddObject(m, "DO_TAKESTOREDVALUE_FORKEY", PyInt_FromLong(2)) < 0) { 2027 INITERROR(); 2028 } 2029 if (PyModule_AddObject(m, "DO_TAKEVALUESFROMDICT", PyInt_FromLong(3)) < 0) { 2030 INITERROR(); 2031 } 2032 if (PyModule_AddObject(m, "DO_SETVALUE_FORKEY", PyInt_FromLong(4)) < 0) { 2033 INITERROR(); 2034 } 2035 if (PyModule_AddObject(m, "DO_SETVALUE_FORKEYPATH", PyInt_FromLong(5)) < 0) { 2036 INITERROR(); 2037 } 2038 if (PyModule_AddObject(m, "DO_SETVALUESFORKEYSFROMDICT", PyInt_FromLong(6)) < 0) { 2039 INITERROR(); 2040 } 2041 2042 2043 if (PyModule_AddObject(m, "UCHAR_MAX", PyInt_FromLong(UCHAR_MAX)) < 0) { 2044 INITERROR(); 2045 } 2046 if (PyModule_AddObject(m, "SCHAR_MAX", PyInt_FromLong(SCHAR_MAX)) < 0) { 2047 INITERROR(); 2048 } 2049 if (PyModule_AddObject(m, "SCHAR_MIN", PyInt_FromLong(SCHAR_MIN)) < 0) { 2050 INITERROR(); 2051 } 2052 if (PyModule_AddObject(m, "CHAR_MAX", PyInt_FromLong(CHAR_MAX)) < 0) { 2053 INITERROR(); 2054 } 2055 if (PyModule_AddObject(m, "CHAR_MIN", PyInt_FromLong(CHAR_MIN)) < 0) { 2056 INITERROR(); 2057 } 2058 2059 if (PyModule_AddObject(m, "USHRT_MAX", PyInt_FromLong(USHRT_MAX)) < 0) { 2060 INITERROR(); 2061 } 2062 if (PyModule_AddObject(m, "SHRT_MAX", PyInt_FromLong(SHRT_MAX)) < 0) { 2063 INITERROR(); 2064 } 2065 if (PyModule_AddObject(m, "SHRT_MIN", PyInt_FromLong(SHRT_MIN)) < 0) { 2066 INITERROR(); 2067 } 2068 2069 if (PyModule_AddObject(m, "UINT_MAX", PyLong_FromUnsignedLongLong(UINT_MAX)) < 0) { 2070 INITERROR(); 2071 } 2072 if (PyModule_AddObject(m, "INT_MAX", PyInt_FromLong(INT_MAX)) < 0) { 2073 INITERROR(); 2074 } 2075 if (PyModule_AddObject(m, "INT_MIN", PyInt_FromLong(INT_MIN)) < 0) { 2076 INITERROR(); 2077 } 2078 2079 if (PyModule_AddObject(m, "ULONG_MAX", PyLong_FromUnsignedLongLong(ULONG_MAX)) < 0) { 2080 INITERROR(); 2081 } 2082 if (PyModule_AddObject(m, "LONG_MAX", PyInt_FromLong(LONG_MAX)) < 0) { 2083 INITERROR(); 2084 } 2085 if (PyModule_AddObject(m, "LONG_MIN", PyInt_FromLong(LONG_MIN)) < 0) { 2086 INITERROR(); 2087 } 2088 2089 if (PyModule_AddObject(m, "ULLONG_MAX", PyLong_FromUnsignedLongLong(ULLONG_MAX)) < 0) { 2090 INITERROR(); 2091 } 2092 if (PyModule_AddObject(m, "LLONG_MAX", PyLong_FromLongLong(LLONG_MAX)) < 0) { 2093 INITERROR(); 2094 } 2095 if (PyModule_AddObject(m, "LLONG_MIN", PyLong_FromLongLong(LLONG_MIN)) < 0) { 2096 INITERROR(); 2097 } 2098 2099 if (PyModule_AddObject(m, "DBL_MAX", PyFloat_FromDouble(DBL_MAX)) < 0) { 2100 INITERROR(); 2101 } 2102 if (PyModule_AddObject(m, "DBL_MIN", PyFloat_FromDouble(DBL_MIN)) < 0) { 2103 INITERROR(); 2104 } 2105 if (PyModule_AddObject(m, "DBL_EPSILON", PyFloat_FromDouble(DBL_EPSILON)) < 0) { 2106 INITERROR(); 2107 } 2108 if (PyModule_AddObject(m, "FLT_MAX", PyFloat_FromDouble(FLT_MAX)) < 0) { 2109 INITERROR(); 2110 } 2111 if (PyModule_AddObject(m, "FLT_MIN", PyFloat_FromDouble(FLT_MIN)) < 0) { 2112 INITERROR(); 2113 } 2114 if (PyModule_AddObject(m, "FLT_EPSILON", PyFloat_FromDouble(FLT_EPSILON)) < 0) { 2115 INITERROR(); 2116 } 2117 2118 2119 INITDONE(); 2120} 2121