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/* "plain" calls */
849-(char)callInstanceCharFuncOf:(OC_TestClass1*)arg;
850-(unsigned char)callInstanceUnsignedCharFuncOf:(OC_TestClass1*)arg;
851
852-(short)callInstanceShortFuncOf:(OC_TestClass1*)arg;
853-(unsigned short)callInstanceUnsignedShortFuncOf:(OC_TestClass1*)arg;
854
855-(int)callInstanceIntFuncOf:(OC_TestClass1*)arg;
856-(unsigned int)callInstanceUnsignedIntFuncOf:(OC_TestClass1*)arg;
857
858-(long)callInstanceLongFuncOf:(OC_TestClass1*)arg;
859-(unsigned long)callInstanceUnsignedLongFuncOf:(OC_TestClass1*)arg;
860
861-(long long)callInstanceLongLongFuncOf:(OC_TestClass1*)arg;
862-(unsigned long long)callInstanceUnsignedLongLongFuncOf:(OC_TestClass1*)arg;
863
864-(float)callInstanceFloatFuncOf:(OC_TestClass1*)arg;
865-(double)callInstanceDoubleFuncOf:(OC_TestClass1*)arg;
866
867-(id)callInstanceIdFuncOf:(OC_TestClass1*)arg;
868-(struct dummy)callInstanceDummyFuncOf:(OC_TestClass1*)arg;
869-(struct dummy2)callInstanceDummy2FuncOf:(OC_TestClass1*)arg;
870-(NSPoint)callInstanceNSPointFuncOf:(OC_TestClass1*)arg;
871
872- (long long)callInstanceLongLongArg:(long long)arg on:(OC_TestClass1*)obj;
873- (unsigned long long)callInstanceUnsignedLongLongArg:(unsigned long long)arg on:(OC_TestClass1*)obj;
874- (long)callInstanceLongArg:(long)arg on:(OC_TestClass1*)obj;
875- (unsigned long)callInstanceUnsignedLongArg:(unsigned long)arg on:(OC_TestClass1*)obj;
876- (int)callInstanceIntArg:(int)arg on:(OC_TestClass1*)obj;
877- (unsigned int)callInstanceUnsignedIntArg:(unsigned int)arg on:(OC_TestClass1*)obj;
878- (short)callInstanceShortArg:(short)arg on:(OC_TestClass1*)obj;
879- (unsigned short)callInstanceUnsignedShortArg:(unsigned short)arg on:(OC_TestClass1*)obj;
880- (char)callInstanceCharArg:(char)arg on:(OC_TestClass1*)obj;
881- (unsigned char)callInstanceUnsignedCharArg:(unsigned char)arg on:(OC_TestClass1*)obj;
882- (float)callInstanceFloatArg:(float)arg on:(OC_TestClass1*)obj;
883- (double)callInstanceDoubleArg:(double)arg on:(OC_TestClass1*)obj;
884- (char*)callInstanceCharpArg:(char*)arg on:(OC_TestClass1*)obj;
885- (id)callInstanceIdArg:(id)arg on:(OC_TestClass1*)obj;
886
887/* "NSInvocation" calls */
888-(char)invokeInstanceCharFuncOf:(OC_TestClass1*)arg;
889-(unsigned char)invokeInstanceUnsignedCharFuncOf:(OC_TestClass1*)arg;
890
891-(short)invokeInstanceShortFuncOf:(OC_TestClass1*)arg;
892-(unsigned short)invokeInstanceUnsignedShortFuncOf:(OC_TestClass1*)arg;
893
894-(int)invokeInstanceIntFuncOf:(OC_TestClass1*)arg;
895-(unsigned int)invokeInstanceUnsignedIntFuncOf:(OC_TestClass1*)arg;
896
897-(long)invokeInstanceLongFuncOf:(OC_TestClass1*)arg;
898-(unsigned long)invokeInstanceUnsignedLongFuncOf:(OC_TestClass1*)arg;
899
900-(long long)invokeInstanceLongLongFuncOf:(OC_TestClass1*)arg;
901-(unsigned long long)invokeInstanceUnsignedLongLongFuncOf:(OC_TestClass1*)arg;
902
903-(float)invokeInstanceFloatFuncOf:(OC_TestClass1*)arg;
904-(double)invokeInstanceDoubleFuncOf:(OC_TestClass1*)arg;
905
906-(id)invokeInstanceIdFuncOf:(OC_TestClass1*)arg;
907-(struct dummy)invokeInstanceDummyFuncOf:(OC_TestClass1*)arg;
908-(struct dummy2)invokeInstanceDummy2FuncOf:(OC_TestClass1*)arg;
909-(NSPoint)invokeInstanceNSPointFuncOf:(OC_TestClass1*)arg;
910
911- (long long)invokeInstanceLongLongArg:(long long)arg on:(OC_TestClass1*)obj;
912- (unsigned long long)invokeInstanceUnsignedLongLongArg:(unsigned long long)arg on:(OC_TestClass1*)obj;
913- (long)invokeInstanceLongArg:(long)arg on:(OC_TestClass1*)obj;
914- (unsigned long)invokeInstanceUnsignedLongArg:(unsigned long)arg on:(OC_TestClass1*)obj;
915- (int)invokeInstanceIntArg:(int)arg on:(OC_TestClass1*)obj;
916- (unsigned int)invokeInstanceUnsignedIntArg:(unsigned int)arg on:(OC_TestClass1*)obj;
917- (short)invokeInstanceShortArg:(short)arg on:(OC_TestClass1*)obj;
918- (unsigned short)invokeInstanceUnsignedShortArg:(unsigned short)arg on:(OC_TestClass1*)obj;
919- (char)invokeInstanceCharArg:(char)arg on:(OC_TestClass1*)obj;
920- (unsigned char)invokeInstanceUnsignedCharArg:(unsigned char)arg on:(OC_TestClass1*)obj;
921- (float)invokeInstanceFloatArg:(float)arg on:(OC_TestClass1*)obj;
922- (double)invokeInstanceDoubleArg:(double)arg on:(OC_TestClass1*)obj;
923- (char*)invokeInstanceCharpArg:(char*)arg on:(OC_TestClass1*)obj;
924- (id)invokeInstanceIdArg:(id)arg on:(OC_TestClass1*)obj;
925
926@end
927
928
929#define SETUP_INVOCATION(inv, target, selector) \
930	inv = [NSInvocation invocationWithMethodSignature: \
931		[target methodSignatureForSelector:selector]]; \
932	[inv setTarget:target]; \
933	[inv setSelector:selector];
934
935@implementation OC_TestClass2
936
937-(char)callInstanceCharFuncOf:(OC_TestClass1*)arg
938{
939	return [arg charFunc];
940}
941
942-(unsigned char)callInstanceUnsignedCharFuncOf:(OC_TestClass1*)arg
943{
944	return [arg ucharFunc];
945}
946
947-(short)callInstanceShortFuncOf:(OC_TestClass1*)arg
948{
949	return [arg shortFunc];
950}
951
952-(unsigned short)callInstanceUnsignedShortFuncOf:(OC_TestClass1*)arg
953{
954	return [arg ushortFunc];
955}
956
957-(int)callInstanceIntFuncOf:(OC_TestClass1*)arg
958{
959	return [arg intFunc];
960}
961
962-(unsigned int)callInstanceUnsignedIntFuncOf:(OC_TestClass1*)arg
963{
964	return [arg uintFunc];
965}
966
967-(long)callInstanceLongFuncOf:(OC_TestClass1*)arg
968{
969	return [arg longFunc];
970}
971
972-(unsigned long)callInstanceUnsignedLongFuncOf:(OC_TestClass1*)arg
973{
974	return [arg ulongFunc];
975}
976
977-(char)invokeInstanceCharFuncOf:(OC_TestClass1*)arg
978{
979	char res;
980	NSInvocation* inv;
981
982	SETUP_INVOCATION(inv, arg, @selector(charFunc))
983
984	[arg forwardInvocation:inv];
985	[inv getReturnValue:&res];
986	return res;
987}
988
989-(unsigned char)invokeInstanceUnsignedCharFuncOf:(OC_TestClass1*)arg
990{
991	unsigned char res;
992	NSInvocation* inv;
993
994	SETUP_INVOCATION(inv, arg, @selector(ucharFunc))
995
996	[arg forwardInvocation:inv];
997	[inv getReturnValue:&res];
998	return res;
999}
1000
1001-(short)invokeInstanceShortFuncOf:(OC_TestClass1*)arg
1002{
1003	short res;
1004	NSInvocation* inv;
1005
1006	SETUP_INVOCATION(inv, arg, @selector(shortFunc))
1007
1008	[arg forwardInvocation:inv];
1009	[inv getReturnValue:&res];
1010	return res;
1011}
1012
1013-(unsigned short)invokeInstanceUnsignedShortFuncOf:(OC_TestClass1*)arg
1014{
1015	unsigned short res;
1016	NSInvocation* inv;
1017
1018	SETUP_INVOCATION(inv, arg, @selector(ushortFunc))
1019
1020	[arg forwardInvocation:inv];
1021	[inv getReturnValue:&res];
1022	return res;
1023}
1024
1025-(int)invokeInstanceIntFuncOf:(OC_TestClass1*)arg
1026{
1027	int res;
1028	NSInvocation* inv;
1029
1030	SETUP_INVOCATION(inv, arg, @selector(intFunc))
1031
1032	[arg forwardInvocation:inv];
1033	[inv getReturnValue:&res];
1034	return res;
1035}
1036
1037-(unsigned int)invokeInstanceUnsignedIntFuncOf:(OC_TestClass1*)arg
1038{
1039	unsigned int res;
1040	NSInvocation* inv;
1041
1042	SETUP_INVOCATION(inv, arg, @selector(uintFunc))
1043
1044	[arg forwardInvocation:inv];
1045	[inv getReturnValue:&res];
1046	return res;
1047}
1048
1049-(long)invokeInstanceLongFuncOf:(OC_TestClass1*)arg
1050{
1051	long res;
1052	NSInvocation* inv;
1053
1054	SETUP_INVOCATION(inv, arg, @selector(longFunc))
1055
1056	[arg forwardInvocation:inv];
1057	[inv getReturnValue:&res];
1058	return res;
1059}
1060
1061-(unsigned long)invokeInstanceUnsignedLongFuncOf:(OC_TestClass1*)arg
1062{
1063	unsigned long res;
1064	NSInvocation* inv;
1065
1066	SETUP_INVOCATION(inv, arg, @selector(ulongFunc))
1067
1068	[arg forwardInvocation:inv];
1069	[inv getReturnValue:&res];
1070	return res;
1071}
1072
1073-(long long)callInstanceLongLongFuncOf:(OC_TestClass1*)arg
1074{
1075	return [arg longlongFunc];
1076}
1077
1078-(unsigned long long)callInstanceUnsignedLongLongFuncOf:(OC_TestClass1*)arg
1079{
1080	return [arg ulonglongFunc];
1081}
1082
1083-(float)callInstanceFloatFuncOf:(OC_TestClass1*)arg
1084{
1085	return [arg floatFunc];
1086}
1087
1088
1089-(double)callInstanceDoubleFuncOf:(OC_TestClass1*)arg
1090{
1091	return [arg doubleFunc];
1092}
1093
1094-(id)callInstanceIdFuncOf:(OC_TestClass1*)arg
1095{
1096	return [arg idFunc];
1097}
1098
1099-(struct dummy)callInstanceDummyFuncOf:(OC_TestClass1*)arg
1100{
1101	return [arg dummyFunc];
1102}
1103
1104-(struct dummy2)callInstanceDummy2FuncOf:(OC_TestClass1*)arg
1105{
1106	struct dummy2 tmpval;
1107
1108	tmpval = [arg dummy2Func];
1109	return tmpval;
1110}
1111
1112-(NSPoint)callInstanceNSPointFuncOf:(OC_TestClass1*)arg
1113{
1114	return [arg nspointFunc];
1115}
1116
1117- (long long)callInstanceLongLongArg:(long long)arg on:(OC_TestClass1*)obj;
1118{
1119	return [obj longlongArg:arg];
1120}
1121
1122- (unsigned long long)callInstanceUnsignedLongLongArg:(unsigned long long)arg on:(OC_TestClass1*)obj;
1123{
1124	return [obj ulonglongArg:arg];
1125}
1126
1127- (long)callInstanceLongArg:(long)arg on:(OC_TestClass1*)obj;
1128{
1129	return [obj longArg:arg];
1130}
1131
1132- (unsigned long)callInstanceUnsignedLongArg:(unsigned long)arg on:(OC_TestClass1*)obj;
1133{
1134	return [obj ulongArg:arg];
1135}
1136
1137- (int)callInstanceIntArg:(int)arg on:(OC_TestClass1*)obj;
1138{
1139	return [obj intArg:arg];
1140}
1141
1142- (unsigned int)callInstanceUnsignedIntArg:(unsigned int)arg on:(OC_TestClass1*)obj;
1143{
1144	return [obj uintArg:arg];
1145}
1146
1147- (short)callInstanceShortArg:(short)arg on:(OC_TestClass1*)obj;
1148{
1149	return [obj shortArg:arg];
1150}
1151
1152- (unsigned short)callInstanceUnsignedShortArg:(unsigned short)arg on:(OC_TestClass1*)obj;
1153{
1154	return [obj ushortArg:arg];
1155}
1156
1157- (char)callInstanceCharArg:(char)arg on:(OC_TestClass1*)obj;
1158{
1159	return [obj charArg:arg];
1160}
1161
1162- (unsigned char)callInstanceUnsignedCharArg:(unsigned char)arg on:(OC_TestClass1*)obj;
1163{
1164	return [obj ucharArg:arg];
1165}
1166
1167- (float)callInstanceFloatArg:(float)arg on:(OC_TestClass1*)obj;
1168{
1169	return [obj floatArg:arg];
1170}
1171
1172- (double)callInstanceDoubleArg:(double)arg on:(OC_TestClass1*)obj;
1173{
1174	return [obj doubleArg:arg];
1175}
1176
1177- (char*)callInstanceCharpArg:(char*)arg on:(OC_TestClass1*)obj;
1178{
1179	return [obj charpArg:arg];
1180}
1181
1182- (id)callInstanceIdArg:(id)arg on:(OC_TestClass1*)obj;
1183{
1184	return [obj idArg:arg];
1185}
1186
1187
1188
1189-(long long)invokeInstanceLongLongFuncOf:(OC_TestClass1*)arg
1190{
1191	long long res;
1192	NSInvocation* inv;
1193
1194	SETUP_INVOCATION(inv, arg, @selector(longlongFunc))
1195
1196	[arg forwardInvocation:inv];
1197	[inv getReturnValue:&res];
1198	return res;
1199}
1200
1201-(unsigned long long)invokeInstanceUnsignedLongLongFuncOf:(OC_TestClass1*)arg
1202{
1203	unsigned long long res;
1204	NSInvocation* inv;
1205
1206	SETUP_INVOCATION(inv, arg, @selector(ulonglongFunc))
1207
1208	[arg forwardInvocation:inv];
1209	[inv getReturnValue:&res];
1210	return res;
1211}
1212
1213-(float)invokeInstanceFloatFuncOf:(OC_TestClass1*)arg
1214{
1215	float res;
1216	NSInvocation* inv;
1217
1218	SETUP_INVOCATION(inv, arg, @selector(floatFunc))
1219
1220	[arg forwardInvocation:inv];
1221	[inv getReturnValue:&res];
1222	return res;
1223}
1224
1225-(double)invokeInstanceDoubleFuncOf:(OC_TestClass1*)arg
1226{
1227	double res;
1228	NSInvocation* inv;
1229
1230	SETUP_INVOCATION(inv, arg, @selector(doubleFunc))
1231
1232	[arg forwardInvocation:inv];
1233	[inv getReturnValue:&res];
1234	return res;
1235}
1236
1237-(id)invokeInstanceIdFuncOf:(OC_TestClass1*)arg
1238{
1239	id res;
1240	NSInvocation* inv;
1241
1242	SETUP_INVOCATION(inv, arg, @selector(idFunc))
1243
1244	[arg forwardInvocation:inv];
1245	[inv getReturnValue:&res];
1246	return res;
1247}
1248
1249-(struct dummy)invokeInstanceDummyFuncOf:(OC_TestClass1*)arg
1250{
1251	struct dummy res;
1252	NSInvocation* inv;
1253
1254	SETUP_INVOCATION(inv, arg, @selector(dummyFunc))
1255
1256	[arg forwardInvocation:inv];
1257	[inv getReturnValue:&res];
1258	return res;
1259}
1260
1261-(struct dummy2)invokeInstanceDummy2FuncOf:(OC_TestClass1*)arg
1262{
1263	struct dummy2 res;
1264	NSInvocation* inv;
1265
1266	SETUP_INVOCATION(inv, arg, @selector(dummy2Func))
1267
1268	[arg forwardInvocation:inv];
1269	[inv getReturnValue:&res];
1270	return res;
1271}
1272
1273-(NSPoint)invokeInstanceNSPointFuncOf:(OC_TestClass1*)arg
1274{
1275	NSPoint res;
1276	NSInvocation* inv;
1277
1278	SETUP_INVOCATION(inv, arg, @selector(nspointFunc))
1279
1280	[arg forwardInvocation:inv];
1281	[inv getReturnValue:&res];
1282	return res;
1283}
1284
1285- (long long)invokeInstanceLongLongArg:(long long)arg on:(OC_TestClass1*)obj;
1286{
1287	long long res;
1288	NSInvocation* inv;
1289
1290	SETUP_INVOCATION(inv, obj, @selector(longlongArg:))
1291	[inv setArgument:&arg atIndex:2]; // First real argument
1292
1293	[obj forwardInvocation:inv];
1294	[inv getReturnValue:&res];
1295	return res;
1296}
1297
1298- (unsigned long long)invokeInstanceUnsignedLongLongArg:(unsigned long long)arg on:(OC_TestClass1*)obj;
1299{
1300	unsigned long long res;
1301	NSInvocation* inv;
1302
1303	SETUP_INVOCATION(inv, obj, @selector(ulonglongArg:))
1304	[inv setArgument:&arg atIndex:2]; // First real argument
1305
1306	[obj forwardInvocation:inv];
1307	[inv getReturnValue:&res];
1308	return res;
1309}
1310
1311- (long)invokeInstanceLongArg:(long)arg on:(OC_TestClass1*)obj;
1312{
1313	long res;
1314	NSInvocation* inv;
1315
1316	SETUP_INVOCATION(inv, obj, @selector(longArg:))
1317	[inv setArgument:&arg atIndex:2]; // First real argument
1318
1319	[obj forwardInvocation:inv];
1320	[inv getReturnValue:&res];
1321	return res;
1322}
1323
1324- (unsigned long)invokeInstanceUnsignedLongArg:(unsigned long)arg on:(OC_TestClass1*)obj;
1325{
1326	unsigned long res;
1327	NSInvocation* inv;
1328
1329	SETUP_INVOCATION(inv, obj, @selector(ulonglongArg:))
1330	[inv setArgument:&arg atIndex:2]; // First real argument
1331
1332	[obj forwardInvocation:inv];
1333	[inv getReturnValue:&res];
1334	return res;
1335}
1336
1337- (int)invokeInstanceIntArg:(int)arg on:(OC_TestClass1*)obj;
1338{
1339	int res;
1340	NSInvocation* inv;
1341
1342	SETUP_INVOCATION(inv, obj, @selector(intArg:))
1343	[inv setArgument:&arg atIndex:2]; // First real argument
1344
1345	[obj forwardInvocation:inv];
1346	[inv getReturnValue:&res];
1347	return res;
1348}
1349
1350- (unsigned int)invokeInstanceUnsignedIntArg:(unsigned int)arg on:(OC_TestClass1*)obj;
1351{
1352	unsigned int res;
1353	NSInvocation* inv;
1354
1355	SETUP_INVOCATION(inv, obj, @selector(uintArg:))
1356	[inv setArgument:&arg atIndex:2]; // First real argument
1357
1358	[obj forwardInvocation:inv];
1359	[inv getReturnValue:&res];
1360	return res;
1361}
1362
1363- (short)invokeInstanceShortArg:(short)arg on:(OC_TestClass1*)obj;
1364{
1365	short res;
1366	NSInvocation* inv;
1367
1368	SETUP_INVOCATION(inv, obj, @selector(shortArg:))
1369	[inv setArgument:&arg atIndex:2]; // First real argument
1370
1371	[obj forwardInvocation:inv];
1372	[inv getReturnValue:&res];
1373	return res;
1374}
1375
1376- (unsigned short)invokeInstanceUnsignedShortArg:(unsigned short)arg on:(OC_TestClass1*)obj;
1377{
1378	unsigned short res;
1379	NSInvocation* inv;
1380
1381	SETUP_INVOCATION(inv, obj, @selector(ushortArg:))
1382	[inv setArgument:&arg atIndex:2]; // First real argument
1383
1384	[obj forwardInvocation:inv];
1385	[inv getReturnValue:&res];
1386	return res;
1387}
1388
1389- (char)invokeInstanceCharArg:(char)arg on:(OC_TestClass1*)obj;
1390{
1391	char res;
1392	NSInvocation* inv;
1393
1394	SETUP_INVOCATION(inv, obj, @selector(charArg:))
1395	[inv setArgument:&arg atIndex:2]; // First real argument
1396
1397	[obj forwardInvocation:inv];
1398	[inv getReturnValue:&res];
1399	return res;
1400}
1401
1402- (unsigned char)invokeInstanceUnsignedCharArg:(unsigned char)arg on:(OC_TestClass1*)obj;
1403{
1404	unsigned char res;
1405	NSInvocation* inv;
1406
1407	SETUP_INVOCATION(inv, obj, @selector(ucharArg:))
1408	[inv setArgument:&arg atIndex:2]; // First real argument
1409
1410	[obj forwardInvocation:inv];
1411	[inv getReturnValue:&res];
1412	return res;
1413}
1414
1415- (float)invokeInstanceFloatArg:(float)arg on:(OC_TestClass1*)obj;
1416{
1417	float res;
1418	NSInvocation* inv;
1419
1420	SETUP_INVOCATION(inv, obj, @selector(floatArg:))
1421	[inv setArgument:&arg atIndex:2]; // First real argument
1422
1423	[obj forwardInvocation:inv];
1424	[inv getReturnValue:&res];
1425	return res;
1426}
1427
1428- (double)invokeInstanceDoubleArg:(double)arg on:(OC_TestClass1*)obj;
1429{
1430	double res;
1431	NSInvocation* inv;
1432
1433	SETUP_INVOCATION(inv, obj, @selector(doubleArg:))
1434	[inv setArgument:&arg atIndex:2]; // First real argument
1435
1436	[obj forwardInvocation:inv];
1437	[inv getReturnValue:&res];
1438	return res;
1439}
1440
1441- (char*)invokeInstanceCharpArg:(char*)arg on:(OC_TestClass1*)obj;
1442{
1443	char* res;
1444	NSInvocation* inv;
1445
1446	SETUP_INVOCATION(inv, obj, @selector(charpArg:))
1447	[inv setArgument:&arg atIndex:2]; // First real argument
1448
1449	[obj forwardInvocation:inv];
1450	[inv getReturnValue:&res];
1451	return res;
1452}
1453
1454- (id)invokeInstanceIdArg:(id)arg on:(OC_TestClass1*)obj;
1455{
1456	id res;
1457	NSInvocation* inv;
1458
1459	SETUP_INVOCATION(inv, obj, @selector(idArg:))
1460	[inv setArgument:&arg atIndex:2]; // First real argument
1461
1462	[obj forwardInvocation:inv];
1463	[inv getReturnValue:&res];
1464	return res;
1465}
1466
1467
1468@end
1469
1470
1471/*============================================================================*/
1472
1473@interface PyObjC_TestClass3 : NSObject
1474{
1475}
1476+createAHostWithAddress:(NSString*)address;
1477+makeACopy:source;
1478+makeDataWithBytes:(Class)cls method:(int)i;
1479+makeDictFromClass:(Class)cls method:(int)i;
1480+getBytes:(NSData*)data;
1481+keyValue:(int)idx forObject: value key: id;
1482+(void)setKeyValue:(int)idx forObject: object key: key value: value;
1483@end
1484
1485@implementation PyObjC_TestClass3
1486
1487+createAHostWithAddress:(NSString*)address
1488{
1489	return [NSHost hostWithAddress:address];
1490}
1491
1492+getBytes:(NSData*)data
1493{
1494	const void* bytes = [data bytes];
1495
1496	if (bytes == NULL) {
1497		return nil;
1498	} else {
1499		return [NSData dataWithBytes:bytes length:[data length]];
1500	}
1501}
1502
1503
1504+makeDataWithBytes:(Class)cls method:(int)i
1505{
1506	if (i == 0) {
1507		return [cls dataWithBytes:"hello world" length:sizeof("hello world")-1];
1508	} else {
1509		id o = [cls alloc];
1510		return [o initWithBytes:"hello world" length:sizeof("hello world")-1];
1511	}
1512}
1513
1514+makeDictFromClass:(Class)cls method:(int)i
1515{
1516	id objects[4];
1517	id keys[4];
1518
1519	objects[0] = [[NSObject alloc] init];
1520	objects[1] = [[NSObject alloc] init];
1521	objects[2] = [[NSObject alloc] init];
1522	objects[3] = [[NSObject alloc] init];
1523
1524	keys[0] = [[NSObject alloc] init];
1525	keys[1] = [[NSObject alloc] init];
1526	keys[2] = [[NSObject alloc] init];
1527	keys[3] = [[NSObject alloc] init];
1528
1529	if (i == 0) {
1530		return [cls
1531				dictionaryWithObjects:objects
1532				forKeys:keys count:4];
1533	} else {
1534		return [[cls alloc]
1535				initWithObjects:objects
1536				forKeys:keys count:4];
1537	}
1538}
1539
1540
1541+makeACopy:source
1542{
1543	id theCopy;
1544	id pool;
1545
1546	/* Copy the source, bracketed by the creation and
1547	 * destruction of an autorelease pool. This should
1548	 * cause a core-dump if the copy is not a 'new'
1549	 * object.
1550	 */
1551	pool = [[NSAutoreleasePool alloc] init];
1552	theCopy = [source copy];
1553	[pool release];
1554	pool = nil;
1555
1556	return theCopy;
1557}
1558
1559+keyValue:(int)idx forObject: object key: key
1560{
1561	switch (idx) {
1562	case 0: return [object valueForKey: key];
1563	case 1: return [object valueForKeyPath: key];
1564	case 2: return [object storedValueForKey: key];
1565	case 3: return [object valuesForKeys: key];
1566	}
1567	return nil;
1568}
1569
1570+(void)setKeyValue:(int)idx forObject: object key: key value: value
1571{
1572	switch (idx) {
1573	case 0: [object takeValue: value forKey: key]; break;
1574	case 1: [object takeValue: value forKeyPath: key]; break;
1575	case 2: [object takeStoredValue: value forKey: key]; break;
1576	case 3: [object takeValuesFromDictionary: value]; break;
1577#if defined (MAC_OS_X_VERSION_10_3) && (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3)
1578
1579	case 4: [object setValue: value forKey: key]; break;
1580	case 5: [object setValue: value forKeyPath: key]; break;
1581	case 6: [object setValuesForKeysWithDictionary: value]; break;
1582#endif
1583	}
1584}
1585
1586+(NSObject*)createObservedOfClass:(Class)class observer:(NSObject*)obj keyPath:(NSString*)path
1587{
1588#if defined (MAC_OS_X_VERSION_10_3) && (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3)
1589	NSObject* o = [[class alloc] init];
1590	[o addObserver:obj
1591	   forKeyPath:path
1592	   options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld
1593	   context:0];
1594	return o;
1595#else
1596	/* Use arguments */
1597	int i;
1598	i = (int)&class; i = (int)&obj; i = (int)&path;
1599	return nil;
1600#endif
1601}
1602@end
1603
1604@interface PyObjC_TestClass4 : NSObject
1605{
1606	id returnObject;
1607}
1608- (void)encodeWithCoder:(NSCoder*)coder;
1609- (void)runThread:(id)object;
1610- (id)returnObject;
1611
1612+ (int)fetchInt:(NSCoder*)coder;
1613+ (double)fetchDouble:(NSCoder*)coder;
1614+ (NSData*)fetchData:(NSCoder*)coder;
1615+ (NSArray*)fetchArray:(NSCoder*)coder;
1616@end
1617
1618@interface NSObject (IKnowWhatImDoing)
1619- call;
1620@end
1621
1622@implementation PyObjC_TestClass4
1623-(int)_privateMethodWithArg:(float)arg
1624{
1625	return (int)arg;
1626}
1627
1628- (void)runThread:(id)object
1629{
1630	NSObject* pool = [[NSAutoreleasePool alloc] init];
1631	returnObject = [object call];
1632	[returnObject retain];
1633	[pool release];
1634}
1635
1636- (id)returnObject;
1637{
1638	return returnObject;
1639}
1640
1641- (void)encodeWithCoder:(NSCoder*)coder
1642{
1643	double d = 1.5;
1644	int iArray[] = { 3,4,5,6};
1645	[coder encodeValueOfObjCType:@encode(double) at:&d];
1646	[coder encodeArrayOfObjCType:@encode(int) count:4 at:iArray];
1647	[coder encodeBytes:"hello world" length:11];
1648}
1649
1650+ (int)fetchInt:(NSCoder*)coder
1651{
1652	int i;
1653	[coder decodeValueOfObjCType:@encode(int) at:&i];
1654	return i;
1655}
1656
1657+ (double)fetchDouble:(NSCoder*)coder
1658{
1659	double i;
1660	[coder decodeValueOfObjCType:@encode(double) at:&i];
1661	return i;
1662}
1663
1664+ (NSData*)fetchData:(NSCoder*)coder
1665{
1666	void* data;
1667	NSUInteger length;
1668
1669	data = [coder decodeBytesWithReturnedLength:&length];
1670	return [NSData dataWithBytes:data length:length];
1671}
1672
1673+ (NSArray*)fetchArray:(NSCoder*)coder
1674{
1675	int data[10];
1676
1677	[coder decodeArrayOfObjCType:@encode(int) count:10 at:data];
1678	return [NSArray arrayWithObjects:
1679			[NSNumber numberWithInt:data[0]],
1680			[NSNumber numberWithInt:data[1]],
1681			[NSNumber numberWithInt:data[2]],
1682			[NSNumber numberWithInt:data[3]],
1683			[NSNumber numberWithInt:data[4]],
1684			[NSNumber numberWithInt:data[5]],
1685			[NSNumber numberWithInt:data[6]],
1686			[NSNumber numberWithInt:data[7]],
1687			[NSNumber numberWithInt:data[8]],
1688			[NSNumber numberWithInt:data[9]],
1689			nil];
1690}
1691
1692+ (NSString*) fetchObjectDescription: (NSObject*) value
1693{
1694	return [value description];
1695}
1696
1697@end
1698
1699
1700@interface PyObjCTest_KVBaseClass : NSObject
1701{
1702    NSString *directString;
1703    NSNumber *directNumber;
1704    NSString *indirectString;
1705    NSNumber *indirectNumber;
1706}
1707@end
1708
1709@implementation PyObjCTest_KVBaseClass
1710- init
1711{
1712    self = [super init];
1713    if (!self) return nil;
1714
1715    directString = [@"Direct String" retain];
1716    directNumber = [[NSNumber numberWithInt: 42] retain];
1717    indirectString = [@"Indirect String" retain];
1718    indirectNumber = [[NSNumber numberWithInt: 84] retain];
1719
1720    return self;
1721}
1722
1723- (NSString *) indirectString; { return indirectString; }
1724- (void) setIndirectString: (NSString *) aString;
1725{
1726    [aString retain];
1727    [indirectString release];
1728    indirectString = aString;
1729}
1730
1731- (NSNumber *) indirectNumber; { return indirectNumber; }
1732- (void) setIndirectNumber: (NSNumber *) aNumber;
1733{
1734    [aNumber retain];
1735    [indirectNumber release];
1736    indirectNumber = aNumber;
1737}
1738@end
1739
1740@interface PyObjCTest_KVPathClass : NSObject
1741{
1742    PyObjCTest_KVBaseClass *directHead;
1743    PyObjCTest_KVBaseClass *indirectHead;
1744}
1745@end
1746
1747@implementation PyObjCTest_KVPathClass
1748- init
1749{
1750    self = [super init];
1751    if (!self) return nil;
1752
1753    directHead = [[PyObjCTest_KVBaseClass alloc] init];
1754    indirectHead = [[PyObjCTest_KVBaseClass alloc] init];
1755
1756    return self;
1757}
1758
1759- (PyObjCTest_KVBaseClass *) indirectHead { return indirectHead; }
1760- (void) setInidrectHead: (PyObjCTest_KVBaseClass *) aHead;
1761{
1762    [aHead retain];
1763    [indirectHead release];
1764    indirectHead = aHead;
1765}
1766@end
1767
1768#if defined (MAC_OS_X_VERSION_10_3) && (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3)
1769
1770@interface PyObjCTest_KeyValueObserver : NSObject
1771{
1772	id observed;
1773	NSString* key;
1774	id value;
1775}
1776-initWithInstanceOfClass:(Class)cls withKey:(NSString*)key;
1777-getValue;
1778-(void)dealloc;
1779-(void)observeValueForKeyPath:(NSString*)keyPath ofObject:(id)object change:(NSDictionary*)change context:(void*)context;
1780@end
1781
1782@implementation PyObjCTest_KeyValueObserver
1783
1784-initWithInstanceOfClass:(Class)cls withKey:(NSString*)aKey
1785{
1786	self = [super init];
1787	if (self == nil) return nil;
1788	value = nil;
1789	observed = nil;
1790
1791	observed = [[cls alloc] init];
1792	if (observed == nil) {
1793		[self release];
1794		return nil;
1795	}
1796
1797	key = aKey;
1798	[aKey retain];
1799
1800	[observed addObserver:self
1801		  forKeyPath:key
1802		  options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld
1803		  context:0];
1804	value = [[observed valueForKey:key] retain];
1805
1806	// Not actually used...
1807	//[observed setValue:@"Hello there" forKey:key];
1808	return self;
1809}
1810
1811-(void)dealloc
1812{
1813	[observed removeObserver:self forKeyPath:key];
1814	[key release];
1815	[observed release];
1816	[value release];
1817	[super dealloc];
1818}
1819
1820-getValue
1821{
1822	return value;
1823}
1824
1825-(void)observeValueForKeyPath:(NSString*)keyPath ofObject:(id)object change:(NSDictionary*)change context:(void*)context
1826{
1827	id newValue = [change objectForKey:NSKeyValueChangeNewKey];
1828	[newValue retain];
1829	[value release];
1830	value = newValue;
1831
1832
1833	// use all arguments to avoid warnings...
1834	(void)&keyPath;
1835	(void)&context;
1836	(void)&object;
1837}
1838
1839
1840@end
1841
1842#endif /* ! MacOS X 10.3 */
1843
1844
1845static PyObject* pyobjcpy(PyObject* self __attribute__((__unused__)), PyObject* args)
1846{
1847	char* signature;
1848	PyObject* o;
1849	char* buf;
1850	int r;
1851
1852	if (!PyArg_ParseTuple(args,
1853#if PY_VERSION_HEX < 0x03000000
1854		"sO",
1855#else
1856		"yO",
1857#endif
1858		&signature, &o)) {
1859
1860		return NULL;
1861	}
1862
1863	buf = PyMem_Malloc(PyObjCRT_SizeOfType(signature));
1864	if (buf == NULL) {
1865		PyErr_NoMemory();
1866		return NULL;
1867	}
1868
1869	r = PyObjC_PythonToObjC(signature, o, buf);
1870	if (r < 0) {
1871		PyMem_Free(buf);
1872		return NULL;
1873	}
1874
1875	o = PyObjC_ObjCToPython(signature, buf);
1876	PyMem_Free(buf);
1877	return o;
1878
1879}
1880
1881static PyObject* carrayMaker(PyObject* self __attribute__((__unused__)), PyObject* args)
1882{
1883	char* signature;
1884	PyObject* o1;
1885	PyObject* o2;
1886	char* buf;
1887	int r;
1888	Py_ssize_t buflen;
1889	PyObject* res;
1890	PyObject* v = NULL;
1891
1892	if (!PyArg_ParseTuple(args,
1893#if PY_VERSION_HEX < 0x03000000
1894		"sOO",
1895#else
1896		"yOO",
1897#endif
1898		&signature, &o1, &o2)) {
1899		return NULL;
1900	}
1901
1902	if (o2 == Py_None) {
1903		buflen = -1;
1904	} else {
1905		r = PyObjC_PythonToObjC(@encode(Py_ssize_t), o2, &buflen);
1906		if (r == -1) {
1907			return NULL;
1908		}
1909	}
1910
1911	r = PyObjC_PythonToCArray(NO, NO, signature, o1, (void**)&buf, &buflen, &v);
1912	Py_XDECREF(v);
1913	if (r == -1) {
1914		return NULL;
1915	}
1916
1917	res = PyObjC_CArrayToPython(signature, buf, buflen);
1918	PyObjC_FreeCArray(r, buf);
1919
1920	return res;
1921}
1922
1923
1924
1925static PyMethodDef mod_methods[] = {
1926	{
1927		"pyObjCPy",
1928		(PyCFunction)pyobjcpy,
1929		METH_VARARGS,
1930
1931		"pyObjCPy(signature, object) -> object\n"
1932		"\n"
1933		"convert object to ObjC and back."
1934	},
1935
1936	{
1937		"carrayMaker",
1938		(PyCFunction)carrayMaker,
1939		METH_VARARGS,
1940
1941		"carrayMaker(signature, seq, count) -> str\n"
1942		"\n"
1943		"Convert a sequence of 'count' 'signature' objects to\n"
1944		"a C buffer, and rebuild a python tuple from it.\n"
1945		"count can be None."
1946	},
1947
1948
1949	{ 0, 0, 0, 0 }
1950};
1951
1952
1953/* Python glue */
1954#if PY_VERSION_HEX >= 0x03000000
1955
1956static struct PyModuleDef mod_module = {
1957	PyModuleDef_HEAD_INIT,
1958	"testbndl",
1959	NULL,
1960	0,
1961	mod_methods,
1962	NULL,
1963	NULL,
1964	NULL,
1965	NULL
1966};
1967
1968#define INITERROR() return NULL
1969#define INITDONE() return m
1970
1971PyObject* PyInit_testbndl(void);
1972
1973PyObject*
1974PyInit_testbndl(void)
1975
1976#else
1977
1978#define INITERROR() return
1979#define INITDONE() return
1980
1981void inittestbndl(void);
1982
1983void
1984inittestbndl(void)
1985#endif
1986{
1987	PyObject* m;
1988
1989#if PY_VERSION_HEX >= 0x03000000
1990	m = PyModule_Create(&mod_module);
1991#else
1992	m = Py_InitModule4("testbndl", mod_methods,
1993		NULL, NULL, PYTHON_API_VERSION);
1994#endif
1995	if (!m) {
1996		INITERROR();
1997	}
1998
1999	if (PyObjC_ImportAPI(m) < 0) {
2000		INITERROR();
2001	}
2002
2003	if (PyModule_AddObject(m, "OC_TestClass1",
2004		PyObjCClass_New([OC_TestClass1 class])) < 0) {
2005		INITERROR();
2006	}
2007	if (PyModule_AddObject(m, "OC_TestClass2",
2008		PyObjCClass_New([OC_TestClass2 class])) < 0) {
2009		INITERROR();
2010	}
2011	if (PyModule_AddObject(m, "PyObjC_TestClass3",
2012		PyObjCClass_New([PyObjC_TestClass3 class])) < 0) {
2013		INITERROR();
2014	}
2015	if (PyModule_AddObject(m, "PyObjC_TestClass4",
2016		PyObjCClass_New([PyObjC_TestClass4 class])) < 0) {
2017		INITERROR();
2018	}
2019	if (PyModule_AddObject(m, "PyObjCTest_KVBaseClass",
2020		PyObjCClass_New([PyObjCTest_KVBaseClass class])) < 0) {
2021		INITERROR();
2022	}
2023	if (PyModule_AddObject(m, "PyObjCTest_KVPathClass",
2024		PyObjCClass_New([PyObjCTest_KVPathClass class])) < 0) {
2025		INITERROR();
2026	}
2027
2028#if defined (MAC_OS_X_VERSION_10_3) && (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3)
2029	if (PyModule_AddObject(m, "PyObjCTest_KeyValueObserver",
2030		PyObjCClass_New([PyObjCTest_KeyValueObserver class])) < 0) {
2031		INITERROR();
2032	}
2033#else
2034	if (PyModule_AddObject(m, "PyObjCTest_KeyValueObserver",  Py_None) < 0) {
2035		INITERROR();
2036	}
2037
2038#endif /* ! MacOS X 10.3 */
2039
2040	if (PyModule_AddObject(m, "DO_VALUEFORKEY", PyInt_FromLong(0)) < 0) {
2041		INITERROR();
2042	}
2043	if (PyModule_AddObject(m, "DO_VALUEFORKEYPATH", PyInt_FromLong(1)) < 0) {
2044		INITERROR();
2045	}
2046	if (PyModule_AddObject(m, "DO_STOREDVALUEFORKEY", PyInt_FromLong(2)) < 0) {
2047		INITERROR();
2048	}
2049	if (PyModule_AddObject(m, "DO_VALUESFORKEYS", PyInt_FromLong(3)) < 0) {
2050		INITERROR();
2051	}
2052
2053	if (PyModule_AddObject(m, "DO_TAKEVALUE_FORKEY", PyInt_FromLong(0)) < 0) {
2054		INITERROR();
2055	}
2056	if (PyModule_AddObject(m, "DO_TAKEVALUE_FORKEYPATH", PyInt_FromLong(1)) < 0) {
2057		INITERROR();
2058	}
2059	if (PyModule_AddObject(m, "DO_TAKESTOREDVALUE_FORKEY", PyInt_FromLong(2)) < 0) {
2060		INITERROR();
2061	}
2062	if (PyModule_AddObject(m, "DO_TAKEVALUESFROMDICT", PyInt_FromLong(3)) < 0) {
2063		INITERROR();
2064	}
2065	if (PyModule_AddObject(m, "DO_SETVALUE_FORKEY", PyInt_FromLong(4)) < 0) {
2066		INITERROR();
2067	}
2068	if (PyModule_AddObject(m, "DO_SETVALUE_FORKEYPATH", PyInt_FromLong(5)) < 0) {
2069		INITERROR();
2070	}
2071	if (PyModule_AddObject(m, "DO_SETVALUESFORKEYSFROMDICT", PyInt_FromLong(6)) < 0) {
2072		INITERROR();
2073	}
2074
2075
2076	if (PyModule_AddObject(m, "UCHAR_MAX", PyInt_FromLong(UCHAR_MAX)) < 0) {
2077		INITERROR();
2078	}
2079	if (PyModule_AddObject(m, "SCHAR_MAX", PyInt_FromLong(SCHAR_MAX)) < 0) {
2080		INITERROR();
2081	}
2082	if (PyModule_AddObject(m, "SCHAR_MIN", PyInt_FromLong(SCHAR_MIN)) < 0) {
2083		INITERROR();
2084	}
2085	if (PyModule_AddObject(m, "CHAR_MAX", PyInt_FromLong(CHAR_MAX)) < 0) {
2086		INITERROR();
2087	}
2088	if (PyModule_AddObject(m, "CHAR_MIN", PyInt_FromLong(CHAR_MIN)) < 0) {
2089		INITERROR();
2090	}
2091
2092	if (PyModule_AddObject(m, "USHRT_MAX", PyInt_FromLong(USHRT_MAX)) < 0) {
2093		INITERROR();
2094	}
2095	if (PyModule_AddObject(m, "SHRT_MAX", PyInt_FromLong(SHRT_MAX)) < 0) {
2096		INITERROR();
2097	}
2098	if (PyModule_AddObject(m, "SHRT_MIN", PyInt_FromLong(SHRT_MIN)) < 0) {
2099		INITERROR();
2100	}
2101
2102	if (PyModule_AddObject(m, "UINT_MAX", PyLong_FromUnsignedLongLong(UINT_MAX)) < 0) {
2103		INITERROR();
2104	}
2105	if (PyModule_AddObject(m, "INT_MAX", PyInt_FromLong(INT_MAX)) < 0) {
2106		INITERROR();
2107	}
2108	if (PyModule_AddObject(m, "INT_MIN", PyInt_FromLong(INT_MIN)) < 0) {
2109		INITERROR();
2110	}
2111
2112	if (PyModule_AddObject(m, "ULONG_MAX", PyLong_FromUnsignedLongLong(ULONG_MAX)) < 0) {
2113		INITERROR();
2114	}
2115	if (PyModule_AddObject(m, "LONG_MAX", PyInt_FromLong(LONG_MAX)) < 0) {
2116		INITERROR();
2117	}
2118	if (PyModule_AddObject(m, "LONG_MIN", PyInt_FromLong(LONG_MIN)) < 0) {
2119		INITERROR();
2120	}
2121
2122	if (PyModule_AddObject(m, "ULLONG_MAX", PyLong_FromUnsignedLongLong(ULLONG_MAX)) < 0) {
2123		INITERROR();
2124	}
2125	if (PyModule_AddObject(m, "LLONG_MAX", PyLong_FromLongLong(LLONG_MAX)) < 0) {
2126		INITERROR();
2127	}
2128	if (PyModule_AddObject(m, "LLONG_MIN", PyLong_FromLongLong(LLONG_MIN)) < 0) {
2129		INITERROR();
2130	}
2131
2132	if (PyModule_AddObject(m, "DBL_MAX", PyFloat_FromDouble(DBL_MAX)) < 0) {
2133		INITERROR();
2134	}
2135	if (PyModule_AddObject(m, "DBL_MIN", PyFloat_FromDouble(DBL_MIN)) < 0) {
2136		INITERROR();
2137	}
2138	if (PyModule_AddObject(m, "DBL_EPSILON", PyFloat_FromDouble(DBL_EPSILON)) < 0) {
2139		INITERROR();
2140	}
2141	if (PyModule_AddObject(m, "FLT_MAX", PyFloat_FromDouble(FLT_MAX)) < 0) {
2142		INITERROR();
2143	}
2144	if (PyModule_AddObject(m, "FLT_MIN", PyFloat_FromDouble(FLT_MIN)) < 0) {
2145		INITERROR();
2146	}
2147	if (PyModule_AddObject(m, "FLT_EPSILON", PyFloat_FromDouble(FLT_EPSILON)) < 0) {
2148		INITERROR();
2149	}
2150
2151
2152	INITDONE();
2153}
2154