3 ms·
The problem with clang blocks is they're represented as a Objective-C object, this makes them unusable in APIs that expect a function pointer, the only way you
by graphitemaster 12y ago
The problem with clang blocks is they're represented as a Objective-C object, this makes them unusable in APIs that expect a function pointer, the only way you can cast them to a function pointer is to define the structure which represents the block and mmap executable code pages to marshal the call. Such a library exists that binds them to libffi here https://github.com/mikeash/MABlockClosure https://github.com/mikeash/MABlockClosure
This feat alone makes blocks essentially useless unless your entire API is also block-based, as in T (^foo)(...) vs T(*foo)(...).
- mtdewcmu 12y agoI don't think they could be an ObjectiveC object, because they're described as C, not ObjectiveC. I think the problems you're describing are ones that are going to be faced in any attempt at C closures. Closures have memory attached, that's the appeal of them and also the source of all the problems.
- graphitemaster 12y agoNope, they're an ObjectiveC object with some specialties at the compiler level. Google how they're implemented.
- fyolnish 12y agoAn objective-c object is just a pointer to a struct. You don't need the objective-c runtime to call a block. Block_copy etc are implemented in libclosure which iirc does not require libobjc either. But of course if you ARE writing objc, they are valid objects and can be treated as such
- graphitemaster 12y agoCorrect, the problem is to call the function you cannot treat it as a standard function since it's pointing to a struct, the struct does contain the actual function pointer but there is an implicit `this' first argument to that function pointer which has to be that struct itself. This means you cannot use the block in an API that explicitly requires a function pointer, instead the API must specifically be aware of the block and would need to support it.
- mtdewcmu 12y agoIt sounds like you could say it's ObjectiveC-compatible, not ObjectiveC per se. But it's an unimportant distinction.
- fyolnish 12y agoOf course, there's no way around that except to write block variants for those functions. stdlib on osx for example has block variants of most functions that take function pointers (`man qsort_b` for an example) But if you write your program to use blocks from the start, that's not a big problem.