5

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

Community
  • 1
  • 1
benjist
  • 2,740
  • 3
  • 31
  • 58

1 Answers1

5

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}
0xced
  • 25,219
  • 10
  • 103
  • 255
  • Thank you very much, that works. But what is "@?"? Is it a hidden argument that I should ignore? – benjist Oct 03 '12 at 21:09
  • `@?` is the type encoding of a block. I guess blocks have a hidden parameter (the block itself probably) like methods have the hidden `self` and `_cmd` parameters. – 0xced Oct 03 '12 at 21:23