File: PerlMethods.m

package info (click to toggle)
camelbones 0.2.3.cvs20040220-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,308 kB
  • ctags: 1,331
  • sloc: sh: 7,585; ansic: 4,413; asm: 2,021; perl: 1,540; objc: 1,463; makefile: 119
file content (615 lines) | stat: -rw-r--r-- 17,505 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
//
//  PerlMethods.m
//  CamelBones
//
//  Copyright (c) 2004 Sherm Pendley. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "CBPerl.h"
#import "CBPerlObject.h"
#import "Conversions.h"
#import "PerlImports.h"

// Get information about a Perl object
NSString* CBGetMethodNameForSelector(SV* sv, SEL selector) {
    // Define a Perl context
    dTHX;

    HV* hash = (HV*)SvRV(sv);
    HV* stash = SvSTASH(hash);
    char *package = HvNAME(stash);
    NSMutableString *ms = [NSMutableString stringWithString: NSStringFromSelector(selector)];

    NSString *methodEntryName = [NSString stringWithFormat: @"$%s::OBJC_EXPORT{'%@'}->{'method'}", package, ms];
    id methodEntry = [[CBPerl sharedPerl] eval: methodEntryName];

    NSRange all;

    if (methodEntry != nil) {
        return methodEntry;
    }
    
    all = [ms rangeOfString: @":"];
    while (all.length != 0) {
        [ms replaceCharactersInRange: all withString: @"_"];
        all = [ms rangeOfString: @":"];
    }
    if ([ms hasSuffix: @"_"]) {
        all = NSMakeRange([ms length]-1, 1);
        [ms deleteCharactersInRange: all];
    }

    if (gv_fetchmethod(stash, [ms UTF8String]) != NULL) {
        return ms;
    } else {
        return nil;
    }
    return nil;
}

NSString* CBGetMethodArgumentSignatureForSelector(SV* sv, SEL selector) {
    // Define a Perl context
    dTHX;

    HV* hash = (HV*)SvRV(sv);
    HV* stash = SvSTASH(hash);
    char *package = HvNAME(stash);
    NSMutableString *ms = [NSMutableString stringWithString: NSStringFromSelector(selector)];
    NSMutableString *params = [NSMutableString stringWithString: @""];

    NSString *methodEntryName = [NSString stringWithFormat: @"$%s::OBJC_EXPORT{'%@'}->{'args'}", package, ms];
    id methodEntry = [[CBPerl sharedPerl] eval: methodEntryName];

    NSRange all;

    if (methodEntry != nil) {
        return methodEntry;
    }

    all = [ms rangeOfString: @":"];
    while (all.length != 0) {
        [params appendString: @"@"];
        [ms replaceCharactersInRange: all withString: @"_"];
        all = [ms rangeOfString: @":"];
    }

    if ([ms hasSuffix: @"_"]) {
        all = NSMakeRange([ms length]-1, 1);
        [ms deleteCharactersInRange: all];
    }

    if (gv_fetchmethod(stash, [ms UTF8String]) != NULL) {
        return params;
    } else {
        return @"";
    }
    return @"";
}

NSString* CBGetMethodReturnSignatureForSelector(SV* sv, SEL selector) {
    // Define a Perl context
    dTHX;

    HV* hash = (HV*)SvRV(sv);
    HV* stash = SvSTASH(hash);
    char *package = HvNAME(stash);
    NSMutableString *ms = [NSMutableString stringWithString: NSStringFromSelector(selector)];

    NSString *methodEntryName = [NSString stringWithFormat: @"$%s::OBJC_EXPORT{'%@'}->{'return'}", package, ms];
    id methodEntry = [[CBPerl sharedPerl] eval: methodEntryName];

    NSRange all;

    if (methodEntry != nil) {
        return methodEntry;
    }

    all = [ms rangeOfString: @":"];
    while (all.length != 0) {
        [ms replaceCharactersInRange: all withString: @"_"];
        all = [ms rangeOfString: @":"];
    }

    if ([ms hasSuffix: @"_"]) {
        all = NSMakeRange([ms length]-1, 1);
        [ms deleteCharactersInRange: all];
    }

    if (gv_fetchmethod(stash, [ms UTF8String]) != NULL) {
        if ([ms hasPrefix: @"get"]) {
            return @"@";
        } else {
            return @"@";
        }
    } else {
        return @"@";
    }
    return @"@";
}

void CBForwardObjectInvocationToObject(id self, NSInvocation *anInvocation, SV *sv) {
    // Define a Perl context
    dTHX;

    dSP;
    int count;

    HV *methodSignaturesHash;

    HE *methodSigEntry;
    SV *methodSigRef;
    HV *methodSigHash;

    HE *methodNameEntry;
    SV *methodNameEntryVal;

    HV *methodStash;

    char *tempMethodName;
    const char *methodName;
    const char *selUTF8String;
    SV *selString;

    NSMethodSignature *methodSignature;
    int numArgs;
    const char *returnType;
    void *buf;
    void *returnValue;
    char *stringBuf;
    char *returnBuf;
    NSMutableArray *returnList;
    id idBuf;
    NSString *className;
    const char *classNameUTF8String;

    BOOL listContext;

    STRLEN n_a;

    n_a = 0;

    // Get this class name
    className = [self perlClassName];
    
    // Get this method signature
    methodSignature = [anInvocation methodSignature];

    buf = malloc([methodSignature frameLength]);
    if (!buf) return;

    selUTF8String = [NSStringFromSelector([anInvocation selector]) UTF8String];

    // Get a string for the selector
    selString = newSVpv(selUTF8String, 0);
    if (!selString)
        return;

    do {
        tempMethodName = NULL;
        listContext = FALSE;

        // Make certain sv is a valid object reference
        if (!(sv && sv_isobject(sv)))
            break;
    
        // Try to get %OBJC_METHOD_SIGNATURES out of the appropriate package
        classNameUTF8String = [[NSString stringWithFormat: @"%@::OBJC_METHOD_SIGNATURES", className] UTF8String];
        methodSignaturesHash = get_hv(classNameUTF8String, NO);
        if (!methodSignaturesHash)
            break;
    
        // Get a reference to the hash for this signature
        methodSigEntry = hv_fetch_ent(methodSignaturesHash, selString, 0, 0);
        if (!methodSigEntry)
            break;
    
        // Verify the reference
        methodSigRef = HeVAL(methodSigEntry);
        if (!methodSigRef)
            break;
        if (!SvROK(methodSigRef))
            break;
    
        // Deref the reference to get the hash table for this method
        methodSigHash = (HV *)SvRV(methodSigRef);
        if (!methodSigHash)
            break;

        // If there exists an 'listContext' entry, flag it as such
        if (hv_exists_ent(methodSigHash, sv_2mortal(newSVpv("listContext", 0)), 0))
            listContext = TRUE;
        else
            listContext = FALSE;
    
        // Get the methodNameEntry entry
        methodNameEntry = hv_fetch_ent(methodSigHash, sv_2mortal(newSVpv("perlMethod", 0)), 0, 0);
        if (!methodNameEntry)
            break;
    
        // Get the value of the args entry
        methodNameEntryVal = HeVAL(methodNameEntry);
        if (!methodNameEntryVal)
            break;
    
        // Finally! Make it a C string
        tempMethodName = SvPV_nolen(methodNameEntryVal);

    } while (0);

    // If the method name is undefined just use the selector name
    if (tempMethodName == NULL || strlen(tempMethodName) == 0)
        methodName = selUTF8String;
    else
        methodName = tempMethodName;

    // Now verify that the method is defined

    // First get the namespace stash for this object
    methodStash = SvSTASH(SvRV((SV *)sv));
    if (!methodStash)
        return;

    // Get the entry for this method name
    methodName = [CBGetMethodNameForSelector(sv, [anInvocation selector]) UTF8String];

    if (!methodName) {
        const char *rt = [methodSignature methodReturnType];
        const char *at = [methodSignature getArgumentTypeAtIndex: 2];

        NSMutableString *methodNameString = [NSStringFromSelector([anInvocation selector]) mutableCopy];

        // May be a candidate for auto-setName: magic
        if ( [methodNameString hasPrefix: @"set"] &&
             [methodSignature numberOfArguments] == 3 &&
             (*rt) == 'v' ) {

            NSRange propNameRange = NSMakeRange(3, [methodNameString length] - 4);
            NSMutableString *ms = [NSMutableString stringWithString: [methodNameString substringWithRange: propNameRange]];

            if ([self hasProperty: ms]) {
                // Do set[Name]: magic
                switch (*at) {
                    case '@':
                        [anInvocation getArgument: &idBuf atIndex: 2];
                        [self setProperty: ms toObject: idBuf];
                    default:
                        break;
                }
                return;
            }

        } else if ([self hasProperty: methodNameString] &&
                   [methodSignature numberOfArguments] == 2 &&
                   (*rt) == '@' ) {
            idBuf = [self getProperty: methodNameString];
            [anInvocation setReturnValue: &idBuf];
            return;

        } else {
            NSLog(@"Property %@ is not defined in Perl class %@", methodNameString, className);
            return;
        }
    }

    ENTER;
    SAVETMPS;
    
    PUSHMARK(SP);

    // Push "self" onto the stack first
    XPUSHs(sv);

    // If there are arguments, push them onto the call stack
    numArgs = [methodSignature numberOfArguments];

    if (numArgs > 2 && buf != NULL) {
        int i;
        for(i = 2; i < numArgs; i++) {
            const char *argType;

            argType = [methodSignature getArgumentTypeAtIndex: i];
            switch (*argType) {
                case 'c':
                    // char
                    [anInvocation getArgument: buf atIndex: i];
                    XPUSHs(sv_2mortal(newSViv(*(char *)buf)));
                    break;

                case 'i':
                    // int
                    [anInvocation getArgument: buf atIndex: i];
                    XPUSHs(sv_2mortal(newSViv(*(int *)buf)));
                    break;

                case 's':
                    // short
                    [anInvocation getArgument: buf atIndex: i];
                    XPUSHs(sv_2mortal(newSViv(*(short *)buf)));
                    break;

                case 'l':
                    // long
                    [anInvocation getArgument: buf atIndex: i];
                    XPUSHs(sv_2mortal(newSViv(*(long *)buf)));
                    break;

                case 'q':
                    // long long
                    NSLog(@"Unknown type long long in position %d", argType, i);

                case 'C':
                    // unsigned char
                    [anInvocation getArgument: buf atIndex: i];
                    XPUSHs(sv_2mortal(newSViv(*(unsigned char *)buf)));
                    break;
                    
                case 'I':
                    // unsigned int
                    [anInvocation getArgument: buf atIndex: i];
                    XPUSHs(sv_2mortal(newSViv(*(unsigned int *)buf)));
                    break;
                    
                case 'S':
                    // unsigned short
                    [anInvocation getArgument: buf atIndex: i];
                    XPUSHs(sv_2mortal(newSViv(*(unsigned short *)buf)));
                    break;
                    
                case 'L':
                    // unsigned long
                    [anInvocation getArgument: buf atIndex: i];
                    XPUSHs(sv_2mortal(newSViv(*(unsigned long *)buf)));
                    break;
                    
                case 'Q':
                    // unsigned long long
                    NSLog(@"Unknown type unsigned long long in position %d", argType, i);

                case 'f':
                    // float
                    [anInvocation getArgument: buf atIndex: i];
                    XPUSHs(sv_2mortal(newSVnv(*(float *)buf)));
                    break;
                    
                case 'd':
                    // double
                    [anInvocation getArgument: buf atIndex: i];
                    XPUSHs(sv_2mortal(newSViv(*(double *)buf)));
                    break;
                    
                case 'v':
                    // void
                    break;

                case '*':
                    // char *
                    [anInvocation getArgument: &stringBuf atIndex: i];
                    XPUSHs(sv_2mortal(newSVpv(stringBuf, 0)));
                    break;
                    
                case '@':
                    // id
                    [anInvocation getArgument: &idBuf atIndex: i];
                    XPUSHs(CBDerefIDtoSV(idBuf));
                    break;
                    
                case '^':
                	XPUSHs(sv_2mortal(newSViv(*(int*)buf)));
                	break;

                case '#':
                    // Class

                case ':':
                    // SEL

                case '[':
                    // array

                case '{':
                    // struct

                case '(':
                    // union

                case 'b':
                    // bit field

                case '?':
                    // unknown

                default:
                    NSLog(@"Unknown type %s in position %d", argType, i);
            }
        }
    }

    PUTBACK;

    // Call the method in an "eval" block so errors can be trapped
    if (listContext)
        count = call_method(methodName, G_EVAL | G_ARRAY);
    else
        count = call_method(methodName, G_EVAL | G_SCALAR);

    SPAGAIN;

    // Check for an error
    if (SvTRUE(ERRSV)) {
        NSLog(@"Perl error: %s", SvPV(ERRSV, n_a));
    }

    // No error
    returnType = [methodSignature methodReturnType];
    returnValue = NULL;
    returnBuf = NULL;
    idBuf = nil;
    if (count) {
        switch (*returnType) {
            case 'c':
                // char
                *(char *)buf = (char)POPi;
                returnValue = buf;
                break;
    
            case 'i':
                // int
                *(int *)buf = (int)POPi;
                returnValue = buf;
                break;
    
            case 's':
                // short
                *(short *)buf = (short)POPi;
                returnValue = buf;
                break;
    
            case 'l':
                // long
                *(long *)buf = (long)POPi;
                returnValue = buf;
                break;
    
            case 'q':
                // long long
                NSLog(@"Long long return value not implemented");
                break;
    
            case 'C':
                // unsigned char
                *(unsigned char *)buf = (unsigned char)POPi;
                returnValue = buf;
                break;
    
            case 'I':
                // unsigned int
                *(unsigned int *)buf = (unsigned int)POPi;
                returnValue = buf;
                break;
                
            case 'S':
                // unsigned short
                *(unsigned short *)buf = (unsigned short)POPi;
                returnValue = buf;
                break;
                
            case 'L':
                // unsigned long
                *(unsigned long *)buf = (unsigned long)POPi;
                returnValue = buf;
                break;
                
            case 'Q':
                // unsigned long long
                NSLog(@"Unsigned long long return value not implemented");
                break;
    
            case 'f':
                // float
                *(float *)buf = (float)POPn;
                returnValue = buf;
                break;
                
            case 'd':
                // double
                *(double *)buf = (double)POPn;
                returnValue = buf;
                break;
                
            case 'v':
                // void
                returnValue = &returnBuf;
                break;
    
            case '*':
                // char *
                returnBuf = (void *) newSVpv(POPp, 0);
                stringBuf = SvPV((SV *) returnBuf, n_a);
                returnValue = &stringBuf;
                break;
    
            case '@':
                // id

                // id supports list context
                if (listContext) {
                    returnList = [NSMutableArray arrayWithCapacity: count];
                    returnValue = &returnList;
                }

                while (count) {
                    idBuf = CBDerefSVtoID(POPs);
        
                    if (listContext)
                        [returnList insertObject: idBuf atIndex: count];
                    else
                        returnValue = &idBuf;

                    count--;
                }

                break;

            case '^':
                // pointer
                *(int *)buf = (int)POPi;
                returnValue = buf;
                break;
    
            case '#':
                // Class
                returnValue = &returnBuf;
                break;
    
            case ':':
                // SEL
                returnValue = &returnBuf;
                break;
    
            case '[':
                // array
                returnValue = &returnBuf;
                break;
    
            case '{':
                // struct
                returnValue = &returnBuf;
                break;
    
            case '(':
                // union
                returnValue = &returnBuf;
                break;
    
            case 'b':
                // bit field
                returnValue = &returnBuf;
                break;
    
            case '?':
                // unknown
                returnValue = &returnBuf;
                break;
    
            default:
                NSLog(@"Unknown return type %s", returnType);
                returnValue = &returnBuf;
                break;
        }
    } else {
        returnValue = &returnBuf;
    }
    
    FREETMPS;
    LEAVE;

    if (*returnType != 'v') {
        [anInvocation setReturnValue: returnValue];
    }

    free(buf);

    if (returnBuf)
        SvREFCNT_dec((SV *) returnBuf);
}