File: NSInvocation%2Badditions.m

package info (click to toggle)
steptalk 0.10.0%2Bgit20200629-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,732 kB
  • sloc: objc: 12,182; yacc: 400; makefile: 40; sh: 34; csh: 4; awk: 3; lisp: 3
file content (368 lines) | stat: -rw-r--r-- 10,990 bytes parent folder | download
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
/**
    NSInvocation+additions
    Various NSInvocation additions
 
    Copyright (c) 2002 Free Software Foundation
   
    Written by: Stefan Urbanek <urbanek@host.sk>
    Date: 2000
   
    This file is part of the StepTalk project.
 
    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    
    <title>NSInvocation class additions</title>
 
 */

#import "NSInvocation+additions.h"

#import <Foundation/NSDebug.h>
#import <Foundation/NSException.h>
#import <Foundation/NSMethodSignature.h>
#import <Foundation/NSString.h>
#import <Foundation/NSValue.h>

#import <GNUstepBase/GSObjCRuntime.h>

#import "STExterns.h"
#import "STObjCRuntime.h"
#import "STScripting.h"
#import "STSelector.h"
#import "STStructure.h"

#if 0
static Class NSNumber_class = nil;
static Class NSString_class = nil;
static Class NSValue_class = nil;
#endif

#define CASE_NUMBER_TYPE(otype,type,msgtype)\
            case otype: object = [NSNumber numberWith##msgtype:*((type *)value)];\
                        NSDebugLLog(@"STStructure",\
                                    @"    is number value '%@'", object);\
                        break

/** This method is a factory method, that means that you have to release the
    object when you no longer need it. */
id STObjectFromValueOfType(void *value, const char *type)
{
    id object;

    NSDebugLLog(@"STStructure",
                @"object from value %p of of type '%c'", value, *type);

    switch (*type)
    {
    case _C_ID:
    case _C_CLASS:
            object = *((id *)value);
            NSDebugLLog(@"STStructure",
                        @"    is object value %p", object);
            break;
    CASE_NUMBER_TYPE(_C_CHR,char,Char);
    CASE_NUMBER_TYPE(_C_UCHR,unsigned char,UnsignedChar);
    CASE_NUMBER_TYPE(_C_SHT,short,Short);
    CASE_NUMBER_TYPE(_C_USHT,unsigned short,UnsignedShort);
    CASE_NUMBER_TYPE(_C_INT,int,Int);
    CASE_NUMBER_TYPE(_C_UINT,unsigned int,UnsignedInt);
    CASE_NUMBER_TYPE(_C_LNG,long,Long);
    CASE_NUMBER_TYPE(_C_ULNG,unsigned long,UnsignedLong);
#ifdef _C_LNG_LNG
    CASE_NUMBER_TYPE(_C_LNG_LNG,long long,LongLong);
    CASE_NUMBER_TYPE(_C_ULNG_LNG,unsigned long long,UnsignedLongLong);
#endif
    CASE_NUMBER_TYPE(_C_FLT,float,Float);
    CASE_NUMBER_TYPE(_C_DBL,double,Double);
    case _C_PTR: 
                object = [NSValue valueWithPointer:*((void **)value)];
                NSDebugLLog(@"STStructure",
                           @"    is pointer value %p", *((void **)value));
                break;
    case _C_CHARPTR: 
                object = [NSString stringWithCString:*((char **)value)];
                NSDebugLLog(@"STStructure",
                           @"    is string value '%s'", *((char **)value));
                break;
    case _C_VOID:
                object = nil;
                break;
    case _C_STRUCT_B:
                object = [[STStructure alloc] initWithValue:value type:type];
                AUTORELEASE(object);
                break;
    case _C_SEL: 
                object = [[STSelector alloc] initWithSelector:*((SEL *)value)];
                AUTORELEASE(object);
                break;
    case _C_CONST:
                object = STObjectFromValueOfType(value, ++type);
                break;
    case _C_BFLD:
    case _C_UNDEF:
    case _C_ARY_B:
    case _C_ARY_E:
    case _C_UNION_B:
    case _C_UNION_E:
    case _C_STRUCT_E:
    default:
        [NSException raise:STInvalidArgumentException
                    format:@"unhandled ObjC type '%s'",
                           type];

    }       
    return object;
}

#define CASE_TYPE(otype,type,msgtype)\
            case otype:*((type *)value) = [anObject msgtype##Value];\
                       NSDebugLLog(@"STStructure",\
                                   @"    is number value '%@'", anObject);\
                       break

void STGetValueOfTypeFromObject(void *value, const char *type, id anObject)
{
    NSDebugLLog(@"STStructure",
                @"value at %p from object '%@' of type '%c'",
                value, anObject, *type);

    switch (*type)
    {
    case _C_ID:
    case _C_CLASS:
            NSDebugLLog(@"STStructure",
                        @"    is object value");
            *((id *)value) = anObject;
            break;
    CASE_TYPE(_C_CHR,char,char);
    CASE_TYPE(_C_UCHR,unsigned char,unsignedChar);
    CASE_TYPE(_C_SHT,short,short);
    CASE_TYPE(_C_USHT,unsigned short,unsignedShort);
    CASE_TYPE(_C_INT,int,int);
    CASE_TYPE(_C_UINT,unsigned int,unsignedInt);
    CASE_TYPE(_C_LNG,long,long);
    CASE_TYPE(_C_ULNG,unsigned long,unsignedLong);
    CASE_TYPE(_C_LNG_LNG,long long,longLong);
    CASE_TYPE(_C_ULNG_LNG,unsigned long long,unsignedLongLong);
    CASE_TYPE(_C_FLT,float,float);
    CASE_TYPE(_C_DBL,double,double);
    CASE_TYPE(_C_PTR,void *,pointer);
    case _C_CHARPTR: /* FIXME: check if this is good (copy/no copy)*/
            *((const char **)value) = [anObject cString];
            NSDebugLLog(@"STStructure",
                        @"    is cstring '%@'", anObject);
            break;
    case _C_STRUCT_B:
            /* FIXME: check for struct compatibility */
            NSDebugLLog(@"STStructure",
                        @"    is structure");
            [(STStructure*)anObject getValue:value];
            break;

    case _C_SEL:
            *((SEL *)value) = [anObject selectorValue];
            break;

    case _C_CONST:
            STGetValueOfTypeFromObject(value, ++type, anObject);
            break;

    case _C_BFLD:
    case _C_VOID:
    case _C_UNDEF:
    case _C_ATOM:
    case _C_ARY_B:
    case _C_ARY_E:
    case _C_UNION_B:
    case _C_UNION_E:
    case _C_STRUCT_E:
    default:
        [NSException raise:STInvalidArgumentException
                    format:@"unhandled ObjC type '%s'",
                           type];
    }        
}


@implementation NSInvocation(STAdditions)
#if 0
/* with this method it does not work, it is not posiible to create an 
   invocation*/
+ (void)initialize
{
    NSNumber_class = [NSNumber class];
    NSString_class = [NSString class];
    NSValue_class = [NSValue class];
}
#endif

+ invocationWithTarget:(id)target selectorName:(NSString *)selectorName
{
    NSMethodSignature *signature;
    NSInvocation      *invocation;
    SEL                sel;
    BOOL               requiresRegistration = NO;

    // NSLog(@"GETTING SELECTOR %@", selectorName);
    sel = NSSelectorFromString(selectorName);

    if (!sel)
    {
        // NSLog(@"REGISTERING SELECTOR");
        const char *name = [selectorName cString];

        sel = sel_registerName(name);

        if (!sel)
        {
            [NSException raise:STInternalInconsistencyException
                         format:@"Unable to register selector '%@'",
                                selectorName];
            return nil;
        }
        requiresRegistration = YES;
    }

    signature = [target methodSignatureForSelector:sel];

    /* FIXME: this is workaround for gnustep DO bug (hight priority) */

    if (requiresRegistration)
    {
        // NSLog(@"REGISTERING SELECTOR TYPES");
        sel = GSSelectorFromNameAndTypes([selectorName cString],
                                         [signature methodReturnType]);
        // NSLog(@"REGISTERED %@", NSStringFromSelector(sel));
    }

    if (!signature)
    {
        [NSException raise:STInternalInconsistencyException
                     format:@"No method signature for selector '%@' for "
                            @"receiver of type %@",
                            selectorName, [target className]];
        return nil;
    }

    invocation = [NSInvocation invocationWithMethodSignature:signature];

    [invocation setSelector:sel];
    [invocation setTarget:target];

    return invocation;
}

+ invocationWithTarget:(id)target selector:(SEL)selector
{
    NSMethodSignature *signature;
    NSInvocation      *invocation;

    signature = [target methodSignatureForSelector:selector];

    if (!signature)
    {
        [NSException raise:STInternalInconsistencyException
                     format:@"No method signature for selector '%@' for "
                            @"receiver of type %@",
                            NSStringFromSelector(selector), [target className]];
        return nil;
    }

    invocation = [NSInvocation invocationWithMethodSignature:signature];

    [invocation setSelector:selector];
    [invocation setTarget:target];

    return invocation;
}

- (void)setArgumentAsObject:(id)anObject atIndex:(NSInteger)anIndex
{
    const char *type;
    NSUInteger size;
    void *value;

    type = [[self methodSignature] getArgumentTypeAtIndex:anIndex];
    NSGetSizeAndAlignment(type, &size, NULL);
    value = NSZoneMalloc(STMallocZone, size);

    STGetValueOfTypeFromObject(value, type, anObject);

    [self setArgument:(void *)value atIndex:anIndex];
    NSZoneFree(STMallocZone, value);
}

- (id)getArgumentAsObjectAtIndex:(NSInteger)anIndex
{
    const char *type;
    NSUInteger  size;
    void *value;
    id    object;

    type = [[self methodSignature] getArgumentTypeAtIndex:anIndex];
    NSGetSizeAndAlignment(type, &size, NULL);
    value = NSZoneMalloc(STMallocZone, size);
    [self getArgument:value atIndex:anIndex];

    object = STObjectFromValueOfType(value, type);

    NSZoneFree(STMallocZone, value);

    return object;
}

- (id)returnValueAsObject
{
    const char *type;
    NSUInteger  returnLength;
    void *value;
    id    returnObject = nil;

    NSMethodSignature *signature = [self methodSignature];

    type = [signature methodReturnType];
    returnLength = [signature methodReturnLength];

    NSDebugLLog(@"STSending",
		@"  return type '%s', buffer length %lu",
		type, (unsigned long)returnLength);

    if (returnLength != 0)
    {
        value = NSZoneMalloc(STMallocZone, returnLength);
        [self getReturnValue:value];

        if (*type == _C_VOID)
        {
            returnObject = [self target];
        }
        else
        {
            returnObject = STObjectFromValueOfType(value, type);
        }

        NSZoneFree(STMallocZone, value);
        NSDebugLLog(@"STSending",
                    @"  returned object %@", returnObject);
    }
    else
    {
        returnObject = [self target];
    }

    return returnObject;
}
@end