Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS NSMethodSignature (or encoding) of NSBlock

I need a way to check the number of arguments and argument types of a given block at runtime (I need this for some object mapping library I'm currently writing, I'm mapping String formatted values to selectors, want the same for blocks).

I tried code from below examples, but for some reason it's not working for me and returnin nil for the string description.

Do you know a way to evaluate block signatures at runtime(preferably afe for iPhone app store submissions)?

This is the code I use:

struct BlockDescriptor {
unsigned long reserved;
unsigned long size;
void *rest[1];
};

struct Block {
void *isa;
int flags;
int reserved;
void (*invoke)(struct __block_literal_1 *);
struct BlockDescriptor *descriptor;
};

enum {
BLOCK_HAS_COPY_DISPOSE = (1 << 25),
BLOCK_HAS_CTOR = (1 << 26), // helpers have C++ code
BLOCK_IS_GLOBAL = (1 << 28),
BLOCK_HAS_STRET = (1 << 29), // IFF BLOCK_HAS_SIGNATURE
BLOCK_HAS_SIGNATURE = (1 << 30),
};

static const char *BlockSig(id blockObj)
{
struct Block *block = (void *)blockObj;
struct BlockDescriptor *descriptor = block->descriptor;

assert(block->flags & BLOCK_HAS_SIGNATURE);

int index = 0;
if(block->flags & BLOCK_HAS_COPY_DISPOSE)
    index += 2;

return descriptor->rest[index];
}


-(NSString*)signatureForBlock:(id)block {
NSString* sig = [NSString stringWithUTF8String:BlockSig(block)];

sig = [sig substringFromIndex:1]; // remove c
NSArray* components = [sig componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"0123456789?"]];
sig = [components componentsJoinedByString:@""];

return sig;
}

Then do:

NSString * (^block)(int, NSArray *) = ^NSString * (int i, NSArray * a){
    return @"Oh, yeah!";
};
NSLog(@"signature %s", BlockSig(block)); // ==> this returns null

Source: Checking Objective-C block type? https://github.com/mikeash/MABlockForwarding/blob/master/main.m

like image 408
benjist Avatar asked Oct 03 '12 19:10

benjist


1 Answers

Using CTBlockDescription, you can get all the runtime information you need as a NSMethodSignature object. Usage is straightforward:

NSString * (^block)(int, NSArray *) = ^NSString * (int i, NSArray * a){
    return @"Oh, yeah!";
};
NSMethodSignature *signature = [[[CTBlockDescription alloc] initWithBlock:block] blockSignature];
NSLog(@"signature %@", [signature debugDescription]);

This will output the following signature:

signature <NSMethodSignature: 0x6844900>
    number of arguments = 3
    frame size = 12
    is special struct return? NO
    return value: -------- -------- -------- --------
        type encoding (@) '@'
        flags {isObject}
        modifiers {}
        frame {offset = 0, offset adjust = 0, size = 4, size adjust = 0}
        memory {offset = 0, size = 4}
    argument 0: -------- -------- -------- --------
        type encoding (@) '@?'
        flags {isObject}
        modifiers {}
        frame {offset = 0, offset adjust = 0, size = 4, size adjust = 0}
        memory {offset = 0, size = 4}
    argument 1: -------- -------- -------- --------
        type encoding (i) 'i'
        flags {isSigned}
        modifiers {}
        frame {offset = 4, offset adjust = 0, size = 4, size adjust = 0}
        memory {offset = 0, size = 4}
    argument 2: -------- -------- -------- --------
        type encoding (@) '@'
        flags {isObject}
        modifiers {}
        frame {offset = 8, offset adjust = 0, size = 4, size adjust = 0}
        memory {offset = 0, size = 4}
like image 157
0xced Avatar answered Oct 18 '22 14:10

0xced