Giter Site home page Giter Site logo

fficlosure's Introduction

FFIClosure

Description

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.

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.

Limitations

FFIClosure does not support unions as closure arguments or return-type

Requirements

  • ObjFW v0.9-dev or higher
  • libffi v3.2.1 or higher
  • Optional: Clang 3.8 or higher (for Blocks-Runtime)

FFIClosure wrapper class features

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;

Get void* pointer castable to pointer to function

- (void *)function;

Apple Objective-C/Objective-C 2.0 features

Create method implementation from block in Apple ObjC style

IMP imp_implementationWithBlock(id block);

Get Block from IMP

id imp_getBlock(IMP imp);

Remove Block associated with IMP

BOOL imp_removeBlock(IMP imp);

Association ObjC-Object with another ObjC-Object (Dynamic properties and ivars for example)

id objc_getAssociatedObject(id object, void * key);

void objc_setAssociatedObject(id object, void * key, id value, objc_AssociationPolicy policy);

void objc_removeAssociatedObjects(id object);

Utils

Some useful functions for working with Blocks/Methods signatures

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);

Usage example

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);

fficlosure's People

Contributors

gungravekoga avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.