|
|
# FFI Support for C Block Objects
|
|
|
|
|
|
|
|
|
Apple recently [ proposed](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1370.pdf) the inclusion of lambda abstractions (closures) into C/C++/Objective-C and facilitated an implementation in the `clang` compiler framework. They called this language extension *blocks* (or *block objects*). It is widely used in the APIs of OS X 10.6 (Snow Leopard) and 10.7 (Lion). This page is about extending the Haskell 2010 FFI to directly support blocks — i.e., to enable Haskell functions to be marshalled as blocks to C and to enable C blocks to be marshalled as Haskell functions to Haskell land. This extension will be enabled by the language option `BlockObjects`.
|
|
|
Apple recently [proposed](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1370.pdf) the inclusion of lambda abstractions (closures) into C/C++/Objective-C and facilitated an implementation in the `clang` compiler framework. They called this language extension *blocks* (or *block objects*). It is widely used in the APIs of OS X 10.6 (Snow Leopard) and 10.7 (Lion). This page is about extending the Haskell 2010 FFI to directly support blocks — i.e., to enable Haskell functions to be marshalled as blocks to C and to enable C blocks to be marshalled as Haskell functions to Haskell land. This extension will be enabled by the language option `BlockObjects`.
|
|
|
|
|
|
## Example: passing a Haskell functions as an argument
|
|
|
|
|
|
|
|
|
As an example, consider the library function [ qsort_b](http://developer.apple.com/library/mac/#documentation/darwin/reference/manpages/man3/qsort_b.3.html):
|
|
|
As an example, consider the library function [qsort_b](http://developer.apple.com/library/mac/#documentation/darwin/reference/manpages/man3/qsort_b.3.html):
|
|
|
|
|
|
```wiki
|
|
|
void
|
... | ... | @@ -14,7 +14,7 @@ qsort_b(void *base, size_t nel, size_t width, int (^compar)(const void *, const |
|
|
```
|
|
|
|
|
|
|
|
|
In C, we might use this function as described in Apple's introduction to blocks: [ Using a Block Directly](http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Blocks/Articles/bxGettingStarted.html#//apple_ref/doc/uid/TP40007502-CH7-SW2). We would like to be able to do the same in Haskell by declaring:
|
|
|
In C, we might use this function as described in Apple's introduction to blocks: [Using a Block Directly](http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Blocks/Articles/bxGettingStarted.html#//apple_ref/doc/uid/TP40007502-CH7-SW2). We would like to be able to do the same in Haskell by declaring:
|
|
|
|
|
|
```wiki
|
|
|
foreign import ccall "stdlib.h" qsort_b
|
... | ... | @@ -116,8 +116,8 @@ The following subpages provide details on implementing this functionality. **(T |
|
|
|
|
|
## Background
|
|
|
|
|
|
- [ Blocks Programming Topics (Apple)](http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Blocks/Articles/00_Introduction.html)
|
|
|
- [ Block Implementation Specification (Apple via clang)](http://clang.llvm.org/docs/Block-ABI-Apple.txt)
|
|
|
- [ N1370: Apple’s Extensions to C](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1370.pdf)
|
|
|
- [ Some background on the implementation of blocks](http://cocoawithlove.com/2009/10/how-blocks-are-implemented-and.html)
|
|
|
- [ Dynamic generation of method implementations with blocks](http://www.friday.com/bbum/2011/03/17/ios-4-3-imp_implementationwithblock/) |
|
|
\ No newline at end of file |
|
|
- [Blocks Programming Topics (Apple)](http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Blocks/Articles/00_Introduction.html)
|
|
|
- [Block Implementation Specification (Apple via clang)](http://clang.llvm.org/docs/Block-ABI-Apple.txt)
|
|
|
- [N1370: Apple’s Extensions to C](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1370.pdf)
|
|
|
- [Some background on the implementation of blocks](http://cocoawithlove.com/2009/10/how-blocks-are-implemented-and.html)
|
|
|
- [Dynamic generation of method implementations with blocks](http://www.friday.com/bbum/2011/03/17/ios-4-3-imp_implementationwithblock/) |
|
|
\ No newline at end of file |