Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call block with arguments from va_list when block's arguments number and types can vary

I have a variable with a block accepting some arguments. Exact number of arguments and their types can vary. For example it can be a block

void(^testBlock1)(int) = ^(int i){}

or a block

void(^testBlock2)(NSString *,BOOL,int,float) = ^(NSString *str,BOOL b,int i,float f){}

Argument types are limited to {id, BOOL, char, int, unsigned int, float}.

I know the current count of arguments and their types. I need to implement a method that can execute block with given arguments:

-(void)runBlock:(id)block withArguments:(va_list)arguments 
          types:(const char *)types count:(NSUInteger)count;

I have one working naive solution, but it is quite ugly, supports only types of no more than 4 bytes size and relies on alignment. So I'm looking for something better. My solution is something like:

#define MAX_ARGS_COUNT 5
-(void)runBlock:(id)block withArguments:(va_list)arguments 
          types:(const char *)types count:(NSUInteger)count{

    // We will store arguments in this array.
    void * args_table[MAX_ARGS_COUNT];

    // Filling array with arguments
    for (int i=0; i<count; ++i) {
        switch (types[i]) {
            case '@':
            case 'c':
            case 'i':
            case 'I':
                args_table[i] = (void *)(va_arg(arguments, int));
                break;
            case 'f':
                *((float *)(args_table+i)) = (float)(va_arg(arguments, double));
                break;
            default:
                @throw [NSException exceptionWithName:@"runBlock" reason:[NSString stringWithFormat:@"unsupported type %c",types[i]] userInfo:nil];
                break;
        }
    }

    // Now we need to call our block with appropriate count of arguments

#define ARG(N) args_table[N]

#define BLOCK_ARG1 void(^)(void *)
#define BLOCK_ARG2 void(^)(void *,void *)
#define BLOCK_ARG3 void(^)(void *,void *,void *)
#define BLOCK_ARG4 void(^)(void *,void *,void *,void *)
#define BLOCK_ARG5 void(^)(void *,void *,void *,void *,void *)
#define BLOCK_ARG(N) BLOCK_ARG##N

    switch (count) {
        case 1:
            ((BLOCK_ARG(1))block)(ARG(0));
            break;
        case 2:
            ((BLOCK_ARG(2))block)(ARG(0),ARG(1));
            break;
        case 3:
            ((BLOCK_ARG(3))block)(ARG(0),ARG(1),ARG(2));
            break;
        case 4:
            ((BLOCK_ARG(4))block)(ARG(0),ARG(1),ARG(2),ARG(3));
            break;
        case 5:
            ((BLOCK_ARG(5))block)(ARG(0),ARG(1),ARG(2),ARG(3),ARG(4));
            break;
        default:
            break;
    }
}
like image 358
Yan Avatar asked Dec 04 '12 12:12

Yan


1 Answers

Well you are running up against the classic lack-of-metadata and ABI problem in C here. Based on Mike Ash's Awesome Article about MABlockClosure, I think you could examine the block's underlying struct and assume that the va_list matches what the block expects. You can cast the block to struct Block_layout, then block->descriptor will give you the struct BlockDescriptor. Then you have the @encode string that represents the block's arguments and types (@encode being a whole other can of worms).

So once you have the list of arguments and their types, you can dig into the block_layout, grab invoke, then treat it as a function pointer where the first parameter is the block that provides the context. Mike Ash also has some information on Trampolining Blocks that might work if you don't care about any of the type information, but just want to invoke the block.

Let me add a big fat "There be dragons here" warning. This is all very touchy, varies based on the ABI, and relies on obscure and/or undocumented features.

It also seems like you could just call the block directly wherever it was needed, possibly using an NSArray as the only parameter and id as the return type. Then you don't have to worry about any "clever" hacks backfiring on you.

edit: You might be able to use NSMethodSignature's signatureWithObjCTypes:, passing in the block's signature. Then you can call NSInvocation's invocationWithMethodSignature:, but you'll have to call the private invokeWithIMP: method to actually trigger it because you don't have a selector. You'll set the target to the block, then invokeWithIMP, passing the Block struct's invoke pointer. See Generic Block Proxying

like image 54
russbishop Avatar answered Oct 06 '22 00:10

russbishop