FFIClosure is a Objective-C wrapper for libffi for ObjFW
FFIClosure use libffi trampolines implementation to extend ObjFW functionality and flexibility and add to ObjFW some Apple Objective-C/Objective-C 2.0 features.
FFIClosure supports automatic mapping of block / method types to libffi types, based on the signatures that Clang creates at compile time, even for С-struct types.
FFIClosure does not support unions as closure arguments or return-type
- ObjFW v0.9-dev or higher
- libffi v3.2.1 or higher
- Optional: Clang 3.8 or higher (for Blocks-Runtime)
Create ffi_closure from Block-closure with any Block-signature to create function pointer with signature < Block-return-type >(*)(Block-arguments-types list)
+ (instancetype)closureWithBlock:(id)block;
- (instancetype)initWithBlock:(id)block;
Create ffi_closure from Block with signature < Any >(^)(__unsafe_unretained id, ...) to create IMP function pointer with signature < Any >(*)(id, SEL, ...)
+ (instancetype)closureAsImpImplementationWithBlock:(id)block;
- (instancetype)initAsImpImplementationWithBlock:(id)block;
Create ffi_closure from ObjC Object method to create function pointer with sinature < Method-return-type >(*)(Method-arguments-types list) with excluded id and SEL arguments
+ (instancetype)closureWithTarget:(id)target selector:(SEL)selector;
- (instancetype)initWithTarget:(id)target selector:(SEL)selector;
- (void *)function;
IMP imp_implementationWithBlock(id block);
id imp_getBlock(IMP imp);
BOOL imp_removeBlock(IMP imp);
id objc_getAssociatedObject(id object, void * key);
void objc_setAssociatedObject(id object, void * key, id value, objc_AssociationPolicy policy);
void objc_removeAssociatedObjects(id object);
const char * of_block_signature(id block);
const char * of_block_to_imp_signature(const char * signature);
const char * of_block_to_cfunction_signature(const char * signature);
const char * of_imp_to_cfunction_signature(const char * signature);
int (*callback_function)(int, float, const char *);
__block OFMutableString *message = [OFMutableString string];
FFIClosure *closure = [FFIClosure closureWithBlock:^int(int a, float b, const char *str) {
[message appendFormat:@"Message from C callback! %d, %f, %s", a, b, str];
return 0;
}];
callback_function = (__typeof__(callback_function))[closure function];
call_C_public_API_function_with_callback(callback_function);
of_log(@"Result message:%@", message);